Skip to content

Commit cb776c0

Browse files
authored
test: update test framework to use current dependencies (#100)
1 parent 41e8647 commit cb776c0

13 files changed

+1405
-397
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ Available targets:
116116

117117
| Name | Source | Version |
118118
|------|--------|---------|
119-
| <a name="module_dns_master"></a> [dns\_master](#module\_dns\_master) | cloudposse/route53-cluster-hostname/aws | 0.12.2 |
120-
| <a name="module_dns_replicas"></a> [dns\_replicas](#module\_dns\_replicas) | cloudposse/route53-cluster-hostname/aws | 0.12.2 |
121-
| <a name="module_ssm_write_db_password"></a> [ssm\_write\_db\_password](#module\_ssm\_write\_db\_password) | cloudposse/ssm-parameter-store/aws | 0.11.0 |
119+
| <a name="module_dns_master"></a> [dns\_master](#module\_dns\_master) | cloudposse/route53-cluster-hostname/aws | 0.13.0 |
120+
| <a name="module_dns_replicas"></a> [dns\_replicas](#module\_dns\_replicas) | cloudposse/route53-cluster-hostname/aws | 0.13.0 |
121+
| <a name="module_ssm_write_db_password"></a> [ssm\_write\_db\_password](#module\_ssm\_write\_db\_password) | cloudposse/ssm-parameter-store/aws | 0.13.0 |
122122
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |
123123

124124
## Resources

docs/terraform.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
| Name | Source | Version |
2121
|------|--------|---------|
22-
| <a name="module_dns_master"></a> [dns\_master](#module\_dns\_master) | cloudposse/route53-cluster-hostname/aws | 0.12.2 |
23-
| <a name="module_dns_replicas"></a> [dns\_replicas](#module\_dns\_replicas) | cloudposse/route53-cluster-hostname/aws | 0.12.2 |
24-
| <a name="module_ssm_write_db_password"></a> [ssm\_write\_db\_password](#module\_ssm\_write\_db\_password) | cloudposse/ssm-parameter-store/aws | 0.11.0 |
22+
| <a name="module_dns_master"></a> [dns\_master](#module\_dns\_master) | cloudposse/route53-cluster-hostname/aws | 0.13.0 |
23+
| <a name="module_dns_replicas"></a> [dns\_replicas](#module\_dns\_replicas) | cloudposse/route53-cluster-hostname/aws | 0.13.0 |
24+
| <a name="module_ssm_write_db_password"></a> [ssm\_write\_db\_password](#module\_ssm\_write\_db\_password) | cloudposse/ssm-parameter-store/aws | 0.13.0 |
2525
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |
2626

2727
## Resources

examples/complete/fixtures.us-east-2.tfvars

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
enabled = true
2-
31
region = "us-east-2"
42

53
availability_zones = ["us-east-2a", "us-east-2b"]
@@ -12,7 +10,7 @@ name = "documentdb-cluster"
1210

1311
vpc_cidr_block = "172.16.0.0/16"
1412

15-
instance_class = "db.r4.large"
13+
instance_class = "db.t4g.medium"
1614

1715
cluster_size = 1
1816

@@ -26,7 +24,8 @@ retention_period = 5
2624

2725
preferred_backup_window = "07:00-09:00"
2826

29-
cluster_family = "docdb3.6"
27+
cluster_family = "docdb5.0"
28+
engine_version = "5.0.0"
3029

3130
engine = "docdb"
3231

examples/complete/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ provider "aws" {
1616

1717
module "vpc" {
1818
source = "cloudposse/vpc/aws"
19-
version = "2.1.0"
19+
version = "2.1.1"
2020

2121
ipv4_primary_cidr_block = var.vpc_cidr_block
2222

@@ -25,7 +25,7 @@ module "vpc" {
2525

2626
module "subnets" {
2727
source = "cloudposse/dynamic-subnets/aws"
28-
version = "2.3.0"
28+
version = "2.4.2"
2929

3030
availability_zones = var.availability_zones
3131
vpc_id = module.vpc.vpc_id

main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ locals {
132132

133133
module "dns_master" {
134134
source = "cloudposse/route53-cluster-hostname/aws"
135-
version = "0.12.2"
135+
version = "0.13.0"
136136

137137
enabled = module.this.enabled && var.zone_id != "" ? true : false
138138
dns_name = local.cluster_dns_name
@@ -144,7 +144,7 @@ module "dns_master" {
144144

145145
module "dns_replicas" {
146146
source = "cloudposse/route53-cluster-hostname/aws"
147-
version = "0.12.2"
147+
version = "0.13.0"
148148

149149
enabled = module.this.enabled && var.zone_id != "" ? true : false
150150
dns_name = local.replicas_dns_name
@@ -156,7 +156,7 @@ module "dns_replicas" {
156156

157157
module "ssm_write_db_password" {
158158
source = "cloudposse/ssm-parameter-store/aws"
159-
version = "0.11.0"
159+
version = "0.13.0"
160160

161161
enabled = module.this.enabled && var.ssm_parameter_enabled == true ? true : false
162162
parameter_write = [

test/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ clean:
3333
all: module examples/complete
3434

3535
## Run basic sanity checks against the module itself
36-
module: export TESTS ?= installed lint get-modules module-pinning get-plugins provider-pinning validate terraform-docs input-descriptions output-descriptions
36+
module: export TESTS ?= installed lint module-pinning provider-pinning validate terraform-docs input-descriptions output-descriptions
3737
module: deps
3838
$(call RUN_TESTS, ../)
3939

4040
## Run tests against example
41-
examples/complete: export TESTS ?= installed lint get-modules get-plugins validate
41+
examples/complete: export TESTS ?= installed lint validate
4242
examples/complete: deps
4343
$(call RUN_TESTS, ../$@)

test/src/Makefile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
export TF_CLI_ARGS_init ?= -get-plugins=true
2-
export TERRAFORM_VERSION ?= $(shell curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r -M '.current_version' | cut -d. -f1-2)
1+
export TERRAFORM_VERSION ?= $(shell curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r -M '.current_version' | cut -d. -f1)
32

43
.DEFAULT_GOAL : all
54

@@ -16,7 +15,7 @@ init:
1615
## Run tests
1716
test: init
1817
go mod download
19-
go test -v -timeout 60m -run TestExamplesComplete
18+
go test -v -timeout 60m -run '^Test[^_]'
2019

2120
## Run tests in docker container
2221
docker/test:
@@ -27,4 +26,4 @@ docker/test:
2726
.PHONY : clean
2827
## Clean up files
2928
clean:
30-
rm -rf ../../examples/complete/*.tfstate*
29+
rm -rf $(TF_DATA_DIR)

test/src/aws_utils.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// This file is a work-in-progress proposed set of utility functions
2+
// to be standardized across all Cloud Posse Terraform modules.
3+
// Most, if not all, of these functions will be replaced by
4+
// Terratest functions as they become available.
5+
// This file should be considered a temporary solution as of June 2024 and should not be duplicated
6+
7+
package test
8+
9+
// Support AWS operations
10+
// See https://aws.github.io/aws-sdk-go-v2/docs/getting-started/
11+
// and https://pkg.go.dev/github.com/aws/aws-sdk-go-v2
12+
//
13+
// For type conversions, see https://pkg.go.dev/github.com/aws/[email protected]/aws#hdr-Value_and_Pointer_Conversion_Utilities
14+
15+
import (
16+
"context"
17+
"github.com/aws/aws-sdk-go-v2/aws"
18+
"github.com/aws/aws-sdk-go-v2/config"
19+
"github.com/aws/aws-sdk-go-v2/service/ssm"
20+
"github.com/stretchr/testify/assert"
21+
"log"
22+
"testing"
23+
)
24+
25+
func AWSConfig() aws.Config {
26+
return AWSConfigWithRegion("us-east-2")
27+
}
28+
29+
func AWSConfigWithRegion(region string) aws.Config {
30+
// Load the Shared AWS Configuration (~/.aws/config)
31+
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
32+
if err != nil {
33+
log.Fatal(err)
34+
}
35+
36+
return cfg
37+
}
38+
39+
func AssertSSMParameterEqual(t *testing.T, ssmClient *ssm.Client, output map[string]interface{}, ssmPathOutput string, expectedValueOutput string) {
40+
41+
if assert.NotEmpty(t, output[expectedValueOutput], "Missing "+expectedValueOutput) &&
42+
assert.NotEmpty(t, output[ssmPathOutput], "Missing "+ssmPathOutput) {
43+
44+
withDecryption := true
45+
param, err := ssmClient.GetParameter(context.TODO(), &ssm.GetParameterInput{
46+
Name: aws.String(output[ssmPathOutput].(string)),
47+
WithDecryption: &withDecryption,
48+
})
49+
50+
if assert.Nil(t, err, "Unable to retrieve "+ssmPathOutput+" from SSM Parameter Store") {
51+
assert.Equal(t, output[expectedValueOutput].(string), aws.ToString(param.Parameter.Value))
52+
}
53+
}
54+
}
55+
56+
func AssertSSMParameterEmpty(t *testing.T, ssmClient *ssm.Client, output map[string]interface{}, ssmPathOutput string) {
57+
58+
// No path given
59+
if output[ssmPathOutput] == nil || output[ssmPathOutput] == "" {
60+
return
61+
}
62+
63+
withDecryption := true
64+
param, err := ssmClient.GetParameter(context.TODO(), &ssm.GetParameterInput{
65+
Name: aws.String(output[ssmPathOutput].(string)),
66+
WithDecryption: &withDecryption,
67+
})
68+
69+
// If a path is given, there should be an empty value to go with it
70+
if assert.Nil(t, err, "Unable to retrieve "+ssmPathOutput+" from SSM Parameter Store") {
71+
assert.Empty(t, aws.ToString(param.Parameter.Value), "Found non-empty value for "+ssmPathOutput)
72+
}
73+
}
74+
75+
func AssertSSMParameterNotEmpty(t *testing.T, ssmClient *ssm.Client, output map[string]interface{}, ssmPathOutput string) {
76+
77+
if assert.NotEmpty(t, output[ssmPathOutput], "Missing "+ssmPathOutput) {
78+
79+
withDecryption := true
80+
param, err := ssmClient.GetParameter(context.TODO(), &ssm.GetParameterInput{
81+
Name: aws.String(output[ssmPathOutput].(string)),
82+
WithDecryption: &withDecryption,
83+
})
84+
85+
if assert.Nil(t, err, "Unable to retrieve "+ssmPathOutput+" from SSM Parameter Store") {
86+
assert.NotEmpty(t, aws.ToString(param.Parameter.Value), "Retrieved empty value for "+ssmPathOutput)
87+
}
88+
}
89+
}

test/src/default_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// This file is a work-in-progress proposed set of `go` Test wrappers
2+
// to be standardized across all Cloud Posse Terraform modules.
3+
// This is likely to change, especially as we move from local helpers
4+
// to Terratest helpers.
5+
// This file should be considered a temporary solution as of June 2024 and should not be duplicated
6+
7+
package test
8+
9+
import (
10+
"github.com/gruntwork-io/terratest/modules/logger"
11+
"github.com/gruntwork-io/terratest/modules/terraform"
12+
"github.com/stretchr/testify/assert"
13+
"os"
14+
"regexp"
15+
"strings"
16+
"testing"
17+
)
18+
19+
// Test the Terraform module in examples/complete using Terratest.
20+
func TestExamplesComplete(t *testing.T) {
21+
t.Parallel()
22+
23+
testRunner(t, nil, testExamplesComplete)
24+
}
25+
26+
func TestExamplesCompleteDisabled(t *testing.T) {
27+
t.Parallel()
28+
29+
vars := map[string]interface{}{
30+
"enabled": false,
31+
}
32+
testRunner(t, vars, testExamplesCompleteDisabled)
33+
}
34+
35+
func testExamplesCompleteDisabled(t *testing.T, terraformOptions *terraform.Options, randID string, results string) {
36+
// Should complete successfully without creating or changing any resources.
37+
// Extract the "Resources:" section of the output to make the error message more readable.
38+
re := regexp.MustCompile(`Resources: [^.]+\.`)
39+
match := re.FindString(results)
40+
assert.Equal(t, "Resources: 0 added, 0 changed, 0 destroyed.", match, "Re-applying the same configuration should not change any resources")
41+
}
42+
43+
// To speed up debugging, allow running the tests on an existing deployment,
44+
// without creating and destroying one.
45+
// Run this manually by creating a deployment in examples/complete with:
46+
//
47+
// export EXISTING_DEPLOYMENT_ATTRIBUTE="<your-name>"
48+
// terraform init -upgrade
49+
// terraform apply -var-file fixtures.us-east-2.tfvars -var "attributes=[\"$EXISTING_DEPLOYMENT_ATTRIBUTE\"]"
50+
//
51+
// then in this directory (test/src) run
52+
// go test -run Test_ExistingDeployment
53+
54+
func Test_ExistingDeployment(t *testing.T) {
55+
randID := strings.ToLower(os.Getenv("EXISTING_DEPLOYMENT_ATTRIBUTE"))
56+
if randID == "" {
57+
t.Skip("(This is normal): EXISTING_DEPLOYMENT_ATTRIBUTE is not set, skipping...")
58+
return
59+
}
60+
61+
attributes := []string{randID}
62+
63+
varFiles := []string{"fixtures.us-east-2.tfvars"}
64+
65+
terraformOptions := &terraform.Options{
66+
// The path to where our Terraform code is located
67+
TerraformDir: "../../examples/complete",
68+
Upgrade: true,
69+
// Variables to pass to our Terraform code using -var-file options
70+
VarFiles: varFiles,
71+
Vars: map[string]interface{}{
72+
"attributes": attributes,
73+
},
74+
}
75+
76+
// Keep the output quiet
77+
if !testing.Verbose() {
78+
terraformOptions.Logger = logger.Discard
79+
}
80+
81+
testExamplesComplete(t, terraformOptions, randID, "")
82+
}

0 commit comments

Comments
 (0)