Skip to content

Commit

Permalink
Init terratest and add first basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsan92 committed Mar 11, 2019
1 parent 38d956b commit 395d60c
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 6 deletions.
25 changes: 20 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
sudo: required
dist: trusty
language: minimal
language: go

go:
- 1.12.x

services:
- docker
Expand All @@ -13,11 +16,23 @@ cache:
directories:
- .terraform

env:
global:
- GOCACHE=off
- GO111MODULE=on
- PATH=$PATH:$TRAVIS_BUILD_DIR/bin

# AWS_CREDENTIALS_FILE
- secure: aWQuVXmzSPGSoTpwAY72mGrbB/qFyWMhgSrgHc8+rJNBv/q6QxcsxhUURKRy5CGo0pm9R+yqKVq62tSh5cX+KDxMhBEAqB2nLwWdKNXjmkwL2rN3LYHFQh+UPdySIcspuGGJBH8YeyMgIpZRwpp/hYSAWg0a1UMaG5PsF1UswEJyWiKs9DwEN4GOlYRhn4eIeZWnXZNJl2ZvkSkoPhp2HXXPrpo5JxL79XqX7F9GxLonm3JCCH/mcNb6o9U6b5I5Y8TXD0V7JKCyPsBKtQ71sSqf/S8ScPvu6M0uoyRPPAwCxPc1DN6U1OtMjTIv3msEkzvDzELaCcRysA2CX+tisHJapYxqDB0u7uyERzgueiB6uvh2j5JemuC4USQSWl3QT3pDwe2cuwQoGF7+DguFWgWpe3NI+RhtvXbM2WFUPkC/tK9p3rSpX7cc7rlZBsXvLcYF6kIJEsbp4InCUqjelCGFJVBi0I2BUfYvXAvM0gUd5zh8/uektP8xQNHwPefVDpWMcNQNBDpceZ+Y9C723IVp0sts+8w2fYl0jiwrCtt4OHVVFH/kf02VqpzsFBi6dJNW+D5ftjTv3PKDCbJUIk1CividOW8NncUi7xKxLHEhXMTFLal3ViM+VmMb6qIIcnFcoHWFPJcjenOoNpnKArQYEp+SUN9d3+vRDpWvMQo=

before_install: ./ci/before_install.sh

script:
- ./bin/terraform init -input=false
- ./bin/terraform validate -check-variables=false .
- if [[ -n "$(./bin/terraform fmt -write=false)" ]]; then echo "Some terraform files need be formatted, run 'terraform fmt' to fix"; exit 1; fi
- ./bin/tflint --debug
- terraform init -input=false
- terraform validate -check-variables=false .
- if [[ -n "$(terraform fmt -write=false)" ]]; then echo "Some terraform files need be formatted, run 'terraform fmt' to fix"; exit 1; fi
- tflint --debug
- $(cd test && go test)

notifications:
email: false
8 changes: 7 additions & 1 deletion bin/terraform
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
#!/usr/bin/env bash

AWS_PROFILE=${AWS_PROFILE:-"test"}
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-"us-east-1"}

exec docker run \
--name terraform-cli \
--interactive \
--tty \
--rm \
--workdir /tmp/workspace/terraform \
--volume "${HOME}/.aws":/root/.aws \
--volume "$(pwd)":/tmp/workspace/terraform \
--volume "$(cd .. && pwd)":/tmp/workspace/fargate-module \
--env AWS_PROFILE="${AWS_PROFILE}" \
--env AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}" \
hashicorp/terraform:0.11.11 "${@}"
7 changes: 7 additions & 0 deletions ci/before_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

set -o errexit
set -o pipefail

