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 Pytest fixtures. Under the hood, the fixtures within this plugin use the awesome tftest Python package.
Fixtures
terra
:
- Scope: Function
- Input: (passed within `pytest.mark.parametrize("terra", [{}]))
command
: Terraform or Terragrunt command to run (e.g. init, plan, apply)binary
: Path to binary (must end withterraform
or `terragrunt)tfdir
: Absolute or relative path tobasedir
basedir
: Base directory fortfdir
(defaults to cwd)env
: Environment variables to pass to the commandskip_teardown
: Skips running fixture's teardown logicuse_cache
: IfTrue
, gets command output fromterra_cache
fixtureextra_args
: Dictionary of extra arguments to pass to the command
- Setup: Updates cache with selected kwargs provided
- Yield: If
use_cache
isTrue
, yields output for the inputcommand
from theterra_cache
fixture. Ifuse_cache
isFalse
, yields output from the execution of the command - Teardown: Runs
terraform destroy -auto-approve
on the inputtfdir
directory
terra_factory
:
- Scope: Session
- Input: (passed within `pytest.mark.parametrize("terra", [{}]))
command
: Terraform or Terragrunt command to run (e.g. init, plan, apply)binary
: Path to binary (must end withterraform
or `terragrunt)tfdir
: Absolute or relative path tobasedir
basedir
: Base directory fortfdir
(defaults to cwd)env
: Environment variables to pass to the commandskip_teardown
: Skips running fixture's teardown logicuse_cache
: IfTrue
, gets command output fromterra_cache
fixtureextra_args
: Dictionary of extra arguments to pass to the command
- Setup: Updates cache with selected kwargs provided
- Yield: If
use_cache
isTrue
, yields output for the inputcommand
from theterra_cache
fixture. Ifuse_cache
isFalse
, yields output from the execution of the command - Teardown: Runs
terraform destroy -auto-approve
on every factory instance's inputtfdir
directory
terra_cache
:
- Scope: Session
- Setup: Runs
terraform init
on the specified directory - Yield: Factory fixture that returns a
tftest.TerraformTest
object that can run subsequent Terraform commands with - Teardown: Runs
terraform destroy -auto-approve
on the specified directory
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 needs to be always present, or else the `terra` or `terra_factory` fixtures' teardown may destroy the Terraform resources and remove the tfstate file.
Examples
terra
import pytest
@pytest.mark.parametrize("terra", [
{
"binary": "terraform",
"command": "apply",
"tfdir": "bar",
"env": {
"TF_LOG": "DEBUG"
},
"skip_teardown": False,
"use_cache": False,
"extra_args": {"state_out": "/foo"},
}
], indirect=['terra'])
def test_terra_param(terra):
assert terra == "zoo"
terra_factory
def test_terra_param(terra):
plan = terra_factory(binary="terraform", command="plan", tfdir="bar", skip_teardown=True)
assert plan["doo"] == "zoo"
Installation
Install via Pip:
pip install pytest-terra-fixt
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-terra-fixt-0.5.0.tar.gz
.
File metadata
- Download URL: pytest-terra-fixt-0.5.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b4b91e14c2dd1d177ea5cdd60519d7615802f02693f91ca44b40421ea49d900 |
|
MD5 | 05845234e91270acd89ea2bd16b8a594 |
|
BLAKE2b-256 | 15e80abe09cbcfd62af0e5ec2c405a439d1d929286f603f7ab38c7857691a83f |
File details
Details for the file pytest_terra_fixt-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: pytest_terra_fixt-0.5.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a4ebca2edbd958166c58f464061c398fab1d43befa1bd02dbecf6780ea38784 |
|
MD5 | a6513a9049719b54524428e265004746 |
|
BLAKE2b-256 | 54ca05bd8da5ee57ed4f08397961906bfcd44518ea0981069690ccdfe7722707 |