Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/cloudformation/test_yamlhelper.py
1569 views
1
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0e
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import tempfile
14
15
from botocore.compat import json
16
from botocore.compat import OrderedDict
17
18
from awscli.testutils import mock, unittest
19
from awscli.customizations.cloudformation.deployer import Deployer
20
from awscli.customizations.cloudformation.yamlhelper import yaml_parse, yaml_dump
21
22
23
class TestYaml(unittest.TestCase):
24
25
yaml_with_tags = """
26
Resource:
27
Key1: !Ref Something
28
Key2: !GetAtt Another.Arn
29
Key3: !FooBar [!Baz YetAnother, "hello"]
30
Key4: !SomeTag {"a": "1"}
31
Key5: !GetAtt OneMore.Outputs.Arn
32
Key6: !Condition OtherCondition
33
"""
34
35
parsed_yaml_dict = {
36
"Resource": {
37
"Key1": {
38
"Ref": "Something"
39
},
40
"Key2": {
41
"Fn::GetAtt": ["Another", "Arn"]
42
},
43
"Key3": {
44
"Fn::FooBar": [
45
{"Fn::Baz": "YetAnother"},
46
"hello"
47
]
48
},
49
"Key4": {
50
"Fn::SomeTag": {
51
"a": "1"
52
}
53
},
54
"Key5": {
55
"Fn::GetAtt": ["OneMore", "Outputs.Arn"]
56
},
57
"Key6": {
58
"Condition": "OtherCondition"
59
}
60
}
61
}
62
63
def test_yaml_with_tags(self):
64
output = yaml_parse(self.yaml_with_tags)
65
self.assertEqual(self.parsed_yaml_dict, output)
66
67
# Make sure formatter and parser work well with each other
68
formatted_str = yaml_dump(output)
69
output_again = yaml_parse(formatted_str)
70
self.assertEqual(output, output_again)
71
72
def test_yaml_getatt(self):
73
# This is an invalid syntax for !GetAtt. But make sure the code does
74
# not crash when we encounter this syntax. Let CloudFormation
75
# interpret this value at runtime
76
yaml_input = """
77
Resource:
78
Key: !GetAtt ["a", "b"]
79
"""
80
81
output = {
82
"Resource": {
83
"Key": {
84
"Fn::GetAtt": ["a", "b"]
85
}
86
87
}
88
}
89
90
actual_output = yaml_parse(yaml_input)
91
self.assertEqual(actual_output, output)
92
93
def test_parse_json_with_tabs(self):
94
template = '{\n\t"foo": "bar"\n}'
95
output = yaml_parse(template)
96
self.assertEqual(output, {'foo': 'bar'})
97
98
def test_parse_json_preserve_elements_order(self):
99
input_template = """
100
{
101
"B_Resource": {
102
"Key2": {
103
"Name": "name2"
104
},
105
"Key1": {
106
"Name": "name1"
107
}
108
},
109
"A_Resource": {
110
"Key2": {
111
"Name": "name2"
112
},
113
"Key1": {
114
"Name": "name1"
115
}
116
}
117
}
118
"""
119
expected_dict = OrderedDict([
120
('B_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})])),
121
('A_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})]))
122
])
123
output_dict = yaml_parse(input_template)
124
self.assertEqual(expected_dict, output_dict)
125
126
def test_parse_yaml_preserve_elements_order(self):
127
input_template = (
128
'B_Resource:\n'
129
' Key2:\n'
130
' Name: name2\n'
131
' Key1:\n'
132
' Name: name1\n'
133
'A_Resource:\n'
134
' Key2:\n'
135
' Name: name2\n'
136
' Key1:\n'
137
' Name: name1\n'
138
)
139
output_dict = yaml_parse(input_template)
140
expected_dict = OrderedDict([
141
('B_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})])),
142
('A_Resource', OrderedDict([('Key2', {'Name': 'name2'}), ('Key1', {'Name': 'name1'})]))
143
])
144
self.assertEqual(expected_dict, output_dict)
145
146
output_template = yaml_dump(output_dict)
147
self.assertEqual(input_template, output_template)
148
149
def test_yaml_merge_tag(self):
150
test_yaml = """
151
base: &base
152
property: value
153
test:
154
<<: *base
155
"""
156
output = yaml_parse(test_yaml)
157
self.assertTrue(isinstance(output, OrderedDict))
158
self.assertEqual(output.get('test').get('property'), 'value')
159
160
def test_unroll_yaml_anchors(self):
161
properties = {
162
"Foo": "bar",
163
"Spam": "eggs",
164
}
165
template = {
166
"Resources": {
167
"Resource1": {"Properties": properties},
168
"Resource2": {"Properties": properties}
169
}
170
}
171
172
expected = (
173
'Resources:\n'
174
' Resource1:\n'
175
' Properties:\n'
176
' Foo: bar\n'
177
' Spam: eggs\n'
178
' Resource2:\n'
179
' Properties:\n'
180
' Foo: bar\n'
181
' Spam: eggs\n'
182
)
183
actual = yaml_dump(template)
184
self.assertEqual(actual, expected)
185
186