Coder Social home page Coder Social logo

ldealmei / terraform-python-testing-helper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlecloudplatform/terraform-python-testing-helper

0.0 0.0 0.0 67 KB

Simple Python test helper for Terraform.

License: Apache License 2.0

HCL 4.99% Python 95.01%

terraform-python-testing-helper's Introduction

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.

terraform-python-testing-helper's People

Contributors

ludoo avatar bastiandg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.