diff --git a/test/src/examples_complete_test.go b/test/src/examples_complete_test.go index c42dfa6..6cb89cd 100644 --- a/test/src/examples_complete_test.go +++ b/test/src/examples_complete_test.go @@ -1,23 +1,40 @@ package test import ( + "strings" + "testing" + + "github.com/gruntwork-io/terratest/modules/random" "github.com/gruntwork-io/terratest/modules/terraform" + testStructure "github.com/gruntwork-io/terratest/modules/test-structure" "github.com/stretchr/testify/assert" - "testing" ) // Test the Terraform module in examples/complete using Terratest. func TestExamplesComplete(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/complete" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/complete", + TerraformDir: tempTestFolder, Upgrade: true, // Variables to pass to our Terraform code using -var-file options - VarFiles: []string{"fixtures.tfvars"}, + VarFiles: varFiles, + Vars: map[string]interface{}{ + "attributes": attributes, + }, } // At the end of the test, run `terraform destroy` to clean up any resources that were created - defer terraform.Destroy(t, terraformOptions) + defer cleanup(t, terraformOptions, tempTestFolder) // This will run `terraform init` and `terraform apply` and fail the test if there are any errors terraform.InitAndApply(t, terraformOptions) diff --git a/test/src/utils.go b/test/src/utils.go new file mode 100644 index 0000000..b9548d5 --- /dev/null +++ b/test/src/utils.go @@ -0,0 +1,13 @@ +package test + +import ( + "os" + "testing" + + "github.com/gruntwork-io/terratest/modules/terraform" +) + +func cleanup(t *testing.T, terraformOptions *terraform.Options, tempTestFolder string) { + terraform.Destroy(t, terraformOptions) + _ = os.RemoveAll(tempTestFolder) +}