{ "info": { "author": "Ludovico Magnocavallo", "author_email": "ludomagno@google.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: Apache Software License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# Python Test Helper for Terraform\n\nThis 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.\n\nIt 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.\n\nAs 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.\n\nThis module is heavily inspired by two projects: [Terratest](https://github.com/gruntwork-io/terratest) for the lightweight approach to testing Terraform, and [python-terraform](https://github.com/beelit94/python-terraform) for wrapping the Terraform command in Python.\n\n## Example Usage\n\nThe [`test`](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/tree/master/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](https://github.com/terraform-google-modules/cloud-foundation-fabric) repository, for which this module was developed.\n\nThis is a test that uses plan output on an actual module:\n\n```hcl\nimport pytest\nimport tftest\n\n\n@pytest.fixture\ndef plan(fixtures_dir):\n tf = tftest.TerraformTest('plan', fixtures_dir)\n tf.setup(extra_files=['plan.auto.tfvars'])\n return tf.plan(output=True)\n\n\ndef test_variables(plan):\n assert 'prefix' in plan.variables\n assert plan.variables['names'] == ['one', 'two']\n\n\ndef test_outputs(plan):\n assert sorted(plan.outputs['gcs_buckets'].keys()) == plan.variables['names']\n\n\ndef test_root_resource(plan):\n res = plan.resources['google_project_iam_member.test_root_resource']\n assert res['values']['project'] == plan.variables['project_id']\n\n\ndef test_modules(plan):\n mod = plan.modules['module.gcs-buckets']\n res = mod.resources['google_storage_bucket.buckets[0]']\n assert res['values']['location'] == plan.variables['gcs_location']\n```\n\n## Terragrunt support\n\nSupport for Terragrunt actually follows the same principle of the thin `TerraformTest` wrapper.\n\nPlease see the following example for how to use it:\n\n```python\nimport pytest\nimport tftest\n\n\n@pytest.fixture\ndef run_all_apply_out(fixtures_dir):\n # notice for run-all, you need to specify when TerragruntTest is constructed\n tg = tftest.TerragruntTest('tg_apply_all', fixtures_dir, tg_run_all=True)\n # the rest is very similar to how you use TerraformTest\n tg.setup()\n # to use --terragrunt-