Skip to content

Commit 4d9f56f

Browse files
authored
Add terratest (#58)
* Add terratest * Add alpine makefile
1 parent 5f28e20 commit 4d9f56f

File tree

8 files changed

+210
-9
lines changed

8 files changed

+210
-9
lines changed

codefresh/test.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ steps:
2121
commands:
2222
- make init
2323
- make -C test/ clean init
24+
- make -C test/src clean init
2425

2526
test:
2627
type: "parallel"
@@ -37,15 +38,22 @@ steps:
3738
- make readme/lint
3839

3940
test_module:
40-
title: Test module
41+
title: Test module with bats
4142
image: ${{TEST_IMAGE}}
4243
stage: Test
4344
commands:
4445
- make -C test/ module
4546

4647
test_examples_complete:
47-
title: Test "examples/complete"
48+
title: Test "examples/complete" with bats
4849
image: ${{TEST_IMAGE}}
4950
stage: Test
5051
commands:
5152
- make -C test/ examples/complete
53+
54+
test_examples_complete_terratest:
55+
title: Test "examples/complete" with terratest
56+
image: ${{TEST_IMAGE}}
57+
stage: Test
58+
commands:
59+
- make -C test/src

test/Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ endef
1212

1313
default: all
1414

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

19-
# Initialize the tests
19+
## Initialize the tests
2020
init: .test-harness
2121

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

26-
# Run all tests
26+
## Run all tests
2727
all: module examples/complete
2828

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

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

38-
# Run tests against example
38+
## Run tests against example
3939
examples/complete: export TESTS ?= init plan apply
4040
examples/complete: deps
4141
$(call RUN_TESTS, ../$@)

test/src/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.gopath
2+
vendor/

test/src/Gopkg.lock

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/src/Gopkg.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[[constraint]]
2+
name = "github.com/stretchr/testify"
3+
version = "1.2.2"
4+
5+
[prune]
6+
go-tests = true
7+
unused-packages = true

test/src/Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
PACKAGE = terraform-null-label
2+
GOPATH = $(CURDIR)/.gopath
3+
GOBIN = $(GOPATH)/bin
4+
BASE = $(GOPATH)/src/$(PACKAGE)
5+
PATH := $(PATH):$(GOBIN)
6+
7+
.PHONY: all
8+
## Default target
9+
all: test
10+
11+
-include Makefile.*
12+
13+
export GOPATH
14+
15+
## Prepare the GOPATH
16+
$(BASE):
17+
@mkdir -p $(dir $@)
18+
@ln -sf $(CURDIR) $@
19+
20+
## install OS-specific dependencies. See Makefile.*
21+
deps::
22+
@exit 0
23+
24+
.PHONY : init
25+
## Initialize tests
26+
init: | $(BASE)
27+
28+
.PHONY : ensure
29+
## Install test dependencies
30+
ensure:
31+
cd $(BASE) && dep ensure
32+
33+
.PHONY : test
34+
## Run tests
35+
test: deps ensure
36+
cd $(BASE) && go test -v -timeout 30m -run TestExamplesComplete
37+
38+
## Clean up files
39+
clean:
40+
rm -rf .gopath/ vendor/
41+
42+

test/src/Makefile.alpine

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ifneq (,$(wildcard /sbin/apk))
2+
.PHONY : deps
3+
deps::
4+
mkdir -p $(GOBIN)
5+
apk add --update go
6+
@curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
7+
endif
8+

test/src/examples_complete_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gruntwork-io/terratest/modules/terraform"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// Test the Terraform module in examples/complete using Terratest.
11+
func TestExamplesComplete(t *testing.T) {
12+
t.Parallel()
13+
14+
terraformOptions := &terraform.Options{
15+
// The path to where our Terraform code is located
16+
TerraformDir: "../../examples/complete",
17+
}
18+
19+
// At the end of the test, run `terraform destroy` to clean up any resources that were created
20+
defer terraform.Destroy(t, terraformOptions)
21+
22+
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
23+
terraform.InitAndApply(t, terraformOptions)
24+
25+
// Run `terraform output` to get the value of an output variable
26+
label1 := terraform.OutputMap(t, terraformOptions, "label1")
27+
28+
// Verify we're getting back the outputs we expect
29+
assert.Equal(t, "winstonchurchroom-uat-build-fire-water-earth-air", label1["id"])
30+
31+
// Run `terraform output` to get the value of an output variable
32+
label2 := terraform.OutputMap(t, terraformOptions, "label2")
33+
34+
// Verify we're getting back the outputs we expect
35+
assert.Equal(t, "charlie+uat+test+fire+water+earth+air", label2["id"])
36+
37+
// Run `terraform output` to get the value of an output variable
38+
label3 := terraform.OutputMap(t, terraformOptions, "label3")
39+
40+
// Verify we're getting back the outputs we expect
41+
assert.Equal(t, "starfish.uat.release.fire.water.earth.air", label3["id"])
42+
}

0 commit comments

Comments
 (0)