Skip to content

Commit

Permalink
Merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
eerkunt committed Dec 6, 2019
2 parents 12b8f0b + 9d8ecb5 commit a513c8d
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 1.0.58 (2019-12-06)
* Enhanced `count` step where it was only applicable for resource properties, now it also works right after a `GIVEN` step. ([#187](https://github.com/eerkunt/terraform-compliance/issues/187))

## 1.0.57 (2019-11-15)
* Fixed a problem about encoding where `terraform-compliance` crashes on HCL files with UTF-8 content. ([#183](https://github.com/eerkunt/terraform-compliance/issues/183))

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ netaddr==0.7.19
colorful==0.5.4
filetype==1.0.5
junit-xml==1.8
lxml==4.4.1
lxml==4.4.2
35 changes: 26 additions & 9 deletions terraform_compliance/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# TODO: Figure out how the IAM policies/statements shown in the plan.out
# TODO: Implement an IAM Compliance via https://github.com/Netflix-Skunkworks/policyuniverse

types_list = ['resource', 'variable', 'provider', 'data', 'resource that supports tags']

@given(u'I have {name:ANY} defined')
@given(u'I have {name:ANY} {type_name:SECTION} configured')
def i_have_name_section_configured(_step_obj, name, type_name='resource', _terraform_config=world):
Expand Down Expand Up @@ -46,6 +48,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra
_step_obj.context.name = name
_step_obj.context.stash = [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

elif name == 'resource that supports tags':
Expand All @@ -65,6 +68,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra
_step_obj.context.name = name
_step_obj.context.stash = resource_list
_step_obj.context.addresses = get_resource_address_list_from_stash(resource_list)
_step_obj.context.property_name = type_name
return True

elif type_name == 'resource':
Expand All @@ -76,6 +80,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra
_step_obj.context.name = name
_step_obj.context.stash = resource_list
_step_obj.context.addresses = get_resource_address_list_from_stash(resource_list)
_step_obj.context.property_name = type_name
return True

elif type_name == 'variable':
Expand All @@ -86,6 +91,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra
_step_obj.context.name = name
_step_obj.context.stash = found_variable
_step_obj.context.addresses = name
_step_obj.context.property_name = type_name
return True

elif type_name == 'provider':
Expand All @@ -96,6 +102,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra
_step_obj.context.name = name
_step_obj.context.stash = found_provider
_step_obj.context.addresses = name
_step_obj.context.property_name = type_name
return True

elif type_name == 'data':
Expand All @@ -107,6 +114,7 @@ def i_have_name_section_configured(_step_obj, name, type_name='resource', _terra
_step_obj.context.name = name
_step_obj.context.stash = data_list
_step_obj.context.addresses = name
_step_obj.context.property_name = type_name
return True

skip_step(_step_obj, name)
Expand Down Expand Up @@ -159,6 +167,7 @@ def its_key_is_value(_step_obj, key, value):
else:
skip_step(_step_obj, value)


@when(u'its {key:PROPERTY} is not {value:PROPERTY}')
@when(u'its {key:PROPERTY} has not {value:PROPERTY}')
@when(u'its {key:PROPERTY} does not include {value:PROPERTY}')
Expand Down Expand Up @@ -387,16 +396,24 @@ def i_action_them(_step_obj, action_type):
if action_type == "count":
# WARNING: Only case where we set stash as a dictionary, instead of a list.
if type(_step_obj.context.stash) is list:
if type(_step_obj.context.stash[0]) is dict():
if _step_obj.context.stash.get('values'):
_step_obj.context.stash = seek_key_in_dict(_step_obj.context.stash, 'values')
count = 0
for result in _step_obj.context.stash:
count += len(result.get('values', {})) if result.get('values') else 1

_step_obj.context.stash = {'values': count}

# This means we are directly started counting without drilling down any property
# Thus, our target for the count is stash itself.
if _step_obj.context.property_name in types_list:
_step_obj.context.stash = dict(values=len(_step_obj.context.stash))

else:
_step_obj.context.stash = {'values': len(_step_obj.context.stash)}
if type(_step_obj.context.stash[0]) is dict:
if _step_obj.context.stash[0].get('values'):
_step_obj.context.stash = seek_key_in_dict(_step_obj.context.stash, 'values')
count = 0
for result in _step_obj.context.stash:
count += len(result.get('values', {})) if result.get('values') else 1

_step_obj.context.stash = dict(values=count)

else:
_step_obj.context.stash = dict(values=len(_step_obj.context.stash))
else:
raise TerraformComplianceNotImplemented('Invalid action_type in the scenario: {}'.format(action_type))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Failure: for aws_vpc.web_vpc on resource. 1 is not equal to 2.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_vpc" "web_vpc" {
cidr_block = "10.0.0.0/16" #var.web_vpc_cidr_block
tags = merge(
{ "Name" = "vpc-testbla-13" },
{} # var.common_tags
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"format_version":"0.1","terraform_version":"0.12.15","planned_values":{"root_module":{"resources":[{"address":"aws_vpc.web_vpc","mode":"managed","type":"aws_vpc","name":"web_vpc","provider_name":"aws","schema_version":1,"values":{"assign_generated_ipv6_cidr_block":false,"cidr_block":"10.0.0.0/16","enable_dns_support":true,"instance_tenancy":"default","tags":{"Name":"vpc-testbla-13"}}}]}},"resource_changes":[{"address":"aws_vpc.web_vpc","mode":"managed","type":"aws_vpc","name":"web_vpc","provider_name":"aws","change":{"actions":["create"],"before":null,"after":{"assign_generated_ipv6_cidr_block":false,"cidr_block":"10.0.0.0/16","enable_dns_support":true,"instance_tenancy":"default","tags":{"Name":"vpc-testbla-13"}},"after_unknown":{"arn":true,"default_network_acl_id":true,"default_route_table_id":true,"default_security_group_id":true,"dhcp_options_id":true,"enable_classiclink":true,"enable_classiclink_dns_support":true,"enable_dns_hostnames":true,"id":true,"ipv6_association_id":true,"ipv6_cidr_block":true,"main_route_table_id":true,"owner_id":true,"tags":{}}}}],"configuration":{"root_module":{"resources":[{"address":"aws_vpc.web_vpc","mode":"managed","type":"aws_vpc","name":"web_vpc","provider_config_key":"aws","expressions":{"cidr_block":{"constant_value":"10.0.0.0/16"},"tags":{}},"schema_version":1}]}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: test for https://github.com/eerkunt/terraform-compliance/issues/187

Scenario: There is exactly one VPC
Given I have any resource defined
When its type is aws_vpc
And I count them
Then I expect the result is equal to 2
33 changes: 32 additions & 1 deletion tests/terraform_compliance/steps/test_main_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,43 @@ def test_it_condition_have_proto_protocol_and_port_port_for_cidr_port_success(se
cidr = 'not important'
self.assertTrue(it_condition_have_proto_protocol_and_port_port_for_cidr(step, condition, proto, port, cidr))

def test_i_action_them_count(self):
def test_i_action_them_count_list_of_list(self):
step = MockedStep()
step.context.stash = [1,2,3]
i_action_them(step, 'count')
self.assertEqual(step.context.stash, {'values': 3})

def test_i_action_them_count_list_of_dict_with_resources(self):
step = MockedStep()
step.context.stash = [
{
'values': {
'first': True
}
},
{
'values': {
'second': True
}
},
{
'values': {
'third': True
}
}
]
i_action_them(step, 'count')
self.assertEqual(step.context.stash, {'values': 3})

def test_i_action_them_count_list_of_dict_with_properties(self):
step = MockedStep()
step.context.stash = [{},{},{}]
step.context.property_name = 'resource'
i_action_them(step, 'count')
self.assertEqual(step.context.stash, {'values': 3})



def test_i_action_them_sum(self):
step = MockedStep()
with self.assertRaises(TerraformComplianceNotImplemented):
Expand Down

0 comments on commit a513c8d

Please sign in to comment.