Skip to content

Commit

Permalink
Merge pull request #866 from axilleastr/master
Browse files Browse the repository at this point in the history
[Terraform] Add Terraform Validate function
  • Loading branch information
brikis98 authored Apr 16, 2021
2 parents 0d654bd + c62e774 commit a3dee0b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
39 changes: 39 additions & 0 deletions modules/terraform/validate.go
Original file line number Diff line number Diff line change
@@ -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)
}
37 changes: 37 additions & 0 deletions modules/terraform/validate_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit a3dee0b

Please sign in to comment.