Kaydet (Commit) 0284eada authored tarafından Daniel Nephin's avatar Daniel Nephin

Merge pull request #805 from sourcelair/enhancement/stats-no-stream

Add support for non-stream stats of containers
root = true
[*]
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 80
[*.md]
trim_trailing_whitespace = false
......@@ -356,9 +356,14 @@ class ContainerApiMixin(object):
@utils.minimum_version('1.17')
@utils.check_resource
def stats(self, container, decode=None):
def stats(self, container, decode=None, stream=True):
url = self._url("/containers/{0}/stats", container)
return self._stream_helper(self._get(url, stream=True), decode=decode)
if stream:
return self._stream_helper(self._get(url, stream=True),
decode=decode)
else:
return self._result(self._get(url, params={'stream': False}),
json=True)
@utils.check_resource
def stop(self, container, timeout=10):
......
......@@ -874,6 +874,8 @@ This will stream statistics for a specific container.
* container (str): The container to stream statistics for
* decode (bool): If set to true, stream will be decoded into dicts on the
fly. False by default.
* stream (bool): If set to false, only the current stats will be returned
instead of a stream. True by default.
```python
>>> from docker import Client
......
......@@ -1040,3 +1040,34 @@ class PauseTest(api_test.BaseTestCase):
self.assertEqual(state['Running'], True)
self.assertIn('Paused', state)
self.assertEqual(state['Paused'], False)
class GetContainerStatsTest(api_test.BaseTestCase):
@requires_api_version('1.19')
def test_get_container_stats_no_stream(self):
container = self.client.create_container(
BUSYBOX, ['sleep', '60'],
)
self.tmp_containers.append(container)
self.client.start(container)
response = self.client.stats(container, stream=0)
self.client.kill(container)
self.assertEqual(type(response), dict)
for key in ['read', 'network', 'precpu_stats', 'cpu_stats',
'memory_stats', 'blkio_stats']:
self.assertIn(key, response)
@requires_api_version('1.17')
def test_get_container_stats_stream(self):
container = self.client.create_container(
BUSYBOX, ['sleep', '60'],
)
self.tmp_containers.append(container)
self.client.start(container)
stream = self.client.stats(container)
for chunk in stream:
self.assertEqual(type(chunk), dict)
for key in ['read', 'network', 'precpu_stats', 'cpu_stats',
'memory_stats', 'blkio_stats']:
self.assertIn(key, chunk)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment