@@ -13,12 +13,16 @@ import (
1313 "github.com/massdriver-cloud/mass/pkg/provisioners"
1414)
1515
16+ const autoGeneratedHeader = "// This file is auto-generated by massdriver from your massdriver.yaml file.\n // Any changes made directly to this file will be overwritten on the next build.\n // To opt a variable out of regeneration, move it to another file (e.g. variables.tf).\n "
17+
1618func TestOpentofuExportMassdriverInputs (t * testing.T ) {
1719 type test struct {
18- name string
19- variables map [string ]any
20- want string
21- errString string
20+ name string
21+ tfFile string // testdata fixture to use as variables.tf; defaults to tc.name
22+ variables map [string ]any
23+ existingMassdriverVars string // pre-populate _massdriver_variables.tf with this content
24+ want string
25+ errString string
2226 }
2327 tests := []test {
2428 {
@@ -49,12 +53,54 @@ func TestOpentofuExportMassdriverInputs(t *testing.T) {
4953 },
5054 },
5155 },
52- want : `// Auto-generated variable declarations from massdriver.yaml
53- variable "bar" {
56+ want : autoGeneratedHeader + `variable "bar" {
57+ type = string
58+ }
59+ ` ,
60+ },
61+ {
62+ name : "regenerate" ,
63+ tfFile : "missingopentofu" ,
64+ variables : map [string ]any {
65+ "required" : []any {"bar" , "foo" },
66+ "properties" : map [string ]any {
67+ "bar" : map [string ]any {
68+ "type" : "string" ,
69+ },
70+ "foo" : map [string ]any {
71+ "type" : "string" ,
72+ },
73+ },
74+ },
75+ existingMassdriverVars : autoGeneratedHeader + `variable "bar" {
76+ type = string
77+ }
78+ ` ,
79+ want : autoGeneratedHeader + `variable "bar" {
5480 type = string
5581}
5682` ,
5783 },
84+ {
85+ name : "regenerateclean" ,
86+ tfFile : "same" ,
87+ variables : map [string ]any {
88+ "required" : []any {"bar" , "foo" },
89+ "properties" : map [string ]any {
90+ "bar" : map [string ]any {
91+ "type" : "string" ,
92+ },
93+ "foo" : map [string ]any {
94+ "type" : "string" ,
95+ },
96+ },
97+ },
98+ existingMassdriverVars : autoGeneratedHeader + `variable "bar" {
99+ type = string
100+ }
101+ ` ,
102+ want : `` ,
103+ },
58104 {
59105 name : "missingmassdriver" ,
60106 variables : map [string ]any {
@@ -77,14 +123,26 @@ variable "bar" {
77123 t .Run (tc .name , func (t * testing.T ) {
78124 testDir := t .TempDir ()
79125
80- content , err := os .ReadFile (path .Join ("testdata" , "opentofu" , fmt .Sprintf ("%s.tf" , tc .name )))
126+ tfFile := tc .tfFile
127+ if tfFile == "" {
128+ tfFile = tc .name
129+ }
130+
131+ content , err := os .ReadFile (path .Join ("testdata" , "opentofu" , fmt .Sprintf ("%s.tf" , tfFile )))
81132 if err != nil {
82- t .Fatalf ("%d, unexpected error" , err )
133+ t .Fatalf ("unexpected error: %v " , err )
83134 }
84135
85136 err = os .WriteFile (path .Join (testDir , "variables.tf" ), content , 0644 )
86137 if err != nil {
87- t .Fatalf ("%d, unexpected error" , err )
138+ t .Fatalf ("unexpected error: %v" , err )
139+ }
140+
141+ if tc .existingMassdriverVars != "" {
142+ err = os .WriteFile (path .Join (testDir , "_massdriver_variables.tf" ), []byte (tc .existingMassdriverVars ), 0644 )
143+ if err != nil {
144+ t .Fatalf ("unexpected error writing existing massdriver vars: %v" , err )
145+ }
88146 }
89147
90148 prov := provisioners.OpentofuProvisioner {}
@@ -150,12 +208,12 @@ func TestOpentofuReadProvisionerInputs(t *testing.T) {
150208
151209 content , err := os .ReadFile (path .Join ("testdata" , "opentofu" , fmt .Sprintf ("%s.tf" , tc .name )))
152210 if err != nil {
153- t .Fatalf ("%d, unexpected error" , err )
211+ t .Fatalf ("unexpected error: %v " , err )
154212 }
155213
156214 err = os .WriteFile (path .Join (testDir , "variables.tf" ), content , 0644 )
157215 if err != nil {
158- t .Fatalf ("%d, unexpected error" , err )
216+ t .Fatalf ("unexpected error: %v " , err )
159217 }
160218
161219 prov := provisioners.OpentofuProvisioner {}
0 commit comments