-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtest_package_command_zip.py
716 lines (643 loc) · 28.4 KB
/
test_package_command_zip.py
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
import os
import pathlib
import re
from subprocess import Popen, PIPE, TimeoutExpired
import tempfile
from unittest import skipIf
from parameterized import parameterized, param
from samcli.lib.utils.hash import dir_checksum
from .package_integ_base import PackageIntegBase
from tests.testing_utils import RUNNING_ON_CI, RUNNING_TEST_FOR_MASTER_ON_CI, RUN_BY_CANARY
# Package tests require credentials and CI/CD will only add credentials to the env if the PR is from the same repo.
# This is to restrict package tests to run outside of CI/CD, when the branch is not master and tests are not run by Canary.
SKIP_PACKAGE_TESTS = RUNNING_ON_CI and RUNNING_TEST_FOR_MASTER_ON_CI and not RUN_BY_CANARY
TIMEOUT = 300
@skipIf(SKIP_PACKAGE_TESTS, "Skip package tests in CI/CD only")
class TestPackageZip(PackageIntegBase):
def setUp(self):
super().setUp()
def tearDown(self):
super().tearDown()
@parameterized.expand(["aws-serverless-function.yaml", "cdk_v1_synthesized_template_zip_functions.json"])
def test_package_template_flag(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name, s3_prefix=self.s3_prefix, template=template_path
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
self.assertIn("{bucket_name}".format(bucket_name=self.s3_bucket.name), process_stdout.decode("utf-8"))
@parameterized.expand(
[
("cdk_v1_synthesized_template_Level1_nested_zip_functions.json", 3),
("cdk_v1_synthesized_template_Level2_nested_zip_functions.json", 2),
]
)
def test_package_nested_template(self, template_file, uploading_count):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name, s3_prefix=self.s3_prefix, template=template_path, force_upload=True
)
process = Popen(command_list, stderr=PIPE)
try:
_, stderr = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stderr = stderr.strip().decode(("utf-8"))
uploads = re.findall(r"Uploading to.+", process_stderr)
self.assertEqual(len(uploads), uploading_count)
@parameterized.expand(
[
"cdk_v1_synthesized_template_zip_functions.json",
"aws-serverless-function.yaml",
"aws-serverless-api.yaml",
"aws-serverless-httpapi.yaml",
"aws-appsync-graphqlschema.yaml",
"aws-appsync-resolver.yaml",
"aws-appsync-functionconfiguration.yaml",
"aws-lambda-function.yaml",
"aws-apigateway-restapi.yaml",
"aws-apigatewayv2-httpapi.yaml",
"aws-elasticbeanstalk-applicationversion.yaml",
"aws-cloudformation-moduleversion.yaml",
"aws-cloudformation-resourceversion.yaml",
"aws-cloudformation-stack.yaml",
"aws-serverless-application.yaml",
"aws-lambda-layerversion.yaml",
"aws-serverless-layerversion.yaml",
"aws-glue-job.yaml",
"aws-serverlessrepo-application.yaml",
"aws-serverless-statemachine.yaml",
"aws-stepfunctions-statemachine.yaml",
"aws-include-transform.yaml",
"aws-serverless-graphqlapi.yaml",
]
)
def test_package_barebones(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name, s3_prefix=self.s3_prefix, template_file=template_path
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
self.assertIn("{bucket_name}".format(bucket_name=self.s3_bucket.name), process_stdout.decode("utf-8"))
def test_package_without_required_args(self):
command_list = PackageIntegBase.get_command_list()
process = Popen(command_list, stdout=PIPE)
try:
process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
self.assertNotEqual(process.returncode, 0)
@parameterized.expand(
[
"cdk_v1_synthesized_template_zip_functions.json",
"aws-serverless-function.yaml",
"aws-serverless-api.yaml",
"aws-serverless-httpapi.yaml",
"aws-appsync-graphqlschema.yaml",
"aws-appsync-resolver.yaml",
"aws-appsync-functionconfiguration.yaml",
"aws-lambda-function.yaml",
"aws-apigateway-restapi.yaml",
"aws-apigatewayv2-httpapi.yaml",
"aws-elasticbeanstalk-applicationversion.yaml",
"aws-cloudformation-moduleversion.yaml",
"aws-cloudformation-resourceversion.yaml",
"aws-cloudformation-stack.yaml",
"aws-serverless-application.yaml",
"aws-lambda-layerversion.yaml",
"aws-serverless-layerversion.yaml",
"aws-glue-job.yaml",
"aws-serverlessrepo-application.yaml",
"aws-serverless-statemachine.yaml",
"aws-stepfunctions-statemachine.yaml",
"aws-serverless-graphqlapi.yaml",
]
)
def test_package_with_prefix(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name, template_file=template_path, s3_prefix=self.s3_prefix
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
self.assertIn("{bucket_name}".format(bucket_name=self.s3_bucket.name), process_stdout.decode("utf-8"))
self.assertIn("{s3_prefix}".format(s3_prefix=self.s3_prefix), process_stdout.decode("utf-8"))
@parameterized.expand(
[
"cdk_v1_synthesized_template_zip_functions.json",
"aws-serverless-function.yaml",
"aws-serverless-api.yaml",
"aws-serverless-httpapi.yaml",
"aws-appsync-graphqlschema.yaml",
"aws-appsync-resolver.yaml",
"aws-appsync-functionconfiguration.yaml",
"aws-lambda-function.yaml",
"aws-apigateway-restapi.yaml",
"aws-apigatewayv2-httpapi.yaml",
"aws-elasticbeanstalk-applicationversion.yaml",
"aws-cloudformation-moduleversion.yaml",
"aws-cloudformation-resourceversion.yaml",
"aws-cloudformation-stack.yaml",
"aws-serverless-application.yaml",
"aws-lambda-layerversion.yaml",
"aws-serverless-layerversion.yaml",
"aws-glue-job.yaml",
"aws-serverlessrepo-application.yaml",
"aws-serverless-statemachine.yaml",
"aws-stepfunctions-statemachine.yaml",
"aws-serverless-graphqlapi.yaml",
]
)
def test_package_with_output_template_file(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
with tempfile.NamedTemporaryFile(delete=False) as output_template:
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name,
template_file=template_path,
s3_prefix=self.s3_prefix,
output_template_file=output_template.name,
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
self.assertIn(
bytes(
"Successfully packaged artifacts and wrote output template to file {output_template_file}".format(
output_template_file=str(output_template.name)
),
encoding="utf-8",
),
process_stdout,
)
@parameterized.expand(
[
"cdk_v1_synthesized_template_zip_functions.json",
"aws-serverless-function.yaml",
"aws-serverless-api.yaml",
"aws-serverless-httpapi.yaml",
"aws-appsync-graphqlschema.yaml",
"aws-appsync-resolver.yaml",
"aws-appsync-functionconfiguration.yaml",
"aws-lambda-function.yaml",
"aws-apigateway-restapi.yaml",
"aws-apigatewayv2-httpapi.yaml",
"aws-elasticbeanstalk-applicationversion.yaml",
"aws-cloudformation-moduleversion.yaml",
"aws-cloudformation-resourceversion.yaml",
"aws-cloudformation-stack.yaml",
"aws-serverless-application.yaml",
"aws-lambda-layerversion.yaml",
"aws-serverless-layerversion.yaml",
"aws-glue-job.yaml",
"aws-serverlessrepo-application.yaml",
"aws-serverless-statemachine.yaml",
"aws-stepfunctions-statemachine.yaml",
"aws-serverless-graphqlapi.yaml",
]
)
def test_package_with_json(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
with tempfile.NamedTemporaryFile(delete=False) as output_template:
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name,
template_file=template_path,
s3_prefix=self.s3_prefix,
output_template_file=output_template.name,
use_json=True,
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
self.assertIn(
bytes(
"Successfully packaged artifacts and wrote output template to file {output_template_file}".format(
output_template_file=str(output_template.name)
),
encoding="utf-8",
),
process_stdout,
)
@parameterized.expand(
[
"cdk_v1_synthesized_template_zip_functions.json",
"aws-serverless-function.yaml",
"aws-serverless-api.yaml",
"aws-serverless-httpapi.yaml",
"aws-appsync-graphqlschema.yaml",
"aws-appsync-resolver.yaml",
"aws-appsync-functionconfiguration.yaml",
"aws-lambda-function.yaml",
"aws-apigateway-restapi.yaml",
"aws-apigatewayv2-httpapi.yaml",
"aws-elasticbeanstalk-applicationversion.yaml",
"aws-cloudformation-moduleversion.yaml",
"aws-cloudformation-resourceversion.yaml",
"aws-cloudformation-stack.yaml",
"aws-serverless-application.yaml",
"aws-lambda-layerversion.yaml",
"aws-serverless-layerversion.yaml",
"aws-glue-job.yaml",
"aws-serverlessrepo-application.yaml",
"aws-serverless-statemachine.yaml",
"aws-stepfunctions-statemachine.yaml",
"aws-serverless-graphqlapi.yaml",
]
)
def test_package_with_force_upload(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
with tempfile.NamedTemporaryFile(delete=False) as output_template:
# Upload twice and see the string to have packaged artifacts both times.
for _ in range(2):
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name,
template_file=template_path,
s3_prefix=self.s3_prefix,
output_template_file=output_template.name,
force_upload=True,
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
self.assertIn(
bytes(
"Successfully packaged artifacts and wrote output template to file {output_template_file}".format(
output_template_file=str(output_template.name)
),
encoding="utf-8",
),
process_stdout,
)
@parameterized.expand(
[
"cdk_v1_synthesized_template_zip_functions.json",
"aws-serverless-function.yaml",
"aws-serverless-api.yaml",
"aws-serverless-httpapi.yaml",
"aws-appsync-graphqlschema.yaml",
"aws-appsync-resolver.yaml",
"aws-appsync-functionconfiguration.yaml",
"aws-lambda-function.yaml",
"aws-apigateway-restapi.yaml",
"aws-apigatewayv2-httpapi.yaml",
"aws-elasticbeanstalk-applicationversion.yaml",
"aws-cloudformation-moduleversion.yaml",
"aws-cloudformation-resourceversion.yaml",
"aws-cloudformation-stack.yaml",
"aws-serverless-application.yaml",
"aws-lambda-layerversion.yaml",
"aws-serverless-layerversion.yaml",
"aws-glue-job.yaml",
"aws-serverlessrepo-application.yaml",
"aws-serverless-statemachine.yaml",
"aws-stepfunctions-statemachine.yaml",
"aws-serverless-graphqlapi.yaml",
]
)
def test_package_with_kms_key(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
with tempfile.NamedTemporaryFile(delete=False) as output_template:
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name,
template_file=template_path,
s3_prefix=self.s3_prefix,
output_template_file=output_template.name,
force_upload=True,
kms_key_id=self.kms_key,
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
self.assertIn(
bytes(
"Successfully packaged artifacts and wrote output template to file {output_template_file}".format(
output_template_file=str(output_template.name)
),
encoding="utf-8",
),
process_stdout,
)
@parameterized.expand(
[
"cdk_v1_synthesized_template_zip_functions.json",
"aws-serverless-function.yaml",
"aws-serverless-api.yaml",
"aws-serverless-httpapi.yaml",
"aws-appsync-graphqlschema.yaml",
"aws-appsync-resolver.yaml",
"aws-appsync-functionconfiguration.yaml",
"aws-lambda-function.yaml",
"aws-apigateway-restapi.yaml",
"aws-apigatewayv2-httpapi.yaml",
"aws-elasticbeanstalk-applicationversion.yaml",
"aws-cloudformation-moduleversion.yaml",
"aws-cloudformation-resourceversion.yaml",
"aws-cloudformation-stack.yaml",
"aws-serverless-application.yaml",
"aws-lambda-layerversion.yaml",
"aws-serverless-layerversion.yaml",
"aws-glue-job.yaml",
"aws-serverlessrepo-application.yaml",
"aws-serverless-statemachine.yaml",
"aws-stepfunctions-statemachine.yaml",
"aws-serverless-graphqlapi.yaml",
]
)
def test_package_with_metadata(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
with tempfile.NamedTemporaryFile(delete=False) as output_template:
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name,
template_file=template_path,
s3_prefix=self.s3_prefix,
output_template_file=output_template.name,
force_upload=True,
metadata={"integ": "yes"},
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
self.assertIn(
bytes(
"Successfully packaged artifacts and wrote output template to file {output_template_file}".format(
output_template_file=str(output_template.name)
),
encoding="utf-8",
),
process_stdout,
)
@parameterized.expand(
[
"cdk_v1_synthesized_template_zip_functions.json",
"aws-serverless-function.yaml",
"aws-serverless-api.yaml",
"aws-appsync-graphqlschema.yaml",
"aws-appsync-resolver.yaml",
"aws-appsync-functionconfiguration.yaml",
"aws-lambda-function.yaml",
"aws-apigateway-restapi.yaml",
"aws-apigatewayv2-httpapi.yaml",
"aws-elasticbeanstalk-applicationversion.yaml",
"aws-cloudformation-moduleversion.yaml",
"aws-cloudformation-resourceversion.yaml",
"aws-cloudformation-stack.yaml",
"aws-serverless-application.yaml",
"aws-lambda-layerversion.yaml",
"aws-serverless-layerversion.yaml",
"aws-glue-job.yaml",
"aws-serverlessrepo-application.yaml",
"aws-serverless-statemachine.yaml",
"aws-stepfunctions-statemachine.yaml",
"aws-serverless-graphqlapi.yaml",
]
)
def test_package_with_resolve_s3(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
with tempfile.NamedTemporaryFile(delete=False) as output_template:
command_list = PackageIntegBase.get_command_list(
template_file=template_path,
s3_prefix=self.s3_prefix,
output_template_file=output_template.name,
force_upload=True,
resolve_s3=True,
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
self.assertIn(
bytes(
"Successfully packaged artifacts and wrote output template to file {output_template_file}".format(
output_template_file=str(output_template.name)
),
encoding="utf-8",
),
process_stdout,
)
@parameterized.expand([(True,), (False,)])
def test_package_with_no_progressbar(self, no_progressbar):
template_path = self.test_data_path.joinpath("aws-serverless-function.yaml")
with tempfile.NamedTemporaryFile(delete=False) as output_template:
command_list = PackageIntegBase.get_command_list(
template_file=template_path,
s3_prefix=self.s3_prefix,
output_template_file=output_template.name,
force_upload=True,
no_progressbar=no_progressbar,
resolve_s3=True,
)
process = Popen(command_list, stdout=PIPE, stderr=PIPE)
try:
_, stderr = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stderr = stderr.strip()
upload_message = bytes("Uploading to", encoding="utf-8")
if no_progressbar:
self.assertNotIn(
upload_message,
process_stderr,
)
else:
self.assertIn(
upload_message,
process_stderr,
)
@parameterized.expand(
[
param("aws-serverless-function-codedeploy-warning.yaml", "CodeDeploy"),
param("aws-serverless-function-codedeploy-condition-warning.yaml", "CodeDeploy DeploymentGroups"),
]
)
def test_package_with_warning_template(self, template_file, warning_keyword):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name, s3_prefix=self.s3_prefix, template=template_path
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip().decode("utf-8")
# Not comparing with full warning message because of line ending mismatch on
# windows and non-windows
self.assertIn(warning_keyword, process_stdout)
def test_package_with_deep_nested_template(self):
"""
this template contains two nested stacks:
- root
- FunctionA
- ChildStackX
- FunctionB
- ChildStackY
- FunctionA
- MyLayerVersion
"""
template_file = os.path.join("deep-nested", "template.yaml")
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name, s3_prefix=self.s3_prefix, template=template_path, force_upload=True
)
process = Popen(command_list, stdout=PIPE, stderr=PIPE)
try:
_, stderr = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stderr = stderr.strip().decode("utf-8")
# there are in total 3 function dir, 1 layer dir and 2 child templates to upload
uploads = re.findall(r"Uploading to.+", process_stderr)
self.assertEqual(len(uploads), 6)
# make sure uploads' checksum match the dirs and child templates
build_dir = pathlib.Path(os.path.dirname(__file__)).parent.joinpath("testdata", "package", "deep-nested")
dirs = [
build_dir.joinpath("FunctionA"),
build_dir.joinpath("ChildStackX", "FunctionB"),
build_dir.joinpath("ChildStackX", "ChildStackY", "FunctionA"),
build_dir.joinpath("ChildStackX", "ChildStackY", "MyLayerVersion"),
]
# here we only verify function/layer code dirs' hash
# because templates go through some pre-process before being uploaded and the hash can not be determined
for dir in dirs:
checksum = dir_checksum(dir.absolute())
self.assertIn(checksum, process_stderr)
# verify both child templates are uploaded
uploads = re.findall(r"\.template", process_stderr)
self.assertEqual(len(uploads), 2)
def test_package_with_stackset(self):
"""
this template contains a stack set:
- root
- FunctionA
- StackSetA
"""
template_file = os.path.join("stackset", "template.yaml")
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name, s3_prefix=self.s3_prefix, template=template_path, force_upload=True
)
prevdir = os.getcwd()
os.chdir(os.path.expanduser(os.path.dirname(template_path)))
process = Popen(command_list, stdout=PIPE, stderr=PIPE)
try:
_, stderr = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stderr = stderr.strip().decode("utf-8")
os.chdir(prevdir)
# there are in total 1 function dir, 1 stackset template to upload
uploads = re.findall(r"Uploading to.+", process_stderr)
self.assertEqual(len(uploads), 2)
# make sure uploads' checksum match the dirs and stackset template
build_dir = pathlib.Path(os.path.dirname(__file__)).parent.joinpath("testdata", "package", "stackset")
dirs = [build_dir.joinpath("FunctionA")]
# here we only verify function/layer code dirs' hash
# because templates go through some pre-process before being uploaded and the hash can not be determined
for dir in dirs:
checksum = dir_checksum(dir.absolute())
self.assertIn(checksum, process_stderr)
# verify stack set template is uploaded
uploads = re.findall(r"\.template", process_stderr)
self.assertEqual(len(uploads), 1)
def test_package_with_stackset_in_a_substack(self):
"""
this template contains a stack set:
- root
- ChildStackX
- FunctionA
- StackSetA
"""
template_file = os.path.join("stackset", "nested-template.yaml")
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name, s3_prefix=self.s3_prefix, template=template_path, force_upload=True
)
prevdir = os.getcwd()
os.chdir(os.path.expanduser(os.path.dirname(template_path)))
process = Popen(command_list, stdout=PIPE, stderr=PIPE)
try:
_, stderr = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stderr = stderr.strip().decode("utf-8")
os.chdir(prevdir)
# there are in total 1 child stack, 1 function dir, 1 stackset template to upload
uploads = re.findall(r"Uploading to.+", process_stderr)
self.assertEqual(len(uploads), 3)
# make sure uploads' checksum match the dirs and stackset template
build_dir = pathlib.Path(os.path.dirname(__file__)).parent.joinpath("testdata", "package", "stackset")
dirs = [build_dir.joinpath("FunctionA")]
# here we only verify function/layer code dirs' hash
# because templates go through some pre-process before being uploaded and the hash can not be determined
for dir in dirs:
checksum = dir_checksum(dir.absolute())
self.assertIn(checksum, process_stderr)
# verify child stack and stack set templates are uploaded
uploads = re.findall(r"\.template", process_stderr)
self.assertEqual(len(uploads), 2)
@parameterized.expand(["aws-serverless-function-cdk.yaml", "cdk_v1_synthesized_template_zip_functions.json"])
def test_package_logs_warning_for_cdk_project(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket.name, s3_prefix=self.s3_prefix, template_file=template_path
)
process = Popen(command_list, stdout=PIPE)
try:
stdout, _ = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stdout = stdout.strip()
warning_message = bytes(
f"Warning: CDK apps are not officially supported with this command.{os.linesep}"
"We recommend you use this alternative command: cdk deploy",
encoding="utf-8",
)
self.assertIn(warning_message, stdout)
self.assertIn("{bucket_name}".format(bucket_name=self.s3_bucket.name), process_stdout.decode("utf-8"))