Skip to content

Commit

Permalink
DockerOperator fix cli.logs giving character array instead of string (
Browse files Browse the repository at this point in the history
  • Loading branch information
pasalkarsachin1 authored Jul 5, 2022
1 parent 6fd06fa commit cc6a44b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 21 deletions.
13 changes: 4 additions & 9 deletions airflow/providers/docker/operators/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,13 @@ def _run_image_with_mounts(
if self.retrieve_output:
return self._attempt_to_retrieve_result()
elif self.do_xcom_push:
log_parameters = {
'container': self.container['Id'],
'stdout': True,
'stderr': True,
'stream': True,
}
if len(log_lines) == 0:
return None
try:
if self.xcom_all:
return [stringify(line).strip() for line in self.cli.logs(**log_parameters)]
return log_lines
else:
lines = [stringify(line).strip() for line in self.cli.logs(**log_parameters, tail=1)]
return lines[-1] if lines else None
return log_lines[-1]
except StopIteration:
# handle the case when there is not a single line to iterate on
return None
Expand Down
13 changes: 1 addition & 12 deletions tests/providers/docker/operators/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ def test_execute(self):
self.client_mock.attach.assert_called_once_with(
container='some_id', stdout=True, stderr=True, stream=True
)
self.client_mock.logs.assert_called_once_with(
container='some_id', stdout=True, stderr=True, stream=True, tail=1
)
self.client_mock.pull.assert_called_once_with('ubuntu:latest', stream=True, decode=True)
self.client_mock.wait.assert_called_once_with('some_id')
assert (
Expand Down Expand Up @@ -195,9 +192,6 @@ def test_execute_no_temp_dir(self):
self.client_mock.attach.assert_called_once_with(
container='some_id', stdout=True, stderr=True, stream=True
)
self.client_mock.logs.assert_called_once_with(
container='some_id', stdout=True, stderr=True, stream=True, tail=1
)
self.client_mock.pull.assert_called_once_with('ubuntu:latest', stream=True, decode=True)
self.client_mock.wait.assert_called_once_with('some_id')
assert (
Expand Down Expand Up @@ -304,9 +298,6 @@ def test_execute_fallback_temp_dir(self):
self.client_mock.attach.assert_called_once_with(
container='some_id', stdout=True, stderr=True, stream=True
)
self.client_mock.logs.assert_called_once_with(
container='some_id', stdout=True, stderr=True, stream=True, tail=1
)
self.client_mock.pull.assert_called_once_with('ubuntu:latest', stream=True, decode=True)
self.client_mock.wait.assert_called_once_with('some_id')
assert (
Expand Down Expand Up @@ -471,7 +462,7 @@ def test_execute_xcom_behavior_bytes(self):
self.client_mock.pull.return_value = [b'{"status":"pull log"}']
self.client_mock.attach.return_value = iter([b'container log 1 ', b'container log 2'])
# Make sure the logs side effect is updated after the change
self.client_mock.logs.side_effect = (
self.client_mock.attach.side_effect = (
lambda **kwargs: iter(self.log_messages[-kwargs['tail'] :])
if 'tail' in kwargs
else iter(self.log_messages)
Expand Down Expand Up @@ -511,8 +502,6 @@ def test_execute_xcom_behavior_no_result(self):
self.log_messages = []
self.client_mock.pull.return_value = [b'{"status":"pull log"}']
self.client_mock.attach.return_value = iter([])
# Make sure the logs side effect is updated after the change
self.client_mock.logs.side_effect = iter([])

kwargs = {
'api_version': '1.19',
Expand Down

0 comments on commit cc6a44b

Please sign in to comment.