summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRhiza <6900588+ArchiMoebius@users.noreply.github.com>2022-08-19 15:10:53 -0400
committerGitHub <noreply@github.com>2022-08-19 15:10:53 -0400
commit923e067dddc3d4b86e4e620a99fcdcdafbd17a98 (patch)
treec26760d6bbc253101f73da7f8f69564fdcd06eb6
parent1c27ec1f0c34f6b9510f5caadada5fd8ecc430d9 (diff)
downloaddocker-py-923e067dddc3d4b86e4e620a99fcdcdafbd17a98.tar.gz
api: add support for floats to docker logs params since / until (#3031)
Add support for floats to docker logs params `since` / `until` since the Docker Engine APIs support it. This allows using fractional seconds for greater precision. Signed-off-by: Archi Moebius <poerhiz@gmail.com>
-rw-r--r--docker/api/container.py17
-rw-r--r--docker/models/containers.py9
-rw-r--r--tests/unit/api_container_test.py18
3 files changed, 33 insertions, 11 deletions
diff --git a/docker/api/container.py b/docker/api/container.py
index f600be1..ce48371 100644
--- a/docker/api/container.py
+++ b/docker/api/container.py
@@ -826,11 +826,12 @@ class ContainerApiMixin:
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 fractional seconds)
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 fractional seconds)
Returns:
(generator or str)
@@ -855,9 +856,11 @@ class ContainerApiMixin:
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, '
+ 'since value should be datetime or positive int/float, '
'not {}'.format(type(since))
)
@@ -870,9 +873,11 @@ class ContainerApiMixin:
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, '
+ 'until value should be datetime or positive int/float, '
'not {}'.format(type(until))
)
diff --git a/docker/models/containers.py b/docker/models/containers.py
index 6661b21..4508557 100644
--- a/docker/models/containers.py
+++ b/docker/models/containers.py
@@ -290,11 +290,12 @@ class Container(Model):
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.
diff --git a/tests/unit/api_container_test.py b/tests/unit/api_container_test.py
index 8f120f4..d7b356c 100644
--- a/tests/unit/api_container_test.py
+++ b/tests/unit/api_container_test.py
@@ -1279,6 +1279,22 @@ class ContainerTest(BaseAPIClientTest):
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)
@@ -1301,7 +1317,7 @@ class ContainerTest(BaseAPIClientTest):
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()