# Extract AWS credentials file so we can use AWS stuff without exporting access key/secret
mkdir -p ~/.aws && echo ${AWS_CREDENTIALS_FILE} | base64 -d > ~/.aws/credentials
24 changes: 24 additions & 0 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Base
FROM node:alpine AS base
ENV HOME=/home/node
WORKDIR $HOME/app
RUN chown -R node:node $HOME/*
USER node
COPY package*.json ./

# Dependencies
FROM base AS dependencies
RUN npm set progress=false && npm config set depth 0
RUN npm install --only=production
RUN cp -R node_modules prod_node_modules
RUN npm install

# Release
FROM dependencies as release

COPY --from=dependencies $HOME/app/prod_node_modules ./node_modules
COPY . .

EXPOSE 3000

CMD ["node", "."]
21 changes: 21 additions & 0 deletions test/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"portMappings": [
{
"hostPort": ${container_port},
"protocol": "tcp",
"containerPort": ${container_port}
}
],
"image": "${repository_url}:latest",
"name": "${container_name}",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "${log_group}",
"awslogs-region": "${region}",
"awslogs-stream-prefix": "ecs"
}
}
}
]
39 changes: 39 additions & 0 deletions test/basic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test

import (
"fmt"
"strings"
"testing"

"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
)

func TestBasicExample(t *testing.T) {
expectedName := fmt.Sprintf("fargate-%s", strings.ToLower(random.UniqueId()))

// Pick a random AWS region to test in. This helps ensure the module works in all regions.
awsRegion := aws.GetRandomStableRegion(t, nil, nil)

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

// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"name": expectedName,
},

// Environment variables to set when running Terraform
EnvVars: map[string]string{
"AWS_DEFAULT_REGION": awsRegion,
},
}

// 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)
}
15 changes: 15 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/strvcom/terraform-aws-fargate

go 1.12

require (
github.com/aws/aws-sdk-go v1.17.14 // indirect
github.com/boombuler/barcode v1.0.0 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/gruntwork-io/terratest v0.14.2
github.com/pquerna/otp v1.1.0 // indirect
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 // indirect
golang.org/x/net v0.0.0-20190311031020-56fb01167e7d // indirect
)
26 changes: 26 additions & 0 deletions test/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/aws/aws-sdk-go v1.17.14 h1:IjqZDIQoLyZ48A93BxVrZOaIGgZPRi4nXt6WQUMJplY=
github.com/aws/aws-sdk-go v1.17.14/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/boombuler/barcode v1.0.0 h1:s1TvRnXwL2xJRaccrdcBQMZxq6X7DvsMogtmJeHDdrc=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gruntwork-io/terratest v0.14.2 h1:A9YUZZlXE/syTnIVeuqhqoyVO5CUJS5Kasvyr5IUsv8=
github.com/gruntwork-io/terratest v0.14.2/go.mod h1:NjUn6YXA5Skxt8Rs20t3isYx5Rl+EgvGB8/+RRXddqk=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/otp v1.1.0 h1:q2gMsMuMl3JzneUaAX1MRGxLvOG6bzXV51hivBaStf0=
github.com/pquerna/otp v1.1.0/go.mod h1:Zad1CMQfSQZI5KLpahDiSUX4tMMREnXw98IvL1nhgMk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311031020-56fb01167e7d h1:vQJbQvu6+H699vOmHa20TEBI9nEqroRbMtf/9biIE3A=
golang.org/x/net v0.0.0-20190311031020-56fb01167e7d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express')
const os = require('os')

const hostname = os.hostname()
const app = express()

app.listen(3000, () => console.log(`Example app listening on port 3000! Host: ${hostname}`))

app.get('/', async (req, res) => res.json({ hostname }))
33 changes: 33 additions & 0 deletions test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
terraform {
required_version = "~> 0.11.11"
}

provider "aws" {
version = "~> 1.54.0"
profile = "test"
}

variable "name" {}

module "fargate" {
source = "../fargate-module"

vpc_create_nat = false

name = "${var.name}"

services = {
api = {
task_definition = "api.json"
container_port = 3000
cpu = "256"
memory = "512"
replicas = 3

registry_retention_count = 15
logs_retention_days = 14
}
}

codepipeline_events_enabled = true
}

0 comments on commit 395d60c

Please sign in to comment.