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

Add support for non-stream stats of containers
This commit is contained in:
Daniel Nephin 2015-11-17 10:47:28 -05:00
commit 0284eadaff
4 changed files with 51 additions and 2 deletions

11
.editorconfig Normal file
View File

@ -0,0 +1,11 @@
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

View File

@ -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):

View File

@ -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

View File

@ -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)