Skip to content

Commit

Permalink
Convert to go modules, further enhance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuru committed Aug 28, 2020
1 parent e21c71c commit f3739c1
Show file tree
Hide file tree
Showing 9 changed files with 631 additions and 138 deletions.
11 changes: 10 additions & 1 deletion examples/complete/fixtures.us-east-2.tfvars
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"

5 changes: 5 additions & 0 deletions examples/complete/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
output "id" {
description = "ID of the created example"
value = module.example.id
}

output "example" {
description = "Output \"example\" from example module"
value = module.example.example
Expand Down
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
output "id" {
description = "ID of the created example"
value = module.this.enabled ? module.this.id : null
}

output "example" {
description = "Example output"
value = module.this.enabled ? local.example : null
Expand Down
92 changes: 0 additions & 92 deletions test/src/Gopkg.lock

This file was deleted.

7 changes: 0 additions & 7 deletions test/src/Gopkg.toml

This file was deleted.

48 changes: 14 additions & 34 deletions test/src/Makefile
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*
57 changes: 53 additions & 4 deletions test/src/examples_complete_test.go
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")

}
12 changes: 12 additions & 0 deletions test/src/go.mod
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
)
Loading

0 comments on commit f3739c1

Please sign in to comment.