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 be 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 plan -out and show) and the outputs (via output -json), and can return them wrapped in simple classes that simplify accessing their attributes.

This module is heavily inspired by two projects: Terragrunt 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. This is the 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']

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.2.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

tftest-1.2.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tftest-1.2.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for tftest-1.2.0.tar.gz
Algorithm Hash digest
SHA256 ce6363a629deff5b0f7d5c8be015f266c73f17d1d2d50d41bb88f08fd2b67cf4
MD5 951981c42ba7b7faf2d34272d1f048d0
BLAKE2b-256 fde139075673889c1b146b7a5c86d4f0b74e86486f1071f3e6d58d74ced9be4e

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: tftest-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3

File hashes

Hashes for tftest-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d3b83cfd069e934dc870f0027ba4c314182ccaa2f8023166117eed3a922f898
MD5 4a2f380f6974d3f9422e87f41c3b3a34
BLAKE2b-256 119787002f69f0c2280d6680da2b6f63d5566ec02b690ed5b3ecd4ce506cd8ae

See more details on using hashes here.

Provenance

Supported by

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