From d36dd7c4a05a7247be4f3617ff0837b87b19e2e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaan=20Kat=C4=B1rc=C4=B1o=C4=9Flu?= <32440802+Kudbettin@users.noreply.github.com> Date: Sat, 4 Jul 2020 00:44:08 +0300 Subject: [PATCH] move jsonification to given (#308) --- CHANGELOG.md | 3 +++ terraform_compliance/common/helper.py | 16 +++++++++++ .../given/i_have_name_section_configured.py | 21 ++++++++------- tests/functional/test_issue-307/main.tf | 27 +++++++++++++++++++ tests/functional/test_issue-307/plan.out.json | 1 + tests/functional/test_issue-307/test.feature | 7 +++++ 6 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 tests/functional/test_issue-307/main.tf create mode 100644 tests/functional/test_issue-307/plan.out.json create mode 100644 tests/functional/test_issue-307/test.feature diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b93886b..804fcf31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +## Unreleased +* Jsonfiy the stash on creation to prevent bugs related to jsonification. + ## 1.2.7 (2020-06-19) * Fixed faulty over restriction in [Then its singular value condition match the "search_regex" regex](https://terraform-compliance.com/pages/bdd-references/then.html#then-its-singular-value-condition-match-the-search-regex-regex). * New scenario tag: [noskip](https://terraform-compliance.com/pages/bdd-references/using_tags.html#supported-tags) tags diff --git a/terraform_compliance/common/helper.py b/terraform_compliance/common/helper.py index fb0b43e2..df1f7e9c 100644 --- a/terraform_compliance/common/helper.py +++ b/terraform_compliance/common/helper.py @@ -205,6 +205,22 @@ def jsonify(string): return string +def recursive_jsonify(haystack): + if isinstance(haystack, str): + haystack = jsonify(haystack) + if isinstance(haystack, (list, dict)): + return recursive_jsonify(haystack) + return haystack + + if isinstance(haystack, dict): + haystack = {key: recursive_jsonify(value) for key, value in haystack.items()} + + if isinstance(haystack, list): + haystack = [recursive_jsonify(value) for value in haystack] + + return haystack + + def get_resource_name_from_stash(stash, alternative_stash=None, address=None): if address is not None: return {'address': address} diff --git a/terraform_compliance/steps/given/i_have_name_section_configured.py b/terraform_compliance/steps/given/i_have_name_section_configured.py index 1ac579eb..73ecbf20 100644 --- a/terraform_compliance/steps/given/i_have_name_section_configured.py +++ b/terraform_compliance/steps/given/i_have_name_section_configured.py @@ -7,7 +7,8 @@ find_root_by_key, remove_mounted_resources, transform_asg_style_tags, - convert_resource_type + convert_resource_type, + recursive_jsonify ) from terraform_compliance.extensions.ext_radish_bdd import skip_step import re @@ -40,7 +41,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra if name in ('a resource', 'any resource', 'resources'): _step_obj.context.type = type_name _step_obj.context.name = name - _step_obj.context.stash = [obj for key, obj in _terraform_config.config.terraform.resources_raw.items()] + _step_obj.context.stash = recursive_jsonify([obj for key, obj in _terraform_config.config.terraform.resources_raw.items()]) _step_obj.context.addresses = get_resource_address_list_from_stash(_step_obj.context.stash) _step_obj.context.property_name = type_name return True @@ -48,7 +49,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra elif name in ('an output', 'any output', 'outputs'): _step_obj.context.type = 'output' _step_obj.context.name = name - _step_obj.context.stash = [obj for key, obj in _terraform_config.config.terraform.configuration['outputs'].items()] + _step_obj.context.stash = recursive_jsonify([obj for key, obj in _terraform_config.config.terraform.configuration['outputs'].items()]) _step_obj.context.addresses = get_resource_address_list_from_stash(_terraform_config.config.terraform.configuration['outputs']) _step_obj.context.property_name = 'output' return True @@ -56,7 +57,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra elif name in ('a variable', 'any variable', 'variables'): _step_obj.context.type = 'variable' _step_obj.context.name = name - _step_obj.context.stash = [obj for key, obj in _terraform_config.config.terraform.configuration['variables'].items()] + _step_obj.context.stash = recursive_jsonify([obj for key, obj in _terraform_config.config.terraform.configuration['variables'].items()]) _step_obj.context.addresses = 'variable' _step_obj.context.property_name = 'variable' return True @@ -79,7 +80,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra if resource_list: _step_obj.context.type = type_name _step_obj.context.name = name - _step_obj.context.stash = resource_list + _step_obj.context.stash = recursive_jsonify(resource_list) _step_obj.context.addresses = get_resource_address_list_from_stash(resource_list) _step_obj.context.property_name = type_name return True @@ -91,7 +92,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra if resource_list: _step_obj.context.type = type_name _step_obj.context.name = name - _step_obj.context.stash = resource_list + _step_obj.context.stash = recursive_jsonify(resource_list) _step_obj.context.addresses = get_resource_address_list_from_stash(resource_list) _step_obj.context.property_name = type_name return True @@ -102,7 +103,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra if found_variable: _step_obj.context.type = type_name _step_obj.context.name = name - _step_obj.context.stash = found_variable + _step_obj.context.stash = recursive_jsonify(found_variable) _step_obj.context.addresses = name _step_obj.context.property_name = type_name return True @@ -113,7 +114,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra if found_output: _step_obj.context.type = type_name _step_obj.context.name = name - _step_obj.context.stash = found_output + _step_obj.context.stash = recursive_jsonify(found_output) _step_obj.context.addresses = name _step_obj.context.property_name = type_name return True @@ -124,7 +125,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra if found_provider: _step_obj.context.type = type_name _step_obj.context.name = name - _step_obj.context.stash = found_provider + _step_obj.context.stash = recursive_jsonify(found_provider) _step_obj.context.addresses = name _step_obj.context.address = name _step_obj.context.property_name = type_name @@ -137,7 +138,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra if data_list: _step_obj.context.type = type_name _step_obj.context.name = name - _step_obj.context.stash = data_list + _step_obj.context.stash = recursive_jsonify(data_list) _step_obj.context.addresses = name _step_obj.context.address = name _step_obj.context.property_name = type_name diff --git a/tests/functional/test_issue-307/main.tf b/tests/functional/test_issue-307/main.tf new file mode 100644 index 00000000..1670d515 --- /dev/null +++ b/tests/functional/test_issue-307/main.tf @@ -0,0 +1,27 @@ +resource "aws_sns_topic" "test" { + name = "my-topic-with-policy" +} + +resource "" "default" { + arn = aws_sns_topic.test.arn + + policy =<