-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_logs_command.py
370 lines (311 loc) · 15.6 KB
/
test_logs_command.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
import itertools
import logging
import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
import boto3
import pytest
import requests
from parameterized import parameterized
from samcli.lib.utils.boto_utils import get_boto_resource_provider_with_config, get_boto_client_provider_with_config
from samcli.lib.utils.cloudformation import get_resource_summaries
from tests.integration.deploy.deploy_integ_base import DeployIntegBase
from tests.integration.logs.logs_integ_base import LogsIntegBase, RETRY_COUNT
from tests.integration.logs.logs_integ_base import RETRY_SLEEP
from tests.testing_utils import (
start_persistent_process,
read_until,
kill_process,
run_command,
method_to_stack_name,
)
LOG = logging.getLogger(__name__)
APIGW_REQUESTS_TO_WARM_UP = 20
class LogsIntegTestCases(LogsIntegBase):
test_template_folder = ""
stack_name = ""
stack_resources: Dict[Any, Any] = {}
stack_info = None
def setUp(self):
self.lambda_client = boto3.client("lambda")
self.sfn_client = boto3.client("stepfunctions")
@pytest.fixture(scope="class")
def deploy_testing_stack(self):
test_data_path = Path(__file__).resolve().parents[1].joinpath("testdata", "logs")
LogsIntegTestCases.stack_name = method_to_stack_name("test_logs_command")
cfn_client = boto3.client("cloudformation")
deploy_cmd = DeployIntegBase.get_deploy_command_list(
stack_name=LogsIntegTestCases.stack_name,
template_file=test_data_path.joinpath(self.test_template_folder, "template.yaml"),
resolve_s3=True,
capabilities_list=["CAPABILITY_IAM", "CAPABILITY_AUTO_EXPAND"],
)
deploy_result = run_command(deploy_cmd)
yield deploy_result, cfn_client
cfn_client.delete_stack(StackName=LogsIntegTestCases.stack_name)
@pytest.fixture(autouse=True, scope="class")
def logs_base(self, deploy_testing_stack):
deploy_result = deploy_testing_stack[0]
self.assertEqual(
deploy_result.process.returncode, 0, f"Deployment of the test stack is failed with {deploy_result.stderr}"
)
stack_resource_summaries = get_resource_summaries(
get_boto_resource_provider_with_config(),
get_boto_client_provider_with_config(),
LogsIntegTestCases.stack_name,
)
LogsIntegTestCases.stack_resources = {
resource_full_path: stack_resource_summary.physical_resource_id
for resource_full_path, stack_resource_summary in stack_resource_summaries.items()
}
cfn_resource = boto3.resource("cloudformation")
LogsIntegTestCases.stack_info = cfn_resource.Stack(LogsIntegTestCases.stack_name)
def _get_physical_id(self, resource_path: str):
return self.stack_resources[resource_path]
def _get_output_value(self, key: str):
for output in self.stack_info.outputs: # type: ignore
if output.get("OutputKey", "") == key:
return output.get("OutputValue", "")
return None
def _test_function_logs(self, function_name):
expected_log_output = f"Hello world from {function_name} function" # Hello world from ApiGwFunction function
LOG.info("Invoking function %s", function_name)
lambda_invoke_result = self.lambda_client.invoke(FunctionName=self._get_physical_id(function_name))
LOG.info("Lambda invoke result %s", lambda_invoke_result)
cmd_list = self.get_logs_command_list(self.stack_name, name=function_name)
self._check_logs(cmd_list, [expected_log_output])
def _test_tail(self, function_name):
cmd_list = self.get_logs_command_list(self.stack_name, name=function_name, tail=True)
tail_process = start_persistent_process(cmd_list)
expected_log_output = f"Hello world from {function_name} function" # Hello world from ApiGwFunction function
LOG.info("Invoking function %s", function_name)
lambda_invoke_result = self.lambda_client.invoke(FunctionName=self._get_physical_id(function_name))
LOG.info("Lambda invoke result %s", lambda_invoke_result)
def _check_logs(output: str, _: List[str]) -> bool:
return expected_log_output in output
try:
read_until(tail_process, _check_logs, timeout=RETRY_COUNT * RETRY_SLEEP)
finally:
kill_process(tail_process)
def _test_filter(self, function_name):
function_name_for_filter = function_name.replace("/", "")
log_filter = f"this should be filtered {function_name_for_filter}" # this should be filtered ApiGwFunction
LOG.info("Invoking function %s", function_name)
lambda_invoke_result = self.lambda_client.invoke(FunctionName=self._get_physical_id(function_name))
LOG.info("Lambda invoke result %s", lambda_invoke_result)
cmd_list = self.get_logs_command_list(self.stack_name, name=function_name, filter=log_filter)
self._check_logs(cmd_list, [log_filter])
def _test_apigw_logs(self, apigw_name, path):
# apigw name in output section doesn't have forward slashes
apigw_name_from_output = apigw_name.replace("/", "")
apigw_url = f"{self._get_output_value(apigw_name_from_output)}{path}"
# make couple of requests to warm-up APIGW to write its logs to CW
for i in range(APIGW_REQUESTS_TO_WARM_UP):
apigw_result = requests.get(apigw_url)
LOG.info("APIGW result %s", apigw_result)
cmd_list = self.get_logs_command_list(self.stack_name, name=apigw_name)
self._check_logs(cmd_list, [f"HTTP Method: GET, Resource Path: /{path}"])
def _test_sfn_logs(self, state_machine_name):
sfn_physical_id = self._get_physical_id(state_machine_name)
sfn_invoke_result = self.sfn_client.start_execution(stateMachineArn=sfn_physical_id)
execution_arn = sfn_invoke_result.get("executionArn", "")
LOG.info("SFN invoke result %s", sfn_invoke_result)
cmd_list = self.get_logs_command_list(self.stack_name, name=state_machine_name)
self._check_logs(cmd_list, [execution_arn])
def _test_end_to_end_apigw(self, apigw_name, path):
# apigw name in output section doesn't have forward slashes
apigw_name_from_output = apigw_name.replace("/", "")
apigw_url = f"{self._get_output_value(apigw_name_from_output)}{path}"
apigw_result = requests.get(apigw_url)
LOG.info("APIGW result %s", apigw_result)
cmd_list = self.get_logs_command_list(self.stack_name)
self._check_logs(
cmd_list,
[
f"HTTP Method: GET, Resource Path: /{path}",
# Hello world from HelloWorldServerlessApi/hello function
f"Hello world from {apigw_name_from_output}/{path} function",
],
)
def _test_end_to_end_sfn(self, apigw_name, path):
# apigw name in output section doesn't have forward slashes
apigw_name_from_output = apigw_name.replace("/", "")
apigw_url = f"{self._get_output_value(apigw_name_from_output)}{path}"
apigw_result = requests.get(apigw_url)
LOG.info("APIGW result %s", apigw_result)
cmd_list = self.get_logs_command_list(self.stack_name)
self._check_logs(
cmd_list,
[
f"HTTP Method: GET, Resource Path: /{path}",
'"type": "TaskStateEntered"',
# Hello world from HelloWorldServerlessApi/world function
f"Hello world from {apigw_name_from_output}/{path} function",
],
)
def _test_output(self, function_name, output):
expected_log_output = f"Hello world from {function_name} function" # Hello world from ApiGwFunction function
LOG.info("Invoking function %s", function_name)
lambda_invoke_result = self.lambda_client.invoke(FunctionName=self._get_physical_id(function_name))
LOG.info("Lambda invoke result %s", lambda_invoke_result)
cmd_list = self.get_logs_command_list(self.stack_name, name=function_name, output=output)
self._check_logs(cmd_list, [expected_log_output], output=output)
def _test_start_end(self, function_name, start_end_time_params):
(start_time, end_time, should_succeed) = start_end_time_params
expected_log_output = f"Hello world from {function_name} function" # Hello world from ApiGwFunction function
LOG.info("Invoking function %s", function_name)
lambda_invoke_result = self.lambda_client.invoke(FunctionName=self._get_physical_id(function_name))
LOG.info("Lambda invoke result %s", lambda_invoke_result)
cmd_list = self.get_logs_command_list(
self.stack_name, name=function_name, start_time=start_time, end_time=end_time
)
if not should_succeed:
with self.assertRaises(ValueError):
self._check_logs(cmd_list, [expected_log_output], retries=2)
else:
self._check_logs(cmd_list, [expected_log_output])
def _test_include_traces(self, function_name):
expected_log_output = f"Hello world from {function_name} function" # Hello world from ApiGwFunction function
LOG.info("Invoking function %s", function_name)
lambda_invoke_result = self.lambda_client.invoke(FunctionName=self._get_physical_id(function_name))
LOG.info("Lambda invoke result %s", lambda_invoke_result)
cmd_list = self.get_logs_command_list(self.stack_name, name=function_name, include_traces=True)
self._check_logs(cmd_list, ["New XRay Service Graph", "XRay Event [revision ", expected_log_output])
def _check_logs(self, cmd_list: List, log_strings: List[str], output: str = "text", retries=RETRY_COUNT):
for _ in range(retries):
cmd_result = run_command(cmd_list)
cmd_stdout = cmd_result.stdout.decode("utf-8")
cmd_stderr = cmd_result.stderr.decode("utf-8")
if cmd_result.process.returncode != 0:
LOG.info(cmd_stdout)
LOG.error(cmd_stderr)
self.assertEqual(cmd_result.process.returncode, 0)
log_string_found = True
for log_string in log_strings:
if output == "json":
if f'"message": "{log_string}\\n"' not in cmd_stdout:
log_string_found = False
break
else:
if log_string not in cmd_stdout:
log_string_found = False
break
if log_string_found:
return
time.sleep(RETRY_SLEEP)
raise ValueError(f"No match found for one of the expected log outputs '{log_strings}'")
REGULAR_STACK_FUNCTION_LIST = [
"ApiGwFunction",
"SfnFunction",
"FunctionWithCustomLoggingConfig",
]
REGULAR_STACK_APIGW_LIST = [
"HelloWorldServerlessApi",
]
REGULAR_STACK_SFN_LIST = [
"MyStateMachine",
]
@pytest.mark.xdist_group(name="sam_logs_regular")
class TestLogsCommandWithRegularStack(LogsIntegTestCases):
test_template_folder = "python-apigw-sfn"
@parameterized.expand(REGULAR_STACK_FUNCTION_LIST)
def test_function_logs(self, function_name: str):
self._test_function_logs(function_name)
@parameterized.expand(REGULAR_STACK_FUNCTION_LIST)
def test_tail(self, function_name: str):
self._test_tail(function_name)
@parameterized.expand(REGULAR_STACK_FUNCTION_LIST)
def test_filter(self, function_name: str):
self._test_filter(function_name)
@parameterized.expand(itertools.product(REGULAR_STACK_APIGW_LIST, ["hello", "world"]))
def test_apigw_logs(self, apigw_name: str, path: str):
self._test_apigw_logs(apigw_name, path)
@parameterized.expand(REGULAR_STACK_SFN_LIST)
def test_sfn_logs(self, state_machine_name: str):
self._test_sfn_logs(state_machine_name)
@parameterized.expand(itertools.product(REGULAR_STACK_APIGW_LIST, ["hello"]))
def test_end_to_end_apigw(self, apigw_name: str, path: str):
self._test_end_to_end_apigw(apigw_name, path)
@parameterized.expand(itertools.product(REGULAR_STACK_APIGW_LIST, ["world"]))
def test_end_to_end_sfn(self, apigw_name: str, path: str):
self._test_end_to_end_sfn(apigw_name, path)
@parameterized.expand(itertools.product(REGULAR_STACK_FUNCTION_LIST, [None, "text", "json"]))
def test_output(self, function_name: str, output: Optional[str]):
self._test_output(function_name, output)
@parameterized.expand(
itertools.product(
REGULAR_STACK_FUNCTION_LIST,
[
(None, None, True),
(None, "1 minute", True),
("1 minute", None, True),
("now", None, False),
],
)
)
def test_start_end(self, function_name: str, start_end_time_params: Tuple[Optional[str], Optional[str], bool]):
self._test_start_end(function_name, start_end_time_params)
@parameterized.expand(REGULAR_STACK_FUNCTION_LIST)
def test_include_traces(self, function_name: str):
self._test_include_traces(function_name)
NESTED_STACK_FUNCTION_LIST = [
"ApiGwFunction",
"SfnFunction",
"ChildStack/ApiGwFunction",
"ChildStack/SfnFunction",
"ChildStack/GrandChildStack/ApiGwFunction",
"ChildStack/GrandChildStack/SfnFunction",
]
NESTED_STACK_APIGW_LIST = [
"HelloWorldServerlessApi",
"ChildStack/HelloWorldServerlessApi",
"ChildStack/GrandChildStack/HelloWorldServerlessApi",
]
NESTED_STACK_SFN_LIST = [
"MyStateMachine",
"ChildStack/MyStateMachine",
"ChildStack/GrandChildStack/MyStateMachine",
]
@pytest.mark.xdist_group(name="sam_logs_nested")
class TestLogsCommandWithNestedStack(LogsIntegTestCases):
test_template_folder = "nested-python-apigw-sfn"
@parameterized.expand(NESTED_STACK_FUNCTION_LIST)
def test_function_logs(self, function_name: str):
self._test_function_logs(function_name)
@parameterized.expand(NESTED_STACK_FUNCTION_LIST)
def test_tail(self, function_name: str):
self._test_tail(function_name)
@parameterized.expand(NESTED_STACK_FUNCTION_LIST)
def test_filter(self, function_name: str):
self._test_filter(function_name)
@parameterized.expand(itertools.product(NESTED_STACK_APIGW_LIST, ["hello", "world"]))
def test_apigw_logs(self, apigw_name: str, path: str):
self._test_apigw_logs(apigw_name, path)
@parameterized.expand(NESTED_STACK_SFN_LIST)
def test_sfn_logs(self, state_machine_name: str):
self._test_sfn_logs(state_machine_name)
@parameterized.expand(itertools.product(NESTED_STACK_APIGW_LIST, ["hello"]))
def test_end_to_end_apigw(self, apigw_name: str, path: str):
self._test_end_to_end_apigw(apigw_name, path)
@parameterized.expand(itertools.product(NESTED_STACK_APIGW_LIST, ["world"]))
def test_end_to_end_sfn(self, apigw_name: str, path: str):
self._test_end_to_end_sfn(apigw_name, path)
@parameterized.expand(itertools.product(NESTED_STACK_FUNCTION_LIST, [None, "text", "json"]))
def test_output(self, function_name: str, output: Optional[str]):
self._test_output(function_name, output)
@parameterized.expand(
itertools.product(
NESTED_STACK_FUNCTION_LIST,
[
(None, None, True),
(None, "1 minute", True),
("1 minute", None, True),
("now", None, False),
],
)
)
def test_start_end(self, function_name: str, start_end_time_params: Tuple[Optional[str], Optional[str], bool]):
self._test_start_end(function_name, start_end_time_params)
@parameterized.expand(NESTED_STACK_FUNCTION_LIST)
def test_include_traces(self, function_name: str):
self._test_include_traces(function_name)