forked from aws/aws-sam-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_lambda_api_integ_base.py
More file actions
284 lines (234 loc) · 10.4 KB
/
Copy pathstart_lambda_api_integ_base.py
File metadata and controls
284 lines (234 loc) · 10.4 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
import shutil
import uuid
from typing import Optional, Dict, List
from unittest import TestCase, skipIf
import threading
from subprocess import Popen, PIPE
import os
import logging
from pathlib import Path
import boto3
from botocore import UNSIGNED
from botocore.config import Config
from docker.errors import APIError
from psutil import NoSuchProcess
from samcli.local.docker.utils import get_validated_container_client
from tests.integration.local.common_utils import random_port, InvalidAddressException, wait_for_local_process
from tests.testing_utils import (
SKIP_DOCKER_TESTS,
SKIP_DOCKER_MESSAGE,
run_command,
kill_process,
get_sam_command,
)
LOG = logging.getLogger(__name__)
@skipIf(SKIP_DOCKER_TESTS, SKIP_DOCKER_MESSAGE)
class StartLambdaIntegBaseClass(TestCase):
template: Optional[str] = None
container_mode: Optional[str] = None
container_host_interface: Optional[str] = None
parameter_overrides: Optional[Dict[str, str]] = None
binary_data_file: Optional[str] = None
integration_dir = str(Path(__file__).resolve().parents[2])
invoke_image: Optional[List] = None
hook_name: Optional[str] = None
terraform_plan_file: Optional[str] = None
beta_features: Optional[bool] = None
collect_start_lambda_process_output: bool = False
function_logical_ids: Optional[List[str]] = None
build_before_invoke = False
build_overrides: Optional[Dict[str, str]] = None
working_dir: Optional[str] = None
@classmethod
def setUpClass(cls):
# This is the directory for tests/integration which will be used to file the testdata
# files for integ tests
cls.template = cls.integration_dir + cls.template_path
cls.working_dir = str(Path(cls.template).resolve().parents[0])
cls.env_var_path = cls.integration_dir + "/testdata/invoke/vars.json"
if cls.build_before_invoke:
cls.build()
# Create the Docker client with automatic container selection
cls.docker_client = get_validated_container_client()
# Only remove containers with SAM CLI labels to avoid interfering with other processes
try:
sam_containers = cls.docker_client.containers.list(
all=True, filters={"label": "sam.cli.container.type=lambda"}
)
for container in sam_containers:
try:
container.remove(force=True)
LOG.info("Removed existing SAM CLI container %s", container.short_id)
except APIError as ex:
LOG.error("Failed to remove existing SAM CLI container %s", container.short_id, exc_info=ex)
except Exception as ex:
LOG.error("Failed to clean up existing SAM CLI containers", exc_info=ex)
cls.start_lambda_with_retry()
@classmethod
def build(cls):
command = get_sam_command()
command_list = [command, "build"]
if cls.build_overrides:
overrides_arg = " ".join(
["ParameterKey={},ParameterValue={}".format(key, value) for key, value in cls.build_overrides.items()]
)
command_list += ["--parameter-overrides", overrides_arg]
if cls.hook_name:
command_list += ["--hook-name", cls.hook_name]
run_command(command_list, cwd=cls.working_dir)
@classmethod
def start_lambda_with_retry(cls, retries=3, input=None, env=None):
retry_count = 0
while retry_count < retries:
cls.port = str(random_port())
try:
cls.start_lambda(input=input, env=env)
except InvalidAddressException:
retry_count += 1
continue
break
if retry_count == retries:
raise ValueError("Ran out of retries attempting to start lambda")
@classmethod
def get_start_lambda_command(
cls,
port=None,
template_path=None,
env_var_path=None,
container_mode=None,
container_host_interface=None,
parameter_overrides=None,
invoke_image=None,
hook_name=None,
beta_features=None,
terraform_plan_file=None,
function_logical_ids=None,
):
command_list = [get_sam_command(), "local", "start-lambda"]
# Add function names as positional arguments first
if function_logical_ids:
command_list.extend(function_logical_ids)
if port:
command_list += ["-p", port]
if template_path:
command_list += ["-t", template_path]
if env_var_path:
command_list += ["--env-vars", env_var_path]
if container_mode:
command_list += ["--warm-containers", container_mode]
if container_host_interface:
command_list += ["--container-host-interface", container_host_interface]
if parameter_overrides:
command_list += ["--parameter-overrides", cls._make_parameter_override_arg(parameter_overrides)]
if invoke_image:
for image in invoke_image:
command_list += ["--invoke-image", image]
if hook_name:
command_list += ["--hook-name", hook_name]
if beta_features is not None:
command_list += ["--beta-features" if beta_features else "--no-beta-features"]
if terraform_plan_file:
command_list += ["--terraform-plan-file", terraform_plan_file]
return command_list
@classmethod
def start_lambda(cls, wait_time=5, input=None, env=None):
command_list = cls.get_start_lambda_command(
port=cls.port,
template_path=cls.template,
env_var_path=cls.env_var_path,
container_mode=cls.container_mode,
container_host_interface=cls.container_host_interface,
parameter_overrides=cls.parameter_overrides,
invoke_image=cls.invoke_image,
hook_name=cls.hook_name,
beta_features=cls.beta_features,
terraform_plan_file=cls.terraform_plan_file,
function_logical_ids=cls.function_logical_ids,
)
# Container labels are no longer needed - container IDs are parsed from output
cls.start_lambda_process = Popen(command_list, stderr=PIPE, stdin=PIPE, env=env, cwd=cls.working_dir)
cls.start_lambda_process_output = ""
if input:
cls.start_lambda_process.stdin.write(input)
cls.start_lambda_process.stdin.close()
cls.start_lambda_process_error = wait_for_local_process(
cls.start_lambda_process, cls.port, cls.collect_start_lambda_process_output
)
cls.stop_reading_thread = False
def read_sub_process_stderr():
while not cls.stop_reading_thread:
line = cls.start_lambda_process.stderr.readline()
if line:
line_str = line.decode("utf-8").strip()
cls.start_lambda_process_output += line_str + "\n"
cls.read_threading = threading.Thread(target=read_sub_process_stderr, daemon=True)
cls.read_threading.start()
@classmethod
def _make_parameter_override_arg(self, overrides):
return " ".join(["ParameterKey={},ParameterValue={}".format(key, value) for key, value in overrides.items()])
@classmethod
def tearDownClass(cls):
# After all the tests run, we need to kill the start_lambda process.
cls.stop_reading_thread = True
try:
kill_process(cls.start_lambda_process)
except NoSuchProcess:
LOG.info("Process has already been terminated")
# Clean up any remaining SAM CLI containers
try:
docker_client = get_validated_container_client()
# Only remove containers with SAM CLI labels to avoid interfering with other processes
sam_containers = docker_client.containers.list(all=True, filters={"label": "sam.cli.container.type=lambda"})
for container in sam_containers:
try:
container.remove(force=True)
LOG.info("Removed SAM CLI container %s", container.short_id)
except APIError as ex:
LOG.error("Failed to remove SAM CLI container %s", container.short_id, exc_info=ex)
except Exception as ex:
LOG.error("Failed to clean up SAM CLI containers", exc_info=ex)
def get_local_lambda_client(self):
url = "http://127.0.0.1:{}".format(self.port)
return boto3.client(
"lambda",
endpoint_url=url,
region_name="us-east-1",
use_ssl=False,
verify=False,
config=Config(signature_version=UNSIGNED, read_timeout=120, retries={"max_attempts": 0}),
)
class WatchWarmContainersIntegBaseClass(StartLambdaIntegBaseClass):
temp_path: Optional[str] = None
template_path: Optional[str] = None
code_path: Optional[str] = None
docker_file_path: Optional[str] = None
@classmethod
def setUpClass(cls):
cls.temp_path = str(uuid.uuid4()).replace("-", "")[:10]
working_dir = str(Path(cls.integration_dir).resolve().joinpath(cls.temp_path))
if Path(working_dir).resolve().exists():
shutil.rmtree(working_dir, ignore_errors=True)
os.mkdir(working_dir)
os.mkdir(Path(cls.integration_dir).resolve().joinpath(cls.temp_path).joinpath("dir"))
cls.template_path = f"/{cls.temp_path}/template.yaml"
cls.code_path = f"/{cls.temp_path}/main.py"
cls.code_path2 = f"/{cls.temp_path}/dir/main2.py"
cls.docker_file_path = f"/{cls.temp_path}/Dockerfile"
cls.docker_file_path2 = f"/{cls.temp_path}/Dockerfile2"
if cls.template_content:
cls._write_file_content(cls.template_path, cls.template_content)
if cls.code_content:
cls._write_file_content(cls.code_path, cls.code_content)
if cls.docker_file_content:
cls._write_file_content(cls.docker_file_path, cls.docker_file_content)
super().setUpClass()
@classmethod
def _write_file_content(cls, path, content):
with open(cls.integration_dir + path, "w") as f:
f.write(content)
@classmethod
def tearDownClass(cls):
super().tearDownClass()
working_dir = str(Path(cls.integration_dir).resolve().joinpath(cls.temp_path))
if Path(working_dir).resolve().exists():
shutil.rmtree(working_dir, ignore_errors=True)