Skip to content

Commit 338698b

Browse files
committed
feat: move logger to new infrastructure
1 parent 138bfc9 commit 338698b

File tree

4 files changed

+55
-132
lines changed

4 files changed

+55
-132
lines changed

tests/e2e/logger/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
3+
from tests.e2e.logger.infrastructure import LoggerStack
4+
from tests.e2e.utils.infrastructure import deploy_once
5+
6+
7+
@pytest.fixture(autouse=True, scope="module")
8+
def infrastructure(request: pytest.FixtureRequest, tmp_path_factory: pytest.TempPathFactory, worker_id: str):
9+
"""Setup and teardown logic for E2E test infrastructure
10+
11+
Parameters
12+
----------
13+
request : pytest.FixtureRequest
14+
pytest request fixture to introspect absolute path to test being executed
15+
tmp_path_factory : pytest.TempPathFactory
16+
pytest temporary path factory to discover shared tmp when multiple CPU processes are spun up
17+
worker_id : str
18+
pytest-xdist worker identification to detect whether parallelization is enabled
19+
20+
Yields
21+
------
22+
Dict[str, str]
23+
CloudFormation Outputs from deployed infrastructure
24+
"""
25+
yield from deploy_once(stack=LoggerStack, request=request, tmp_path_factory=tmp_path_factory, worker_id=worker_id)

tests/e2e/logger/handlers/no_context_handler.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/e2e/logger/infrastructure.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from pathlib import Path
2+
3+
from tests.e2e.utils.infrastructure import BaseInfrastructureV2
4+
5+
6+
class LoggerStack(BaseInfrastructureV2):
7+
LOG_MESSAGE: str = "logger message test"
8+
LOG_LEVEL: str = "INFO"
9+
10+
def __init__(self, handlers_dir: Path, feature_name: str = "logger") -> None:
11+
super().__init__(feature_name, handlers_dir)
12+
13+
def create_resources(self):
14+
env_vars = {
15+
"MESSAGE": self.LOG_MESSAGE,
16+
"LOG_LEVEL": self.LOG_LEVEL,
17+
"ADDITIONAL_KEY": "extra_info",
18+
}
19+
self.create_lambda_functions(function_props={"environment": env_vars})

tests/e2e/logger/test_logger.py

Lines changed: 11 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,22 @@
11
import boto3
22
import pytest
3-
from e2e import conftest
43

54
from tests.e2e.utils import data_fetcher
65

76

8-
@pytest.fixture(scope="module")
9-
def config() -> conftest.LambdaConfig:
10-
return {
11-
"parameters": {},
12-
"environment_variables": {
13-
"MESSAGE": "logger message test",
14-
"LOG_LEVEL": "INFO",
15-
"ADDITIONAL_KEY": "extra_info",
16-
},
17-
}
7+
@pytest.fixture
8+
def basic_handler_fn(infrastructure: dict) -> str:
9+
return infrastructure.get("BasicHandler", "")
1810

1911

20-
def test_basic_lambda_logs_visible(execute_lambda: conftest.InfrastructureOutput, config: conftest.LambdaConfig):
21-
# GIVEN
22-
lambda_name = execute_lambda.get_lambda_function_name(cf_output_name="basichandlerarn")
23-
timestamp = execute_lambda.get_lambda_execution_time_timestamp()
24-
cw_client = boto3.client("logs")
25-
26-
# WHEN
27-
filtered_logs = data_fetcher.get_logs(lambda_function_name=lambda_name, start_time=timestamp, log_client=cw_client)
12+
@pytest.fixture
13+
def basic_handler_fn_arn(infrastructure: dict) -> str:
14+
return infrastructure.get("BasicHandlerArn", "")
2815

29-
# THEN
30-
assert any(
31-
log.message == config["environment_variables"]["MESSAGE"]
32-
and log.level == config["environment_variables"]["LOG_LEVEL"]
33-
for log in filtered_logs
34-
)
3516

