-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_nested_stack_manager.py
More file actions
296 lines (255 loc) · 13.3 KB
/
test_nested_stack_manager.py
File metadata and controls
296 lines (255 loc) · 13.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
import os
from unittest import TestCase
from unittest.mock import Mock, patch, ANY, call
from parameterized import parameterized
from samcli.lib.bootstrap.nested_stack.nested_stack_manager import (
NESTED_STACK_NAME,
NestedStackManager,
)
from samcli.lib.build.app_builder import ApplicationBuildResult
from samcli.lib.providers.provider import Stack
from samcli.lib.sync.exceptions import InvalidRuntimeDefinitionForFunction
from samcli.lib.utils.osutils import BUILD_DIR_PERMISSIONS
from samcli.lib.utils.resources import AWS_SQS_QUEUE, AWS_SERVERLESS_FUNCTION
class TestNestedStackManager(TestCase):
def setUp(self) -> None:
self.stack_name = "stack_name"
self.build_dir = "build_dir"
self.stack = Mock(
wraps=Stack("", "", "", template_dict={}, parameters={}), stack_path="", location="foo/bar", resources={}
)
def test_nothing_to_add(self):
template = {}
app_build_result = ApplicationBuildResult(Mock(), {})
nested_stack_manager = NestedStackManager(
self.stack, self.stack_name, self.build_dir, template, app_build_result
)
result = nested_stack_manager.generate_auto_dependency_layer_stack()
self.assertEqual(template, result)
def test_unsupported_resource(self):
resources = {"MySqsQueue": {"Type": AWS_SQS_QUEUE}}
self.stack.resources = resources
template = {"Resources": resources}
app_build_result = ApplicationBuildResult(Mock(), {})
nested_stack_manager = NestedStackManager(
self.stack, self.stack_name, self.build_dir, template, app_build_result
)
result = nested_stack_manager.generate_auto_dependency_layer_stack()
self.assertEqual(template, result)
def test_image_function(self):
resources = {
"MyFunction": {
"Type": AWS_SERVERLESS_FUNCTION,
"Properties": {"Runtime": "unsupported_runtime", "PackageType": "Image"},
}
}
self.stack.resources = resources
template = {"Resources": resources}
app_build_result = ApplicationBuildResult(Mock(), {"MyFunction": "path/to/build/dir"})
nested_stack_manager = NestedStackManager(
self.stack, self.stack_name, self.build_dir, template, app_build_result
)
result = nested_stack_manager.generate_auto_dependency_layer_stack()
self.assertEqual(template, result)
def test_unsupported_runtime(self):
resources = {
"MyFunction": {
"Type": AWS_SERVERLESS_FUNCTION,
"Properties": {"Runtime": "unsupported_runtime", "Handler": "FakeHandler"},
}
}
self.stack.resources = resources
template = {"Resources": resources}
app_build_result = ApplicationBuildResult(Mock(), {"MyFunction": "path/to/build/dir"})
nested_stack_manager = NestedStackManager(
self.stack, self.stack_name, self.build_dir, template, app_build_result
)
result = nested_stack_manager.generate_auto_dependency_layer_stack()
self.assertEqual(template, result)
def test_no_function_build_definition(self):
resources = {
"MyFunction": {
"Type": AWS_SERVERLESS_FUNCTION,
"Properties": {"Runtime": "python3.8", "Handler": "FakeHandler"},
}
}
self.stack.resources = resources
template = {"Resources": resources}
build_graph = Mock()
build_graph.get_function_build_definition_with_full_path.return_value = None
app_build_result = ApplicationBuildResult(build_graph, {"MyFunction": "path/to/build/dir"})
nested_stack_manager = NestedStackManager(
self.stack, self.stack_name, self.build_dir, template, app_build_result
)
result = nested_stack_manager.generate_auto_dependency_layer_stack()
self.assertEqual(template, result)
def test_function_build_definition_without_dependencies_dir(self):
resources = {
"MyFunction": {
"Type": AWS_SERVERLESS_FUNCTION,
"Properties": {"Runtime": "python3.8", "Handler": "FakeHandler"},
}
}
self.stack.resources = resources
template = {"Resources": resources}
build_graph = Mock()
build_graph.get_function_build_definition_with_full_path.return_value = Mock(dependencies_dir=None)
app_build_result = ApplicationBuildResult(build_graph, {"MyFunction": "path/to/build/dir"})
nested_stack_manager = NestedStackManager(
self.stack, self.stack_name, self.build_dir, template, app_build_result
)
result = nested_stack_manager.generate_auto_dependency_layer_stack()
self.assertEqual(template, result)
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.move_template")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.osutils")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.os.path.isdir")
def test_with_zip_function(self, patched_isdir, patched_osutils, patched_move_template):
resources = {
"MyFunction": {
"Type": AWS_SERVERLESS_FUNCTION,
"Properties": {"Runtime": "python3.8", "Handler": "FakeHandler"},
}
}
self.stack.resources = resources
template = {"Resources": resources}
# prepare build graph
dependencies_dir = Mock()
function = Mock()
function.name = "MyFunction"
functions = [function]
build_graph = Mock()
function_definition_mock = Mock(dependencies_dir=dependencies_dir, functions=functions)
build_graph.get_function_build_definition_with_logical_id.return_value = function_definition_mock
app_build_result = ApplicationBuildResult(build_graph, {"MyFunction": "path/to/build/dir"})
patched_isdir.return_value = True
nested_stack_manager = NestedStackManager(
self.stack, self.stack_name, self.build_dir, template, app_build_result
)
with patch.object(nested_stack_manager, "_add_layer_readme_info") as patched_add_readme:
result = nested_stack_manager.generate_auto_dependency_layer_stack()
patched_move_template.assert_called_with(
self.stack.location, os.path.join(self.build_dir, "adl_nested_template.yaml"), ANY
)
self.assertNotEqual(template, result)
resources = result.get("Resources")
self.assertIn(NESTED_STACK_NAME, resources.keys())
self.assertTrue(resources.get("MyFunction", {}).get("Properties", {}).get("Layers", []))
def test_adding_readme_file(self):
with patch("builtins.open") as patched_open:
dependencies_dir = "dependencies"
function_name = "function_name"
NestedStackManager._add_layer_readme_info(dependencies_dir, function_name)
patched_open.assert_has_calls(
[
call(os.path.join(dependencies_dir, "AWS_SAM_CLI_README"), "w+"),
call()
.__enter__()
.write(
f"This layer contains dependencies of function {function_name} and automatically added by AWS SAM CLI command 'sam sync'"
),
],
any_order=True,
)
def test_update_layer_folder_raise_exception_with_no_runtime(self):
with self.assertRaises(InvalidRuntimeDefinitionForFunction):
NestedStackManager.update_layer_folder(Mock(), Mock(), Mock(), Mock(), None)
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.Path")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.shutil")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.osutils")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.NestedStackManager._add_layer_readme_info")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.os.path.isdir")
def test_update_layer_folder(
self, patched_isdir, patched_add_layer_readme, patched_osutils, patched_shutil, patched_path
):
build_dir = "build_dir"
dependencies_dir = "dependencies_dir"
layer_logical_id = "layer_logical_id"
function_logical_id = "function_logical_id"
function_runtime = "python3.9"
layer_contents_folder = Mock()
layer_root_folder = Mock()
layer_root_folder.exists.return_value = True
layer_root_folder.joinpath.return_value = layer_contents_folder
patched_path.return_value.joinpath.return_value = layer_root_folder
patched_isdir.return_value = True
layer_folder = NestedStackManager.update_layer_folder(
build_dir, dependencies_dir, layer_logical_id, function_logical_id, function_runtime
)
patched_shutil.rmtree.assert_called_with(layer_root_folder)
layer_root_folder.mkdir.assert_called_with(BUILD_DIR_PERMISSIONS, parents=True)
layer_contents_folder.mkdir.assert_called_with(BUILD_DIR_PERMISSIONS, parents=True)
patched_osutils.copytree.assert_called_with(dependencies_dir, str(layer_contents_folder))
patched_add_layer_readme.assert_called_with(str(layer_root_folder), function_logical_id)
self.assertEqual(layer_folder, str(layer_root_folder))
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.Path")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.shutil")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.osutils")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.NestedStackManager._add_layer_readme_info")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.os.path.isdir")
def test_skipping_dependency_copy_when_function_has_no_dependencies(
self, patched_isdir, patched_add_layer_readme, patched_osutils, patched_shutil, patched_path
):
build_dir = "build_dir"
dependencies_dir = "dependencies_dir"
layer_logical_id = "layer_logical_id"
function_logical_id = "function_logical_id"
function_runtime = "python3.9"
layer_contents_folder = Mock()
layer_root_folder = Mock()
layer_root_folder.exists.return_value = True
layer_root_folder.joinpath.return_value = layer_contents_folder
patched_path.return_value.joinpath.return_value = layer_root_folder
patched_isdir.return_value = False
NestedStackManager.update_layer_folder(
build_dir, dependencies_dir, layer_logical_id, function_logical_id, function_runtime
)
layer_root_folder.mkdir.assert_called_with(BUILD_DIR_PERMISSIONS, parents=True)
layer_contents_folder.mkdir.assert_not_called()
patched_osutils.copytree.assert_not_called()
patched_add_layer_readme.assert_called_with(str(layer_root_folder), function_logical_id)
@parameterized.expand([("python3.8", True), ("ruby3.2", False)])
def test_is_runtime_supported(self, runtime, supported):
self.assertEqual(NestedStackManager.is_runtime_supported(runtime), supported)
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.move_template")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.osutils")
@patch("samcli.lib.bootstrap.nested_stack.nested_stack_manager.os.path.isdir")
def test_with_intrinsic_function_layers_skips_auto_layer(
self, patched_isdir, patched_osutils, patched_move_template
):
"""Test that functions with intrinsic function Layers are skipped with a warning"""
resources = {
"MyFunction": {
"Type": AWS_SERVERLESS_FUNCTION,
"Properties": {
"Runtime": "python3.8",
"Handler": "FakeHandler",
"Layers": {"Fn::If": ["HasLayer", [{"Ref": "MyLayer"}], []]},
},
}
}
self.stack.resources = resources
template = {"Resources": resources}
# prepare build graph
dependencies_dir = Mock()
function = Mock()
function.name = "MyFunction"
functions = [function]
build_graph = Mock()
function_definition_mock = Mock(dependencies_dir=dependencies_dir, functions=functions)
build_graph.get_function_build_definition_with_logical_id.return_value = function_definition_mock
app_build_result = ApplicationBuildResult(build_graph, {"MyFunction": "path/to/build/dir"})
patched_isdir.return_value = True
nested_stack_manager = NestedStackManager(
self.stack, self.stack_name, self.build_dir, template, app_build_result
)
with patch.object(nested_stack_manager, "_add_layer_readme_info"):
result = nested_stack_manager.generate_auto_dependency_layer_stack()
# Should not create nested stack since function was skipped
patched_move_template.assert_not_called()
# Template should remain unchanged
self.assertEqual(template, result)
# Layers should still be the intrinsic function (not modified)
self.assertEqual(
result.get("Resources", {}).get("MyFunction", {}).get("Properties", {}).get("Layers"),
{"Fn::If": ["HasLayer", [{"Ref": "MyLayer"}], []]},
)