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']

Caching

The TerraformTest setup, init, plan, apply, output and destroy methods have the ability to cache it's associate output to a local .tftest-cache directory. For subsequent calls of the method, the cached value can be returned instead of calling the actual underlying terraform command. Using the cache value can be significantly faster than running the Terraform command again especially if the command is time-intensive.

To determine if the cache should be used, first a hash value is generated using the current TerraformTest instance __init__ and calling method arguments, file contents of the tfdir and file contents of any tf_var_file or extra_files method argument. The hash value is compared to the hash value of the cached instance's associated arguments. If the hash is the same then the cache is used, otherwise the method is executed.

The benefits of the caching feature include:

  • Faster setup time for testing terraform modules that don't change between testing sessions
  • Writing tests without worrying about errors within their test code resulting in the Terraform setup logic to run again

Please see the following example for how to use it:

import pytest
import tftest


@pytest.fixture
def output(fixtures_dir):
  tf = tftest.TerraformTest('apply', fixtures_dir, enable_cache=True)
  tf.setup(use_cache=True)
  tf.apply(use_cache=True)
  yield tf.output(use_cache=True)
  tf.destroy(use_cache=True, **{"auto_approve": True})


def test_apply(output):
  value = output['triggers']
  assert len(value) == 2
  assert list(value[0].keys()) == ['name', 'template']
  assert value[0]['name'] == 'one'

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.8.7.tar.gz (20.7 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.8.7-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tftest-1.8.7.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tftest-1.8.7.tar.gz
Algorithm Hash digest
SHA256 c1d3bc6800cf9dfc86e9790369f681308f29e8e92b7eefd230ac522897e6aaed
MD5 2fa49fb01b6ba7beac932e5d3d5e018b
BLAKE2b-256 06d36f7c80748936eec8cfe610705d2b6dc2457dd4185fc1ea56e0109f85730b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tftest-1.8.7.tar.gz:

Publisher: publish-pypi.yml on GoogleCloudPlatform/terraform-python-testing-helper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: tftest-1.8.7-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tftest-1.8.7-py3-none-any.whl
Algorithm Hash digest
SHA256 acfb00aef7018595678651605fb79cf4866d94aeb6a0dbf818f38cfb4b33c379
MD5 12365545ca1c650934e7acfabfa47f76
BLAKE2b-256 4fe11deb83d445d9450916c867d5b4b09dcde2ddca6024b2c0c7c0245e55636b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tftest-1.8.7-py3-none-any.whl:

Publisher: publish-pypi.yml on GoogleCloudPlatform/terraform-python-testing-helper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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