-
Notifications
You must be signed in to change notification settings - Fork 14
Always regen _massdriver_variables.tf on bundle build #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,12 +13,16 @@ import ( | |
| "github.com/massdriver-cloud/mass/pkg/provisioners" | ||
| ) | ||
|
|
||
| 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" | ||
|
||
|
|
||
| func TestOpentofuExportMassdriverInputs(t *testing.T) { | ||
| type test struct { | ||
| name string | ||
| variables map[string]any | ||
| want string | ||
| errString string | ||
| name string | ||
| tfFile string // testdata fixture to use as variables.tf; defaults to tc.name | ||
| variables map[string]any | ||
| existingMassdriverVars string // pre-populate _massdriver_variables.tf with this content | ||
| want string | ||
| errString string | ||
| } | ||
| tests := []test{ | ||
| { | ||
|
|
@@ -49,12 +53,54 @@ func TestOpentofuExportMassdriverInputs(t *testing.T) { | |
| }, | ||
| }, | ||
| }, | ||
| want: `// Auto-generated variable declarations from massdriver.yaml | ||
| variable "bar" { | ||
| want: autoGeneratedHeader + `variable "bar" { | ||
| type = string | ||
| } | ||
| `, | ||
| }, | ||
|
Comment on lines
28
to
+60
|
||
| { | ||
| name: "regenerate", | ||
| tfFile: "missingopentofu", | ||
| variables: map[string]any{ | ||
| "required": []any{"bar", "foo"}, | ||
| "properties": map[string]any{ | ||
| "bar": map[string]any{ | ||
| "type": "string", | ||
| }, | ||
| "foo": map[string]any{ | ||
| "type": "string", | ||
| }, | ||
| }, | ||
| }, | ||
| existingMassdriverVars: autoGeneratedHeader + `variable "bar" { | ||
| type = string | ||
| } | ||
| `, | ||
| want: autoGeneratedHeader + `variable "bar" { | ||
| type = string | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| name: "regenerateclean", | ||
| tfFile: "same", | ||
| variables: map[string]any{ | ||
| "required": []any{"bar", "foo"}, | ||
| "properties": map[string]any{ | ||
| "bar": map[string]any{ | ||
| "type": "string", | ||
| }, | ||
| "foo": map[string]any{ | ||
| "type": "string", | ||
| }, | ||
| }, | ||
| }, | ||
| existingMassdriverVars: autoGeneratedHeader + `variable "bar" { | ||
| type = string | ||
| } | ||
| `, | ||
| want: ``, | ||
| }, | ||
| { | ||
| name: "missingmassdriver", | ||
| variables: map[string]any{ | ||
|
|
@@ -77,14 +123,26 @@ variable "bar" { | |
| t.Run(tc.name, func(t *testing.T) { | ||
| testDir := t.TempDir() | ||
|
|
||
| content, err := os.ReadFile(path.Join("testdata", "opentofu", fmt.Sprintf("%s.tf", tc.name))) | ||
| tfFile := tc.tfFile | ||
| if tfFile == "" { | ||
| tfFile = tc.name | ||
| } | ||
|
|
||
| content, err := os.ReadFile(path.Join("testdata", "opentofu", fmt.Sprintf("%s.tf", tfFile))) | ||
| if err != nil { | ||
| t.Fatalf("%d, unexpected error", err) | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
chrisghill marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| err = os.WriteFile(path.Join(testDir, "variables.tf"), content, 0644) | ||
| if err != nil { | ||
| t.Fatalf("%d, unexpected error", err) | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| if tc.existingMassdriverVars != "" { | ||
| err = os.WriteFile(path.Join(testDir, "_massdriver_variables.tf"), []byte(tc.existingMassdriverVars), 0644) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error writing existing massdriver vars: %v", err) | ||
| } | ||
| } | ||
|
|
||
| prov := provisioners.OpentofuProvisioner{} | ||
|
|
@@ -150,12 +208,12 @@ func TestOpentofuReadProvisionerInputs(t *testing.T) { | |
|
|
||
| content, err := os.ReadFile(path.Join("testdata", "opentofu", fmt.Sprintf("%s.tf", tc.name))) | ||
| if err != nil { | ||
| t.Fatalf("%d, unexpected error", err) | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| err = os.WriteFile(path.Join(testDir, "variables.tf"), content, 0644) | ||
| if err != nil { | ||
| t.Fatalf("%d, unexpected error", err) | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| prov := provisioners.OpentofuProvisioner{} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
massdriverVarsBackup is always the fixed path "_massdriver_variables.tf.bak". If that backup already exists (e.g., a prior run crashed after the rename), os.Rename may fail on Windows (and can overwrite on Unix). Consider removing/archiving any existing backup first or using a unique backup name (PID/timestamp) to avoid collisions and cross-platform behavior differences.