Skip to content

Commit 52702f7

Browse files
committed
removed aggregation for now and added a unit test. also using stream_helper now
1 parent f402af5 commit 52702f7

File tree

4 files changed

+176
-19
lines changed

4 files changed

+176
-19
lines changed

docker/client.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -966,28 +966,11 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
966966
res = self._post_json(url, data=start_config)
967967
self._raise_for_status(res)
968968

969-
def stats(self, container, aggregate=0):
969+
def stats(self, container):
970970
if isinstance(container, dict):
971971
container = container.get('Id')
972972
url = self._url("/containers/{0}/stats".format(container))
973-
res = self._get(url, stream=True)
974-
self._raise_for_status(res)
975-
976-
stats_stream = res.iter_lines()
977-
stats_array = []
978-
stats_aggregated = 0
979-
980-
if aggregate:
981-
for stat_obj in stats_stream:
982-
if stats_aggregated >= aggregate:
983-
stats_aggregated = 0
984-
yield stats_array
985-
stats_array = []
986-
stats_array.append(stat_obj)
987-
stats_aggregated += 1
988-
else:
989-
for stat_obj in stats_stream:
990-
yield stat_obj
973+
return self._stream_helper(self._get(url, stream=True))
991974

992975
def stop(self, container, timeout=10):
993976
if isinstance(container, dict):

tests/fake_api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import fake_stat
16+
1517
CURRENT_VERSION = 'v1.17'
1618

1719
FAKE_CONTAINER_ID = '3cc2351ab11b'
@@ -330,6 +332,11 @@ def post_fake_tag_image():
330332
return status_code, response
331333

332334

335+
def get_fake_stats():
336+
status_code = 200
337+
response = fake_stat.OBJ
338+
return status_code, response
339+
333340
# Maps real api url to fake response callback
334341
prefix = 'http+unix://var/run/docker.sock'
335342
fake_responses = {
@@ -383,6 +390,8 @@ def post_fake_tag_image():
383390
post_fake_restart_container,
384391
'{1}/{0}/containers/3cc2351ab11b'.format(CURRENT_VERSION, prefix):
385392
delete_fake_remove_container,
393+
'{1}/{0}/containers/3cc2351ab11b/stats'.format(CURRENT_VERSION, prefix):
394+
get_fake_stats,
386395
'{1}/{0}/images/create'.format(CURRENT_VERSION, prefix):
387396
post_fake_image_create,
388397
'{1}/{0}/images/e9aa60c60128'.format(CURRENT_VERSION, prefix):

tests/fake_stat.py

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
OBJ = {
2+
"read": "2015-02-11T19:20:46.667237763+02:00",
3+
"network": {
4+
"rx_bytes": 567224,
5+
"rx_packets": 3773,
6+
"rx_errors": 0,
7+
"rx_dropped": 0,
8+
"tx_bytes": 1176,
9+
"tx_packets": 13,
10+
"tx_errors": 0,
11+
"tx_dropped": 0
12+
},
13+
"cpu_stats": {
14+
"cpu_usage": {
15+
"total_usage": 157260874053,
16+
"percpu_usage": [
17+
52196306950,
18+
24118413549,
19+
53292684398,
20+
27653469156
21+
],
22+
"usage_in_kernelmode": 37140000000,
23+
"usage_in_usermode": 62140000000
24+
},
25+
"system_cpu_usage": 3.0881377e+14,
26+
"throttling_data": {
27+
"periods": 0,
28+
"throttled_periods": 0,
29+
"throttled_time": 0
30+
}
31+
},
32+
"memory_stats": {
33+
"usage": 179314688,
34+
"max_usage": 258166784,
35+
"stats": {
36+
"active_anon": 90804224,
37+
"active_file": 2195456,
38+
"cache": 3096576,
39+
"hierarchical_memory_limit": 1.844674407371e+19,
40+
"inactive_anon": 85516288,
41+
"inactive_file": 798720,
42+
"mapped_file": 2646016,
43+
"pgfault": 101034,
44+
"pgmajfault": 1207,
45+
"pgpgin": 115814,
46+
"pgpgout": 75613,
47+
"rss": 176218112,
48+
"rss_huge": 12582912,
49+
"total_active_anon": 90804224,
50+
"total_active_file": 2195456,
51+
"total_cache": 3096576,
52+
"total_inactive_anon": 85516288,
53+
"total_inactive_file": 798720,
54+
"total_mapped_file": 2646016,
55+
"total_pgfault": 101034,
56+
"total_pgmajfault": 1207,
57+
"total_pgpgin": 115814,
58+
"total_pgpgout": 75613,
59+
"total_rss": 176218112,
60+
"total_rss_huge": 12582912,
61+
"total_unevictable": 0,
62+
"total_writeback": 0,
63+
"unevictable": 0,
64+
"writeback": 0
65+
},
66+
"failcnt": 0,
67+
"limit": 8039038976
68+
},
69+
"blkio_stats": {
70+
"io_service_bytes_recursive": [
71+
{
72+
"major": 8,
73+
"minor": 0,
74+
"op": "Read",
75+
"value": 72843264
76+
},
77+
{
78+
"major": 8,
79+
"minor": 0,
80+
"op": "Write",
81+
"value": 4096
82+
},
83+
{
84+
"major": 8,
85+
"minor": 0,
86+
"op": "Sync",
87+
"value": 4096
88+
},
89+
{
90+
"major": 8,
91+
"minor": 0,
92+
"op": "Async",
93+
"value": 72843264
94+
},
95+
{
96+
"major": 8,
97+
"minor": 0,
98+
"op": "Total",
99+
"value": 72847360
100+
}
101+
],
102+
"io_serviced_recursive": [
103+
{
104+
"major": 8,
105+
"minor": 0,
106+
"op": "Read",
107+
"value": 10581
108+
},
109+
{
110+
"major": 8,
111+
"minor": 0,
112+
"op": "Write",
113+
"value": 1
114+
},
115+
{
116+
"major": 8,
117+
"minor": 0,
118+
"op": "Sync",
119+
"value": 1
120+
},
121+
{
122+
"major": 8,
123+
"minor": 0,
124+
"op": "Async",
125+
"value": 10581
126+
},
127+
{
128+
"major": 8,
129+
"minor": 0,
130+
"op": "Total",
131+
"value": 10582
132+
}
133+
],
134+
"io_queue_recursive": [
135+
136+
],
137+
"io_service_time_recursive": [
138+
139+
],
140+
"io_wait_time_recursive": [
141+
142+
],
143+
"io_merged_recursive": [
144+
145+
],
146+
"io_time_recursive": [
147+
148+
],
149+
"sectors_recursive": [
150+
151+
]
152+
}
153+
}

tests/test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,18 @@ def test_inspect_container(self):
16211621
timeout=docker.client.DEFAULT_TIMEOUT_SECONDS
16221622
)
16231623

1624+
def test_container_stats(self):
1625+
try:
1626+
self.client.stats(fake_api.FAKE_CONTAINER_ID)
1627+
except Exception as e:
1628+
self.fail('Command should not raise exception: {0}'.format(e))
1629+
1630+
fake_request.assert_called_with(
1631+
url_prefix + 'containers/3cc2351ab11b/stats',
1632+
timeout=60,
1633+
stream=True
1634+
)
1635+
16241636
##################
16251637
# IMAGES TESTS #
16261638
##################

0 commit comments

Comments
 (0)