Skip to content

Commit

Permalink
update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Benbentwo committed Jul 22, 2024
1 parent ed685ab commit d88b891
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
25 changes: 21 additions & 4 deletions test/src/examples_complete_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
13 changes: 13 additions & 0 deletions test/src/utils.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d88b891

Please sign in to comment.