From 09e532b6f9c31024b4be25e40846f5c71af47fd4 Mon Sep 17 00:00:00 2001 From: davidtam Date: Mon, 7 Jun 2021 23:16:39 +0100 Subject: [PATCH] Terragrunt support (#32) * Add Terragrunt integration and general parser (#24) * - add kw parser - add tg command methods * - add plan_all parse output progress - replace terra* command func cli params with **kw * add tgbinary to ci * - add tg shared doc string decorator - separate some of the public methods tg_xxx() | tf_xxx() - fix parse_args() styling * - Rollback parse_args() - Add tg global args to parse_args() - Add tg global args decorator - Add all param to tg_xxx methods * - mv tg flags before tf flags for cli args - add common tg args to tg methods * - fix tg source module warning - add output() fixture to tg test * bump version * Terragrunt support (#29) * Don't fail when resource_changes key not present in output (#27) * Resolves #26 tftest failed when terraform plan is empty * add missing copyright boilerplate Co-authored-by: Ludovico Magnocavallo * Update CHANGELOG.md * Update CHANGELOG.md * v1.5.6 for pypi * made minimal changes to fix tests (now all passed locally for me) * added a bit of code to parse run-all output and test to assert output * added terragrunt run in single dir test case + simplified run-all * added test cases for terragrunt parse args * refactored terragrunt cmd line parameters parsing * refactored as a separated class(TerragruntTest) but reused most of TerraformTest Co-authored-by: Mike Brown II Co-authored-by: Ludovico Magnocavallo Co-authored-by: Ludovico Magnocavallo * speed up tests(session scope to reuse fixtures) + work around for strange tf run-all plan behaviour * added back change log * adding newline at end of file * Adding terragrunt section to readme * added some doc strings * remove binary param from _clean_up to fix tests * Update README.md * Update README.md * Update CHANGELOG.md * Update tftest.py Co-authored-by: Marshall Mamiya <44485531+marshall7m@users.noreply.github.com> Co-authored-by: Ludovico Magnocavallo Co-authored-by: Mike Brown II Co-authored-by: Ludovico Magnocavallo --- .ci/cloudbuild.test.yaml | 5 +- CHANGELOG.md | 2 + README.md | 34 +++- test/fixtures/tg_apply_all/bar/terragrunt.hcl | 11 ++ test/fixtures/tg_apply_all/foo/terragrunt.hcl | 11 ++ test/fixtures/tg_apply_all/terragrunt.hcl | 11 ++ test/test_args.py | 49 ++++- test/test_no_outputs.py | 2 +- test/test_plan.py | 2 +- test/test_sample_apply.py | 2 +- test/test_sample_plan.py | 2 +- test/test_tg_all.py | 77 ++++++++ tftest.py | 181 +++++++++++++++--- 13 files changed, 354 insertions(+), 35 deletions(-) create mode 100644 test/fixtures/tg_apply_all/bar/terragrunt.hcl create mode 100644 test/fixtures/tg_apply_all/foo/terragrunt.hcl create mode 100644 test/fixtures/tg_apply_all/terragrunt.hcl create mode 100644 test/test_tg_all.py diff --git a/.ci/cloudbuild.test.yaml b/.ci/cloudbuild.test.yaml index 86dc4fc..5671854 100644 --- a/.ci/cloudbuild.test.yaml +++ b/.ci/cloudbuild.test.yaml @@ -23,7 +23,9 @@ steps: wget https://releases.hashicorp.com/terraform/${_TERRAFORM_VERSION}/terraform_${_TERRAFORM_VERSION}_linux_amd64.zip && unzip terraform_${_TERRAFORM_VERSION}_linux_amd64.zip -d /builder/home/.local/bin && rm terraform_${_TERRAFORM_VERSION}_linux_amd64.zip && - chmod 755 /builder/home/.local/bin/terraform + wget -O /builder/home/.local/bin/terragrunt https://github.com/gruntwork-io/terragrunt/releases/download/v${_TERRAGRUNT_VERSION}/terragrunt_linux_amd64 && + chmod 755 /builder/home/.local/bin/terraform && + chmod 755 /builder/home/.local/bin/terragrunt # TODO(ludoo): split into two triggers with different filters - name: python:3.6-alpine id: test @@ -36,6 +38,7 @@ steps: substitutions: _TERRAFORM_VERSION: 0.15.4 + _TERRAGRUNT_VERSION: 0.29.8 tags: - "ci" diff --git a/CHANGELOG.md b/CHANGELOG.md index dd69bbe..d05bf05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Terragrunt support (@davidtam) [#32] + ## [1.5.7] - improve Windows support [#28] diff --git a/README.md b/README.md index 1b4b79f..576cf41 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ It allows for different types of tests: lightweight tests that only use Terrafor As an additional convenience, the module also provides an easy way to request and access the plan output (via `terraform plan -out` and `terraform show`) and the outputs (via `terraform output -json`), and return them wrapped in simple classes that streamline accessing their attributes. -This module is heavily inspired by two projects: [Terragrunt](https://github.com/gruntwork-io/terragrunt) for the lightweight approach to testing Terraform, and [python-terraform](https://github.com/beelit94/python-terraform) for wrapping the Terraform command in Python. +This module is heavily inspired by two projects: [Terratest](https://github.com/gruntwork-io/terratest) for the lightweight approach to testing Terraform, and [python-terraform](https://github.com/beelit94/python-terraform) for wrapping the Terraform command in Python. ## Example Usage @@ -46,6 +46,38 @@ def test_modules(plan): assert res['values']['location'] == plan.variables['gcs_location'] ``` +## Terragrunt support + +Support for Terragrunt actually follows the same principle of the thin `TerraformTest` wrapper. + +Please see the following example for how to use it: + +```python +import pytest +import tftest + + +@pytest.fixture +def run_all_apply_out(fixtures_dir): + # notice for run-all, you need to specify when TerragruntTest is constructed + tg = tftest.TerragruntTest('tg_apply_all', fixtures_dir, tg_run_all=True) + # the rest is very similar to how you use TerraformTest + tg.setup() + # to use --terragrunt-