-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #866 from axilleastr/master
[Terraform] Add Terraform Validate function
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |