def test_del_section_with_no_globals_section_in_template(self): template = {"a": "b"} expected = {"a": "b"} Globals.del_section(template) self.assertEquals(expected, template)
def test_parse_should_not_error_if_value_is_empty(self): template = {"Globals": {"type1": {}}} # empty value globals = Globals(template) parsed = globals._parse(template["Globals"]) self.assertTrue("prefix_type1" in parsed) self.assertEqual({}, parsed["prefix_type1"].global_properties)
def test_parse_should_parse_all_known_resource_types(self): globals = Globals(self.template) parsed_globals = globals._parse(self.template["Globals"]) self.assertTrue("prefix_type1" in parsed_globals) self.assertEquals(self.template["Globals"]["type1"], parsed_globals["prefix_type1"].global_properties) self.assertTrue("prefix_type2" in parsed_globals) self.assertEquals(self.template["Globals"]["type2"], parsed_globals["prefix_type2"].global_properties)
def test_del_section_with_no_globals_section_in_template(self): template = { "a": "b" } expected = { "a": "b" } Globals.del_section(template) self.assertEquals(expected, template)
def test_merge_end_to_end_unknown_type(self): type = "some unknown type" properties = {"a": "b", "key": [1, 2, 3]} # Output equals input expected = {"a": "b", "key": [1, 2, 3]} globals = Globals(self.template) result = globals.merge(type, properties) self.assertEquals(expected, result)
def test_merge_must_skip_with_no_types(self, parse_mock): parse_mock.return_value = {} local_properties = {"a": "b"} expected = {"a": "b"} globals = Globals(self.template) # Since type is not available in the globals, nothing should happen result = globals.merge("some random type", local_properties) self.assertEquals(expected, result)
def test_parse_should_not_error_if_value_is_empty(self): template = { "Globals": { "type1": {} # empty value } } globals = Globals(template) parsed = globals._parse(template["Globals"]) self.assertTrue("prefix_type1" in parsed) self.assertEquals({}, parsed["prefix_type1"].global_properties)
def test_merge_must_skip_with_no_types(self, parse_mock): parse_mock.return_value = { } local_properties = {"a": "b"} expected = {"a": "b"} globals = Globals(self.template) # Since type is not available in the globals, nothing should happen result = globals.merge("some random type", local_properties) self.assertEquals(expected, result)
def test_merge_must_skip_unsupported_types(self, parse_mock): type1_mock = Mock() parse_mock.return_value = {"type1": type1_mock} local_properties = {"a": "b"} expected = {"a": "b"} globals = Globals(self.template) # Since type is not available in the globals, nothing should happen result = globals.merge("some random type", local_properties) self.assertEquals(expected, result) type1_mock.merge.assert_not_called()
def test_parse_should_error_if_globals_contains_unknown_properties_of_known_type( self): template = {"Globals": {"type1": {"unknown_property": "value"}}} with self.assertRaises(InvalidGlobalsSectionException): Globals(template)
def test_merge_end_to_end_on_known_type1(self): type = "prefix_type1" properties = {"prop1": "overridden value", "a": "b", "key": [1, 2, 3]} expected = { "prop1": "overridden value", "prop2": "value2", "a": "b", "key": [1, 2, 3] } # inherited from global globals = Globals(self.template) result = globals.merge(type, properties) self.assertEqual(expected, result)
def test_merge_end_to_end_on_known_type2(self): type = "prefix_type2" properties = {"a": "b", "key": [1, 2, 3]} expected = { "otherprop1": "value1", # inherited from global "otherprop2": "value2", # inherited from global "a": "b", "key": [1, 2, 3] } globals = Globals(self.template) result = globals.merge(type, properties) self.assertEquals(expected, result)
def test_merge_must_skip_unsupported_types(self, parse_mock): type1_mock = Mock() parse_mock.return_value = { "type1": type1_mock } local_properties = {"a": "b"} expected = {"a": "b"} globals = Globals(self.template) # Since type is not available in the globals, nothing should happen result = globals.merge("some random type", local_properties) self.assertEquals(expected, result) type1_mock.merge.assert_not_called()
def test_merge_must_actually_do_merge(self, parse_mock): type1_mock = Mock() type2_mock = Mock() parse_mock.return_value = {"type1": type1_mock, "type2": type2_mock} local_properties = {"a": "b"} expected = "some merged value" type1_mock.merge.return_value = expected # Try to merge for type1 globals = Globals(self.template) result = globals.merge("type1", local_properties) self.assertEqual(expected, result) type1_mock.merge.assert_called_with(local_properties) type2_mock.merge.assert_not_called()
def on_before_transform_template(self, template_dict): """ Hook method that runs before a template gets transformed. In this method, we parse and process Globals section from the template (if present). :param dict template_dict: SAM template as a dictionary """ try: global_section = Globals(template_dict) except InvalidGlobalsSectionException as ex: raise InvalidDocumentException([ex]) # For each resource in template, try and merge with Globals if necessary template = SamTemplate(template_dict) for logicalId, resource in template.iterate(): resource.properties = global_section.merge(resource.type, resource.properties) template.set(logicalId, resource) # Remove the Globals section from template if necessary Globals.del_section(template_dict) # If there was a global openApiVersion flag, check and convert swagger # to the right version Globals.fix_openapi_definitions(template_dict)
def test_merge_end_to_end_unknown_type(self): type = "some unknown type" properties = { "a": "b", "key": [1,2,3] } # Output equals input expected = { "a": "b", "key": [1,2,3] } globals = Globals(self.template) result = globals.merge(type, properties) self.assertEquals(expected, result)
def test_merge_end_to_end_on_known_type2(self): type = "prefix_type2" properties = { "a": "b", "key": [1,2,3] } expected = { "otherprop1": "value1", # inherited from global "otherprop2": "value2", # inherited from global "a": "b", "key": [1,2,3] } globals = Globals(self.template) result = globals.merge(type, properties) self.assertEquals(expected, result)
def test_merge_must_actually_do_merge(self, parse_mock): type1_mock = Mock() type2_mock = Mock() parse_mock.return_value = { "type1": type1_mock, "type2": type2_mock, } local_properties = {"a": "b"} expected = "some merged value" type1_mock.merge.return_value = expected # Try to merge for type1 globals = Globals(self.template) result = globals.merge("type1", local_properties) self.assertEquals(expected, result) type1_mock.merge.assert_called_with(local_properties) type2_mock.merge.assert_not_called()
def on_before_transform_template(self, template_dict): """ Hook method that runs before a template gets transformed. In this method, we parse and process Globals section from the template (if present). :param dict template_dict: SAM template as a dictionary """ try: global_section = Globals(template_dict) except InvalidGlobalsSectionException as ex: raise InvalidDocumentException([ex]) # For each resource in template, try and merge with Globals if necessary template = SamTemplate(template_dict) for logicalId, resource in template.iterate(): resource.properties = global_section.merge(resource.type, resource.properties) template.set(logicalId, resource) # Remove the Globals section from template if necessary Globals.del_section(template_dict)
def test_parse_should_error_if_globals_contains_unknown_types(self): template = { "Globals": { "random_type": { "key": "value" }, "type1": { "key": "value" } } } with self.assertRaises(InvalidGlobalsSectionException): Globals(template)
def test_openapi_postprocess(self): for test in self.table_driven: global_obj = Globals(self.template) global_obj.fix_openapi_definitions(test["input"]) self.assertEqual(test["input"], test["expected"], test["name"])
def test_parse_should_error_if_globals_is_not_dict(self): template = {"Globals": "hello"} with self.assertRaises(InvalidGlobalsSectionException): Globals(template)
def test_del_section_with_globals_section_in_template(self): template = self.template expected = {} Globals.del_section(template) self.assertEquals(expected, template)
def test_parse_should_error_if_value_is_not_dictionary(self): template = {"Globals": {"type1": "string value"}} with self.assertRaises(InvalidGlobalsSectionException): Globals(template)
def test_init_without_globals_section_in_template(self): template = {"a": "b"} global_obj = Globals(template) self.assertEquals({}, global_obj.template_globals)