36-
37-
def test_basic_lambda_no_debug_logs_visible(
38-
execute_lambda: conftest.InfrastructureOutput, config: conftest.LambdaConfig
39-
):
17+
def test_basic_lambda_logs_visible(basic_handler_fn, basic_handler_fn_arn):
4018
# GIVEN
41-
lambda_name = execute_lambda.get_lambda_function_name(cf_output_name="basichandlerarn")
42-
timestamp = execute_lambda.get_lambda_execution_time_timestamp()
4319
cw_client = boto3.client("logs")
44-
45-
# WHEN
46-
filtered_logs = data_fetcher.get_logs(lambda_function_name=lambda_name, start_time=timestamp, log_client=cw_client)
47-
48-
# THEN
49-
assert not any(
50-
log.message == config["environment_variables"]["MESSAGE"] and log.level == "DEBUG" for log in filtered_logs
51-
)
52-
53-
54-
def test_basic_lambda_contextual_data_logged(execute_lambda: conftest.InfrastructureOutput):
55-
# GIVEN
5620
required_keys = (
5721
"xray_trace_id",
5822
"function_request_id",
@@ -62,82 +26,11 @@ def test_basic_lambda_contextual_data_logged(execute_lambda: conftest.Infrastruc
6226
"cold_start",
6327
)
6428

65-
lambda_name = execute_lambda.get_lambda_function_name(cf_output_name="basichandlerarn")
66-
timestamp = execute_lambda.get_lambda_execution_time_timestamp()
67-
cw_client = boto3.client("logs")
68-
6929
# WHEN
70-
filtered_logs = data_fetcher.get_logs(lambda_function_name=lambda_name, start_time=timestamp, log_client=cw_client)
71-
72-
# THEN
73-
assert all(keys in logs.dict(exclude_unset=True) for logs in filtered_logs for keys in required_keys)
74-
75-
76-
def test_basic_lambda_additional_key_persistence_basic_lambda(
77-
execute_lambda: conftest.InfrastructureOutput, config: conftest.LambdaConfig
78-
):
79-
# GIVEN
80-
lambda_name = execute_lambda.get_lambda_function_name(cf_output_name="basichandlerarn")
81-
timestamp = execute_lambda.get_lambda_execution_time_timestamp()
82-
cw_client = boto3.client("logs")
83-
84-
# WHEN
85-
filtered_logs = data_fetcher.get_logs(lambda_function_name=lambda_name, start_time=timestamp, log_client=cw_client)
86-
87-
# THEN
88-
assert any(
89-
log.extra_info
90-
and log.message == config["environment_variables"]["MESSAGE"]
91-
and log.level == config["environment_variables"]["LOG_LEVEL"]
92-
for log in filtered_logs
30+
_, execution_time = data_fetcher.get_lambda_response(lambda_arn=basic_handler_fn_arn)
31+
filtered_logs = data_fetcher.get_logs(
32+
lambda_function_name=basic_handler_fn, start_time=int(execution_time.timestamp()), log_client=cw_client
9333
)
9434

95-
96-
def test_basic_lambda_empty_event_logged(execute_lambda: conftest.InfrastructureOutput):
97-
98-
# GIVEN
99-
lambda_name = execute_lambda.get_lambda_function_name(cf_output_name="basichandlerarn")
100-
timestamp = execute_lambda.get_lambda_execution_time_timestamp()
101-
cw_client = boto3.client("logs")
102-
103-
# WHEN
104-
filtered_logs = data_fetcher.get_logs(lambda_function_name=lambda_name, start_time=timestamp, log_client=cw_client)
105-
106-
# THEN
107-
assert any(log.message == {} for log in filtered_logs)
108-
109-
110-
def test_no_context_lambda_contextual_data_not_logged(execute_lambda: conftest.InfrastructureOutput):
111-
112-
# GIVEN
113-
required_missing_keys = (
114-
"function_request_id",
115-
"function_arn",
116-
"function_memory_size",
117-
"function_name",
118-
"cold_start",
119-
)
120-
121-
lambda_name = execute_lambda.get_lambda_function_name(cf_output_name="nocontexthandlerarn")
122-
timestamp = execute_lambda.get_lambda_execution_time_timestamp()
123-
cw_client = boto3.client("logs")
124-
125-
# WHEN
126-
filtered_logs = data_fetcher.get_logs(lambda_function_name=lambda_name, start_time=timestamp, log_client=cw_client)
127-
12835
# THEN
129-
assert not any(keys in logs.dict(exclude_unset=True) for logs in filtered_logs for keys in required_missing_keys)
130-
131-
132-
def test_no_context_lambda_event_not_logged(execute_lambda: conftest.InfrastructureOutput):
133-
134-
# GIVEN
135-
lambda_name = execute_lambda.get_lambda_function_name(cf_output_name="nocontexthandlerarn")
136-
timestamp = execute_lambda.get_lambda_execution_time_timestamp()
137-
cw_client = boto3.client("logs")
138-
139-
# WHEN
140-
filtered_logs = data_fetcher.get_logs(lambda_function_name=lambda_name, start_time=timestamp, log_client=cw_client)
141-
142-
# THEN
143-
assert not any(log.message == {} for log in filtered_logs)
36+
assert all(keys in logs.dict(exclude_unset=True) for logs in filtered_logs for keys in required_keys)

0 commit comments

Comments
 (0)