def test_find_keys_that_has_kv_structure(self, *args): step = MockedStep() step.context.stash = [ { 'address': 'some_resource.id', 'type': 'some_resource_type', 'name': 'some_name', 'values': [ { 'key': 'some_other_key', 'value': 'some_other_value' }, { 'key': 'some_key', 'value': 'some_value' } ] } ] step.context.type = 'resource' step.context.name = 'some_name' step.context.property_name = 'tags' step.context_sensitive_sentence = 'must' it_contains_something_old(step, 'some_key') self.assertEqual(step.context.stash[0]['values'], 'some_value')
def test_its_value_condition_match_the_search_regex_regex_success(self): step = MockedStep() step.context.stash = [{ 'address': 'some_resource.id', 'type': 'some_resource_type', 'name': 'some_name', 'values': [{ 'key': 'some_other_key', 'value': 'some_other_value' }, { 'key': 'some_key', 'value': 'some_value' }] }] step.context.type = 'resource' step.context.name = 'some_name' step.context.property_name = 'tags' step.context_sensitive_sentence = 'must' self.assertEqual( its_value_condition_match_the_search_regex_regex( step, 'must', 'some_.*'), None) with self.assertRaises(Failure): self.assertEqual( its_value_condition_match_the_search_regex_regex( step, 'must not', 'some_.*'), None) self.assertEqual( its_value_condition_match_the_search_regex_regex( step, 'must not', 'some_other.*'), None)
def test_its_value_condition_equals(self): step = MockedStep() expected_value = r"https://www.stackoverflow.com[as](.*)\s\t+$" step.context.stash = [{ 'address': 'some_resource.id', 'type': 'some_resource_type', 'name': 'some_name', 'values': { 'some_key': expected_value } }] step.context.type = 'resource' step.context.name = 'some_name' step.context.property_name = 'tags' step.context_sensitive_sentence = 'must' self.assertEqual( its_value_condition_equal(step, 'must', expected_value), None) self.assertEqual( its_value_condition_equal(step, 'must not', expected_value * 2), None) with self.assertRaises(Failure): self.assertEqual( its_value_condition_equal(step, 'must', expected_value + ' '), None) self.assertEqual( its_value_condition_equal(step, 'must not', expected_value), None)
def test_it_condition_contain_something_resource_is_not_dict_failure(self): step = MockedStep() step.context_sensitive_sentence = 'it must contain something' step.context.type = 'resource' step.context.stash = ['some_resource'] with self.assertRaises(Failure): self.assertIsNone(it_condition_contain_something(step, 'something'))
def test_it_must_contain_something_property_can_not_be_found(self, *args): step = MockedStep() step.context.stash = MockedTerraformPropertyList() step.sentence = 'Then it must contain' with self.assertRaises(AssertionError) as err: it_condition_contain_something(step, 'non_existent_property_value', MockedTerraformPropertyList) self.assertEqual(str(err.exception), 'non_existent_property_value property in test_name can not be found in ' 'test_resource_name (test_resource_type). It is set to test_value instead')
def test_its_property_contain_something_property_can_not_be_found(self): step = MockedStep() step.context.stash = MockedTerraformResourceList() step.sentence = 'When its .. contains Name' its_property_contains_key(step=step, property="???", key="Name", resourcelist=MockedTerraformResourceList) self.assertEqual(step.state, 'skipped')
def test_its_property_contains_key_property_key_is_found(self): step = MockedStep() step.context.stash = MockedTerraformResourceList() step.sentence = 'When its something contains key' its_property_contains_key(step=step, property='tags', key="key", resourcelist=MockedTerraformResourceList) self.assertEqual(step.context.stash.__class__, MockedTerraformPropertyList)
def test_it_condition_must_something_property_is_found(self): step = MockedStep() step.context.stash = MockedTerraformResourceList() step.sentence = 'Then it must ..' it_condition_contain_something( step_obj=step, something='something', resourcelist=MockedTerraformResourceList) self.assertEqual(step.context.stash[0].__class__, MockedTerraformPropertyList)
def test_step_condition(self): step = MockedStep() step.sentence = 'Given I do this' self.assertEqual(step_condition(step), 'given') step.sentence = 'When I do this' self.assertEqual(step_condition(step), 'when') step.sentence = 'Then I do this' self.assertEqual(step_condition(step), 'then')
def test_it_condition_must_something_property_can_not_be_found(self): step = MockedStep() step.context.stash = MockedTerraformResourceList() step.sentence = 'Then it must ..' with self.assertRaises(Exception) as err: it_condition_contain_something(step_obj=step, something=None, resourcelist=MockedTerraformResourceList) self.assertEqual(str(err.exception), 'should_have_properties hit') step.sentence = 'When it contains' it_condition_contain_something(step_obj=step, something=None, resourcelist=MockedTerraformResourceList) self.assertEqual(step.state, 'skipped')
def test_it_condition_should_something_property_stash_is_dict_found(self): step = MockedStep() step.context.stash = {} step.sentence = 'Then it must contain' with self.assertRaises(AssertionError) as err: it_condition_contain_something(step_obj=step, something='something', resourcelist=MockedTerraformResourceList) self.assertEqual(str(err.exception), 'something does not exist.') step.sentence = 'When it contains' step.context.stash = {} it_condition_contain_something(step_obj=step, something='something', resourcelist=MockedTerraformResourceList) self.assertEqual(step.state, 'skipped')
def test_it_condition_contain_something_resource_value_is_list_but_invalid( self): step = MockedStep() step.context_sensitive_sentence = 'it contains something' step.context.type = 'resource' step.context.stash = [{ 'address': 'some_address', 'type': 'resource', 'values': ['something'] }] with self.assertRaises(TerraformComplianceInternalFailure): it_condition_contain_something(step, 'something')
def test_it_condition_contain_something_resource_found(self, *args): step = MockedStep() step.context_sensitive_sentence = 'it contains something' step.context.type = 'resource' step.context.stash = [{ 'address': 'some_address', 'type': 'resource', 'values': { 'something': True } }] self.assertTrue(it_condition_contain_something(step, 'something'))
def test_it_condition_contain_something_resource_value_is_list(self): step = MockedStep() step.context_sensitive_sentence = 'it contains something' step.context.type = 'resource' step.context.stash = [{ 'address': 'some_address', 'type': 'resource', 'values': [{ 'key': 'something', 'value': 'some_value' }] }] self.assertTrue(it_condition_contain_something(step, 'something'))
def test_its_key_is_not_value(self, test_case): step = MockedStep() step.context.stash = test_case.stash with patch('terraform_compliance.extensions.ext_radish_bdd.world', return_value=MockedWorld()): its_key_is_not_value(step, *test_case.args) def collect_ids(val): if isinstance(val, dict): for k, v in val.items(): if k == 'id': yield v else: yield from collect_ids(v) elif isinstance(val, list): for child in val: yield from collect_ids(child) all_ids = set(collect_ids(test_case.stash)) expected_ids = all_ids - test_case.expected_ids assert step.state == ('passed' if expected_ids else 'skipped') if expected_ids: actual_ids = set(o['id'] for o in step.context.stash) assert actual_ids == expected_ids
def test_i_expect_the_result_is_more_and_equal_than_number_failure(self): step = MockedStep() step.context.stash = 1 with self.assertRaises(AssertionError) as err: i_expect_the_result_is_operator_than_number( step, 'more and equal', 2) self.assertEqual(str(err.exception), '1 is not more and equal than 2')
def test_i_have_name_section_configured_resource_found(self): step = MockedStep() world = MockedWorld() name = 'resource_type_without_tags' type_name = 'resource' self.assertTrue(i_have_name_section_configured(step, name, type_name, world))
def test_i_expect_the_result_is_operator_than_number_resource_list_as_dict( self): step = MockedStep() step.context.stash.resource_list = None self.assertIsNone( i_expect_the_result_is_operator_than_number( step, 'operator', 'not_important'))
def test_its_value_must_match_not_the_search_regex_regex_string_unicode_success( self): step = MockedStep() step.context.stash = 'some string' self.assertIsNone( its_value_condition_match_the_search_regex_regex( step, 'must not', 'non_match_regex'))
def test_its_value_condition_match_the_search_regex_regex_resource_list( self): step = MockedStep() step.context.stash.resource_list = None self.assertIsNone( its_value_condition_match_the_search_regex_regex( step, 'condition', 'some_regex'))
def test_its_key_is_value_dict_kv_failure_2(self, *args): step = MockedStep() step.context.stash = [ { 'type': 'aws_db_instance', 'some_key': 'some_value', 'values': { 'tags': { 'tag_key_one': 'tag_value_one', 'tag_key_two': 'tag_value_two' } } }, { 'type': 'aws_db_instance', 'some_key': 'some_other_value', 'values': { 'tags': { 'tag_key_one': 'tag_value_one', 'tag_key_three': 'tag_value_three' } } } ] its_key_is_value(step, 'tags', 'tag_key_three', 'not_tag_key_three') self.assertEqual(step.state, 'skipped')
def test_i_have_name_section_configured_type_not_found(self): step = MockedStep() with self.assertRaises(AssertionError) as err: i_have_name_section_configured(step, 'some_name', 'some_invalid_type', 'some_world') self.assertEqual(str(err.exception), 'some_invalid_type configuration type does not exist or not implemented ' 'yet. Use resource(s), provider(s), variable(s) or data(s) instead.')
def test_i_have_name_section_configured_provider_found(self): step = MockedStep() world = MockedWorld() name = 'some_provider' type_name = 'providers' self.assertTrue(i_have_name_section_configured(step, name, type_name, world))
def test_i_have_name_section_configured_variable_found(self): step = MockedStep() world = MockedWorld() name = 'some_variable' type_name = 'variables' self.assertTrue(i_have_name_section_configured(step, name, type_name, world))
def test_i_expect_the_result_is_more_than_number_failure(self): step = MockedStep() step.context.stash = 1 with self.assertRaises(AssertionError) as err: self.assertIsNone( i_expect_the_result_is_operator_than_number(step, 'more', 1)) self.assertEqual(str(err.exception), '1 is not more than 1')
def test_its_key_is_not_value_dict_key_multi_success(self, *args): step = MockedStep() step.context.stash = [{ 'type': 'aws_db_instance', 'some_key': 'some_value', 'values': { 'tags': { 'tag_key_one': 'tag_value_one', 'tag_key_two': 'tag_value_two' } } }, { 'type': 'aws_db_instance', 'some_key': 'some_other_value', 'values': { 'tags': { 'tag_key_one': 'tag_value_one', 'tag_key_three': 'tag_value_three' } } }] its_key_is_not_value(step, 'tags', 'tag_key_four') self.assertTrue(type(step.context.stash) is list) self.assertEqual(len(step.context.stash), 2) self.assertEqual(step.context.stash[0]['some_key'], 'some_value') self.assertEqual(step.context.stash[1]['some_key'], 'some_other_value')
def test_i_expect_the_result_is_operator_than_number_less(self): step = MockedStep() step.context.stash = {'values': 42} self.assertIsNone(i_expect_the_result_is_operator_than_number(step, 'less', 43)) with self.assertRaises(AssertionError) as err: i_expect_the_result_is_operator_than_number(step, 'less', 41) self.assertEqual(str(err.exception), '42 is not less than 41')
def test_i_expect_the_result_is_operator_than_number_equal(self, *args): step = MockedStep() step.context.stash = {'values': 42} self.assertIsNone(i_expect_the_result_is_operator_than_number(step, 'equal', 42)) with self.assertRaises(Failure) as err: i_expect_the_result_is_operator_than_number(step, 'equal', 41) self.assertTrue('42 is not equal to 41' in str(err.exception))
def test_it_condition_contain_something_resource_not_found(self, *args): step = MockedStep() step.context_sensitive_sentence = 'it must contain something' step.context.type = 'resource' step.context.stash = [{ 'address': 'some_address', 'type': 'resource', 'values': { 'something': True } }] with self.assertRaises(Failure) as err: self.assertIsNone( it_condition_contain_something(step, 'something else')) self.assertEqual( str(err.exception), '{} ({}) does not have {} property.'.format( 'some_address', 'resource', 'something else'))
def test_i_expect_the_result_is_operator_than_number_more(self): step = MockedStep() step.context.stash = {'values': 42} self.assertIsNone( i_expect_the_result_is_operator_than_number(step, 'more', 41)) with self.assertRaises(Failure) as err: i_expect_the_result_is_operator_than_number(step, 'more', 43) self.assertTrue('42 is not more than 43' in str(err.exception))