From 7f0cf79872ed91d4d894103d7c0150a0f4597aa7 Mon Sep 17 00:00:00 2001 From: Andrew Peabody Date: Fri, 14 Jun 2024 18:24:11 +0000 Subject: [PATCH 1/3] feat(tft): add GetTFSetupJsonOutput() --- infra/blueprint-test/pkg/tft/terraform.go | 27 ++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/infra/blueprint-test/pkg/tft/terraform.go b/infra/blueprint-test/pkg/tft/terraform.go index 86df7ec1503..99d1da8f8c5 100644 --- a/infra/blueprint-test/pkg/tft/terraform.go +++ b/infra/blueprint-test/pkg/tft/terraform.go @@ -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 @@ -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 a primitive, if setupDir is not configured, or invalid JSON. +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: %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 { From 7a779485ca7382878f0937d58cbe4196ef68f2c0 Mon Sep 17 00:00:00 2001 From: Andrew Peabody Date: Tue, 18 Jun 2024 09:09:58 -0700 Subject: [PATCH 2/3] Update infra/blueprint-test/pkg/tft/terraform.go Co-authored-by: Bharath KKB --- infra/blueprint-test/pkg/tft/terraform.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/blueprint-test/pkg/tft/terraform.go b/infra/blueprint-test/pkg/tft/terraform.go index 99d1da8f8c5..b4f908c808c 100644 --- a/infra/blueprint-test/pkg/tft/terraform.go +++ b/infra/blueprint-test/pkg/tft/terraform.go @@ -425,7 +425,7 @@ func (b *TFBlueprintTest) GetTFSetupStringOutput(key string) string { 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: %s", v) + b.t.Fatalf("Invalid JSON in setup output override: %s", v) } return gjson.Parse(v.(string)) } From 988f9343613b7527faf30a5a9fd8923f3f46e37b Mon Sep 17 00:00:00 2001 From: Andrew Peabody Date: Tue, 18 Jun 2024 09:14:11 -0700 Subject: [PATCH 3/3] Update terraform.go --- infra/blueprint-test/pkg/tft/terraform.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/blueprint-test/pkg/tft/terraform.go b/infra/blueprint-test/pkg/tft/terraform.go index b4f908c808c..bb0bce9aee7 100644 --- a/infra/blueprint-test/pkg/tft/terraform.go +++ b/infra/blueprint-test/pkg/tft/terraform.go @@ -421,7 +421,7 @@ func (b *TFBlueprintTest) GetTFSetupStringOutput(key string) string { // 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 a primitive, if setupDir is not configured, or invalid JSON. +// 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)) {