├── .github └── workflows │ ├── linting.yml │ ├── scripts │ └── check_boilerplate.py │ └── tests.yml ├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── DEV-REQUIREMENTS.txt ├── LICENSE ├── README.md ├── setup.py ├── test ├── __init__.py ├── conftest.py ├── fixtures │ ├── _modules │ │ └── iam-service-account │ │ │ ├── README.md │ │ │ ├── main.tf │ │ │ ├── outputs.tf │ │ │ ├── variables.tf │ │ │ └── versions.tf │ ├── apply │ │ ├── main.tf │ │ ├── outputs.tf │ │ ├── template.tftpl │ │ └── variables.tf │ ├── backend_config │ │ ├── backend.tf │ │ └── main.tf │ ├── no_outputs │ │ ├── main.tf │ │ └── variables.tf │ ├── plan.auto.tfvars │ ├── plan │ │ ├── main.tf │ │ ├── outputs.tf │ │ └── variables.tf │ ├── plan_no_resource_changes │ │ ├── main.tf │ │ └── terragrunt.hcl │ ├── plan_no_variables │ │ ├── main.tf │ │ └── outputs.tf │ ├── plan_output.json │ ├── plan_output_no_prior.json │ ├── prevent_destroy │ │ ├── main.tf │ │ └── module │ │ │ └── main.tf │ ├── state.json │ └── tg_apply_all │ │ ├── bar │ │ └── terragrunt.hcl │ │ ├── foo │ │ └── terragrunt.hcl │ │ └── terragrunt.hcl ├── test_args.py ├── test_backend_config.py ├── test_cache.py ├── test_files.py ├── test_no_outputs.py ├── test_pickle.py ├── test_plan.py ├── test_prevent_destroy.py ├── test_sample_apply.py ├── test_sample_plan.py ├── test_sample_plan_error.py ├── test_sample_plan_no_variables.py ├── test_state.py ├── test_tg_all.py ├── test_value_dict.py └── test_workspace.py ├── tftest.py └── tools ├── REQUIREMENTS.txt └── changelog.py /.github/workflows/linting.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | name: Linting 16 | on: 17 | pull_request: 18 | branches: 19 | - master 20 | tags: 21 | - ci 22 | - lint 23 | 24 | jobs: 25 | linting: 26 | name: Linting 27 | runs-on: ubuntu-latest 28 | steps: 29 | - uses: actions/checkout@v2 30 | 31 | - name: Set up Python 32 | id: python-setup 33 | uses: actions/setup-python@v2 34 | with: 35 | python-version: "3.9" 36 | 37 | - name: Install dependencies 38 | id: python-dependencies 39 | run: | 40 | pip install -r DEV-REQUIREMENTS.txt 41 | 42 | - name: Boilerplate 43 | id: boilerplate 44 | run: | 45 | python3 .github/workflows/scripts/check_boilerplate.py $GITHUB_WORKSPACE 46 | 47 | - name: Check python formatting 48 | id: lint 49 | run: | 50 | echo '[pep8]' > pep8 51 | echo 'indent-size = 2' >> pep8 52 | autopep8 --diff --global-config pep8 --recursive --exit-code \ 53 | *.py test 54 | -------------------------------------------------------------------------------- /.github/workflows/scripts/check_boilerplate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Copyright 2019 Google LLC 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # https://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | 17 | import glob 18 | import os 19 | import re 20 | import sys 21 | 22 | 23 | _EXCLUDE_DIRS = ('.git', '.terraform') 24 | _MATCH_FILES = ( 25 | 'Dockerfile', '.py', '.sh', '.tf', '.yaml', '.yml' 26 | ) 27 | _MATCH_STRING = ( 28 | r'^\s*[#\*]\sCopyright [0-9]{4} Google LLC$\s+[#\*]\s+' 29 | r'[#\*]\sLicensed under the Apache License, Version 2.0 ' 30 | r'\(the "License"\);\s+' 31 | ) 32 | _MATCH_RE = re.compile(_MATCH_STRING, re.M) 33 | 34 | 35 | def main(dir): 36 | "Cycle through files in dir and check for the Apache 2.0 boilerplate." 37 | errors, warnings = [], [] 38 | for root, dirs, files in os.walk(dir): 39 | dirs[:] = [d for d in dirs if d not in _EXCLUDE_DIRS] 40 | for fname in files: 41 | if fname in _MATCH_FILES or os.path.splitext(fname)[1] in _MATCH_FILES: 42 | fpath = os.path.abspath(os.path.join(root, fname)) 43 | try: 44 | if not _MATCH_RE.search(open(fpath).read()): 45 | errors.append(fpath) 46 | except (IOError, OSError): 47 | warnings.append(fpath) 48 | if warnings: 49 | print('The following files cannot be accessed:') 50 | print('\n'.join(' - {}'.format(s) for s in warnings)) 51 | if errors: 52 | print('The following files are missing the license boilerplate:') 53 | print('\n'.join(' - {}'.format(s) for s in errors)) 54 | sys.exit(1) 55 | 56 | 57 | if __name__ == '__main__': 58 | if len(sys.argv) != 2: 59 | raise SystemExit('No directory passed.') 60 | main(sys.argv[1]) 61 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | name: Python tests 16 | on: 17 | pull_request: 18 | branches: 19 | - master 20 | tags: 21 | - ci 22 | - test 23 | 24 | env: 25 | GOOGLE_APPLICATION_CREDENTIALS: "/home/runner/credentials.json" 26 | PYTEST_ADDOPTS: "--color=yes" 27 | PYTHON_VERSION: 3.9 28 | TF_PLUGIN_CACHE_DIR: "/home/runner/.terraform.d/plugin-cache" 29 | TF_VERSION: 1.1.8 30 | TG_VERSION: 0.36.9 31 | 32 | jobs: 33 | tests: 34 | name: Python tests 35 | runs-on: ubuntu-latest 36 | steps: 37 | - uses: actions/checkout@v2 38 | 39 | - name: Config auth 40 | id: auth 41 | run: | 42 | echo '{"type": "service_account", "project_id": "test-only"}' \ 43 | | tee -a $GOOGLE_APPLICATION_CREDENTIALS 44 | 45 | - name: Set up Python 46 | id: python-setup 47 | uses: actions/setup-python@v2 48 | with: 49 | python-version: ${{ env.PYTHON_VERSION }} 50 | 51 | - name: Set up Terraform 52 | id: tf-setup 53 | uses: hashicorp/setup-terraform@v1 54 | with: 55 | terraform_version: ${{ env.TF_VERSION }} 56 | terraform_wrapper: false 57 | 58 | - name: Install Terragrunt 59 | id: tg-setup 60 | run: | 61 | sudo wget -q -O /bin/terragrunt \ 62 | https://github.com/gruntwork-io/terragrunt/releases/download/v${{ env.TG_VERSION }}/terragrunt_linux_amd64 63 | sudo chmod +x /bin/terragrunt 64 | terragrunt -v 65 | 66 | - name: Run tests 67 | id: python-test 68 | run: | 69 | mkdir -p ${{ env.TF_PLUGIN_CACHE_DIR }} 70 | pip install -r DEV-REQUIREMENTS.txt 71 | pytest -vv 72 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | 53 | # Translations 54 | *.mo 55 | *.pot 56 | 57 | # Django stuff: 58 | *.log 59 | local_settings.py 60 | db.sqlite3 61 | 62 | # Flask stuff: 63 | instance/ 64 | .webassets-cache 65 | 66 | # Scrapy stuff: 67 | .scrapy 68 | 69 | # Sphinx documentation 70 | docs/_build/ 71 | 72 | # PyBuilder 73 | target/ 74 | 75 | # Jupyter Notebook 76 | .ipynb_checkpoints 77 | 78 | # IPython 79 | profile_default/ 80 | ipython_config.py 81 | 82 | # pyenv 83 | .python-version 84 | 85 | # pipenv 86 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 87 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 88 | # having no cross-platform support, pipenv may install dependencies that don’t work, or not 89 | # install all needed dependencies. 90 | #Pipfile.lock 91 | 92 | # celery beat schedule file 93 | celerybeat-schedule 94 | 95 | # SageMath parsed files 96 | *.sage.py 97 | 98 | # Environments 99 | .env 100 | .venv 101 | env/ 102 | venv/ 103 | ENV/ 104 | env.bak/ 105 | venv.bak/ 106 | 107 | # Spyder project settings 108 | .spyderproject 109 | .spyproject 110 | 111 | # Rope project settings 112 | .ropeproject 113 | 114 | # mkdocs documentation 115 | /site 116 | 117 | # mypy 118 | .mypy_cache/ 119 | .dmypy.json 120 | dmypy.json 121 | 122 | # Pyre type checker 123 | .pyre/ 124 | 125 | # Local .terraform directories 126 | **/.terraform/* 127 | 128 | # Local .terragrunt directories 129 | **/.terragrunt-cache/* 130 | 131 | # Local .tftest-cache directories 132 | **/.tftest-cache/* 133 | 134 | # .tfstate files 135 | *.tfstate 136 | *.tfstate.* 137 | 138 | # Crash log files 139 | crash.log 140 | 141 | **/.terraform.lock.hcl 142 | 143 | # Ignore any .tfvars files that are generated automatically for each Terraform run. Most 144 | # .tfvars files are managed as part of configuration and so should be included in 145 | # version control. 146 | # 147 | # example.tfvars 148 | 149 | # Ignore override files as they are usually used to override resources locally and so 150 | # are not checked in 151 | override.tf 152 | override.tf.json 153 | *_override.tf 154 | *_override.tf.json 155 | 156 | # Include override files you do wish to add to version control using negated pattern 157 | # 158 | # !example_override.tf 159 | 160 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan 161 | # example: *tfplan* 162 | 163 | # Ignore IDE configuration files 164 | **/.idea/* 165 | **/.vscode/* 166 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | 6 | ## [Unreleased] 7 | 8 | ## [1.8.5] - 2023-10-11 9 | 10 | 11 | - [[#76](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/76)] feat(75): provide access to prior state resources if available ([fergoid](https://github.com/fergoid)) 12 | 13 | ## [1.8.4] - 2023-04-24 14 | 15 | 16 | - [[#72](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/72)] Stop Windows hangs when last output is on stderr ([andrewesweet](https://github.com/andrewesweet)) 17 | - [[#71](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/71)] Make cache directory path Windows friendly ([andrewesweet](https://github.com/andrewesweet)) 18 | 19 | ## [1.8.3] - 2023-04-16 20 | 21 | - [[#70](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/70)] Parse state flag ([andrewesweet](https://github.com/andrewesweet)) 22 | 23 | ## [1.8.2] - 2023-01-26 24 | 25 | 26 | - [[#69](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/69)] [feature] added support to pass a complex dict ([MrImpossibru](https://github.com/MrImpossibru)) 27 | 28 | ## [1.8.1] - 2022-12-01 29 | 30 | 31 | - [[#66](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/66)] Support both lists and tuples in var-file arg and add relevant test ([ludoo](https://github.com/ludoo)) 32 | - [[#65](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/65)] Allow passing a list to tf_var_file ([juliocc](https://github.com/juliocc)) 33 | 34 | ## [1.8.0] - 2022-11-09 35 | 36 | 37 | - [[#64](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/64)] Create cache hash after command ([marshall7m](https://github.com/marshall7m)) 38 | 39 | ## [1.7.7] - 2022-11-04 40 | 41 | 42 | - [[#63](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/63)] Cache hidden dir bug ([marshall7m](https://github.com/marshall7m)) 43 | 44 | ## [1.7.6] - 2022-11-02 45 | 46 | - [[#62](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/62)] Inherit TerraformJSONBase from Mapping ABC ([juliocc](https://github.com/juliocc)) 47 | 48 | ## [1.7.5] - 2022-11-02 49 | 50 | 51 | - [[#61](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/61)] Replace cache checksumdir pypi pkg with internal _dirhash() ([marshall7m](https://github.com/marshall7m)) 52 | - [[#60](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/60)] Finer grained cache hash input ([marshall7m](https://github.com/marshall7m)) 53 | 54 | ## [1.7.4] - 2022-10-02 55 | 56 | 57 | - [[#57](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/57)] Cache TerraformTest methods ([marshall7m](https://github.com/marshall7m)) 58 | 59 | ## [1.7.3] - 2022-09-29 60 | 61 | 62 | - [[#58](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/58)] fix: Handle terraform crashes ([grahamhar](https://github.com/grahamhar)) 63 | 64 | ## [1.7.2] - 2022-09-15 65 | 66 | 67 | - [[#56](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/56)] add ability to pickle tftest instance objects ([marshall7m](https://github.com/marshall7m)) 68 | 69 | ## [1.7.1] - 2022-08-04 70 | 71 | 72 | 73 | - [[#53](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/53)] Manage changelog ([ludoo](https://github.com/ludoo)) 74 | - [[#50](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/50)] Add command error to exception class, cleanup backup files ([ludoo](https://github.com/ludoo)) 75 | - [[#51](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/51)] Fix double close on Windows 10 ([sueastside](https://github.com/sueastside)) 76 | - [[#52](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/52)] Fix _abspath for windows ([sueastside](https://github.com/sueastside)) 77 | 78 | ## [1.7.0] - 2022-07-11 79 | 80 | 81 | 82 | - [[#48](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/48)] feat: Add support for Terraform workspaces ([SplinterHead](https://github.com/SplinterHead)) 83 | 84 | ## [1.6.5] - 2022-05-04 85 | 86 | 87 | 88 | - [[#45](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/45)] Log output while Popen is in progress ([leighpascoe](https://github.com/leighpascoe)) 89 | - [[#47](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/47)] Add job names in workflows ([ludoo](https://github.com/ludoo)) 90 | - [[#46](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/46)] Switch to Github workflows ([ludoo](https://github.com/ludoo)) 91 | 92 | ## [1.6.4] - 2022-01-24 93 | 94 | 95 | 96 | - [[#43](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/43)] feat: Add ability to disable lifecycle prevent_destroy ([grahamhar](https://github.com/grahamhar)) 97 | 98 | ## [1.6.3] - 2022-01-06 99 | 100 | 101 | 102 | - [[#40](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/40)] Adds support for the upgrade argument of the init command ([lorengordon](https://github.com/lorengordon)) 103 | 104 | ## [1.6.2] - 2021-12-31 105 | 106 | 107 | 108 | - [[#38](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/38)] fix: On windows if a file is read only the cleanup reports errors. ([grahamhar](https://github.com/grahamhar)) 109 | 110 | ## [1.6.1] - 2021-11-25 111 | 112 | 113 | 114 | - [[#32](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/32)] Terragrunt support ([davidtam](https://github.com/davidtam)) 115 | 116 | ## [1.6.0] - 2021-06-08 117 | 118 | 119 | 120 | - [[#31](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/31)] Update CI to use Terraform 0.15 ([ludoo](https://github.com/ludoo)) 121 | 122 | ## [1.5.7] - 2021-06-02 123 | 124 | 125 | 126 | - [[#28](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/28)] Windows fixes ([andrewesweet](https://github.com/andrewesweet)) 127 | 128 | ## [1.5.6] - 2021-04-15 129 | 130 | 131 | 132 | - [[#27](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/27)] Don't fail when resource_changes key not present in output ([brownmike](https://github.com/brownmike)) 133 | 134 | ## [1.5.5] - 2021-04-15 135 | 136 | 137 | 138 | - [[#20](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/20)] Fix quoting in backend config args ([ludoo](https://github.com/ludoo)) 139 | 140 | ## [1.5.4] - 2020-11-26 141 | 142 | 143 | 144 | - [[#19](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/19)] New test to verify issue #17 ([ludoo](https://github.com/ludoo)) 145 | 146 | ## [1.5.3] - 2020-11-07 147 | 148 | 149 | 150 | - [[#16](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/16)] Allow passing custom environment variables to terraform ([juliocc](https://github.com/juliocc)) 151 | 152 | ## [1.5.2] - 2020-11-05 153 | 154 | 155 | 156 | - [[#15](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/15)] Accept plan outputs with no variables ([ludoo](https://github.com/ludoo)) 157 | 158 | ## [1.5.1] - 2020-06-04 159 | 160 | 161 | 162 | - [[#12](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/12)] add support for target option ([stenh0use](https://github.com/stenh0use)) 163 | 164 | ## [1.5.0] - 2020-02-15 165 | 166 | 167 | 168 | - [[#11](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/11)] Fix errors on missing Terraform outputs ([ludoo](https://github.com/ludoo)) 169 | 170 | ## [1.4.1] - 2020-01-11 171 | 172 | 173 | 174 | - [[#8](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/8)] Add an option to leave terraform init data on exit ([ludoo](https://github.com/ludoo)) 175 | 176 | ## [1.2.0] - 2019-11-05 177 | 178 | 179 | 180 | - [[#4](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/4)] Refactor module and tests for v1.0.0 ([ludoo](https://github.com/ludoo)) 181 | 182 | ## [1.1.0] - 2019-11-05 183 | 184 | 185 | 186 | - [[#6](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/6)] Proxy raw dict attributes in TerraformValueDict ([ludoo](https://github.com/ludoo)) 187 | - [[#5](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/5)] Minimal fixes to v1.0.0 ([ludoo](https://github.com/ludoo)) 188 | 189 | ## [1.0.1] - 2019-10-30 190 | 191 | 192 | 193 | - [[#2](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/2)] Add support for force-copy and refresh arguments ([ludoo](https://github.com/ludoo)) 194 | 195 | ## [1.0.0] - 2019-10-30 196 | 197 | 198 | 199 | - [[#3](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/3)] WIP: add support for Terraform plan JSON output ([ludoo](https://github.com/ludoo)) 200 | 201 | ## [0.6.2] - 2019-09-10 202 | 203 | 204 | 205 | - [[#1](https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/pull/1)] remove tfstate ; add tf files to gitignore ([bastiandg](https://github.com/bastiandg)) 206 | 207 | ## [0.6.0] - 2019-09-10 208 | 209 | 210 | 211 | 212 | [Unreleased]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.8.4...HEAD 213 | [1.8.4]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.8.3...v1.8.4 214 | [1.8.3]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.8.2...v1.8.3 215 | [1.8.2]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.8.1...v1.8.2 216 | [1.8.1]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.8.0...v1.8.1 217 | [1.8.0]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.7.7...v1.8.0 218 | [1.7.7]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.7.6...v1.7.7 219 | [1.7.6]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.7.5...v1.7.6 220 | [1.7.5]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.7.4...v1.7.5 221 | [1.7.4]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.7.3...v1.7.4 222 | [1.7.3]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.7.2...v1.7.3 223 | [1.7.2]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.7.1...v1.7.2 224 | [1.7.1]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.7.0...v1.7.1 225 | [1.7.0]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.6.5...v1.7.0 226 | [1.6.5]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.6.4...v1.6.5 227 | [1.6.4]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.6.3...v1.6.4 228 | [1.6.3]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.6.2...v1.6.3 229 | [1.6.2]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.6.1...v1.6.2 230 | [1.6.1]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.6.0...v1.6.1 231 | [1.6.0]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.5.7...v1.6.0 232 | [1.5.7]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.5.6...v1.5.7 233 | [1.5.6]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.5.5...v1.5.6 234 | [1.5.5]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.5.4...v1.5.5 235 | [1.5.4]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.5.3...v1.5.4 236 | [1.5.3]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.5.2...v1.5.3 237 | [1.5.2]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.5.1...v1.5.2 238 | [1.5.1]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.5.0...v1.5.1 239 | [1.5.0]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.4.1...v1.5.0 240 | [1.4.1]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.2.0...v1.4.1 241 | [1.2.0]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.1.0...v1.2.0 242 | [1.1.0]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.0.1...v1.1.0 243 | [1.0.1]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v1.0.0...v1.0.1 244 | [1.0.0]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v0.6.2...v1.0.0 245 | [0.6.2]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v0.6.0...v0.6.2 246 | [0.6.0]: https://github.com/GoogleCloudPlatform/terraform-python-testing-helper/compare/v0.1...v0.6.0 247 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | We'd love to accept your patches and contributions to this project. There are 4 | just a few small guidelines you need to follow. 5 | 6 | ## Contributor License Agreement 7 | 8 | Contributions to this project must be accompanied by a Contributor License 9 | Agreement. You (or your employer) retain the copyright to your contribution; 10 | this simply gives us permission to use and redistribute your contributions as 11 | part of the project. Head over to to see 12 | your current agreements on file or to sign a new one. 13 | 14 | You generally only need to submit a CLA once, so if you've already submitted one 15 | (even if it was for a different project), you probably don't need to do it 16 | again. 17 | 18 | ## Code reviews 19 | 20 | All submissions, including submissions by project members, require review. We 21 | use GitHub pull requests for this purpose. Consult 22 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more 23 | information on using pull requests. 24 | 25 | ## Community Guidelines 26 | 27 | This project follows [Google's Open Source Community 28 | Guidelines](https://opensource.google.com/conduct/). 29 | -------------------------------------------------------------------------------- /DEV-REQUIREMENTS.txt: -------------------------------------------------------------------------------- 1 | autopep8>=2.0 2 | pytest>=7.2 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Test Helper for Terraform 2 | 3 | This 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. 4 | 5 | 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 run the full `apply`/`output`/`destroy` cycle, and can then be used to test the actual created resources, or the state file. 6 | 7 | As 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. 8 | 9 | This 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. 10 | 11 | ## Example Usage 12 | 13 | The [`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. 14 | 15 | This is a test that uses plan output on an actual module: 16 | 17 | ```hcl 18 | import pytest 19 | import tftest 20 | 21 | 22 | @pytest.fixture 23 | def plan(fixtures_dir): 24 | tf = tftest.TerraformTest('plan', fixtures_dir) 25 | tf.setup(extra_files=['plan.auto.tfvars']) 26 | return tf.plan(output=True) 27 | 28 | 29 | def test_variables(plan): 30 | assert 'prefix' in plan.variables 31 | assert plan.variables['names'] == ['one', 'two'] 32 | 33 | 34 | def test_outputs(plan): 35 | assert sorted(plan.outputs['gcs_buckets'].keys()) == plan.variables['names'] 36 | 37 | 38 | def test_root_resource(plan): 39 | res = plan.resources['google_project_iam_member.test_root_resource'] 40 | assert res['values']['project'] == plan.variables['project_id'] 41 | 42 | 43 | def test_modules(plan): 44 | mod = plan.modules['module.gcs-buckets'] 45 | res = mod.resources['google_storage_bucket.buckets[0]'] 46 | assert res['values']['location'] == plan.variables['gcs_location'] 47 | ``` 48 | 49 | ## Terragrunt support 50 | 51 | Support for Terragrunt actually follows the same principle of the thin `TerraformTest` wrapper. 52 | 53 | Please see the following example for how to use it: 54 | 55 | ```python 56 | import pytest 57 | import tftest 58 | 59 | 60 | @pytest.fixture 61 | def run_all_apply_out(fixtures_dir): 62 | # notice for run-all, you need to specify when TerragruntTest is constructed 63 | tg = tftest.TerragruntTest('tg_apply_all', fixtures_dir, tg_run_all=True) 64 | # the rest is very similar to how you use TerraformTest 65 | tg.setup() 66 | # to use --terragrunt-