Skip to content

Commit

Permalink
feat(tft): add GetTFSetupJsonOutput() (#2423)
Browse files Browse the repository at this point in the history
Co-authored-by: Bharath KKB <bharathkrishnakb@gmail.com>
  • Loading branch information
apeabody and bharathkkb committed Jun 18, 2024
1 parent ed940dc commit 1e7bc1b
Showing 1 changed file with 26 additions and 1 deletion.
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

0 comments on commit 1e7bc1b

Please sign in to comment.