Skip to content
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

ECSOperator: fix KeyError on missing exitCode #20264

Merged
merged 3 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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 airflow/providers/amazon/aws/operators/ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def _check_success_task(self) -> None:
)
containers = task['containers']
for container in containers:
if container.get('lastStatus') == 'STOPPED' and container['exitCode'] != 0:
if container.get('lastStatus') == 'STOPPED' and container.get('exitCode', 1) != 0:
if self.task_log_fetcher:
last_logs = "\n".join(
self.task_log_fetcher.get_last_log_messages(self.number_logs_exception)
Expand Down
20 changes: 20 additions & 0 deletions tests/providers/amazon/aws/operators/test_ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,26 @@ def test_check_success_tasks_raises_logs_disabled(self):
assert "'exitCode': 1" in str(ctx.value)
client_mock.describe_tasks.assert_called_once_with(cluster='c', tasks=['arn'])

def test_check_success_tasks_handles_initialization_failure(self):
client_mock = mock.Mock()
self.ecs.arn = 'arn'
self.ecs.client = client_mock

# exitCode is missing during some container initialization failures
client_mock.describe_tasks.return_value = {
'tasks': [{'containers': [{'name': 'foo', 'lastStatus': 'STOPPED'}]}]
}

with pytest.raises(Exception) as ctx:
self.ecs._check_success_task()

print(str(ctx.value))
assert "This task is not in success state " in str(ctx.value)
assert "'name': 'foo'" in str(ctx.value)
assert "'lastStatus': 'STOPPED'" in str(ctx.value)
assert "exitCode" not in str(ctx.value)
client_mock.describe_tasks.assert_called_once_with(cluster='c', tasks=['arn'])

def test_check_success_tasks_raises_pending(self):
client_mock = mock.Mock()
self.ecs.client = client_mock
Expand Down