forked from aws/aws-sam-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_build_cmd_python.py
More file actions
623 lines (543 loc) · 20.6 KB
/
Copy pathtest_build_cmd_python.py
File metadata and controls
623 lines (543 loc) · 20.6 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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import logging
import pathlib
from typing import Set
from unittest import skipIf
from uuid import uuid4
import pytest
from parameterized import parameterized, parameterized_class
from tests.integration.buildcmd.build_integ_base import (
BuildIntegBase,
BuildIntegPythonBase,
show_container_in_test_name,
)
from tests.testing_utils import (
CI_OVERRIDE,
IS_WINDOWS,
RUN_BY_CANARY,
RUNNING_ON_CI,
RUNNING_TEST_FOR_MASTER_ON_CI,
SKIP_DOCKER_BUILD,
SKIP_DOCKER_MESSAGE,
SKIP_DOCKER_TESTS,
run_command,
)
LOG = logging.getLogger(__name__)
# SAR tests require credentials. This is to skip running the test where credentials are not available.
SKIP_SAR_TESTS = RUNNING_ON_CI and RUNNING_TEST_FOR_MASTER_ON_CI and not RUN_BY_CANARY
@skipIf(
# Hits public ECR pull limitation, move it to canary tests
(not RUN_BY_CANARY and not CI_OVERRIDE),
"Skip build tests on windows when running in CI unless overridden",
)
@pytest.mark.python
class TestBuildCommand_PythonFunctions_Images(BuildIntegBase):
template = "template_image.yaml"
EXPECTED_FILES_PROJECT_MANIFEST: Set[str] = set()
FUNCTION_LOGICAL_ID_IMAGE = "ImageFunction"
def _test_default_requirements_wrapper(self, runtime, dockerfile):
tag = uuid4().hex
overrides = {
"Runtime": runtime,
"Handler": "main.handler",
"DockerFile": dockerfile,
"Tag": tag,
}
cmdlist = self.get_command_list(use_container=False, parameter_overrides=overrides)
command_result = run_command(cmdlist, cwd=self.working_dir)
self.assertEqual(command_result.process.returncode, 0)
self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID_IMAGE,
"ImageUri",
f"{self.FUNCTION_LOGICAL_ID_IMAGE.lower()}:{tag}",
)
expected = {"pi": "3.14"}
self._verify_invoke_built_function(
self.built_template, self.FUNCTION_LOGICAL_ID_IMAGE, self._make_parameter_override_arg(overrides), expected
)
@parameterized.expand(
[
*[(runtime, "Dockerfile") for runtime in ["3.8", "3.9", "3.10", "3.11"]],
*[(runtime, "Dockerfile.production") for runtime in ["3.8", "3.9", "3.10", "3.11"]],
]
)
def test_with_default_requirements(self, runtime, dockerfile):
self._test_default_requirements_wrapper(runtime, dockerfile)
@parameterized.expand(
[
*[(runtime, "Dockerfile") for runtime in ["3.12", "3.13", "3.14"]],
*[(runtime, "Dockerfile.production") for runtime in ["3.12", "3.13", "3.14"]],
]
)
@pytest.mark.al2023
def test_with_default_requirements_al2023(self, runtime, dockerfile):
self._test_default_requirements_wrapper(runtime, dockerfile)
def test_intermediate_container_deleted(self):
_tag = uuid4().hex
overrides = {
"Runtime": "3.9",
"Handler": "main.handler",
"DockerFile": "Dockerfile",
"Tag": _tag,
}
cmdlist = self.get_command_list(use_container=False, parameter_overrides=overrides)
_num_of_containers_before_build = self.get_number_of_created_containers()
command_result = run_command(cmdlist, cwd=self.working_dir)
self.assertEqual(command_result.process.returncode, 0)
_num_of_containers_after_build = self.get_number_of_created_containers()
self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID_IMAGE,
"ImageUri",
f"{self.FUNCTION_LOGICAL_ID_IMAGE.lower()}:{_tag}",
)
expected = {"pi": "3.14"}
self._verify_invoke_built_function(
self.built_template, self.FUNCTION_LOGICAL_ID_IMAGE, self._make_parameter_override_arg(overrides), expected
)
self.assertEqual(
_num_of_containers_before_build, _num_of_containers_after_build, "Intermediate containers are not removed"
)
@skipIf(
# Hits public ECR pull limitation, move it to canary tests
(not RUN_BY_CANARY and not CI_OVERRIDE),
"Skip build tests on windows when running in CI unless overridden",
)
@pytest.mark.python
class TestBuildCommand_PythonFunctions_ImagesWithSharedCode(BuildIntegBase):
template = "template_images_with_shared_code.yaml"
EXPECTED_FILES_PROJECT_MANIFEST: Set[str] = set()
FUNCTION_LOGICAL_ID_IMAGE = "ImageFunction"
def _test_default_requirements_wrapper(self, runtime, dockerfile, expected):
tag = uuid4().hex
overrides = {
"Runtime": runtime,
"Handler": "main.handler",
"DockerFile": dockerfile,
"Tag": tag,
}
cmdlist = self.get_command_list(use_container=False, parameter_overrides=overrides)
command_result = run_command(cmdlist, cwd=self.working_dir)
self.assertEqual(command_result.process.returncode, 0)
self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID_IMAGE,
"ImageUri",
f"{self.FUNCTION_LOGICAL_ID_IMAGE.lower()}:{tag}",
)
self._verify_invoke_built_function(
self.built_template, self.FUNCTION_LOGICAL_ID_IMAGE, self._make_parameter_override_arg(overrides), expected
)
@parameterized.expand(
[
*[(runtime, "feature_phi/Dockerfile", {"phi": "1.62"}) for runtime in ["3.8", "3.9", "3.10", "3.11"]],
*[(runtime, "feature_pi/Dockerfile", {"pi": "3.14"}) for runtime in ["3.8", "3.9", "3.10", "3.11"]],
]
)
def test_with_default_requirements(self, runtime, dockerfile, expected):
self._test_default_requirements_wrapper(runtime, dockerfile, expected)
@parameterized.expand(
[
*[(runtime, "feature_phi/Dockerfile", {"phi": "1.62"}) for runtime in ["3.12", "3.13"]],
*[(runtime, "feature_pi/Dockerfile", {"pi": "3.14"}) for runtime in ["3.12", "3.13"]],
]
)
@pytest.mark.al2023
def test_with_default_requirements_al2023(self, runtime, dockerfile, expected):
self._test_default_requirements_wrapper(runtime, dockerfile, expected)
@parameterized.expand(
[
("feature_phi/Dockerfile", {"phi": "1.62"}),
("feature_pi/Dockerfile", {"pi": "3.14"}),
]
)
def test_intermediate_container_deleted(self, dockerfile, expected):
_tag = uuid4().hex
overrides = {
"Runtime": "3.9",
"Handler": "main.handler",
"DockerFile": dockerfile,
"Tag": _tag,
}
cmdlist = self.get_command_list(use_container=False, parameter_overrides=overrides)
_num_of_containers_before_build = self.get_number_of_created_containers()
command_result = run_command(cmdlist, cwd=self.working_dir)
self.assertEqual(command_result.process.returncode, 0)
_num_of_containers_after_build = self.get_number_of_created_containers()
self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID_IMAGE,
"ImageUri",
f"{self.FUNCTION_LOGICAL_ID_IMAGE.lower()}:{_tag}",
)
self._verify_invoke_built_function(
self.built_template, self.FUNCTION_LOGICAL_ID_IMAGE, self._make_parameter_override_arg(overrides), expected
)
self.assertEqual(
_num_of_containers_before_build, _num_of_containers_after_build, "Intermediate containers are not removed"
)
@parameterized.expand(
[
("feature_phi\\Dockerfile", {"phi": "1.62"}),
("feature_pi\\Dockerfile", {"pi": "3.14"}),
]
)
@skipIf(not IS_WINDOWS, "Skipping passing Windows path for dockerfile path on non Windows platform")
def test_windows_dockerfile_present_sub_dir(self, dockerfile, expected):
_tag = uuid4().hex
overrides = {
"Runtime": "3.9",
"Handler": "main.handler",
"DockerFile": dockerfile,
"Tag": _tag,
}
cmdlist = self.get_command_list(use_container=False, parameter_overrides=overrides)
command_result = run_command(cmdlist, cwd=self.working_dir)
self.assertEqual(command_result.process.returncode, 0)
self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID_IMAGE,
"ImageUri",
f"{self.FUNCTION_LOGICAL_ID_IMAGE.lower()}:{_tag}",
)
self._verify_invoke_built_function(
self.built_template, self.FUNCTION_LOGICAL_ID_IMAGE, self._make_parameter_override_arg(overrides), expected
)
# @skipIf(
# # Hits public ECR pull limitation, move it to canary tests
# ((not RUN_BY_CANARY) or (IS_WINDOWS and RUNNING_ON_CI) and not CI_OVERRIDE),
# "Skip build tests on windows when running in CI unless overridden",
# )
@parameterized_class(
("template", "prop"),
[
("template_local_prebuilt_image.yaml", "ImageUri"),
("template_cfn_local_prebuilt_image.yaml", "Code.ImageUri"),
],
)
@parameterized_class(
(
"runtime",
"codeuri",
),
[
("python3.9", "Python"),
("python3.10", "Python"),
("python3.11", "Python"),
("python3.9", "PythonPEP600"),
("python3.10", "PythonPEP600"),
("python3.11", "PythonPEP600"),
],
)
class TestBuildCommand_PythonFunctions_WithoutDocker(BuildIntegPythonBase):
template = "template.yaml"
FUNCTION_LOGICAL_ID = "Function"
overrides = True
runtime = "python3.9"
codeuri = "Python"
check_function_only = False
use_container = False
prop = "CodeUri"
def test_with_default_requirements(self):
self._test_with_default_requirements(
self.runtime,
self.codeuri,
self.use_container,
self.test_data_path,
do_override=self.overrides,
check_function_only=self.check_function_only,
)
@parameterized_class(
("template", "prop"),
[
("template_local_prebuilt_image.yaml", "ImageUri"),
("template_cfn_local_prebuilt_image.yaml", "Code.ImageUri"),
],
)
@parameterized_class(
(
"runtime",
"codeuri",
),
[
("python3.12", "Python"),
("python3.12", "PythonPEP600"),
("python3.13", "Python"),
("python3.13", "PythonPEP600"),
("python3.14", "Python"),
("python3.14", "PythonPEP600"),
],
)
@pytest.mark.al2023
class TestBuildCommand_PythonFunctions_WithoutDocker_al2023(BuildIntegPythonBase):
template = "template.yaml"
FUNCTION_LOGICAL_ID = "Function"
overrides = True
runtime = "python3.9"
codeuri = "Python"
check_function_only = False
use_container = False
prop = "CodeUri"
def test_with_default_requirements(self):
self._test_with_default_requirements(
self.runtime,
self.codeuri,
self.use_container,
self.test_data_path,
do_override=self.overrides,
check_function_only=self.check_function_only,
)
@skipIf(SKIP_DOCKER_TESTS or SKIP_DOCKER_BUILD, SKIP_DOCKER_MESSAGE)
class TestBuildCommand_PythonFunctions_WithDocker(BuildIntegPythonBase):
template = "template.yaml"
FUNCTION_LOGICAL_ID = "Function"
overrides = True
codeuri = "Python"
use_container = "use_container"
check_function_only = False
prop = "CodeUri"
@parameterized.expand(
[
("python3.9",),
("python3.10",),
("python3.11",),
]
)
def test_with_default_requirements_in_container(self, runtime):
self._test_with_default_requirements(
runtime,
self.codeuri,
self.use_container,
self.test_data_path,
do_override=self.overrides,
check_function_only=self.check_function_only,
)
@parameterized.expand(
[
("python3.12",),
("python3.13",),
# skip this test until the python 3.14 build image is released
# ("python3.14",),
]
)
@pytest.mark.al2023
def test_with_default_requirements_al2023_in_container(self, runtime):
self._test_with_default_requirements(
runtime,
self.codeuri,
self.use_container,
self.test_data_path,
do_override=self.overrides,
check_function_only=self.check_function_only,
)
@skipIf(
# Hits public ECR pull limitation, move it to canary tests
SKIP_DOCKER_TESTS,
"Skip build tests that requires Docker in CI environment",
)
@parameterized_class(
(
"template",
"FUNCTION_LOGICAL_ID",
"overrides",
"runtime",
"codeuri",
"use_container",
"check_function_only",
"prop",
),
[
(
"cdk_v1_synthesized_template_zip_image_functions.json",
"RandomCitiesFunction5C47A2B8",
False,
None,
None,
False,
True,
"Code",
),
],
)
class TestBuildCommand_PythonFunctions_CDK(TestBuildCommand_PythonFunctions_WithoutDocker):
use_container = False
def test_cdk_app_with_default_requirements(self):
self._test_with_default_requirements(
self.runtime,
self.codeuri,
self.use_container,
self.test_data_path,
do_override=self.overrides,
check_function_only=self.check_function_only,
)
@skipIf(
# Hits public ECR pull limitation, move it to canary tests
SKIP_DOCKER_TESTS,
"Skip build tests that requires Docker in CI environment",
)
@parameterized_class(
(
"template",
"FUNCTION_LOGICAL_ID",
"overrides",
"use_container",
"prop",
),
[
(
"cdk_v2_synthesized_template_image_function_shared_code.json",
"TestLambdaFunctionC089708A",
False,
False,
"Code.ImageUri",
),
],
)
class TestBuildCommandCDKPythonImageFunctionSharedCode(BuildIntegPythonBase):
def test_cdk_app_with_default_requirements(self):
expected = "Hello World"
cmdlist = self.get_command_list(use_container=self.use_container)
command_result = run_command(cmdlist, cwd=self.working_dir)
self.assertEqual(command_result.process.returncode, 0)
self._verify_image_build_artifact(
self.built_template,
self.FUNCTION_LOGICAL_ID,
self.prop,
f"{self.FUNCTION_LOGICAL_ID.lower()}:latest",
)
self._verify_invoke_built_function(self.built_template, self.FUNCTION_LOGICAL_ID, {}, expected)
class TestBuildCommand_PythonFunctions_With_Specified_Architecture(BuildIntegPythonBase):
template = "template_with_architecture.yaml"
@parameterized.expand(
[
("python3.9", "Python", False, "x86_64"),
("python3.10", "Python", False, "x86_64"),
("python3.11", "Python", False, "x86_64"),
("python3.9", "PythonPEP600", False, "x86_64"),
("python3.10", "PythonPEP600", False, "x86_64"),
("python3.11", "PythonPEP600", False, "x86_64"),
("python3.9", "Python", "use_container", "x86_64"),
("python3.10", "Python", "use_container", "x86_64"),
("python3.11", "Python", "use_container", "x86_64"),
],
name_func=show_container_in_test_name,
)
def test_with_default_requirements(self, runtime, codeuri, use_container, architecture):
self._test_with_default_requirements(
runtime, codeuri, use_container, self.test_data_path, architecture=architecture
)
@parameterized.expand(
[
("python3.12", "Python", False, "x86_64"),
("python3.12", "PythonPEP600", False, "x86_64"),
("python3.12", "Python", "use_container", "x86_64"),
("python3.13", "Python", False, "x86_64"),
("python3.13", "PythonPEP600", False, "x86_64"),
("python3.13", "Python", "use_container", "x86_64"),
("python3.14", "Python", False, "x86_64"),
("python3.14", "PythonPEP600", False, "x86_64"),
# skip this test until python 3.14 build image is released
# ("python3.14", "Python", "use_container", "x86_64"),
],
name_func=show_container_in_test_name,
)
@pytest.mark.al2023
def test_with_default_requirements_al2023(self, runtime, codeuri, use_container, architecture):
self._test_with_default_requirements(
runtime, codeuri, use_container, self.test_data_path, architecture=architecture
)
def test_invalid_architecture(self):
overrides = {"Runtime": "python3.11", "Architectures": "fake"}
cmdlist = self.get_command_list(parameter_overrides=overrides)
process_execute = run_command(cmdlist, cwd=self.working_dir)
self.assertEqual(1, process_execute.process.returncode)
self.assertIn("Build Failed", str(process_execute.stdout))
self.assertIn("Architecture fake is not supported", str(process_execute.stderr))
class TestBuildCommand_ParentPackages(BuildIntegPythonBase):
template = "template_with_metadata_python.yaml"
runtime = "python3.12"
use_container = False
prop = "CodeUri"
logical_id_one = "FunctionOne"
logical_id_two = "FunctionTwo"
parent_packages_one = "src.fnone"
parent_packages_two = "src.fntwo"
codeuri_one = "PythonParentPackages/src/fnone"
codeuri_two = "PythonParentPackages/src/fntwo"
handler_one = f"{parent_packages_one}.main.handler"
handler_two = f"{parent_packages_two}.main.handler"
overrides = {
"Runtime": runtime,
"CodeUriOne": codeuri_one,
"CodeUriTwo": codeuri_two,
"HandlerOne": handler_one,
"HandlerTwo": handler_two,
}
expected_files_project_manifest = {"numpy", "src"}
expected_source_files = {
"__init__.py",
"main.py",
"requirements.txt",
}
def test_parent_package_mode_explicit(self):
# Arrange
cmdlist = self.get_command_list(
use_container=self.use_container,
parameter_overrides={
**self.overrides,
"ParentPackageMode": "explicit",
"ParentPackagesOne": self.parent_packages_one,
"ParentPackagesTwo": self.parent_packages_two,
},
)
# Act
run_command(cmdlist, cwd=self.working_dir)
# Assert
self._verify_built_artifacts()
def test_parent_package_mode_auto(self):
# Arrange
cmdlist = self.get_command_list(
use_container=self.use_container,
parameter_overrides={**self.overrides, "ParentPackageMode": "auto"},
)
# Act
run_command(cmdlist, cwd=self.working_dir)
# Assert
self._verify_built_artifacts()
def _verify_built_artifacts(self):
[
self._verify_built_artifact(self.default_build_dir, logical_id, self.expected_files_project_manifest)
for logical_id in [self.logical_id_one, self.logical_id_two]
]
[
self._verify_source_files(
self.default_build_dir,
self.expected_source_files,
logical_id,
parent_packages,
)
for (logical_id, parent_packages) in [
(self.logical_id_one, self.parent_packages_one),
(self.logical_id_two, self.parent_packages_two),
]
]
def _verify_source_files(
self, build_dir: pathlib.Path, expected_files: set[str], function_logical_id: str, parent_packages: str
) -> None:
relative_code_uri = parent_packages.replace(".", "/")
function_artifact_dir = build_dir / function_logical_id / relative_code_uri
all_artifacts = set(
map(
lambda artifact: str(object=artifact.relative_to(function_artifact_dir)),
function_artifact_dir.glob("*"),
)
)
actual_files = all_artifacts.intersection(expected_files)
self.assertEqual(actual_files, expected_files)
class TestBuildCommand_ErrorCases(BuildIntegBase):
def test_unsupported_runtime(self):
overrides = {"Runtime": "unsupportedpython", "CodeUri": "Python"}
cmdlist = self.get_command_list(parameter_overrides=overrides)
process_execute = run_command(cmdlist, cwd=self.working_dir)
self.assertEqual(1, process_execute.process.returncode)
self.assertIn("Build Failed", str(process_execute.stdout))