summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Roxby <107427605+aroxby-wayscript@users.noreply.github.com>2023-02-16 10:27:45 -0500
committerGitHub <noreply@github.com>2023-02-16 10:27:45 -0500
commite9d4ddfaece229fbd2df1a1bfa3f4f513f33d1ac (patch)
tree50e889f87381d7b887d69e84f79e57d42eb83eba
parentaca129dd69fa068da247d37266110c3a9889a780 (diff)
downloaddocker-py-e9d4ddfaece229fbd2df1a1bfa3f4f513f33d1ac.tar.gz
api: add `one-shot` option to container `stats` (#3089)
Signed-off-by: Andy Roxby <107427605+aroxby-wayscript@users.noreply.github.com>
-rw-r--r--docker/api/container.py24
-rw-r--r--tests/unit/api_container_test.py13
2 files changed, 32 insertions, 5 deletions
diff --git a/docker/api/container.py b/docker/api/container.py
index ce48371..82a8922 100644
--- a/docker/api/container.py
+++ b/docker/api/container.py
@@ -1126,7 +1126,7 @@ class ContainerApiMixin:
self._raise_for_status(res)
@utils.check_resource('container')
- def stats(self, container, decode=None, stream=True):
+ def stats(self, container, decode=None, stream=True, one_shot=None):
"""
Stream statistics for a specific container. Similar to the
``docker stats`` command.
@@ -1138,6 +1138,9 @@ class ContainerApiMixin:
False by default.
stream (bool): If set to false, only the current stats will be
returned instead of a stream. True by default.
+ one_shot (bool): If set to true, Only get a single stat instead of
+ waiting for 2 cycles. Must be used with stream=false. False by
+ default.
Raises:
:py:class:`docker.errors.APIError`
@@ -1145,16 +1148,29 @@ class ContainerApiMixin:
"""
url = self._url("/containers/{0}/stats", container)
+ params = {
+ 'stream': stream
+ }
+ if one_shot is not None:
+ if utils.version_lt(self._version, '1.41'):
+ raise errors.InvalidVersion(
+ 'one_shot is not supported for API version < 1.41'
+ )
+ params['one-shot'] = one_shot
if stream:
- return self._stream_helper(self._get(url, stream=True),
+ if one_shot:
+ raise errors.InvalidArgument(
+ 'one_shot is only available in conjunction with '
+ 'stream=False'
+ )
+ return self._stream_helper(self._get(url, params=params),
decode=decode)
else:
if decode:
raise errors.InvalidArgument(
"decode is only available in conjunction with stream=True"
)
- return self._result(self._get(url, params={'stream': False}),
- json=True)
+ return self._result(self._get(url, params=params), json=True)
@utils.check_resource('container')
def stop(self, container, timeout=None):
diff --git a/tests/unit/api_container_test.py b/tests/unit/api_container_test.py
index d7b356c..c605da3 100644
--- a/tests/unit/api_container_test.py
+++ b/tests/unit/api_container_test.py
@@ -1529,7 +1529,18 @@ class ContainerTest(BaseAPIClientTest):
'GET',
url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stats',
timeout=60,
- stream=True
+ params={'stream': True}
+ )
+
+ def test_container_stats_with_one_shot(self):
+ self.client.stats(
+ fake_api.FAKE_CONTAINER_ID, stream=False, one_shot=True)
+
+ fake_request.assert_called_with(
+ 'GET',
+ url_prefix + 'containers/' + fake_api.FAKE_CONTAINER_ID + '/stats',
+ timeout=60,
+ params={'stream': False, 'one-shot': True}
)
def test_container_top(self):