-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathtest_globals.py
More file actions
593 lines (489 loc) · 22.3 KB
/
Copy pathtest_globals.py
File metadata and controls
593 lines (489 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
from unittest import TestCase
from unittest.mock import Mock, patch
from parameterized import parameterized
from samtranslator.model.exceptions import InvalidResourceAttributeTypeException
from samtranslator.plugins.globals.globals import GlobalProperties, Globals, InvalidGlobalsSectionException
from samtranslator.plugins.globals.merge_strategy import REPLACE, merge_by_key
class GlobalPropertiesTestCases:
dict_with_single_level_should_be_merged = {
"global": {"a": 1, "b": 2},
"local": {"a": "foo", "c": 3, "d": 4},
"expected_output": {"a": "foo", "b": 2, "c": 3, "d": 4},
}
dict_keys_are_case_sensitive = {
"global": {"banana": "is tasty"},
"local": {"BaNaNa": "is not tasty"},
"expected_output": {"banana": "is tasty", "BaNaNa": "is not tasty"},
}
dict_properties_with_different_types_must_be_overridden_str_and_dict = {
"global": {"key": "foo"},
"local": {"key": {"a": "b"}},
"expected_output": {"key": {"a": "b"}},
}
dict_properties_with_different_types_must_be_overridden_boolean_and_int = {
"global": {"key": True},
"local": {"key": 1},
"expected_output": {"key": 1},
}
dict_properties_with_different_types_must_be_overridden_dict_and_array = {
"global": {"key": {"a": "b"}},
"local": {"key": ["a"]},
"expected_output": {"key": ["a"]},
}
dict_with_empty_local_must_merge = {"global": {"a": "b"}, "local": {}, "expected_output": {"a": "b"}}
nested_dict_keys_should_be_merged = {
"global": {"key1": {"key2": {"key3": {"key4": "value"}}}},
"local": {"key1": {"key2": {"key3": {"key4": "local value"}}}},
"expected_output": {"key1": {"key2": {"key3": {"key4": "local value"}}}},
}
nested_dict_with_different_levels_should_be_merged = {
"global": {"key1": {"key2": {"key3": "value3"}, "globalOnlyKey": "global value"}},
"local": {"key1": {"key2": "foo", "localOnlyKey": "local value"}},
"expected_output": {
"key1": {
# Key2 does not recurse any further
"key2": "foo",
"globalOnlyKey": "global value",
"localOnlyKey": "local value",
}
},
}
nested_dicts_with_non_overridden_keys_should_be_copied = {
"global": {"key1": {"key2": {"key3": {"key4": "value"}}, "globalOnly": {"globalOnlyKey": "globalOnlyValue"}}},
"local": {
"key1": {
"key2": {"key3": {"localkey4": "other value 4"}, "localkey3": "other value 3"},
"localkey2": "other value 2",
}
},
"expected_output": {
"key1": {
"key2": {"key3": {"key4": "value", "localkey4": "other value 4"}, "localkey3": "other value 3"},
"localkey2": "other value 2",
"globalOnly": {"globalOnlyKey": "globalOnlyValue"},
}
},
}
arrays_with_mutually_exclusive_elements_must_be_concatenated = {
"global": [1, 2, 3],
"local": [11, 12, 13],
"expected_output": [1, 2, 3, 11, 12, 13],
}
arrays_with_duplicate_elements_must_be_concatenated = {
"global": ["a", "b", "c", "z"],
"local": ["a", "b", "x", "y", "z"],
"expected_output": ["a", "b", "c", "z", "a", "b", "x", "y", "z"],
}
arrays_with_nested_dict_must_be_concatenated = {
"global": [{"a": 1}, {"b": 2}],
"local": [{"x": 1}, {"y": 2}],
"expected_output": [{"a": 1}, {"b": 2}, {"x": 1}, {"y": 2}],
}
arrays_with_mixed_element_types_must_be_concatenated = {
"global": [1, 2, "foo", True, {"x": "y"}, ["nested", "array"]],
"local": [False, 9, 8, "bar"],
"expected_output": [1, 2, "foo", True, {"x": "y"}, ["nested", "array"], False, 9, 8, "bar"],
}
arrays_with_exactly_same_values_must_be_concatenated = {
"global": [{"a": 1}, {"b": 2}, "foo", 1, 2, True, False],
"local": [{"a": 1}, {"b": 2}, "foo", 1, 2, True, False],
"expected_output": [{"a": 1}, {"b": 2}, "foo", 1, 2, True, False, {"a": 1}, {"b": 2}, "foo", 1, 2, True, False],
}
# Arrays are concatenated. Other keys in dictionary are merged
nested_dict_with_array_values_must_be_merged_and_concatenated = {
"global": {"key": "global value", "nested": {"array_key": [1, 2, 3]}, "globalOnlyKey": "global value"},
"local": {"key": "local value", "nested": {"array_key": [8, 9, 10]}, "localOnlyKey": "local value"},
"expected_output": {
"key": "local value",
"nested": {"array_key": [1, 2, 3, 8, 9, 10]},
"globalOnlyKey": "global value",
"localOnlyKey": "local value",
},
}
intrinsic_function_must_be_overridden = {
"global": {"Ref": "foo"},
"local": {"Fn::Spooky": "bar"},
"expected_output": {"Fn::Spooky": "bar"},
}
intrinsic_function_in_global_must_override_dict_value_in_local = {
"global": {"Ref": "foo"},
"local": {"a": "b"},
"expected_output": {"a": "b"},
}
intrinsic_function_in_local_must_override_dict_value_in_global = {
"global": {"a": "b"},
"local": {"Fn::Something": "value"},
"expected_output": {"Fn::Something": "value"},
}
intrinsic_function_in_nested_dict_must_be_overridden = {
"global": {"key1": {"key2": {"key3": {"Ref": "foo"}, "globalOnlyKey": "global value"}}},
"local": {"key1": {"key2": {"key3": {"Fn::Something": "New value"}}}},
"expected_output": {
"key1": {"key2": {"key3": {"Fn::Something": "New value"}, "globalOnlyKey": "global value"}}
},
}
invalid_intrinsic_function_dict_must_be_merged = {
"global": {
# This is not an intrinsic function because the dict contains two keys
"Ref": "foo",
"key": "global value",
},
"local": {"Fn::Something": "bar", "other": "local value"},
"expected_output": {"Ref": "foo", "key": "global value", "Fn::Something": "bar", "other": "local value"},
}
intrinsic_function_in_local_must_override_invalid_intrinsic_in_global = {
"global": {
# This is not an intrinsic function because the dict contains two keys
"Ref": "foo",
"key": "global value",
},
"local": {
# This is an intrinsic function which essentially resolves to a primitive type.
# So local is primitive type whereas global is a dictionary. Prefer local
"Fn::Something": "bar"
},
"expected_output": {"Fn::Something": "bar"},
}
primitive_type_inputs_must_be_handled = {"global": "input string", "local": 123, "expected_output": 123}
mixed_type_inputs_must_be_handled = {"global": {"a": "b"}, "local": [1, 2, 3], "expected_output": [1, 2, 3]}
# Merge strategy: REPLACE — local list fully replaces global list (flat and nested paths).
# Add new test cases here when new rules are added to CUSTOM_STRATEGIES.
list_with_replace_strategy_must_use_local = {
"global": {"Architectures": ["x86_64"], "VpcConfig": {"SecurityGroupIds": ["sg-global"]}},
"local": {"Architectures": ["arm64"], "VpcConfig": {"SecurityGroupIds": ["sg-local"]}},
"expected_output": {"Architectures": ["arm64"], "VpcConfig": {"SecurityGroupIds": ["sg-local"]}},
"schema": {"Architectures": REPLACE, "VpcConfig.SecurityGroupIds": REPLACE},
}
# Merge strategy: MERGE_BY_KEY — deduplicates by key field, local overrides; non-dict items preserved.
list_with_merge_by_key_strategy = {
"global": {"Tags": [{"Key": "env", "Value": "dev"}, {"Key": "team", "Value": "lambda"}, "plain-string"]},
"local": {"Tags": [{"Key": "env", "Value": "prod"}, {"Key": "app", "Value": "my"}]},
"expected_output": {
"Tags": [
{"Key": "env", "Value": "prod"},
{"Key": "team", "Value": "lambda"},
"plain-string",
{"Key": "app", "Value": "my"},
]
},
"schema": {"Tags": merge_by_key("Key")},
}
# Multiple strategies applied to different properties in one merge.
multiple_strategies_applied_per_property = {
"global": {"Architectures": ["x86_64"], "Tags": [{"Key": "env", "Value": "dev"}], "Layers": ["arn:layer1"]},
"local": {"Architectures": ["arm64"], "Tags": [{"Key": "env", "Value": "prod"}], "Layers": ["arn:layer2"]},
"expected_output": {
"Architectures": ["arm64"],
"Tags": [{"Key": "env", "Value": "prod"}],
"Layers": ["arn:layer1", "arn:layer2"],
},
"schema": {"Architectures": REPLACE, "Tags": merge_by_key("Key")},
}
class TestGlobalPropertiesMerge(TestCase):
# Get all attributes of the test case object which is not a built-in method like __str__
@parameterized.expand([d for d in dir(GlobalPropertiesTestCases) if not d.startswith("__")])
def test_global_properties_merge(self, testcase):
configuration = getattr(GlobalPropertiesTestCases, testcase)
if not configuration:
raise Exception("Invalid configuration for test case " + testcase)
schema = configuration.get("schema", {})
global_properties = GlobalProperties(configuration["global"], schema=schema)
actual = global_properties.merge(configuration["local"])
self.assertEqual(actual, configuration["expected_output"])
class TestGlobalsPropertiesEdgeCases(TestCase):
@patch.object(GlobalProperties, "_token_of")
def test_merge_with_objects_of_unsupported_token_type(self, token_of_mock):
token_of_mock.return_value = "some random type"
properties = GlobalProperties("global value")
with self.assertRaises(TypeError):
# Raise type error because token type is invalid
properties.merge("local value")
class TestGlobalsObject(TestCase):
def setUp(self):
self._originals = {
"resource_prefix": Globals._RESOURCE_PREFIX,
"supported_properties": Globals.supported_properties,
"unreleased_properties": Globals.unreleased_properties,
}
Globals._RESOURCE_PREFIX = "prefix_"
Globals.supported_properties = {
"prefix_type1": ["prop1", "prop2"],
"prefix_type2": ["otherprop1", "otherprop2"],
}
Globals.unreleased_properties = {
"prefix_type1": ["prop2"],
}
self.template = {
"Globals": {
"type1": {"prop1": "value1", "prop2": "value2"},
"type2": {"otherprop1": "value1", "otherprop2": "value2"},
}
}
def tearDown(self):
Globals._RESOURCE_PREFIX = self._originals["resource_prefix"]
Globals.supported_properties = self._originals["supported_properties"]
Globals.unreleased_properties = self._originals["unreleased_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.assertEqual(self.template["Globals"]["type1"], parsed_globals["prefix_type1"].global_properties)
self.assertTrue("prefix_type2" in parsed_globals)
self.assertEqual(self.template["Globals"]["type2"], parsed_globals["prefix_type2"].global_properties)
def test_parse_should_error_if_globals_is_not_dict(self):
template = {"Globals": "hello"}
with self.assertRaises(InvalidGlobalsSectionException):
Globals(template)
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_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_parse_should_error_if_value_is_not_dictionary(self):
template = {"Globals": {"type1": "string value"}}
with self.assertRaises(InvalidGlobalsSectionException):
Globals(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_init_without_globals_section_in_template(self):
template = {"a": "b"}
global_obj = Globals(template)
self.assertEqual({}, global_obj.template_globals)
def test_del_section_with_globals_section_in_template(self):
template = self.template
expected = {}
Globals.del_section(template)
self.assertEqual(expected, template)
def test_del_section_with_no_globals_section_in_template(self):
template = {"a": "b"}
expected = {"a": "b"}
Globals.del_section(template)
self.assertEqual(expected, template)
@patch.object(Globals, "_parse")
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()
@patch.object(Globals, "_parse")
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.assertEqual(expected, result)
type1_mock.merge.assert_not_called()
@patch.object(Globals, "_parse")
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.assertEqual(expected, result)
def test_get_template_globals_star_ignore_globals(self):
type = "prefix_type1"
globals = Globals(self.template)
result = globals.get_template_globals("MyFunction", type, "*")
self.assertEqual(result.global_properties, GlobalProperties({}).global_properties)
def test_get_template_globals_list_ignore_globals(self):
type = "prefix_type1"
globals = Globals(self.template)
result = globals.get_template_globals("MyFunction", type, ["prop1"])
self.assertEqual(result.global_properties, GlobalProperties({"prop2": "value2"}).global_properties)
def test_get_template_globals_error(self):
type = "prefix_type1"
globals = Globals(self.template)
with self.assertRaises(InvalidResourceAttributeTypeException):
globals.get_template_globals("MyFunction", type, ["prop3"])
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.assertEqual(expected, result)
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.assertEqual(expected, result)
def test_should_not_include_unreleased_properties_in_error_message(self):
template = {"Globals": {"type1": {"unsupported_property": "value"}}}
with self.assertRaises(InvalidGlobalsSectionException) as exc:
Globals(template)
expected_message = (
"'Globals' section is invalid. 'unsupported_property' is not a supported property of 'type1'. "
+ "Must be one of the following values - ['prop1']"
)
self.assertEqual(exc.exception.message, expected_message)
class TestGlobalsOpenApi(TestCase):
template = {"Globals": {"Api": {"OpenApiVersion": "3.0"}}}
table_driven = [
{
"name": "happy case",
"input": {
"Resources": {
"MyApi": {
"Type": "AWS::Serverless::Api",
"Properties": {
"__MANAGE_SWAGGER": True,
"OpenApiVersion": "3.0",
"DefinitionBody": {"swagger": "2.0"},
},
}
}
},
"expected": {
"Resources": {
"MyApi": {
"Type": "AWS::Serverless::Api",
"Properties": {
"__MANAGE_SWAGGER": True,
"OpenApiVersion": "3.0",
"DefinitionBody": {"openapi": "3.0"},
},
}
}
},
},
{
"name": "no openapi",
"input": {
"Resources": {
"MyApi": {"Type": "AWS::Serverless::Api", "Properties": {"DefinitionBody": {"swagger": "2.0"}}}
}
},
"expected": {
"Resources": {
"MyApi": {"Type": "AWS::Serverless::Api", "Properties": {"DefinitionBody": {"swagger": "2.0"}}}
}
},
},
{
"name": "Openapi set to 2.0",
"input": {
"Resources": {
"MyApi": {
"Type": "AWS::Serverless::Api",
"Properties": {
"__MANAGE_SWAGGER": True,
"OpenApiVersion": "2.0",
"DefinitionBody": {"swagger": "2.0"},
},
}
}
},
"expected": {
"Resources": {
"MyApi": {
"Type": "AWS::Serverless::Api",
"Properties": {
"__MANAGE_SWAGGER": True,
"OpenApiVersion": "2.0",
"DefinitionBody": {"swagger": "2.0"},
},
}
}
},
},
{
"name": "No definition body",
"input": {
"Resources": {
"MyApi": {
"Type": "AWS::Serverless::Api",
"Properties": {"__MANAGE_SWAGGER": True, "OpenApiVersion": "3.0"},
}
}
},
"expected": {
"Resources": {
"MyApi": {
"Type": "AWS::Serverless::Api",
"Properties": {"__MANAGE_SWAGGER": True, "OpenApiVersion": "3.0"},
}
}
},
},
{
"name": "ignore customer defined swagger",
"input": {
"Resources": {
"MyApi": {
"Type": "AWS::Serverless::Api",
"Properties": {"OpenApiVersion": "3.0", "DefinitionBody": {"swagger": "2.0"}},
}
}
},
"expected": {
"Resources": {
"MyApi": {
"Type": "AWS::Serverless::Api",
"Properties": {"OpenApiVersion": "3.0", "DefinitionBody": {"swagger": "2.0"}},
}
}
},
},
{
"name": "No Resources",
"input": {"some": "other", "swagger": "donottouch"},
"expected": {"some": "other", "swagger": "donottouch"},
},
]
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"])
class TestMergeSchemaWiring(TestCase):
"""Tests that require the full Globals(template) pipeline (schema slicing by resource type).
Add new test cases to GlobalPropertiesTestCases for merge behavior.
Only add here for wiring-specific tests (IgnoreGlobals interaction, resource-type routing).
"""
def test_ignore_globals_skips_schema(self):
"""IgnoreGlobals for a registered property means schema is never consulted."""
template = {"Globals": {"Function": {"Architectures": ["arm64"], "Runtime": "python3.12"}}}
g = Globals(template)
result = g.merge(
"AWS::Serverless::Function",
{"Architectures": ["x86_64"]},
logical_id="MyFunc",
ignore_globals=["Architectures"],
)
self.assertEqual(result["Architectures"], ["x86_64"])
self.assertEqual(result["Runtime"], "python3.12")