Skip to content

Commit

Permalink
Add terratest (#58)
Browse files Browse the repository at this point in the history
* Add terratest

* Add alpine makefile
  • Loading branch information
osterman authored May 28, 2019
1 parent 5f28e20 commit 4d9f56f
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 9 deletions.
12 changes: 10 additions & 2 deletions codefresh/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ steps:
commands:
- make init
- make -C test/ clean init
- make -C test/src clean init

test:
type: "parallel"
Expand All @@ -37,15 +38,22 @@ steps:
- make readme/lint

test_module:
title: Test module
title: Test module with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ module

test_examples_complete:
title: Test "examples/complete"
title: Test "examples/complete" with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ examples/complete

test_examples_complete_terratest:
title: Test "examples/complete" with terratest
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/src
14 changes: 7 additions & 7 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ endef

default: all

# Provision the test-harnesss
## Provision the test-harnesss
.test-harness:
[ -d $@ ] || git clone --depth=1 -b $(TEST_HARNESS_BRANCH) $(TEST_HARNESS) $@

# Initialize the tests
## Initialize the tests
init: .test-harness

# Clean up the test harness
## Clean up the test harness
clean:
[ "$(TEST_HARNESS_PATH)" == "/" ] || rm -rf $(TEST_HARNESS_PATH)

# Run all tests
## Run all tests
all: module examples/complete

# Install all dependencies (OS specific)
## Install all dependencies (OS specific)
deps: init
@[ ! -x /sbin/apk ] || apk add --update terraform-docs@cloudposse

# Run basic sanity checks against the module itself
## Run basic sanity checks against the module itself
module: export TESTS ?= installed lint get-modules validate terraform-docs input-descriptions output-descriptions
module: deps
$(call RUN_TESTS, ../)

# Run tests against example
## Run tests against example
examples/complete: export TESTS ?= init plan apply
examples/complete: deps
$(call RUN_TESTS, ../$@)
2 changes: 2 additions & 0 deletions test/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.gopath
vendor/
92 changes: 92 additions & 0 deletions test/src/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/src/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.2"

[prune]
go-tests = true
unused-packages = true
42 changes: 42 additions & 0 deletions test/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
PACKAGE = terraform-null-label
GOPATH = $(CURDIR)/.gopath
GOBIN = $(GOPATH)/bin
BASE = $(GOPATH)/src/$(PACKAGE)
PATH := $(PATH):$(GOBIN)

.PHONY: all
## Default target
all: test

-include Makefile.*

export GOPATH

## Prepare the GOPATH
$(BASE):
@mkdir -p $(dir $@)
@ln -sf $(CURDIR) $@

## install OS-specific dependencies. See Makefile.*
deps::
@exit 0

.PHONY : init
## Initialize tests
init: | $(BASE)

.PHONY : ensure
## Install test dependencies
ensure:
cd $(BASE) && dep ensure

.PHONY : test
## Run tests
test: deps ensure
cd $(BASE) && go test -v -timeout 30m -run TestExamplesComplete

## Clean up files
clean:
rm -rf .gopath/ vendor/


8 changes: 8 additions & 0 deletions test/src/Makefile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ifneq (,$(wildcard /sbin/apk))
.PHONY : deps
deps::
mkdir -p $(GOBIN)
apk add --update go
@curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
endif

42 changes: 42 additions & 0 deletions test/src/examples_complete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package test

import (
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)

// Test the Terraform module in examples/complete using Terratest.
func TestExamplesComplete(t *testing.T) {
t.Parallel()

terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../../examples/complete",
}

// 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
label1 := terraform.OutputMap(t, terraformOptions, "label1")

// Verify we're getting back the outputs we expect
assert.Equal(t, "winstonchurchroom-uat-build-fire-water-earth-air", label1["id"])

// Run `terraform output` to get the value of an output variable
label2 := terraform.OutputMap(t, terraformOptions, "label2")

// Verify we're getting back the outputs we expect
assert.Equal(t, "charlie+uat+test+fire+water+earth+air", label2["id"])

// Run `terraform output` to get the value of an output variable
label3 := terraform.OutputMap(t, terraformOptions, "label3")

// Verify we're getting back the outputs we expect
assert.Equal(t, "starfish.uat.release.fire.water.earth.air", label3["id"])
}

0 comments on commit 4d9f56f

Please sign in to comment.