Skip to content

Commit 986386c

Browse files
authored
Update testing framework (#157)
1 parent 03b3277 commit 986386c

File tree

10 files changed

+907
-44
lines changed

10 files changed

+907
-44
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ SHELL := /bin/bash
33
# List of targets the `readme` target should call before generating the readme
44
export README_DEPS ?= docs/targets.md docs/terraform.md
55

6-
-include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness)
6+
-include $(shell curl -sSL -o .build-harness "https://cloudposse.tools/build-harness"; echo .build-harness)
77

88
## Lint terraform code
99
lint:
10-
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate
10+
$(SELF) terraform/install terraform/get-modules terraform/lint terraform/validate

examples/existing-ips/fixtures.us-east-2.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ namespace = "eg"
66

77
stage = "test"
88

9-
name = "subnets-vpc-test"
9+
name = "subnets-vpc-eip-test"

examples/existing-ips/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ provider "aws" {
44

55
module "vpc" {
66
source = "cloudposse/vpc/aws"
7-
version = "0.18.1"
7+
version = "1.1.0"
88

99
cidr_block = "172.16.0.0/16"
1010

examples/existing-ips/outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "existing_ips" {
2+
value = values(aws_eip.nat_ips).*.public_ip
3+
}
4+
5+
output "nat_ips" {
6+
value = module.subnets.nat_ips
7+
}

test/Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +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
43-
rm -rf ../examples/complete/*.tfstate
4443
$(call RUN_TESTS, ../$@)

test/src/Makefile

Lines changed: 2 additions & 3 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
.PHONY: all
@@ -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
2019

2120
## Run tests in docker container
2221
docker/test:

test/src/examples_complete_test.go

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
11
package test
22

33
import (
4+
"github.com/gruntwork-io/terratest/modules/random"
5+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
6+
"os"
7+
"strings"
48
"testing"
59

610
"github.com/gruntwork-io/terratest/modules/terraform"
711
"github.com/stretchr/testify/assert"
812
)
913

14+
func cleanup(t *testing.T, terraformOptions *terraform.Options, tempTestFolder string) {
15+
terraform.Destroy(t, terraformOptions)
16+
os.RemoveAll(tempTestFolder)
17+
}
18+
1019
// Test the Terraform module in examples/complete using Terratest.
1120
func TestExamplesComplete(t *testing.T) {
1221
t.Parallel()
22+
randID := strings.ToLower(random.UniqueId())
23+
attributes := []string{randID}
24+
25+
rootFolder := "../../"
26+
terraformFolderRelativeToRoot := "examples/complete"
27+
varFiles := []string{"fixtures.us-east-2.tfvars"}
28+
29+
tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot)
1330

1431
terraformOptions := &terraform.Options{
1532
// The path to where our Terraform code is located
16-
TerraformDir: "../../examples/complete",
33+
TerraformDir: tempTestFolder,
1734
Upgrade: true,
1835
// Variables to pass to our Terraform code using -var-file options
19-
VarFiles: []string{"fixtures.us-east-2.tfvars"},
36+
VarFiles: varFiles,
37+
Vars: map[string]interface{}{
38+
"attributes": attributes,
39+
},
2040
}
2141

2242
// At the end of the test, run `terraform destroy` to clean up any resources that were created
23-
defer terraform.Destroy(t, terraformOptions)
43+
defer cleanup(t, terraformOptions, tempTestFolder)
2444

2545
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
2646
terraform.InitAndApply(t, terraformOptions)
@@ -39,3 +59,36 @@ func TestExamplesComplete(t *testing.T) {
3959
// Verify we're getting back the outputs we expect
4060
assert.Equal(t, expectedPublicSubnetCidrs, publicSubnetCidrs)
4161
}
62+
63+
func TestExamplesCompleteDisabled(t *testing.T) {
64+
t.Parallel()
65+
randID := strings.ToLower(random.UniqueId())
66+
attributes := []string{randID}
67+
68+
rootFolder := "../../"
69+
terraformFolderRelativeToRoot := "examples/complete"
70+
varFiles := []string{"fixtures.us-east-2.tfvars"}
71+
72+
tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot)
73+
74+
terraformOptions := &terraform.Options{
75+
// The path to where our Terraform code is located
76+
TerraformDir: tempTestFolder,
77+
Upgrade: true,
78+
// Variables to pass to our Terraform code using -var-file options
79+
VarFiles: varFiles,
80+
Vars: map[string]interface{}{
81+
"attributes": attributes,
82+
"enabled": "false",
83+
},
84+
}
85+
86+
// At the end of the test, run `terraform destroy` to clean up any resources that were created
87+
defer cleanup(t, terraformOptions, tempTestFolder)
88+
89+
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
90+
results := terraform.InitAndApply(t, terraformOptions)
91+
92+
// Should complete successfully without creating or changing any resources
93+
assert.Contains(t, results, "Resources: 0 added, 0 changed, 0 destroyed.")
94+
}
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package test
22

33
import (
4+
"github.com/gruntwork-io/terratest/modules/random"
5+
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
6+
"strings"
47
"testing"
58

69
"github.com/gruntwork-io/terratest/modules/terraform"
@@ -10,25 +13,36 @@ import (
1013
// Test the Terraform module in examples/existing-ips using Terratest.
1114
func TestExamplesExistingIps(t *testing.T) {
1215
t.Parallel()
16+
randID := strings.ToLower(random.UniqueId())
17+
attributes := []string{randID}
18+
19+
rootFolder := "../../"
20+
terraformFolderRelativeToRoot := "examples/existing-ips"
21+
varFiles := []string{"fixtures.us-east-2.tfvars"}
22+
23+
tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot)
1324

1425
terraformOptions := &terraform.Options{
1526
// The path to where our Terraform code is located
16-
TerraformDir: "../../examples/existing-ips",
27+
TerraformDir: tempTestFolder,
1728
Upgrade: true,
1829
// Variables to pass to our Terraform code using -var-file options
19-
VarFiles: []string{"fixtures.us-east-2.tfvars"},
30+
VarFiles: varFiles,
31+
Vars: map[string]interface{}{
32+
"attributes": attributes,
33+
},
2034
}
2135

2236
// At the end of the test, run `terraform destroy` to clean up any resources that were created
23-
defer terraform.Destroy(t, terraformOptions)
37+
defer cleanup(t, terraformOptions, tempTestFolder)
2438

2539
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
2640
terraform.InitAndApply(t, terraformOptions)
2741

2842
// Run `terraform output` to get the value of an output variable
2943
usedNatIps := terraform.OutputList(t, terraformOptions, "nat_ips")
30-
expectedNatIps := []string{"3.52.100.1", "3.52.100.2", "3.52.100.3"}
44+
existingIps := terraform.OutputList(t, terraformOptions, "existing_ips")
3145

3246
// Verify we're getting back the outputs we expect
33-
assert.Equal(t, expectedNatIps, usedNatIps)
47+
assert.ElementsMatch(t, existingIps, usedNatIps)
3448
}

test/src/go.mod

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,88 @@
11
module github.com/cloudposse/terraform-aws-dynamic-subnets
22

3-
go 1.14
3+
go 1.17
44

55
require (
6-
github.com/aws/aws-sdk-go v1.34.11 // indirect
6+
github.com/gruntwork-io/terratest v0.39.0
7+
github.com/stretchr/testify v1.7.0
8+
)
9+
10+
require (
11+
cloud.google.com/go v0.83.0 // indirect
12+
cloud.google.com/go/storage v1.10.0 // indirect
13+
github.com/agext/levenshtein v1.2.3 // indirect
14+
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
15+
github.com/aws/aws-sdk-go v1.40.56 // indirect
16+
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
17+
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
18+
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
719
github.com/davecgh/go-spew v1.1.1 // indirect
8-
github.com/google/uuid v1.1.1 // indirect
9-
github.com/gruntwork-io/terratest v0.16.0
20+
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
21+
github.com/go-errors/errors v1.0.2-0.20180813162953-d98b870cc4e0 // indirect
22+
github.com/go-logr/logr v0.2.0 // indirect
23+
github.com/go-sql-driver/mysql v1.4.1 // indirect
24+
github.com/gogo/protobuf v1.3.2 // indirect
25+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
26+
github.com/golang/protobuf v1.5.2 // indirect
27+
github.com/golang/snappy v0.0.3 // indirect
28+
github.com/google/gofuzz v1.1.0 // indirect
29+
github.com/google/uuid v1.2.0 // indirect
30+
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
31+
github.com/googleapis/gnostic v0.4.1 // indirect
32+
github.com/gruntwork-io/go-commons v0.8.0 // indirect
33+
github.com/hashicorp/errwrap v1.0.0 // indirect
34+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
35+
github.com/hashicorp/go-getter v1.5.9 // indirect
36+
github.com/hashicorp/go-multierror v1.1.0 // indirect
37+
github.com/hashicorp/go-safetemp v1.0.0 // indirect
38+
github.com/hashicorp/go-version v1.3.0 // indirect
39+
github.com/hashicorp/hcl/v2 v2.9.1 // indirect
40+
github.com/hashicorp/terraform-json v0.13.0 // indirect
41+
github.com/imdario/mergo v0.3.11 // indirect
42+
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a // indirect
43+
github.com/jmespath/go-jmespath v0.4.0 // indirect
44+
github.com/json-iterator/go v1.1.11 // indirect
45+
github.com/jstemmer/go-junit-report v0.9.1 // indirect
46+
github.com/klauspost/compress v1.13.0 // indirect
47+
github.com/mattn/go-zglob v0.0.2-0.20190814121620-e3c945676326 // indirect
48+
github.com/mitchellh/go-homedir v1.1.0 // indirect
49+
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
50+
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
51+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
52+
github.com/modern-go/reflect2 v1.0.1 // indirect
53+
github.com/pmezard/go-difflib v1.0.0 // indirect
1054
github.com/pquerna/otp v1.2.0 // indirect
11-
github.com/stretchr/testify v1.5.1
12-
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f // indirect
13-
golang.org/x/sys v0.0.0-20190527104216-9cd6430ef91e // indirect
55+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
56+
github.com/spf13/pflag v1.0.5 // indirect
57+
github.com/tmccombs/hcl2json v0.3.3 // indirect
58+
github.com/ulikunitz/xz v0.5.8 // indirect
59+
github.com/urfave/cli v1.22.2 // indirect
60+
github.com/zclconf/go-cty v1.9.1 // indirect
61+
go.opencensus.io v0.23.0 // indirect
62+
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
63+
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
64+
golang.org/x/mod v0.4.2 // indirect
65+
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
66+
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c // indirect
67+
golang.org/x/sys v0.0.0-20210603125802-9665404d3644 // indirect
68+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
69+
golang.org/x/text v0.3.6 // indirect
70+
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
71+
golang.org/x/tools v0.1.2 // indirect
72+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
73+
google.golang.org/api v0.47.0 // indirect
74+
google.golang.org/appengine v1.6.7 // indirect
75+
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
76+
google.golang.org/grpc v1.38.0 // indirect
77+
google.golang.org/protobuf v1.26.0 // indirect
78+
gopkg.in/inf.v0 v0.9.1 // indirect
79+
gopkg.in/yaml.v2 v2.4.0 // indirect
80+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
81+
k8s.io/api v0.20.6 // indirect
82+
k8s.io/apimachinery v0.20.6 // indirect
83+
k8s.io/client-go v0.20.6 // indirect
84+
k8s.io/klog/v2 v2.4.0 // indirect
85+
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
86+
sigs.k8s.io/structured-merge-diff/v4 v4.0.3 // indirect
87+
sigs.k8s.io/yaml v1.2.0 // indirect
1488
)

0 commit comments

Comments
 (0)