Skip to main content

Terraform and Terragrunt fixtures for pytest

Project description

pytest-terra-fixt

With the use of this plugin, users can run Terragrunt and Terraform commands within parametrizable Pytest fixtures. Under the hood, the fixtures within this plugin use the awesome tftest Python package. In addition, given the lengthy time it takes to set up Terraform configurations, the fixtures use the PyTest built-in config.cache fixture to cache Terraform results that can be accessed across PyTest sessions.

Fixtures

All fixtures below can be parametrized via (@pytest.mark.parametrize())

terra:

  • Scope: Session
  • Parameters [Required]:
    • binary: Path to binary (must end with terraform or `terragrunt)
    • tfdir: Absolute or relative path to basedir
    • basedir: Base directory for tfdir (defaults to cwd)
    • env: Environment variables to pass to the command
    • skip_teardown: Skips running fixture's teardown logic
  • Yield: Either the tftest.TerragruntTest or tftest.TerraformTest class depending on the binary parameter
  • Teardown: Runs terraform destroy -auto-approve on the tfdir directory if skip_teardown is set to False

terra_setup

  • Scope: Function
  • Parameters [Optional]: A: Dictionary of keyword arguments to passed to the tftest.TerraformTest.setup() method B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned above
  • Returns: The terra fixture's associated setup output

terra_plan

  • Scope: Function
  • Parameters [Optional]: A: Dictionary of keyword arguments to passed to the tftest.TerraformTest.plan() method B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned above
  • Returns: The terra fixture's associated plan output

terra_apply

  • Scope: Function
  • Parameters [Optional]: A: Dictionary of keyword arguments to passed to the tftest.TerraformTest.apply() method B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned above
  • Returns: The terra fixture's associated apply output

terra_output

  • Scope: Function
  • Parameters [Optional]: A: Dictionary of keyword arguments to passed to the tftest.TerraformTest.output() method B: Dictionary of terra directories and their respective keyword arguments to pass to the method mentioned above
  • Returns: The terra fixture's associated output

CLI Arguments

--skip-teardown: Skips running terraform destroy -auto-approve on teardown and preserves the Terraform backend tfstate for future testing. This flag is useful for checking out the Terraform resources within the cloud provider console or for running experimental tests without having to wait for the resources to spin up after every Pytest invocation.

NOTE: To continually preserve the Terraform tfstate, the `--skip-teardown` flag or the `"skip_teardown": True` attribute within the `terra` fixture parameters needs to be always present. If not, the `terra` fixture's teardown may destroy the Terraform resources and remove the tfstate files.

Examples

conftest.py

import pytest

def pytest_generate_tests(metafunc):
    if "terra" in metafunc.fixturenames:
        metafunc.parametrize(
            "terra",
            [
                pytest.param(
                    {
                        "binary": "terraform",
                        "skip_teardown": True,
                        "env": {
                           "TF_VAR_foo": "bar"
                        },
                        "tfdir": "fixtures",
                    },
                )
            ],
            indirect=True,
            scope="session",
        )

test_tf.py

import pytest

@pytest.mark.usefixtures("terra_setup", "terra_apply")
class TestModule:

   @pytest.mark.parametrize("terra_plan", [{"extra_files": ["plan.tfvars"]}])
   def test_plan(self, terra, terra_plan):
      assert terra_plan["bar"] == "zoo"

   def test_out(self, terra, terra_output):
      assert terra_output["doo"] == "foo"

Installation

Install via Pip:

pip install pytest-terra-fixt

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

pytest-terra-fixt-0.6.1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

pytest_terra_fixt-0.6.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest-terra-fixt-0.6.1.tar.gz.

File metadata

  • Download URL: pytest-terra-fixt-0.6.1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for pytest-terra-fixt-0.6.1.tar.gz
Algorithm Hash digest
SHA256 38005399fc2de637dd4d5262381b0eceaafa2a5a05912060458588821e2cbd62
MD5 1edf9ba55535986437ecca066edaa3d9
BLAKE2b-256 3db1bf625c5ac6975b937734b83403ef6cbc702e73655a0348612b7d940d87ff

See more details on using hashes here.

File details

Details for the file pytest_terra_fixt-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_terra_fixt-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bb9faf15099adc4eab63539c783ccf1c2540c70af0f71fc3acd5358acf8de117
MD5 d4b3b9ac9c76b5fdb8cbe5c3b51e06ab
BLAKE2b-256 508a08c774eb9c7ed2700025a7de2d7a61d313a6b8d93f741473b381a7c5d212

See more details on using hashes here.

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