Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,5 @@ jobs:
- name: Check python formatting
id: lint
run: |
echo '[pep8]' > pep8
echo 'indent-size = 2' >> pep8
autopep8 --diff --global-config pep8 --recursive --exit-code \
yapf --diff --recursive --parallel \
*.py test
4 changes: 4 additions & 0 deletions .style.yapf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[style]
based_on_style=google
indent_width=2
split_before_named_assigns=false
2 changes: 1 addition & 1 deletion DEV-REQUIREMENTS.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
autopep8>=2.0
yapf>=0.40
pytest>=7.2
32 changes: 0 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,6 @@ def test_modules(plan):
assert res['values']['location'] == plan.variables['gcs_location']
```

## Terragrunt support

Support for Terragrunt actually follows the same principle of the thin `TerraformTest` wrapper.

Please see the following example for how to use it:

```python
import pytest
import tftest


@pytest.fixture
def run_all_apply_out(fixtures_dir):
# notice for run-all, you need to specify when TerragruntTest is constructed
tg = tftest.TerragruntTest('tg_apply_all', fixtures_dir, tg_run_all=True)
# the rest is very similar to how you use TerraformTest
tg.setup()
# to use --terragrunt-<option>, pass in tg_<option in snake case>
tg.apply(output=False, tg_non_interactive=True)
yield tg.output()
tg.destroy(auto_approve=True, tg_non_interactive=True)


