-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: update test framework to use current dependencies (#100)
- Loading branch information
Showing
13 changed files
with
1,405 additions
and
397 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
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
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
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
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
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
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
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,89 @@ | ||
// This file is a work-in-progress proposed set of utility functions | ||
// to be standardized across all Cloud Posse Terraform modules. | ||
// Most, if not all, of these functions will be replaced by | ||
// Terratest functions as they become available. | ||
// This file should be considered a temporary solution as of June 2024 and should not be duplicated | ||
|
||
package test | ||
|
||
// Support AWS operations | ||
// See https://aws.github.io/aws-sdk-go-v2/docs/getting-started/ | ||
// and https://pkg.go.dev/github.com/aws/aws-sdk-go-v2 | ||
// | ||
// For type conversions, see https://pkg.go.dev/github.com/aws/[email protected]/aws#hdr-Value_and_Pointer_Conversion_Utilities | ||
|
||
import ( | ||
"context" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/ssm" | ||
"github.com/stretchr/testify/assert" | ||
"log" | ||
"testing" | ||
) | ||
|
||
func AWSConfig() aws.Config { | ||
return AWSConfigWithRegion("us-east-2") | ||
} | ||
|
||
func AWSConfigWithRegion(region string) aws.Config { | ||
// Load the Shared AWS Configuration (~/.aws/config) | ||
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
func AssertSSMParameterEqual(t *testing.T, ssmClient *ssm.Client, output map[string]interface{}, ssmPathOutput string, expectedValueOutput string) { | ||
|
||
if assert.NotEmpty(t, output[expectedValueOutput], "Missing "+expectedValueOutput) && | ||
assert.NotEmpty(t, output[ssmPathOutput], "Missing "+ssmPathOutput) { | ||
|
||
withDecryption := true | ||
param, err := ssmClient.GetParameter(context.TODO(), &ssm.GetParameterInput{ | ||
Name: aws.String(output[ssmPathOutput].(string)), | ||
WithDecryption: &withDecryption, | ||
}) | ||
|
||
if assert.Nil(t, err, "Unable to retrieve "+ssmPathOutput+" from SSM Parameter Store") { | ||
assert.Equal(t, output[expectedValueOutput].(string), aws.ToString(param.Parameter.Value)) | ||
} | ||
} | ||
} | ||
|
||
func AssertSSMParameterEmpty(t *testing.T, ssmClient *ssm.Client, output map[string]interface{}, ssmPathOutput string) { | ||
|
||
// No path given | ||
if output[ssmPathOutput] == nil || output[ssmPathOutput] == "" { | ||
return | ||
} | ||
|
||
withDecryption := true | ||
param, err := ssmClient.GetParameter(context.TODO(), &ssm.GetParameterInput{ | ||
Name: aws.String(output[ssmPathOutput].(string)), | ||
WithDecryption: &withDecryption, | ||
}) | ||
|
||
// If a path is given, there should be an empty value to go with it | ||
if assert.Nil(t, err, "Unable to retrieve "+ssmPathOutput+" from SSM Parameter Store") { | ||
assert.Empty(t, aws.ToString(param.Parameter.Value), "Found non-empty value for "+ssmPathOutput) | ||
} | ||
} | ||
|
||
func AssertSSMParameterNotEmpty(t *testing.T, ssmClient *ssm.Client, output map[string]interface{}, ssmPathOutput string) { | ||
|
||
if assert.NotEmpty(t, output[ssmPathOutput], "Missing "+ssmPathOutput) { | ||
|
||
withDecryption := true | ||
param, err := ssmClient.GetParameter(context.TODO(), &ssm.GetParameterInput{ | ||
Name: aws.String(output[ssmPathOutput].(string)), | ||
WithDecryption: &withDecryption, | ||
}) | ||
|
||
if assert.Nil(t, err, "Unable to retrieve "+ssmPathOutput+" from SSM Parameter Store") { | ||
assert.NotEmpty(t, aws.ToString(param.Parameter.Value), "Retrieved empty value for "+ssmPathOutput) | ||
} | ||
} | ||
} |
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,82 @@ | ||
// This file is a work-in-progress proposed set of `go` Test wrappers | ||
// to be standardized across all Cloud Posse Terraform modules. | ||
// This is likely to change, especially as we move from local helpers | ||
// to Terratest helpers. | ||
// This file should be considered a temporary solution as of June 2024 and should not be duplicated | ||
|
||
package test | ||
|
||
import ( | ||
"github.com/gruntwork-io/terratest/modules/logger" | ||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// Test the Terraform module in examples/complete using Terratest. | ||
func TestExamplesComplete(t *testing.T) { | ||
t.Parallel() | ||
|
||
testRunner(t, nil, testExamplesComplete) | ||
} | ||
|
||
func TestExamplesCompleteDisabled(t *testing.T) { | ||
t.Parallel() | ||
|
||
vars := map[string]interface{}{ | ||
"enabled": false, | ||
} | ||
testRunner(t, vars, testExamplesCompleteDisabled) | ||
} | ||
|
||
func testExamplesCompleteDisabled(t *testing.T, terraformOptions *terraform.Options, randID string, results string) { | ||
// 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") | ||
} | ||
|
||
// To speed up debugging, allow running the tests on an existing deployment, | ||
// without creating and destroying one. | ||
// Run this manually by creating a deployment in examples/complete with: | ||
// | ||
// export EXISTING_DEPLOYMENT_ATTRIBUTE="<your-name>" | ||
// terraform init -upgrade | ||
// terraform apply -var-file fixtures.us-east-2.tfvars -var "attributes=[\"$EXISTING_DEPLOYMENT_ATTRIBUTE\"]" | ||
// | ||
// then in this directory (test/src) run | ||
// go test -run Test_ExistingDeployment | ||
|
||
func Test_ExistingDeployment(t *testing.T) { | ||
randID := strings.ToLower(os.Getenv("EXISTING_DEPLOYMENT_ATTRIBUTE")) | ||
if randID == "" { | ||
t.Skip("(This is normal): EXISTING_DEPLOYMENT_ATTRIBUTE is not set, skipping...") | ||
return | ||
} | ||
|
||
attributes := []string{randID} | ||
|
||
varFiles := []string{"fixtures.us-east-2.tfvars"} | ||
|
||
terraformOptions := &terraform.Options{ | ||
// The path to where our Terraform code is located | ||
TerraformDir: "../../examples/complete", | ||
Upgrade: true, | ||
// Variables to pass to our Terraform code using -var-file options | ||
VarFiles: varFiles, | ||
Vars: map[string]interface{}{ | ||
"attributes": attributes, | ||
}, | ||
} | ||
|
||
// Keep the output quiet | ||
if !testing.Verbose() { | ||
terraformOptions.Logger = logger.Discard | ||
} | ||
|
||
testExamplesComplete(t, terraformOptions, randID, "") | ||
} |
Oops, something went wrong.