Path: blob/develop/tests/unit/customizations/cloudformation/test_yamlhelper.py
1569 views
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0e7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import tempfile1314from botocore.compat import json15from botocore.compat import OrderedDict1617from awscli.testutils import mock, unittest18from awscli.customizations.cloudformation.deployer import Deployer19from awscli.customizations.cloudformation.yamlhelper import yaml_parse, yaml_dump202122class TestYaml(unittest.TestCase):2324yaml_with_tags = """25Resource:26Key1: !Ref Something27Key2: !GetAtt Another.Arn28Key3: !FooBar [!Baz YetAnother, "hello"]29Key4: !SomeTag {"a": "1"}30Key5: !GetAtt OneMore.Outputs.Arn31Key6: !Condition OtherCondition32"""3334parsed_yaml_dict = {35"Resource": {36"Key1": {37"Ref": "Something"38},39"Key2": {40"Fn::GetAtt": ["Another", "Arn"]41},42"Key3": {43"Fn::FooBar": [44{"Fn::Baz": "YetAnother"},45"hello"46]47},48"Key4": {49"Fn::SomeTag": {50"a": "1"51}52},53"Key5": {54"Fn::GetAtt": ["OneMore", "Outputs.Arn"]55},56"Key6": {57"Condition": "OtherCondition"58}59}60}6162def test_yaml_with_tags(self):63output = yaml_parse(self.yaml_with_tags)64self.assertEqual(self.parsed_yaml_dict, output)6566# Make sure formatter and parser work well with each other67formatted_str = yaml_dump(output)68output_again = yaml_parse(formatted_str)69self.assertEqual(output, output_again)7071def test_yaml_getatt(self):72# This is an invalid syntax for !GetAtt. But make sure the code does73# not crash when we encounter this syntax. Let CloudFormation74# interpret this value at runtime75yaml_input = """76Resource:77Key: !GetAtt ["a", "b"]78"""7980output = {81"Resource": {82"Key": {83"Fn::GetAtt": ["a", "b"]84}8586}87}8889actual_output = yaml_parse(yaml_input)90self.assertEqual(actual_output, output)9192def test_parse_json_with_tabs(self):93template = '{\n\t"foo": "bar"\n}'94output = yaml_parse(template)95self.assertEqual(output, {'foo': 'bar'})9697def test_parse_json_preserve_elements_order(self):98input_template = """99{100"B_Resource": {101"Key2": {102"Name": "name2"103},104"Key1": {105"Name": "name1"106}107},108"A_Resource": {109"Key2": {110"Name": "name2"111},112"Key1": {113"Name": "name1"114}115}116}117"""118expected_dict = OrderedDict([119('B_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})])),120('A_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})]))121])122output_dict = yaml_parse(input_template)123self.assertEqual(expected_dict, output_dict)124125def test_parse_yaml_preserve_elements_order(self):126input_template = (127'B_Resource:\n'128' Key2:\n'129' Name: name2\n'130' Key1:\n'131' Name: name1\n'132'A_Resource:\n'133' Key2:\n'134' Name: name2\n'135' Key1:\n'136' Name: name1\n'137)138output_dict = yaml_parse(input_template)139expected_dict = OrderedDict([140('B_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})])),141('A_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})]))142])143self.assertEqual(expected_dict, output_dict)144145output_template = yaml_dump(output_dict)146self.assertEqual(input_template, output_template)147148def test_yaml_merge_tag(self):149test_yaml = """150base: &base151property: value152test:153<<: *base154"""155output = yaml_parse(test_yaml)156self.assertTrue(isinstance(output, OrderedDict))157self.assertEqual(output.get('test').get('property'), 'value')158159def test_unroll_yaml_anchors(self):160properties = {161"Foo": "bar",162"Spam": "eggs",163}164template = {165"Resources": {166"Resource1": {"Properties": properties},167"Resource2": {"Properties": properties}168}169}170171expected = (172'Resources:\n'173' Resource1:\n'174' Properties:\n'175' Foo: bar\n'176' Spam: eggs\n'177' Resource2:\n'178' Properties:\n'179' Foo: bar\n'180' Spam: eggs\n'181)182actual = yaml_dump(template)183self.assertEqual(actual, expected)184185186