Skip to main content

Simple Terraform test helper

Project description

Python Test Helper for Terraform

This simple helper facilitates testing Terraform modules from Python unit tests, by wrapping the Terraform executable and exposing convenience methods to set up fixtures, execute Terraform commands, and parse their output.

It allows for different types of tests: lightweight tests that only use Terraform init and plan to ensure code is syntactically correct and the right number and type of resources should be created, or full-fledged tests that run the full apply/output/destroy cycle, and can then be used to test the actual created resources, or the state file.

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: Terratest for the lightweight approach to testing Terraform, and python-terraform for wrapping the Terraform command in Python.

Example Usage

The test folder contains simple examples on how to write tests for both plan and apply, using either synthetic fixtures (simple representations of the plan output and output files), or minimal root modules. More examples can be found in the Cloud Foundation Fabric repository, for which this module was developed.

This is a test that uses plan output on an actual module:

import pytest
import tftest


@pytest.fixture
def plan(fixtures_dir):
  tf = tftest.TerraformTest('plan', fixtures_dir)
  tf.setup(extra_files=['plan.auto.tfvars'])
  return tf.plan(output=True)


def test_variables(plan):
  assert 'prefix' in plan.variables
  assert plan.variables['names'] == ['one', 'two']


def test_outputs(plan):
  assert sorted(plan.outputs['gcs_buckets'].keys()) == plan.variables['names']


def test_root_resource(plan):
  res = plan.resources['google_project_iam_member.test_root_resource']
  assert res['values']['project'] == plan.variables['project_id']


def test_modules(plan):
  mod = plan.modules['module.gcs-buckets']
  res = mod.resources['google_storage_bucket.buckets[0]']
  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:

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-<option>, pass in tg_<option in snake case>
  tg.apply(output=False, tg_non_interactive=True)
  yield tg.output()
  tg.destroy(auto_approve=True, tg_non_interactive=True)

  
def test_run_all_apply(run_all_apply_out):
    triggers = [o["triggers"] for o in run_all_apply_out]
    assert [{'name': 'foo', 'template': 'sample template foo'}] in triggers
    assert [{'name': 'bar', 'template': 'sample template bar'}] in triggers
    assert [{'name': 'one', 'template': 'sample template one'},
            {'name': 'two', 'template': 'sample template two'}] in triggers
    assert len(run_all_apply_out) == 3

Compatibility

Starting from version 1.0.0 Terraform 0.12 is required, and tests written with previous versions of this module are incompatible. Check the CHANGELOG.md file for details on what's changed.

Testing

Tests use the pytest framework and have no other dependency except on the Terraform binary. The version used during development is in the DEV-REQUIREMENTS.txt file.

Disclaimer

This is not an officially supported Google product.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tftest-1.7.3.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tftest-1.7.3-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file tftest-1.7.3.tar.gz.

File metadata

  • Download URL: tftest-1.7.3.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for tftest-1.7.3.tar.gz
Algorithm Hash digest
SHA256 c327d766e337541eb8489bf5e5424234f1aded44a3e3f6195e63eaba72a2e51c
MD5 9694e64fa4c36900dda1a34f67857c7e
BLAKE2b-256 ed1d6c5cd7fc6c91aed44250324a0207e2416b9fa584b83ccba0c9cac627fc86

See more details on using hashes here.

File details

Details for the file tftest-1.7.3-py3-none-any.whl.

File metadata

  • Download URL: tftest-1.7.3-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for tftest-1.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e08b2e94eb352deade3bb65099e790904cdcae4933a7c6cfba73beb2744f40f3
MD5 4d82f7c5022c6f378fb455cf7fd67b7d
BLAKE2b-256 1237976d663fe7448726a85acebaabaf2771ae9a21e195b0485810340f172fea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page