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 test. It does so 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 also run Terraform apply and output (destroying if needed), and can check created resources through outputs, state, and then running live tests (eg pinging an instance) against created resources.

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 example folder contains one example for each test style. This is a snippet from the plan-based tests:

TF = tftest.TerraformTest(os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'foo'))

def setup():
  TF.setup(command='plan')

def test_resources():
  """Test that plan contains all expected resources."""
  values = re.findall(r'(?m)^\s*\+\s+(null_resource\S+)\s*^', TF.setup_output)
  assert values == ['null_resource.foo_resource'], values

And from the apply-based tests:

TF = tftest.TerraformTest(os.path.join(ROOT, 'foo'))

def setup():
  TF.setup(command='output', destroy=True)

def teardown():
  TF.teardown()

def test_output():
  """Test that apply creates the correct resources and outputs are correct."""
  assert TF.setup_output['foos'] == [
      {'foo': 'foo', 'index': '0'}], TF.setup_output['foos']

def test_state():
  """Test that state has the correct resources and attributes."""
  resources = TF.state_pull().modules['root'].resources
  assert resources['null_resource.foo_resource'].attributes['triggers.%'] == '2'

Testing

The simple tests in the tests folder require Python 3.x and can be run with nosetests3 or pytest.

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

Uploaded Source

Built Distribution

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

tftest-0.5.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tftest-0.5.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for tftest-0.5.0.tar.gz
Algorithm Hash digest
SHA256 f9e81bcd0c494342ae5cf952dcaa4244460b3143433ad2f92279d8fe796fe50d
MD5 5130a0aba37d09292f6ccece33bda379
BLAKE2b-256 58e9345e599d64276a00399c665feaa3b3b13c83216b0d772e5305ebc5b1464a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tftest-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for tftest-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 efb9e0ede550e6aff2b5105f07682e84b811fa760a05e263a8115fa78644c117
MD5 9b4b2d92717ef01bf37cb504b6593d3f
BLAKE2b-256 e2c69c40867c53c06059fe55476e08ce12beeec80ed8a81550bf509309203d0f

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