Skip to content
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

feat(tft): add GetTFSetupJsonOutput() #2423

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion infra/blueprint-test/pkg/tft/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func (b *TFBlueprintTest) GetStringOutputList(name string) []string {
}

// GetJsonOutput returns TF output for key as gjson.Result.
// An empty string for key can be used to return all values
// An empty string for key can be used to return all values.
// It fails test on invalid JSON.
func (b *TFBlueprintTest) GetJsonOutput(key string) gjson.Result {
// allow only parallel reads as Terraform plugin cache isn't concurrent safe
Expand Down Expand Up @@ -419,6 +419,31 @@ func (b *TFBlueprintTest) GetTFSetupStringOutput(key string) string {
return terraform.Output(b.t, &terraform.Options{TerraformDir: b.setupDir, Logger: b.logger, NoColor: true}, key)
}

// GetTFSetupJsonOutput returns TF setup output for a given key as gjson.Result.
// An empty string for key can be used to return all values.
// It fails test if given key does not output valid JSON or if setupDir is not configured.
func (b *TFBlueprintTest) GetTFSetupJsonOutput(key string) gjson.Result {
if v, ok := b.setupOutputOverrides[key]; ok {
if !gjson.Valid(v.(string)) {
b.t.Fatalf("Invalid JSON in setup output override: %s", v)
}
return gjson.Parse(v.(string))
}
if b.setupDir == "" {
b.t.Fatal("Setup path not set")
}
// allow only parallel reads as Terraform plugin cache isn't concurrent safe
rUnlockFn := b.rLockFn()
defer rUnlockFn()

jsonString := terraform.OutputJson(b.t, &terraform.Options{TerraformDir: b.setupDir, Logger: b.logger, NoColor: true}, key)
if !gjson.Valid(jsonString) {
b.t.Fatalf("Invalid JSON: %s", jsonString)
}

return gjson.Parse(jsonString)
}

// loadTFEnvVar adds new env variables prefixed with TF_VAR_ to an existing map of variables.
func loadTFEnvVar(m map[string]string, new map[string]string) {
for k, v := range new {
Expand Down
Loading