def test_len_single_resource(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId1", "property2", "value2") self.assertEqual( 1, len(resource_refs)) # Each unique logicalId adds one to the len
def test_get_on_non_existent_property(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") self.assertEquals(None, resource_refs.get("logicalId1", "SomeProperty")) self.assertEquals(None, resource_refs.get("SomeLogicalId", "property1"))
def test_add_must_error_on_duplicate_value(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId", "property", "value1") with self.assertRaises(ValueError): resource_refs.add("logicalId", "property", "value2")
def test_add_must_error_on_duplicate_value(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId", "property", "value1") with self.assertRaises(ValueError): resource_refs.add("logicalId", "property", "value2")
def test_add_multiple_logical_ids(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId2", "property2", "value2") resource_refs.add("logicalId3", "property3", "value3") self.assertEqual({"property1": "value1"}, resource_refs.get_all("logicalId1")) self.assertEqual({"property2": "value2"}, resource_refs.get_all("logicalId2")) self.assertEqual({"property3": "value3"}, resource_refs.get_all("logicalId3"))
def test_add_multiple_logical_ids(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId2", "property2", "value2") resource_refs.add("logicalId3", "property3", "value3") self.assertEquals({"property1": "value1"}, resource_refs.get_all("logicalId1")) self.assertEquals({"property2": "value2"}, resource_refs.get_all("logicalId2")) self.assertEquals({"property3": "value3"}, resource_refs.get_all("logicalId3"))
def test_get_must_return_correct_value(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId1", "property2", "value2") resource_refs.add("newLogicalId", "newProperty", "newValue") self.assertEquals("value1", resource_refs.get("logicalId1", "property1")) self.assertEquals("value2", resource_refs.get("logicalId1", "property2")) self.assertEquals("newValue", resource_refs.get("newLogicalId", "newProperty"))
def test_get_must_return_correct_value(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId1", "property2", "value2") resource_refs.add("newLogicalId", "newProperty", "newValue") self.assertEqual("value1", resource_refs.get("logicalId1", "property1")) self.assertEqual("value2", resource_refs.get("logicalId1", "property2")) self.assertEqual("newValue", resource_refs.get("newLogicalId", "newProperty"))
def test_add_multiple_properties_to_one_logicalId(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId", "property1", "value1") resource_refs.add("logicalId", "property2", "value2") resource_refs.add("logicalId", "property3", "value3") expected = { "property1": "value1", "property2": "value2", "property3": "value3" } self.assertEqual(expected, resource_refs.get_all("logicalId"))
def test_add_multiple_properties_to_one_logicalId(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId", "property1", "value1") resource_refs.add("logicalId", "property2", "value2") resource_refs.add("logicalId", "property3", "value3") expected = { "property1": "value1", "property2": "value2", "property3": "value3" } self.assertEquals(expected, resource_refs.get_all("logicalId"))
def test_add_must_error_when_property_is_absent(self): resource_refs = SupportedResourceReferences() with self.assertRaises(ValueError): resource_refs.add("logicalId", "", "value1")
def test_add_must_error_if_value_is_not_string(self): resource_refs = SupportedResourceReferences() with self.assertRaises(ValueError): resource_refs.add("logicalId", "property", {"a": "b"})
def test_len_multiple_resources(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId2", "property2", "value2") self.assertEqual(2, len(resource_refs))
class TestSubCanResolveResourceRefs(TestCase): def setUp(self): self.supported_resource_refs = SupportedResourceReferences() self.supported_resource_refs.add("id1", "prop1", "value1") self.supported_resource_refs.add("id1", "prop2", "value2") self.supported_resource_refs.add("id2", "prop3", "value3") self.input_sub_value = "Hello ${id1.prop1} ${id1.prop2}${id2.prop3} ${id1.prop1.arn} ${id1.prop2.arn.name.foo} ${!id1.prop1} ${unknown} ${some.arn} World" self.expected_output_sub_value = "Hello ${value1} ${value2}${value3} ${value1.arn} ${value2.arn.name.foo} ${!id1.prop1} ${unknown} ${some.arn} World" def test_must_resolve_string_value(self): input = {"Fn::Sub": self.input_sub_value} expected = {"Fn::Sub": self.expected_output_sub_value} sub = SubAction() result = sub.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, result) def test_must_resolve_array_value(self): input = {"Fn::Sub": [self.input_sub_value, {"unknown": "a"}]} expected = { "Fn::Sub": [self.expected_output_sub_value, { "unknown": "a" }] } sub = SubAction() result = sub.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, result) def test_sub_all_refs_with_list_input(self): parameters = {"key1": "value1", "key2": "value2"} input = {"Fn::Sub": ["key1", "key2"]} expected = {"Fn::Sub": ["key1", "key2"]} sub = SubAction() result = sub.resolve_resource_refs(input, parameters) self.assertEqual(expected, result) def test_sub_all_refs_with_dict_input(self): parameters = {"key1": "value1", "key2": "value2"} input = {"Fn::Sub": {"a": "key1", "b": "key2"}} expected = {"Fn::Sub": {"a": "key1", "b": "key2"}} sub = SubAction() result = sub.resolve_resource_refs(input, parameters) self.assertEqual(expected, result) @patch.object(SubAction, "can_handle") def test_return_value_if_cannot_handle(self, can_handle_mock): parameters = {"key": "value"} input = {"Fn::Sub": "${key}"} expected = {"Fn::Sub": "${key}"} sub = SubAction() can_handle_mock.return_value = False # Simulate failure to handle the input. Result should be same as input self.assertEqual(expected, sub.resolve_resource_refs(input, parameters))
def test_get_all_on_non_existent_logical_id(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId", "property", "value1") self.assertEquals(None, resource_refs.get_all("some logical id"))
def test_add_must_error_when_property_is_absent(self): resource_refs = SupportedResourceReferences() with self.assertRaises(ValueError): resource_refs.add("logicalId", "", "value1")
def test_add_must_error_if_value_is_not_string(self): resource_refs = SupportedResourceReferences() with self.assertRaises(ValueError): resource_refs.add("logicalId", "property", {"a": "b"})
def test_get_all_on_non_existent_logical_id(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId", "property", "value1") self.assertEqual(None, resource_refs.get_all("some logical id"))
def test_get_on_non_existent_property(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") self.assertEquals(None, resource_refs.get("logicalId1", "SomeProperty")) self.assertEquals(None, resource_refs.get("SomeLogicalId", "property1"))
class TestSubCanResolveResourceRefs(TestCase): def setUp(self): self.supported_resource_refs = SupportedResourceReferences() self.supported_resource_refs.add("id1", "prop1", "value1") self.supported_resource_refs.add("id1", "prop2", "value2") self.supported_resource_refs.add("id2", "prop3", "value3") self.input_sub_value = "Hello ${id1.prop1} ${id1.prop2}${id2.prop3} ${id1.prop1.arn} ${id1.prop2.arn.name.foo} ${!id1.prop1} ${unknown} ${some.arn} World" self.expected_output_sub_value = "Hello ${value1} ${value2}${value3} ${value1.arn} ${value2.arn.name.foo} ${!id1.prop1} ${unknown} ${some.arn} World" def test_must_resolve_string_value(self): input = { "Fn::Sub": self.input_sub_value } expected = { "Fn::Sub": self.expected_output_sub_value } sub = SubAction() result = sub.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, result) def test_must_resolve_array_value(self): input = { "Fn::Sub": [self.input_sub_value, {"unknown":"a"}] } expected = { "Fn::Sub": [self.expected_output_sub_value, {"unknown": "a"}] } sub = SubAction() result = sub.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, result) def test_sub_all_refs_with_list_input(self): parameters = { "key1": "value1", "key2": "value2" } input = { "Fn::Sub": ["key1", "key2"] } expected = { "Fn::Sub": ["key1", "key2"] } sub = SubAction() result = sub.resolve_resource_refs(input, parameters) self.assertEqual(expected, result) def test_sub_all_refs_with_dict_input(self): parameters = { "key1": "value1", "key2": "value2" } input = { "Fn::Sub": {"a": "key1", "b": "key2"} } expected = { "Fn::Sub": {"a": "key1", "b": "key2"} } sub = SubAction() result = sub.resolve_resource_refs(input, parameters) self.assertEqual(expected, result) @patch.object(SubAction, "can_handle") def test_return_value_if_cannot_handle(self, can_handle_mock): parameters = { "key": "value" } input = { "Fn::Sub": "${key}" } expected = { "Fn::Sub": "${key}" } sub = SubAction() can_handle_mock.return_value = False # Simulate failure to handle the input. Result should be same as input self.assertEqual(expected, sub.resolve_resource_refs(input, parameters))
class TestGetAttCanResolveResourceRefs(TestCase): def setUp(self): self.supported_resource_refs = SupportedResourceReferences() self.supported_resource_refs.add("id1", "prop1", "value1") def test_must_resolve_simple_refs(self): input = {"Fn::GetAtt": ["id1.prop1", "Arn"]} expected = {"Fn::GetAtt": ["value1", "Arn"]} getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_resolve_refs_with_many_attributes(self): input = {"Fn::GetAtt": ["id1.prop1", "Arn1", "Arn2", "Arn3"]} expected = {"Fn::GetAtt": ["value1", "Arn1", "Arn2", "Arn3"]} getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_resolve_with_splitted_resource_refs(self): input = { # Reference to resource `id1.prop1` is split into different elements "Fn::GetAtt": ["id1", "prop1", "Arn1", "Arn2", "Arn3"] } expected = {"Fn::GetAtt": ["value1", "Arn1", "Arn2", "Arn3"]} getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_refs_without_attributes(self): input = { # No actual attributes. But since we have two entries in the array, we try to resolve them "Fn::GetAtt": ["id1", "prop1"] } expected = {"Fn::GetAtt": ["value1"]} getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_refs_without_attributes_in_concatenated_form(self): input = { # No actual attributes. with just only one entry in array, the value never gets resolved "Fn::GetAtt": ["id1.prop1"] } expected = {"Fn::GetAtt": ["id1.prop1"]} getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_invalid_value_array(self): input = { # No actual attributes "Fn::GetAtt": ["id1"] } expected = {"Fn::GetAtt": ["id1"]} getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_invalid_value_type(self): input = { # No actual attributes "Fn::GetAtt": { "a": "b" } } expected = {"Fn::GetAtt": {"a": "b"}} getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_missing_properties_with_dot_after(self): input = {"Fn::GetAtt": ["id1.", "foo"]} expected = {"Fn::GetAtt": ["id1.", "foo"]} getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_missing_properties_with_dot_before(self): input = {"Fn::GetAtt": [".id1", "foo"]} expected = {"Fn::GetAtt": [".id1", "foo"]} getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) @patch.object(GetAttAction, "can_handle") def test_return_value_if_cannot_handle(self, can_handle_mock): input = {"Fn::GetAtt": ["id1", "prop1"]} expected = {"Fn::GetAtt": ["id1", "prop1"]} getatt = GetAttAction() can_handle_mock.return_value = False # Simulate failure to handle the input. Result should be same as input self.assertEqual( expected, getatt.resolve_resource_refs(input, self.supported_resource_refs))
def test_len_single_resource(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId1", "property2", "value2") self.assertEquals(1, len(resource_refs)) # Each unique logicalId adds one to the len
class TestGetAttCanResolveResourceRefs(TestCase): def setUp(self): self.supported_resource_refs = SupportedResourceReferences() self.supported_resource_refs.add("id1", "prop1", "value1") def test_must_resolve_simple_refs(self): input = { "Fn::GetAtt": ["id1.prop1", "Arn"] } expected = { "Fn::GetAtt": ["value1", "Arn"] } getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_resolve_refs_with_many_attributes(self): input = { "Fn::GetAtt": ["id1.prop1", "Arn1", "Arn2", "Arn3"] } expected = { "Fn::GetAtt": ["value1", "Arn1", "Arn2", "Arn3"] } getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_resolve_with_splitted_resource_refs(self): input = { # Reference to resource `id1.prop1` is split into different elements "Fn::GetAtt": ["id1", "prop1", "Arn1", "Arn2", "Arn3"] } expected = { "Fn::GetAtt": ["value1", "Arn1", "Arn2", "Arn3"] } getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_refs_without_attributes(self): input = { # No actual attributes. But since we have two entries in the array, we try to resolve them "Fn::GetAtt": ["id1", "prop1"] } expected = { "Fn::GetAtt": ["value1"] } getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_refs_without_attributes_in_concatenated_form(self): input = { # No actual attributes. with just only one entry in array, the value never gets resolved "Fn::GetAtt": ["id1.prop1"] } expected = { "Fn::GetAtt": ["id1.prop1"] } getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_invalid_value_array(self): input = { # No actual attributes "Fn::GetAtt": ["id1"] } expected = { "Fn::GetAtt": ["id1"] } getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_invalid_value_type(self): input = { # No actual attributes "Fn::GetAtt": {"a": "b"} } expected = { "Fn::GetAtt": {"a": "b"} } getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_missing_properties_with_dot_after(self): input = { "Fn::GetAtt": ["id1.", "foo"] } expected = { "Fn::GetAtt": ["id1.", "foo"] } getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) def test_must_ignore_missing_properties_with_dot_before(self): input = { "Fn::GetAtt": [".id1", "foo"] } expected = { "Fn::GetAtt": [".id1", "foo"] } getatt = GetAttAction() output = getatt.resolve_resource_refs(input, self.supported_resource_refs) self.assertEqual(expected, output) @patch.object(GetAttAction, "can_handle") def test_return_value_if_cannot_handle(self, can_handle_mock): input = { "Fn::GetAtt": ["id1", "prop1"] } expected = { "Fn::GetAtt": ["id1", "prop1"] } getatt = GetAttAction() can_handle_mock.return_value = False # Simulate failure to handle the input. Result should be same as input self.assertEqual(expected, getatt.resolve_resource_refs(input, self.supported_resource_refs))
def test_len_multiple_resources(self): resource_refs = SupportedResourceReferences() resource_refs.add("logicalId1", "property1", "value1") resource_refs.add("logicalId2", "property2", "value2") self.assertEquals(2, len(resource_refs))