From c62e774709ccb747227c685e18a0fbe79ec3ba41 Mon Sep 17 00:00:00 2001 From: Achilleas Triantafyllou Date: Tue, 13 Apr 2021 00:56:50 +0300 Subject: [PATCH] Add terraform validate functions --- modules/terraform/validate.go | 39 ++++++++++++++++++++++++++++++ modules/terraform/validate_test.go | 37 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 modules/terraform/validate.go create mode 100644 modules/terraform/validate_test.go diff --git a/modules/terraform/validate.go b/modules/terraform/validate.go new file mode 100644 index 000000000..3ce200846 --- /dev/null +++ b/modules/terraform/validate.go @@ -0,0 +1,39 @@ +package terraform + +import ( + "github.com/gruntwork-io/terratest/modules/testing" + "github.com/stretchr/testify/require" +) + +// Validate calls terraform validate and returns stdout/stderr. +func Validate(t testing.TestingT, options *Options) string { + out, err := ValidateE(t, options) + require.NoError(t, err) + return out +} + +// ValidateE calls terraform validate and returns stdout/stderr. +func ValidateE(t testing.TestingT, options *Options) (string, error) { + return RunTerraformCommandE(t, options, FormatArgs(options, "validate")...) +} + +// InitAndValidate runs terraform init and validate with the given options and returns stdout/stderr from the validate command. +// This will fail the test if there is an error in the command. +func InitAndValidate(t testing.TestingT, options *Options) string { + out, err := InitAndValidateE(t, options) + require.NoError(t, err) + return out +} + +// InitAndValidateE runs terraform init and validate with the given options and returns stdout/stderr from the validate command. +func InitAndValidateE(t testing.TestingT, options *Options) (string, error) { + if _, err := InitE(t, options); err != nil { + return "", err + } + + if _, err := GetE(t, options); err != nil { + return "", err + } + + return ValidateE(t, options) +} diff --git a/modules/terraform/validate_test.go b/modules/terraform/validate_test.go new file mode 100644 index 000000000..430fb2eb8 --- /dev/null +++ b/modules/terraform/validate_test.go @@ -0,0 +1,37 @@ +package terraform + +import ( + "testing" + + "github.com/gruntwork-io/terratest/modules/files" + "github.com/stretchr/testify/require" +) + +func TestInitAndValidateWithNoError(t *testing.T) { + t.Parallel() + + testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-basic-configuration", t.Name()) + require.NoError(t, err) + + options := &Options{ + TerraformDir: testFolder, + } + + out := InitAndValidate(t, options) + require.Contains(t, out, "The configuration is valid") +} + +func TestInitAndValidateWithError(t *testing.T) { + t.Parallel() + + testFolder, err := files.CopyTerraformFolderToTemp("../../test/fixtures/terraform-with-plan-error", t.Name()) + require.NoError(t, err) + + options := &Options{ + TerraformDir: testFolder, + } + + out, err := InitAndValidateE(t, options) + require.Error(t, err) + require.Contains(t, out, "Reference to undeclared input variable") +}