diff --git a/go.mod b/go.mod index 966339f..dad8291 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/aztfmod/terratest-helper-caf go 1.16 -require github.com/gruntwork-io/terratest v0.34.8 +require ( + github.com/gruntwork-io/terratest v0.34.8 + github.com/stretchr/testify v1.4.0 +) diff --git a/state/tfstate.go b/state/tfstate.go index a3c51fd..46189d5 100644 --- a/state/tfstate.go +++ b/state/tfstate.go @@ -8,7 +8,9 @@ import ( "strings" "testing" + "github.com/gruntwork-io/terratest/modules/logger" terraform "github.com/gruntwork-io/terratest/modules/terraform" + "github.com/stretchr/testify/require" ) type Resource = map[string]interface{} @@ -32,13 +34,26 @@ type TerraFormState struct { var TfState TerraFormState +func ReadTerraformOutputJson(t *testing.T, Options *terraform.Options, key string) string { + args := []string{"output", "-no-color", "-json"} + args = append(args, key) + + Options.Logger = logger.Discard + output, err := terraform.RunTerraformCommandAndGetStdoutE(t, Options, args...) + + require.NoError(t, err) + + //return the output + return output +} + func NewTerraformState(t *testing.T, key string) *TerraFormState { os.Unsetenv("TF_DATA_DIR") tfState := new(TerraFormState) options := &terraform.Options{ TerraformDir: os.Getenv("STATE_FILE_PATH"), } - outputJson := terraform.OutputJson(t, options, "objects") + outputJson := ReadTerraformOutputJson(t, options, "objects") json.Unmarshal([]byte(outputJson), &tfState.Objects) tfState.Key = key tfState.SubscriptionID = os.Getenv("ARM_SUBSCRIPTION_ID")