-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from kbalk/add-pytest-targets
Add targets for testing Terraform modules using pytest, tftest
- Loading branch information
Showing
17 changed files
with
538 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ | |
|
||
# ec (editorconfig-checker) | ||
.git/ | ||
|
||
# python caching when pytest is used | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Tardigrade-ci Integration Testing | ||
|
||
The tardigrade-ci `Makefile` provides targets to facilitate automated | ||
integration testing of terraform modules through the use of a mock AWS stack. | ||
|
||
This document describes the integration-specific `Makefile` targets and | ||
the environment variables to customize those targets. In addition, | ||
the steps to try out a test are described as are the potential CI/CD | ||
changes to incorporate automated integration testing. | ||
|
||
## Integration test-specific targets and environment variables | ||
|
||
| Target name | Description | | ||
| ---------------- | ------------------------------------------ | | ||
| mockstack/pytest | From within a Docker container, invoke `pytest` to execute the integration tests. | | ||
| mockstack/up | Start up a Docker container running the mock AWS stack. | | ||
| mockstack/down | Bring down the Docker container running the mock AWS stack. | | ||
| mockstack/clean | Bring down the Docker container running the mock AWS stack, then remove the docker image. | | ||
| terraform/pytest | Invoke `pytest` to execute the integration tests. The mock AWS stack must be started before using this `Makefile` target. | | ||
|
||
Defaults: | ||
|
||
* `LocalStack` is used for the mock AWS stack, with moto serving ports for | ||
services not yet suported by `LocalStack`. | ||
* The Terraform modules used for the integration tests are expected to | ||
be located in the directory `tests` off the repo\'s root directory. | ||
|
||
### Environment variables | ||
|
||
| Environment variable | Default value | | ||
| -------------------------------- | --------------------------------------- | | ||
| INTEGRATION_TEST_BASE_IMAGE_NAME | $(basename $PWD)-integration-test | | ||
| TERRAFORM_PYTEST_ARGS | | | ||
| TERRAFORM_PYTEST_DIR | $PWD/tests/terraform/pytest | | ||
|
||
### Arguments to the automation script | ||
|
||
These are values that can be specified through the environment variable | ||
TERRAFORM_PYTEST_ARGS. | ||
|
||
| Command line option | Description | | ||
| ------------------- | ----------------------------------------------- | | ||
| --nomock | Use AWS, not mocked AWS services | | ||
| --alternate-profile | Configure an alternate profile in addition to default profile | | ||
| --tf-dir=TF_DIR | Directory of Terraform files under test; default: './tests' | | ||
|
||
## Executing a Terraform test | ||
|
||
The tardigrade-ci `Makefile` expects the test Terraform modules to be under | ||
the repo\s `tests` directory. There can be multiple sets of tests, each | ||
under their own `tests` subdirectory. | ||
|
||
If a test requires an initial test setup, then those Terraform "test setup" | ||
files should be placed in the directory `prereq` under that test\'s | ||
subdirectory. For example: | ||
|
||
``` | ||
. | ||
├── tests | ||
│ ├── create_all | ||
│ │ ├── main.tf | ||
│ │ └── prereq | ||
│ │ └── main.tf | ||
│ ├── create_groups | ||
│ │ ├── main.tf | ||
... | ||
``` | ||
|
||
To verify that a Terraform test will work, bring up the default AWS mock | ||
stack (`LocalStack`) first, then execute the test: | ||
|
||
```bash | ||
make mockstack/up | ||
make terraform/pytest | ||
|
||
# To execute a specific set of tests, use the "-k" option and a string | ||
# that will pattern match on the desired subdirectory name. The "-k" | ||
# option also allows booleans, e.g., "not" or "or". | ||
# | ||
# The following will match on the subdirectory "create_groups". | ||
make terraform/pytest TERRAFORM_PYTEST_ARGS="-k groups" | ||
|
||
# When testing is complete: | ||
make mockstack/clean | ||
``` | ||
|
||
## Potential CI/CD changes | ||
|
||
These are suggested changes and may not apply to all repos: | ||
|
||
1. If the repo currently does not contain or use Python scripts, | ||
update `.gitignore` to add: | ||
|
||
```bash | ||
# Caching directory created by pytest. | ||
tests/__pycache__/ | ||
``` | ||
|
||
2. For a Travis workflow, logic similar to the following would | ||
need to be added to the `.travis.yml` file: | ||
|
||
```bash | ||
stage: test | ||
|
||
name: Terraform Integration Tests | ||
|
||
language: python | ||
|
||
python: | ||
- "3.8" | ||
|
||
install: make mockstack/up | ||
|
||
script: make mockstack/pytest | ||
after_script: make mockstack/clean | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env bats | ||
|
||
TEST_DIR="$(pwd)/terraform_pytest_failure" | ||
|
||
function setup() { | ||
rm -rf "$TEST_DIR" | ||
working_dirs=("$TEST_DIR/test" "$TEST_DIR/test/prereq") | ||
|
||
# Bad prereq Terraform file | ||
mkdir -p "$TEST_DIR/test/prereq" | ||
cat > "$TEST_DIR/test/prereq/main.tf" <<-EOF | ||
variable "foo" { | ||
default = "bar" | ||
} | ||
output "baz" { | ||
# this variable does not exist | ||
value = var.bar | ||
} | ||
EOF | ||
|
||
# Good Terraform file | ||
mkdir -p "$TEST_DIR/test" | ||
cat > "$TEST_DIR/test/main.tf" <<-EOF | ||
variable "foo" { | ||
default = "bar" | ||
} | ||
output "baz" { | ||
value = foo.bar | ||
} | ||
EOF | ||
} | ||
|
||
@test "test: terraform pytest failure" { | ||
run make terraform/pytest TERRAFORM_PYTEST_DIR=../terraform_pytest TERRAFORM_PYTEST_ARGS="--tf-dir $TEST_DIR" | ||
[ "$status" -eq 2 ] | ||
[[ "$output" == *"FAILED ../terraform_pytest/test_terraform_install.py::test_modules[test]"* ]] | ||
} | ||
|
||
function teardown() { | ||
rm -rf "$TEST_DIR" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env bats | ||
|
||
TEST_DIR="$(pwd)/terraform_pytest_success" | ||
|
||
function setup() { | ||
rm -rf "$TEST_DIR" | ||
working_dirs=("$TEST_DIR/test" "$TEST_DIR/test/prereq") | ||
for working_dir in "${working_dirs[@]}" | ||
do | ||
|
||
mkdir -p "$working_dir" | ||
cat > "$working_dir/main.tf" <<-EOF | ||
variable "foo" { | ||
default = "bar" | ||
} | ||
output "baz" { | ||
value = var.foo | ||
} | ||
EOF | ||
done | ||
} | ||
|
||
@test "test: terraform pytest success" { | ||
run make terraform/pytest TERRAFORM_PYTEST_DIR=../terraform_pytest TERRAFORM_PYTEST_ARGS="--tf-dir $TEST_DIR" | ||
[ "$status" -eq 0 ] | ||
[[ "$output" == *"test_modules[test] PASSED [100%]"* ]] | ||
} | ||
|
||
function teardown() { | ||
rm -rf "$TEST_DIR" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
provider "aws" { | ||
region = "us-east-1" | ||
} |
Oops, something went wrong.