-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert to go modules, further enhance tests
- Loading branch information
Showing
9 changed files
with
631 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
example = "Hello, world!" | ||
region = "us-east-2" | ||
|
||
namespace = "eg" | ||
|
||
environment = "ue2" | ||
|
||
stage = "test" | ||
|
||
name = "example" | ||
|
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,50 +1,30 @@ | ||
PACKAGE = terraform-aws-ecs-container-definition | ||
GOEXE ?= /usr/bin/go | ||
GOPATH = $(CURDIR)/.gopath | ||
GOBIN = $(GOPATH)/bin | ||
BASE = $(GOPATH)/src/$(PACKAGE) | ||
PATH := $(PATH):$(GOBIN) | ||
|
||
export TF_DATA_DIR ?= $(CURDIR)/.terraform | ||
export TF_CLI_ARGS_init ?= -get-plugins=true | ||
export GOPATH | ||
export TERRAFORM_VERSION ?= $(shell curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | jq -r -M '.current_version' | cut -d. -f1-2) | ||
|
||
.DEFAULT_GOAL : all | ||
.PHONY: all | ||
|
||
## Default target | ||
all: test | ||
|
||
ifneq (,$(wildcard /sbin/apk)) | ||
## Install go, if not installed | ||
$(GOEXE): | ||
apk add --update go | ||
endif | ||
|
||
ifneq ($(findstring $(shell uname -s),Linux Darwin),) | ||
## Install all `dep`, if not installed | ||
$(GOBIN)/dep: | ||
@mkdir -p $(GOBIN) | ||
@curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh | ||
endif | ||
|
||
## Prepare the GOPATH | ||
$(BASE): $(GOEXE) | ||
@mkdir -p $(dir $@) | ||
@ln -sf $(CURDIR) $@ | ||
|
||
## Download vendor dependencies to vendor/ | ||
$(BASE)/vendor: $(BASE) $(GOBIN)/dep | ||
cd $(BASE) && dep ensure | ||
|
||
.PHONY : init | ||
## Initialize tests | ||
init: $(BASE)/vendor | ||
init: | ||
@exit 0 | ||
|
||
.PHONY : test | ||
## Run tests | ||
test: init | ||
cd $(BASE) && go test -v -timeout 30m -run TestExamplesComplete | ||
go mod download | ||
go test -v -timeout 60m -run TestExamplesComplete | ||
|
||
## Run tests in docker container | ||
docker/test: | ||
docker run --name terratest --rm -it -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_SESSION_TOKEN -e GITHUB_TOKEN \ | ||
-e PATH="/usr/local/terraform/$(TERRAFORM_VERSION)/bin:/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \ | ||
-v $(CURDIR)/../../:/module/ cloudposse/test-harness:latest -C /module/test/src test | ||
|
||
.PHONY : clean | ||
## Clean up files | ||
clean: | ||
rm -rf .gopath/ vendor/ $(TF_DATA_DIR) | ||
rm -rf ../../examples/complete/*.tfstate* |
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 |
---|---|---|
@@ -1,32 +1,81 @@ | ||
package test | ||
|
||
import ( | ||
"math/rand" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
// Test the Terraform module in examples/complete using Terratest. | ||
func TestExamplesComplete(t *testing.T) { | ||
t.Parallel() | ||
|
||
randId := strconv.Itoa(rand.Intn(100000)) | ||
attributes := []string{randId} | ||
|
||
exampleInput := "Hello, world!" | ||
|
||
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: []string{"fixtures.us-east-2.tfvars"}, | ||
// We always include a random attribute so that parallel tests | ||
// and AWS resources do not interfere with each other | ||
Vars: map[string]interface{}{ | ||
"attributes": attributes, | ||
"example": exampleInput, | ||
}, | ||
} | ||
|
||
// At the end of the test, run `terraform destroy` to clean up any resources that were created | ||
defer terraform.Destroy(t, terraformOptions) | ||
|
||
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors | ||
terraform.InitAndApply(t, terraformOptions) | ||
|
||
// Run `terraform output` to get the value of an output variable | ||
outputExample := terraform.Output(t, terraformOptions, "example") | ||
id := terraform.Output(t, terraformOptions, "id") | ||
example := terraform.Output(t, terraformOptions, "example") | ||
random := terraform.Output(t, terraformOptions, "random") | ||
|
||
// Verify we're getting back the outputs we expect | ||
assert.Regexp(t, "^Hello world! [0-9]+$", outputExample) | ||
// Ensure we get a random number appended | ||
assert.Equal(t, exampleInput+" "+random, example) | ||
// Ensure we get the attribute included in the ID | ||
assert.Equal(t, "eg-ue2-test-example-"+randId, id) | ||
|
||
// ************************************************************************ | ||
// This steps below are unusual, not generally part of the testing | ||
// but included here as an example of testing this specific module. | ||
// This module has a random number that is supposed to change | ||
// only when the example changes. So we run it again to ensure | ||
// it does not change. | ||
|
||
// This will run `terraform apply` a second time and fail the test if there are any errors | ||
terraform.Apply(t, terraformOptions) | ||
|
||
id2 := terraform.Output(t, terraformOptions, "id") | ||
example2 := terraform.Output(t, terraformOptions, "example") | ||
random2 := terraform.Output(t, terraformOptions, "random") | ||
|
||
assert.Equal(t, id, id2, "Expected `id` to be stable") | ||
assert.Equal(t, example, example2, "Expected `example` to be stable") | ||
assert.Equal(t, random, random2, "Expected `random` to be stable") | ||
|
||
// Then we run change the example and run it a third time and | ||
// verify that the random number changed | ||
newExample := "Goodbye" | ||
terraformOptions.Vars["example"] = newExample | ||
terraform.Apply(t, terraformOptions) | ||
|
||
example3 := terraform.Output(t, terraformOptions, "example") | ||
random3 := terraform.Output(t, terraformOptions, "random") | ||
|
||
assert.NotEqual(t, random, random3, "Expected `random` to change when `example` changed") | ||
assert.Equal(t, newExample+" "+random3, example3, "Expected `example` to use new random number") | ||
|
||
} |
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,12 @@ | ||
module github.com/cloudposse/terraform-example-module | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/gruntwork-io/terratest v0.28.15 | ||
github.com/stretchr/testify v1.6.1 | ||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect | ||
golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect | ||
golang.org/x/sys v0.0.0-20200828194041-157a740278f4 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect | ||
) |
Oops, something went wrong.