Skip to content

Commit 7884ab9

Browse files
committed
Merge pull request #789 from PierreF/top_ps_args
Allow to specify ps_args when listing processes
2 parents f51ae4b + c157760 commit 7884ab9

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
lines changed

docker/api/container.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,12 @@ def stop(self, container, timeout=10):
328328
self._raise_for_status(res)
329329

330330
@utils.check_resource
331-
def top(self, container):
331+
def top(self, container, ps_args=None):
332332
u = self._url("/containers/{0}/top", container)
333-
return self._result(self._get(u), True)
333+
params = {}
334+
if ps_args is not None:
335+
params['ps_args'] = ps_args
336+
return self._result(self._get(u, params=params), True)
334337

335338
@utils.check_resource
336339
def unpause(self, container):

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ Display the running processes of a container.
878878
**Params**:
879879

880880
* container (str): The container to inspect
881+
* ps_args (str): An optional arguments passed to ps (e.g., aux)
881882

882883
**Returns** (str): The output of the top
883884

tests/fake_api.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,33 @@ def get_fake_stats():
383383
return status_code, response
384384

385385

386+
def get_fake_top():
387+
return 200, {
388+
'Processes': [
389+
[
390+
'root',
391+
'26501',
392+
'6907',
393+
'0',
394+
'10:32',
395+
'pts/55',
396+
'00:00:00',
397+
'sleep 60',
398+
],
399+
],
400+
'Titles': [
401+
'UID',
402+
'PID',
403+
'PPID',
404+
'C',
405+
'STIME',
406+
'TTY',
407+
'TIME',
408+
'CMD',
409+
],
410+
}
411+
412+
386413
def get_fake_volume_list():
387414
status_code = 200
388415
response = {
@@ -462,6 +489,8 @@ def fake_remove_volume():
462489

463490
'{1}/{0}/containers/3cc2351ab11b/stats'.format(CURRENT_VERSION, prefix):
464491
get_fake_stats,
492+
'{1}/{0}/containers/3cc2351ab11b/top'.format(CURRENT_VERSION, prefix):
493+
get_fake_top,
465494
'{1}/{0}/containers/3cc2351ab11b/stop'.format(CURRENT_VERSION, prefix):
466495
post_fake_stop_container,
467496
'{1}/{0}/containers/3cc2351ab11b/kill'.format(CURRENT_VERSION, prefix):

tests/integration_test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,44 @@ def runTest(self):
787787
self.client.kill(id)
788788

789789

790+
class TestContainerTop(BaseTestCase):
791+
def runTest(self):
792+
container = self.client.create_container(
793+
BUSYBOX, ['sleep', '60'])
794+
795+
id = container['Id']
796+
797+
self.client.start(container)
798+
res = self.client.top(container['Id'])
799+
print(res)
800+
self.assertEqual(
801+
res['Titles'],
802+
['UID', 'PID', 'PPID', 'C', 'STIME', 'TTY', 'TIME', 'CMD']
803+
)
804+
self.assertEqual(len(res['Processes']), 1)
805+
self.assertEqual(res['Processes'][0][7], 'sleep 60')
806+
self.client.kill(id)
807+
808+
809+
class TestContainerTopWithPsArgs(BaseTestCase):
810+
def runTest(self):
811+
container = self.client.create_container(
812+
BUSYBOX, ['sleep', '60'])
813+
814+
id = container['Id']
815+
816+
self.client.start(container)
817+
res = self.client.top(container['Id'], 'waux')
818+
self.assertEqual(
819+
res['Titles'],
820+
['USER', 'PID', '%CPU', '%MEM', 'VSZ', 'RSS',
821+
'TTY', 'STAT', 'START', 'TIME', 'COMMAND'],
822+
)
823+
self.assertEqual(len(res['Processes']), 1)
824+
self.assertEqual(res['Processes'][0][10], 'sleep 60')
825+
self.client.kill(id)
826+
827+
790828
class TestRestart(BaseTestCase):
791829
def runTest(self):
792830
container = self.client.create_container(BUSYBOX, ['sleep', '9999'])

tests/test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,26 @@ def test_container_stats(self):
17371737
stream=True
17381738
)
17391739

1740+
def test_container_top(self):
1741+
self.client.top(fake_api.FAKE_CONTAINER_ID)
1742+
1743+
fake_request.assert_called_with(
1744+
'GET',
1745+
url_prefix + 'containers/3cc2351ab11b/top',
1746+
params={},
1747+
timeout=DEFAULT_TIMEOUT_SECONDS
1748+
)
1749+
1750+
def test_container_top_with_psargs(self):
1751+
self.client.top(fake_api.FAKE_CONTAINER_ID, 'waux')
1752+
1753+
fake_request.assert_called_with(
1754+
'GET',
1755+
url_prefix + 'containers/3cc2351ab11b/top',
1756+
params={'ps_args': 'waux'},
1757+
timeout=DEFAULT_TIMEOUT_SECONDS
1758+
)
1759+
17401760
##################
17411761
# IMAGES TESTS #
17421762
##################

0 commit comments

Comments
 (0)