Skip to content

Commit

Permalink
feat: add support for floats to docker logs params since / until sinc…
Browse files Browse the repository at this point in the history
…e the docker engine supports it
  • Loading branch information
ArchiMoebius committed Aug 17, 2022
1 parent e901eac commit 7c1e4cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
13 changes: 9 additions & 4 deletions docker/api/container.py
Expand Up @@ -826,11 +826,12 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
tail (str or int): Output specified number of lines at the end of
logs. Either an integer of number of lines or the string
``all``. Default ``all``
since (datetime or int): Show logs since a given datetime or
integer epoch (in seconds)
since (datetime, int, or float): Show logs since a given datetime,
integer epoch (in seconds) or float (in nanoseconds)
follow (bool): Follow log output. Default ``False``
until (datetime or int): Show logs that occurred before the given
datetime or integer epoch (in seconds)
until (datetime, int, or float): Show logs that occurred before
the given datetime, integer epoch (in seconds), or
float (in nanoseconds)
Returns:
(generator or str)
Expand All @@ -855,6 +856,8 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
params['since'] = utils.datetime_to_timestamp(since)
elif (isinstance(since, int) and since > 0):
params['since'] = since
elif (isinstance(since, float) and since > 0.0):
params['since'] = since
else:
raise errors.InvalidArgument(
'since value should be datetime or positive int, '
Expand All @@ -870,6 +873,8 @@ def logs(self, container, stdout=True, stderr=True, stream=False,
params['until'] = utils.datetime_to_timestamp(until)
elif (isinstance(until, int) and until > 0):
params['until'] = until
elif (isinstance(until, float) and until > 0.0):
params['until'] = until
else:
raise errors.InvalidArgument(
'until value should be datetime or positive int, '
Expand Down
9 changes: 5 additions & 4 deletions docker/models/containers.py
Expand Up @@ -290,11 +290,12 @@ def logs(self, **kwargs):
tail (str or int): Output specified number of lines at the end of
logs. Either an integer of number of lines or the string
``all``. Default ``all``
since (datetime or int): Show logs since a given datetime or
integer epoch (in seconds)
since (datetime, int, or float): Show logs since a given datetime,
integer epoch (in seconds) or float (in nanoseconds)
follow (bool): Follow log output. Default ``False``
until (datetime or int): Show logs that occurred before the given
datetime or integer epoch (in seconds)
until (datetime, int, or float): Show logs that occurred before
the given datetime, integer epoch (in seconds), or
float (in nanoseconds)
Returns:
(generator or str): Logs from the container.
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/api_container_test.py
Expand Up @@ -1279,6 +1279,22 @@ def test_log_since(self):
stream=False
)

def test_log_since_with_float(self):
ts = 809222400.000000
with mock.patch('docker.api.client.APIClient.inspect_container',
fake_inspect_container):
self.client.logs(fake_api.FAKE_CONTAINER_ID, stream=False,
follow=False, since=ts)

fake_request.assert_called_with(
'GET',
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/logs',
params={'timestamps': 0, 'follow': 0, 'stderr': 1, 'stdout': 1,
'tail': 'all', 'since': ts},
timeout=DEFAULT_TIMEOUT_SECONDS,
stream=False
)

def test_log_since_with_datetime(self):
ts = 809222400
time = datetime.datetime.utcfromtimestamp(ts)
Expand All @@ -1301,7 +1317,7 @@ def test_log_since_with_invalid_value_raises_error(self):
fake_inspect_container):
with pytest.raises(docker.errors.InvalidArgument):
self.client.logs(fake_api.FAKE_CONTAINER_ID, stream=False,
follow=False, since=42.42)
follow=False, since="42.42")

def test_log_tty(self):
m = mock.Mock()
Expand Down

0 comments on commit 7c1e4cc

Please sign in to comment.