Skip to content

fix(tests): make logs fetching more robust #1878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/e2e/logger/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_basic_lambda_logs_visible(basic_handler_fn, basic_handler_fn_arn):
data_fetcher.get_lambda_response(lambda_arn=basic_handler_fn_arn, payload=payload)

# THEN
logs = data_fetcher.get_logs(function_name=basic_handler_fn, start_time=execution_time)
logs = data_fetcher.get_logs(function_name=basic_handler_fn, start_time=execution_time, expected_number_of_logs=2)

assert len(logs) == 2
assert len(logs.get_cold_start_log()) == 1
Expand Down
10 changes: 9 additions & 1 deletion tests/e2e/utils/data_fetcher/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __len__(self) -> int:
def get_logs(
function_name: str,
start_time: datetime,
expected_number_of_logs: Optional[int],
filter_expression: Optional[str] = None,
log_client: Optional[CloudWatchLogsClient] = None,
) -> LogFetcher:
Expand All @@ -133,6 +134,8 @@ def get_logs(
Name of Lambda function to fetch logs for
start_time : datetime
Start date range to filter traces
expected_number_of_logs : Optional[int]
Retry fetching logs until this number of log lines are obtained
log_client : Optional[CloudWatchLogsClient], optional
Amazon CloudWatch Logs Client, by default boto3.client('logs)
filter_expression : Optional[str], optional
Expand All @@ -143,6 +146,11 @@ def get_logs(
LogFetcher
LogFetcher instance with logs available as properties and methods
"""
return LogFetcher(
log_fetcher = LogFetcher(
function_name=function_name, start_time=start_time, filter_expression=filter_expression, log_client=log_client
)

if expected_number_of_logs is not None and len(log_fetcher) < expected_number_of_logs:
raise ValueError(f"expected {expected_number_of_logs} logs but only got ${len(log_fetcher)}")

return log_fetcher