def test_run_all_apply(run_all_apply_out):
triggers = [o["triggers"] for o in run_all_apply_out]
assert [{'name': 'foo', 'template': 'sample template foo'}] in triggers
assert [{'name': 'bar', 'template': 'sample template bar'}] in triggers
assert [{'name': 'one', 'template': 'sample template one'},
{'name': 'two', 'template': 'sample template two'}] in triggers
assert len(run_all_apply_out) == 3
```

## Caching

The `TerraformTest` `setup`, `init`, `plan`, `apply`, `output` and `destroy` methods have the ability to cache it's associate output to a local `.tftest-cache` directory. For subsequent calls of the method, the cached value can be returned instead of calling the actual underlying `terraform` command. Using the cache value can be significantly faster than running the Terraform command again especially if the command is time-intensive.
Expand Down
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

from tftest import __version__


with open("README.md", "r") as fh:
long_description = fh.read()


setuptools.setup(
name="tftest",
version=__version__,
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/plan_no_resource_changes/terragrunt.hcl

This file was deleted.

11 changes: 0 additions & 11 deletions test/fixtures/tg_apply_all/bar/terragrunt.hcl

This file was deleted.

11 changes: 0 additions & 11 deletions test/fixtures/tg_apply_all/foo/terragrunt.hcl

This file was deleted.

11 changes: 0 additions & 11 deletions test/fixtures/tg_apply_all/terragrunt.hcl

This file was deleted.

212 changes: 124 additions & 88 deletions test/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,128 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"Test the function for mapping Terraform arguments."
import pytest

import tftest


ARGS_TESTS = (
({'auto_approve': True}, ['-auto-approve']),
({'auto_approve': False}, []),
({'backend': True}, []),
({'backend': None}, []),
({'backend': False}, ['-backend=false']),
({'color': True}, []),
({'color': False}, ['-no-color']),
({'color': False, 'input': False}, ['-no-color', '-input=false']),
({'force_copy': True}, ['-force-copy']),
({'force_copy': None}, []),
({'force_copy': False}, []),
({'input': True}, []),
({'input': False}, ['-input=false']),
({'json_format': True}, ['-json']),
({'json_format': False}, []),
({'lock': True}, []),
({'lock': False}, ['-lock=false']),
({'plugin_dir': ''}, []),
({'plugin_dir': 'abc'}, ['-plugin-dir', 'abc']),
({'refresh': True}, []),
({'refresh': None}, []),
({'refresh': False}, ['-refresh=false']),
({'upgrade': True}, ['-upgrade']),
({'upgrade': False}, []),
({'tf_var_file': None}, []),
({'tf_var_file': 'foo.tfvar'}, ['-var-file=foo.tfvar']),
({'tf_var_file': ['foo.tfvar', 'bar.tfvar']}, [
'-var-file=foo.tfvar', '-var-file=bar.tfvar']),
({'tf_vars': {'text': 'text'}}, ['-var', 'text=text']),
({'tf_vars': {'number': 0}}, ['-var', 'number=0']),
({'tf_vars': {'bool': False}}, ['-var', 'bool=False']),
({'tf_vars': {'dict': {'text': 'text'}}},
['-var', 'dict={"text": "text"}']),
({'tf_vars': {'list': ['item1', 'item2']}},
['-var', 'list=["item1", "item2"]']),
({'tf_vars': {'dict': {'list': ['item1', 'item2']}}},
['-var', 'dict={"list": ["item1", "item2"]}']),
({
'auto_approve': True
}, ['-auto-approve']),
({
'auto_approve': False
}, []),
({
'backend': True
}, []),
({
'backend': None
}, []),
({
'backend': False
}, ['-backend=false']),
({
'color': True
}, []),
({
'color': False
}, ['-no-color']),
({
'color': False,
'input': False
}, ['-no-color', '-input=false']),
({
'force_copy': True
}, ['-force-copy']),
({
'force_copy': None
}, []),
({
'force_copy': False
}, []),
({
'input': True
}, []),
({
'input': False
}, ['-input=false']),
({
'json_format': True
}, ['-json']),
({
'json_format': False
}, []),
({
'lock': True
}, []),
({
'lock': False
}, ['-lock=false']),
({
'plugin_dir': ''
}, []),
({
'plugin_dir': 'abc'
}, ['-plugin-dir', 'abc']),
({
'refresh': True
}, []),
({
'refresh': None
}, []),
({
'refresh': False
}, ['-refresh=false']),
({
'upgrade': True
}, ['-upgrade']),
({
'upgrade': False
}, []),
({
'tf_var_file': None
}, []),
({
'tf_var_file': 'foo.tfvar'
}, ['-var-file=foo.tfvar']),
({
'tf_var_file': ['foo.tfvar', 'bar.tfvar']
}, ['-var-file=foo.tfvar', '-var-file=bar.tfvar']),
({
'tf_vars': {
'text': 'text'
}
}, ['-var', 'text=text']),
({
'tf_vars': {
'number': 0
}
}, ['-var', 'number=0']),
({
'tf_vars': {
'bool': False
}
}, ['-var', 'bool=False']),
({
'tf_vars': {
'dict': {
'text': 'text'
}
}
}, ['-var', 'dict={"text": "text"}']),
({
'tf_vars': {
'list': ['item1', 'item2']
}
}, ['-var', 'list=["item1", "item2"]']),
({
'tf_vars': {
'dict': {
'list': ['item1', 'item2']
}
}
}, ['-var', 'dict={"list": ["item1", "item2"]}']),
)


Expand All @@ -65,56 +142,15 @@ def test_args(kwargs, expected):
assert tftest.parse_args(**kwargs) == expected


TERRAGRUNT_ARGS_TESTCASES = [
({"tg_config": "Obama"}, ['--terragrunt-config', 'Obama']),
({"tg_tfpath": "Barrack"}, ['--terragrunt-tfpath', 'Barrack']),
({"tg_no_auto_init": True}, ['--terragrunt-no-auto-init']),
({"tg_no_auto_init": False}, []),
({"tg_no_auto_retry": True}, ['--terragrunt-no-auto-retry']),
({"tg_no_auto_retry": False}, []),
({"tg_non_interactive": True}, ['--terragrunt-non-interactive']),
({"tg_non_interactive": False}, []),
({"tg_working_dir": "George"}, ['--terragrunt-working-dir', 'George']),
({"tg_download_dir": "Bush"}, ['--terragrunt-download-dir', 'Bush']),
({"tg_source": "Clinton"}, ['--terragrunt-source', 'Clinton']),
({"tg_source_update": True}, ['--terragrunt-source-update']),
({"tg_source_update": False}, []),
({"tg_iam_role": "Bill"}, ['--terragrunt-iam-role', 'Bill']),
({"tg_ignore_dependency_errors": True}, [
'--terragrunt-ignore-dependency-errors']),
({"tg_ignore_dependency_errors": False}, []),
({"tg_ignore_dependency_order": True}, [
'--terragrunt-ignore-dependency-order']),
({"tg_ignore_dependency_order": False}, []),
({"tg_ignore_external_dependencies": "dont care what is here"},
['--terragrunt-ignore-external-dependencies']),
({"tg_include_external_dependencies": True}, [
'--terragrunt-include-external-dependencies']),
({"tg_include_external_dependencies": False}, []),
({"tg_parallelism": 20}, ['--terragrunt-parallelism 20']),
({"tg_exclude_dir": "Ronald"}, ['--terragrunt-exclude-dir', 'Ronald']),
({"tg_include_dir": "Reagan"}, ['--terragrunt-include-dir', 'Reagan']),
({"tg_check": True}, ['--terragrunt-check']),
({"tg_check": False}, []),
({"tg_hclfmt_file": "Biden"}, ['--terragrunt-hclfmt-file', 'Biden']),
({"tg_override_attr": {"Iron": "Man", "Captain": "America"}},
['--terragrunt-override-attr=Iron=Man', '--terragrunt-override-attr=Captain=America']),
({"tg_debug": True}, ['--terragrunt-debug']),
({"tg_debug": False}, []),

]


@pytest.mark.parametrize("kwargs, expected", TERRAGRUNT_ARGS_TESTCASES)
def test_terragrunt_args(kwargs, expected):
assert tftest.parse_args(**kwargs) == expected


def test_var_args():
assert sorted(tftest.parse_args(init_vars={'a': 1, 'b': '["2"]'})) == sorted(
["-backend-config=a=1", '-backend-config=b=["2"]'])
assert sorted(tftest.parse_args(tf_vars={'a': 1, 'b': '["2"]'})) == sorted(
['-var', 'b=["2"]', '-var', 'a=1'])
assert sorted(tftest.parse_args(init_vars={
'a': 1,
'b': '["2"]'
})) == sorted(["-backend-config=a=1", '-backend-config=b=["2"]'])
assert sorted(tftest.parse_args(tf_vars={
'a': 1,
'b': '["2"]'
})) == sorted(['-var', 'b=["2"]', '-var', 'a=1'])


def test_targets():
Expand Down
1 change: 0 additions & 1 deletion test/test_backend_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"Test the plan output wrapper classes."

import pytest
Expand Down
Loading
Loading