{"id":17868243,"url":"https://github.com/matmoore/terraform-testing-examples","last_synced_at":"2025-07-11T04:32:51.776Z","repository":{"id":191438883,"uuid":"684655057","full_name":"MatMoore/terraform-testing-examples","owner":"MatMoore","description":null,"archived":false,"fork":false,"pushed_at":"2023-09-29T09:23:30.000Z","size":57,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-02T22:30:36.100Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MatMoore.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-29T15:27:25.000Z","updated_at":"2023-08-29T15:27:48.000Z","dependencies_parsed_at":"2023-08-29T23:44:48.967Z","dependency_job_id":"e3eba757-08dd-442f-9345-358a9ec4bbf8","html_url":"https://github.com/MatMoore/terraform-testing-examples","commit_stats":null,"previous_names":["matmoore/terraform-testing-examples"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MatMoore/terraform-testing-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatMoore%2Fterraform-testing-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatMoore%2Fterraform-testing-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatMoore%2Fterraform-testing-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatMoore%2Fterraform-testing-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MatMoore","download_url":"https://codeload.github.com/MatMoore/terraform-testing-examples/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatMoore%2Fterraform-testing-examples/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264729332,"owners_count":23654993,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-28T09:57:21.890Z","updated_at":"2025-07-11T04:32:51.761Z","avatar_url":"https://github.com/MatMoore.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Testing terraform examples\n\nExamples of \"unit\" testing terraform deployments.\n\nSuch tests actually deploy a terraform module and then verify the result. For example, you can verify that a service returns a 200 response.\n\nThe goal of this repo is to evaluate some of the tools for unit testing infrastructure. See also [adopting_terraform_testing.md](./adopting_terraform_testing.md) for a checklist for actually adopting one of these tools into an existing delivery pipeline.\n\n## Prerequisites\n\nMake sure [terraform](https://developer.hashicorp.com/terraform/downloads?product_intent=terraform) is installed and available on the path.\n\n## Testing with Terratest\n\nThese tests are written in Go. It can test terraform in addition to other things, like docker containers.\n\n### Prerequisites\n\n[Install go](https://golang.org/).\n\nConfigure dependencies:\n\n```\ncd terratest\ngo mod init \"github.com/MatMoore/terraform-testing-examples\"\ngo mod tidy\n```\n\n### Run tests\n\n```\ngo test -v -timeout 30m\n```\n\nWarning: If the test times out, the terraform destroy will not run, and you will have hanging resources!\n\n### Test descriptions\n\n#### [terraform_basic_example_test](./terratest/terraform_basic_example_test.go) \nThis test does not use any external providers, so the test just applies the plan and asserts against the outputs.\n\n#### [terraform_aws_hello_world_example_test](./terratest/terraform_aws_hello_world_example_test.go) \nThis test deploys a resource to AWS.\n\n[You will need credentials configured to run the test.](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#authentication-and-configuration)\n\nThe test applies the plan and retrieves the IP of the web server from the output. Then it queries the server to check it is running as expected.\n\n\n### Writing new tests\n\nTests live in files like *_test.go.\n\nThe core of each test looks like this:\n\n```go\n\tt.Parallel()\n\n\tterraformOptions := terraform.WithDefaultRetryableErrors(t, \u0026terraform.Options{\n\t\tTerraformDir: \"../examples/terraform-aws-hello-world-example\",\n\t})\n\n\tdefer terraform.Destroy(t, terraformOptions)\n\n\tterraform.InitAndApply(t, terraformOptions)\n\n    // ... make some assertions\n```\n\nTo make the test stronger, you can swap out `InitAndApply` for `InitAndApplyIdempotent`, which applies the terraform module twice, and checks nothing changes.\n\nThere are different ways to assert against things in the test:\n\n- Directly assert that module outputs (e.g. domains, IPs) match expected values\n- Retrieve IDs of AWS resources and interract with them via the [aws subpackage](https://pkg.go.dev/github.com/gruntwork-io/terratest@v0.43.13/modules/aws) ([example 1](https://github.com/gruntwork-io/terratest/blob/v0.43.13/test/terraform_aws_rds_example_test.go) [example 2](https://github.com/gruntwork-io/terratest/blob/v0.43.13/test/terraform_aws_s3_example_test.go)) (only possible for some services)\n- Retrieve an IP/Domain and issue an HTTP request, as in [terraform_aws_hello_world_example_test](./test/terraform_aws_hello_world_example_test.go)\n\n## Testing with Python Test Helper for Terraform (tftest)\n\nThese tests are written in python and pytest.\n\nCompared to terratest, this tool is more basic/simple. There is no functionality for directly interacting with an AWS stack, the library just manages running the terraform for you.\n\n### Prerequisites\n\nYou will need some recent version of python.\n\n```\ncd tftest\npython3 -m pip install requirements.txt\n```\n\n### Run tests\n\n```\npytest\n```\n\n### Test descriptions\n\n#### [terraform_basic_example_test](./tftest/test_terraform_basic_example.py) \nThis test does not use any external providers, so the test just applies the plan and asserts against the outputs.\n\n### Writing new tests\nYou can test just the planning stage like so\n\n```python\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\nAnd apply like so:\n\n```python\n@pytest.fixture\ndef output(fixtures_dir):\n  tf = tftest.TerraformTest('apply', fixtures_dir)\n  tf.setup()\n  tf.apply()\n  yield tf.output()\n  tf.destroy(**{\"auto_approve\": True})\n\n\ndef test_apply(output):\n  value = output['triggers']\n  assert len(value) == 2\n  assert list(value[0].keys()) == ['name', 'template']\n  assert value[0]['name'] == 'one'\n```\n\nThings we can do in the test:\n\n- Directly assert that module outputs (e.g. domains, IPs) match expected values\n- Retrieve an IP/Domain and issue an HTTP request (e.g. via `requests`)\n\nIf you want to inspect resource configuration, you'll need to import a seperate library like [boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatmoore%2Fterraform-testing-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatmoore%2Fterraform-testing-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatmoore%2Fterraform-testing-examples/lists"}