-
Notifications
You must be signed in to change notification settings - Fork 17.2k
Fix KeyError when log event is missing 'level' key in TriggerRunner #67719
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1051,6 +1051,48 @@ async def asend_side_effect(msg): | |
| assert len(first_call.events) == 3 | ||
| assert len(second_call.events) == 2 | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "payload", | ||
| [ | ||
| pytest.param( | ||
| {"event": "hello"}, | ||
| id="missing-level-key", | ||
| ), | ||
| pytest.param( | ||
| {"event": "hello", "level": "bogus_unknown_level"}, | ||
| id="unknown-level-value", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_process_log_messages_tolerates_bad_level(self, payload): | ||
| """Log events with missing or unknown 'level' must not raise KeyError.""" | ||
| import json | ||
|
|
||
| trigger_runner = TriggerRunner() | ||
| gen = trigger_runner._process_log_messages_from_subprocess() | ||
| next(gen) # advance to yield | ||
|
|
||
| # Must not raise KeyError (the bug this test guards against) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this case only checks if exception is not raised. Missing log lines is still silently ignored
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed test now mocks structlog.get_logger and asserts mock_log.log.assert_called_once() |
||
| gen.send(json.dumps(payload).encode()) | ||
|
|
||
| def test_process_log_messages_valid_event_processed(self): | ||
| """Log events with a valid 'level' key are passed to the logger.""" | ||
| import json | ||
| from unittest.mock import patch | ||
|
|
||
| trigger_runner = TriggerRunner() | ||
| mock_log = MagicMock() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like the import is missing here.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| with patch("structlog.get_logger", return_value=mock_log): | ||
| gen = trigger_runner._process_log_messages_from_subprocess() | ||
| next(gen) | ||
| gen.send(json.dumps({"event": "test message", "level": "info"}).encode()) | ||
|
|
||
| mock_log.log.assert_called_once() | ||
| args = mock_log.log.call_args | ||
| # Positional args: (numeric_level, event_message) | ||
| assert args[0][0] == 20 # INFO level | ||
| assert args[0][1] == "test message" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.usefixtures("testing_dag_bundle") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worth adding assertions for log level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed added assert mock_log.log.call_args[0][0] == logging.WARNING