Skip to content

Commit

Permalink
feat: add terraform tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jta committed Oct 17, 2023
1 parent 0bda1aa commit 7b0eb2d
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 0 deletions.
10 changes: 10 additions & 0 deletions integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Integration tests

This directory contains integration tests for our apps. Tests are written using the [terraform test framework](https://developer.hashicorp.com/terraform/language/tests)

To run the full test suite, run:

```
terraform init
terraform test
```
12 changes: 12 additions & 0 deletions integration/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
data "aws_region" "current" {}

resource "aws_cloudformation_stack" "this" {
name = var.name
template_body = file("../.aws-sam/build/${var.app}/${data.aws_region.current.name}/packaged.yaml")
parameters = var.parameters
capabilities = var.capabilities
}

output "stack" {
value = aws_cloudformation_stack.this
}
6 changes: 6 additions & 0 deletions integration/scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
cat << EOF
{
"error": "it failed!"
}
EOF
5 changes: 5 additions & 0 deletions integration/tests/check/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "external" "check" {
program = var.program
query = var.query
working_dir = var.working_dir
}
3 changes: 3 additions & 0 deletions integration/tests/check/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "result" {
value = data.external.check.result
}
28 changes: 28 additions & 0 deletions integration/tests/check/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
variable "program" {
description = <<-EOF
A list of strings, whose first element is the program to run and whose
subsequent elements are optional command line arguments to the program.
Terraform does not execute the program through a shell, so it is not
necessary to escape shell metacharacters nor add quotes around arguments
containing spaces.
EOF
type = list(string)
nullable = false
}

variable "query" {
description = <<-EOF
A map of string values to pass to the external program as the query
arguments. If not supplied, the program will receive an empty object as its
input.
EOF
type = map(string)
default = {}
nullable = false
}

variable "working_dir" {
description = "Working directory of the program"
type = string
default = null
}
18 changes: 18 additions & 0 deletions integration/tests/check/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Exit if any of the intermediate steps fail
set -e

# Extract "foo" and "baz" arguments from the input into
# FOO and BAZ shell variables.
# jq will ensure that the values are properly quoted
# and escaped for consumption by the shell.
eval "$(jq -r '@sh "FOO=\(.foo) BAZ=\(.baz)"')"

# Placeholder for whatever data-fetching logic your script implements
FOOBAZ="$FOO $BAZ"

# Safely produce a JSON object containing the result value.
# jq will ensure that the value is properly quoted
# and escaped to produce a valid JSON string.
jq -n --arg foobaz "$FOOBAZ" '{"foobaz":$foobaz}'
11 changes: 11 additions & 0 deletions integration/tests/check/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_version = "~> 1.6"
required_providers {
external = {
source = "hashicorp/external"
version = "~> 2.3"
}
}
}


20 changes: 20 additions & 0 deletions integration/tests/forwarder.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
run "setup" {
module {
source = "./tests/setup"
}
}

run "install_forwarder" {
variables {
name = run.setup.id
app = "forwarder"
parameters = {
DataAccessPointArn = run.setup.source.arn
DestinationUri = "s3://${run.setup.source.alias}"
}
capabilities = [
"CAPABILITY_NAMED_IAM",
"CAPABILITY_AUTO_EXPAND",
]
}
}
1 change: 1 addition & 0 deletions integration/tests/scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "tes"
16 changes: 16 additions & 0 deletions integration/tests/setup/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "random_pet" "run" {
length = 2
}

resource "aws_s3_bucket" "source" {
bucket = "${random_pet.run.id}-source"
}

resource "aws_s3_bucket" "destination" {
bucket = "${random_pet.run.id}-destination"
}

resource "aws_s3_access_point" "source" {
bucket = aws_s3_bucket.source.id
name = random_pet.run.id
}
14 changes: 14 additions & 0 deletions integration/tests/setup/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "id" {
value = random_pet.run.id
description = "Random test identifier"
}

output "source" {
value = aws_s3_access_point.source
description = "S3 bucket where files are copied from"
}

output "destination" {
value = aws_s3_bucket.destination
description = "S3 bucket where files are copied to"
}
13 changes: 13 additions & 0 deletions integration/tests/setup/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_version = "~> 1.6"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.3"
}
random = {
source = "hashicorp/random"
version = ">= 3.5.1"
}
}
}
20 changes: 20 additions & 0 deletions integration/tests/simple.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
run "setup" {
module {
source = "./tests/setup"
}
}

run "check" {
module {
source = "./tests/check"
}

variables {
program = ["./scripts/test.sh", run.setup.id ]
}

assert {
condition = output.result.error == ""
error_message = "Failed to test thing"
}
}
21 changes: 21 additions & 0 deletions integration/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
variable "name" {
description = "Stack name"
type = string
}

variable "app" {
description = "App name"
type = string
}

variable "parameters" {
description = "Stack parameters"
type = map(string)
}

variable "capabilities" {
description = "Stack capabilities"
type = list(string)
}


11 changes: 11 additions & 0 deletions integration/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_version = "~> 1.6"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.3"
}
}
}


0 comments on commit 7b0eb2d

Please sign in to comment.