From edb5c3db21c6c5b2739d9ab9f0bb50734938b0f3 Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Mon, 22 Jul 2024 10:51:40 -0700 Subject: [PATCH] update other tests, add enabled: false test --- test/src/examples_advanced_features.go | 23 ++++++++++-- test/src/examples_alert_policy_test.go | 22 +++++++++-- test/src/examples_api_integration_test.go | 22 +++++++++-- test/src/examples_complete_test.go | 37 +++++++++++++++++++ test/src/examples_config_test.go | 22 +++++++++-- test/src/examples_escalation_test.go | 22 +++++++++-- test/src/examples_integration_action.go | 25 ++++++++++--- test/src/examples_notification_policy_test.go | 22 +++++++++-- test/src/examples_team_routing_rule_test.go | 22 +++++++++-- test/src/examples_team_test.go | 22 +++++++++-- test/src/examples_user_test.go | 24 ++++++++++-- 11 files changed, 229 insertions(+), 34 deletions(-) diff --git a/test/src/examples_advanced_features.go b/test/src/examples_advanced_features.go index 23267eb..c11cae7 100644 --- a/test/src/examples_advanced_features.go +++ b/test/src/examples_advanced_features.go @@ -1,23 +1,38 @@ package test import ( + "github.com/gruntwork-io/terratest/modules/random" "github.com/gruntwork-io/terratest/modules/terraform" + testStructure "github.com/gruntwork-io/terratest/modules/test-structure" + "strings" "testing" ) - // Test the Terraform module in examples/advanced_features using Terratest. func TestExamplesAdvancedFeatures(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/advanced_features" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/advanced_features", + 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/examples_alert_policy_test.go b/test/src/examples_alert_policy_test.go index bca23c5..9d8b727 100644 --- a/test/src/examples_alert_policy_test.go +++ b/test/src/examples_alert_policy_test.go @@ -1,23 +1,39 @@ package test import ( + "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" + "strings" "testing" ) // Test the Terraform module in examples/alert_policy using Terratest. func TestExamplesAlertPolicy(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/alert_policy" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/alert_policy", + 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/examples_api_integration_test.go b/test/src/examples_api_integration_test.go index 486052b..d1381f6 100644 --- a/test/src/examples_api_integration_test.go +++ b/test/src/examples_api_integration_test.go @@ -1,23 +1,39 @@ package test import ( + "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" + "strings" "testing" ) // Test the Terraform module in examples/api_integration using Terratest. func TestExamplesApiIntegration(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/api_integration" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/api_integration", + 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/examples_complete_test.go b/test/src/examples_complete_test.go index 13d52c0..8b76a28 100644 --- a/test/src/examples_complete_test.go +++ b/test/src/examples_complete_test.go @@ -1,6 +1,7 @@ package test import ( + "regexp" "strings" "testing" @@ -67,3 +68,39 @@ func TestExamplesComplete(t *testing.T) { // Verify we're getting back the outputs we expect assert.Equal(t, expectedTeamName, outputTeamName) } + +func TestExamplesCompleteDisabled(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: tempTestFolder, + Upgrade: true, + // Variables to pass to our Terraform code using -var-file options + VarFiles: varFiles, + Vars: map[string]interface{}{ + "attributes": attributes, + "enabled": false, + }, + } + + // At the end of the test, run `terraform destroy` to clean up any resources that were created + defer cleanup(t, terraformOptions, tempTestFolder) + + // This will run `terraform init` and `terraform apply` and fail the test if there are any errors + results := terraform.InitAndApply(t, terraformOptions) + + // Should complete successfully without creating or changing any resources. + // Extract the "Resources:" section of the output to make the error message more readable. + re := regexp.MustCompile(`Resources: [^.]+\.`) + match := re.FindString(results) + assert.Equal(t, "Resources: 0 added, 0 changed, 0 destroyed.", match, "Re-applying the same configuration should not change any resources") +} diff --git a/test/src/examples_config_test.go b/test/src/examples_config_test.go index 45e4086..e3e193f 100644 --- a/test/src/examples_config_test.go +++ b/test/src/examples_config_test.go @@ -1,22 +1,38 @@ package test import ( + "github.com/gruntwork-io/terratest/modules/random" "github.com/gruntwork-io/terratest/modules/terraform" + testStructure "github.com/gruntwork-io/terratest/modules/test-structure" + "strings" "testing" ) // Test the Terraform module in examples/config using Terratest. func TestExamplesConfig(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/config" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/config", + 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/examples_escalation_test.go b/test/src/examples_escalation_test.go index 0baf0fd..eee0b7f 100644 --- a/test/src/examples_escalation_test.go +++ b/test/src/examples_escalation_test.go @@ -1,23 +1,39 @@ package test import ( + "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" + "strings" "testing" ) // Test the Terraform module in examples/escalation using Terratest. func TestExamplesEscalation(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/escalation" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/escalation", + 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/examples_integration_action.go b/test/src/examples_integration_action.go index 2b88e89..3de4ede 100644 --- a/test/src/examples_integration_action.go +++ b/test/src/examples_integration_action.go @@ -1,23 +1,38 @@ package test import ( + "github.com/gruntwork-io/terratest/modules/random" "github.com/gruntwork-io/terratest/modules/terraform" + testStructure "github.com/gruntwork-io/terratest/modules/test-structure" + "strings" "testing" ) - // Test the Terraform module in examples/integration_action using Terratest. func TestExamplesIntegrationAction(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/integration_action" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/integration_action", + 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) + // At the end of the test, run `terraform destroy` to clean up any resources that were created + 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/examples_notification_policy_test.go b/test/src/examples_notification_policy_test.go index 59b7563..33b610e 100644 --- a/test/src/examples_notification_policy_test.go +++ b/test/src/examples_notification_policy_test.go @@ -1,23 +1,39 @@ package test import ( + "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" + "strings" "testing" ) // Test the Terraform module in examples/notification_policy using Terratest. func TestExamplesNotificationPolicy(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/notification_policy" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/notification_policy", + 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/examples_team_routing_rule_test.go b/test/src/examples_team_routing_rule_test.go index a8fc1f2..11b097e 100644 --- a/test/src/examples_team_routing_rule_test.go +++ b/test/src/examples_team_routing_rule_test.go @@ -1,23 +1,39 @@ package test import ( + "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" + "strings" "testing" ) // Test the Terraform module in examples/team_routing_rule using Terratest. func TestExamplesTeamRoutingRule(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/team_routing_rule" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/team_routing_rule", + 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/examples_team_test.go b/test/src/examples_team_test.go index 3f99dc8..2f0b3bd 100644 --- a/test/src/examples_team_test.go +++ b/test/src/examples_team_test.go @@ -1,23 +1,39 @@ package test import ( + "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" + "strings" "testing" ) // Test the Terraform module in examples/team using Terratest. func TestExamplesTeam(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/team" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/team", + 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/examples_user_test.go b/test/src/examples_user_test.go index becf7a2..f804e19 100644 --- a/test/src/examples_user_test.go +++ b/test/src/examples_user_test.go @@ -1,23 +1,39 @@ package test import ( + "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" + "strings" "testing" ) -// Test the Terraform module in examples/team using Terratest. +// Test the Terraform module in examples/user using Terratest. func TestExamplesUser(t *testing.T) { + t.Parallel() + randID := strings.ToLower(random.UniqueId()) + attributes := []string{randID} + + rootFolder := "../../" + terraformFolderRelativeToRoot := "examples/user" + varFiles := []string{"fixtures.tfvars"} + + tempTestFolder := testStructure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) + terraformOptions := &terraform.Options{ // The path to where our Terraform code is located - TerraformDir: "../../examples/user", + 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)