-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_package_command_image.py
273 lines (234 loc) · 11 KB
/
test_package_command_image.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
import os
import tempfile
from subprocess import Popen, PIPE, TimeoutExpired
from unittest import skipIf
from urllib.parse import urlparse
import boto3
from parameterized import parameterized
import docker
from samcli.commands._utils.template import get_template_data
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 TestPackageImage(PackageIntegBase):
@classmethod
def setUpClass(cls):
cls.docker_client = docker.from_env()
cls.local_images = [
("public.ecr.aws/sam/emulation-python3.8", "latest"),
]
# setup some images locally by pulling them.
for repo, tag in cls.local_images:
cls.docker_client.api.pull(repository=repo, tag=tag)
cls.docker_client.api.tag(f"{repo}:{tag}", "emulation-python3.8", tag="latest")
cls.docker_client.api.tag(f"{repo}:{tag}", "emulation-python3.8-2", tag="latest")
cls.docker_client.api.tag(f"{repo}:{tag}", "colorsrandomfunctionf61b9209", tag="latest")
super(TestPackageImage, cls).setUpClass()
def setUp(self):
super(TestPackageImage, self).setUp()
def tearDown(self):
super(TestPackageImage, self).tearDown()
@parameterized.expand(
[
"aws-serverless-function-image.yaml",
"aws-lambda-function-image.yaml",
"cdk_v1_synthesized_template_image_functions.json",
]
)
def test_package_template_without_image_repository(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(template=template_path)
process = Popen(command_list, stdout=PIPE, stderr=PIPE)
try:
stdout, stderr = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stderr = stderr.strip()
self.assertIn(
"Error: Missing option '--image-repositories', '--image-repository'", process_stderr.decode("utf-8")
)
self.assertEqual(2, process.returncode)
@parameterized.expand(
[
"aws-serverless-function-image.yaml",
"aws-lambda-function-image.yaml",
"aws-lambda-function-image-and-api.yaml",
"cdk_v1_synthesized_template_image_functions.json",
]
)
def test_package_template_with_image_repository(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(image_repository=self.ecr_repo_name, 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.assertEqual(0, process.returncode)
self.assertIn(f"{self.ecr_repo_name}", process_stdout.decode("utf-8"))
@parameterized.expand(
[
("Hello", "aws-serverless-function-image.yaml"),
("MyLambdaFunction", "aws-lambda-function-image.yaml"),
("ColorsRandomFunctionF61B9209", "cdk_v1_synthesized_template_image_functions.json"),
("ColorsRandomFunction", "cdk_v1_synthesized_template_image_functions.json"),
]
)
def test_package_template_with_image_repositories(self, resource_id, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
image_repositories=f"{resource_id}={self.ecr_repo_name}", 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(f"{self.ecr_repo_name}", process_stdout.decode("utf-8"))
self.assertEqual(0, process.returncode)
@parameterized.expand(
[
("ColorsRandomFunctionF61B9209", "cdk_v1_synthesized_template_Level2_nested_image_functions.json"),
("ColorsRandomFunction", "cdk_v1_synthesized_template_Level2_nested_image_functions.json"),
("Level2Stack/ColorsRandomFunction", "cdk_v1_synthesized_template_Level2_nested_image_functions.json"),
("ColorsRandomFunctionF61B9209", "cdk_v1_synthesized_template_Level1_nested_image_functions.json"),
("ColorsRandomFunction", "cdk_v1_synthesized_template_Level1_nested_image_functions.json"),
(
"Level1Stack/Level2Stack/ColorsRandomFunction",
"cdk_v1_synthesized_template_Level1_nested_image_functions.json",
),
]
)
def test_package_template_with_image_repositories_nested_stack(self, resource_id, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
image_repositories=f"{resource_id}={self.ecr_repo_name}", template=template_path, resolve_s3=True
)
process = Popen(command_list, stderr=PIPE)
try:
_, stderr = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stderr = stderr.strip()
self.assertIn(f"{self.ecr_repo_name}", process_stderr.decode("utf-8"))
self.assertEqual(0, process.returncode)
@parameterized.expand(
[
"aws-serverless-function-image.yaml",
"aws-lambda-function-image.yaml",
"cdk_v1_synthesized_template_image_functions.json",
]
)
def test_package_template_with_non_ecr_repo_uri_image_repository(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
image_repository="non-ecr-repo-uri", template=template_path, resolve_s3=True
)
process = Popen(command_list, stderr=PIPE)
try:
_, stderr = process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
process_stderr = stderr.strip()
self.assertEqual(2, process.returncode)
self.assertIn("Error: Invalid value for '--image-repository'", process_stderr.decode("utf-8"))
@parameterized.expand(
[
"aws-serverless-function-image.yaml",
"aws-lambda-function-image.yaml",
"cdk_v1_synthesized_template_image_functions.json",
]
)
def test_package_template_and_s3_bucket(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
s3_bucket=self.s3_bucket, s3_prefix=self.s3_prefix, template=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()
self.assertEqual(2, process.returncode)
self.assertIn(
"Error: Missing option '--image-repositories', '--image-repository'", process_stderr.decode("utf-8")
)
@parameterized.expand(["aws-serverless-application-image.yaml"])
def test_package_template_with_image_function_in_nested_application(self, template_file):
template_path = self.test_data_path.joinpath(template_file)
# when image function is not in main template, erc_repo_name does not show up in stdout
# here we download the nested application template file and verify its content
with tempfile.NamedTemporaryFile() as packaged_file, tempfile.TemporaryFile() as packaged_nested_file:
# https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
# Closes the NamedTemporaryFile as on Windows NT or later, NamedTemporaryFile cannot be opened twice.
packaged_file.close()
command_list = PackageIntegBase.get_command_list(
image_repository=self.ecr_repo_name,
template=template_path,
resolve_s3=True,
output_template_file=packaged_file.name,
)
process = Popen(command_list, stdout=PIPE, stderr=PIPE)
try:
process.communicate(timeout=TIMEOUT)
except TimeoutExpired:
process.kill()
raise
self.assertEqual(0, process.returncode)
# download the root template and locate nested template url
template_dict = get_template_data(packaged_file.name)
nested_app_template_uri = (
template_dict.get("Resources", {}).get("myApp", {}).get("Properties").get("Location")
)
# extract bucket name and object key from the url
parsed = urlparse(nested_app_template_uri)
bucket_name, key = parsed.path.lstrip("/").split("/")
# download and verify it contains ecr_repo_name
s3 = boto3.resource("s3")
s3.Object(bucket_name, key).download_fileobj(packaged_nested_file)
packaged_nested_file.seek(0)
self.assertIn(f"{self.ecr_repo_name}", packaged_nested_file.read().decode())
def test_package_with_deep_nested_template_image(self):
"""
this template contains two nested stacks:
- root
- FunctionA
- ChildStackX
- FunctionB
- ChildStackY
- FunctionA
"""
template_file = os.path.join("deep-nested-image", "template.yaml")
template_path = self.test_data_path.joinpath(template_file)
command_list = PackageIntegBase.get_command_list(
image_repository=self.ecr_repo_name, resolve_s3=True, 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")
# verify all function images are pushed
images = [
("emulation-python3.8", "latest"),
("emulation-python3.8-2", "latest"),
]
for image, tag in images:
# check string like this:
# ...python-ce689abb4f0d-3.9-slim: digest:...
self.assertRegex(process_stderr, rf"{image}-.+-{tag}: digest:")