mirror of https://github.com/docker/docker-py.git
commit
ab0f989154
10
README.md
10
README.md
|
|
@ -50,10 +50,10 @@ Identical to the `docker info` command.
|
|||
* `c.insert(url, path)`
|
||||
Identical to the `docker insert` command.
|
||||
|
||||
* `c.inspect_container(container_id)`
|
||||
Identical to the `docker inspect` command, but can only be used with a container ID.
|
||||
* `c.inspect_container(container)`
|
||||
Identical to the `docker inspect` command.
|
||||
|
||||
* `c.inspect_image(container_id)`
|
||||
* `c.inspect_image(image_id)`
|
||||
Identical to the `docker inspect` command, but can only be used with an image ID.
|
||||
|
||||
* `c.kill(containers...)`
|
||||
|
|
@ -86,11 +86,11 @@ Identical to the `docker restart` command.
|
|||
* `c.search(term)`
|
||||
Identical to the `docker search` command.
|
||||
|
||||
* `c.start(container)`
|
||||
* `c.start(containers)`
|
||||
Identical to the `docker start` command, but doesn't support attach options.
|
||||
Use `docker logs` to recover `stdout`/`stderr`
|
||||
|
||||
* `c.start(container, binds={'/host': '/mnt'})`
|
||||
* `c.start(containers, binds={'/host': '/mnt'})`
|
||||
Allows to bind a directory in the host to the container.
|
||||
Similar to the `docker run` command with the `-b="/host:/mnt"`.
|
||||
Requires the container to be created with the volumes argument:
|
||||
|
|
|
|||
|
|
@ -216,6 +216,8 @@ class Client(requests.Session):
|
|||
'stderr': 1,
|
||||
'stream': 1
|
||||
}
|
||||
if isinstance(container, dict):
|
||||
container = container.get('Id')
|
||||
|
||||
u = self._url("/containers/{0}/attach".format(container))
|
||||
res = self.post(u, None, params=params, stream=True)
|
||||
|
|
@ -304,10 +306,14 @@ class Client(requests.Session):
|
|||
return self._result(res, True)
|
||||
|
||||
def diff(self, container):
|
||||
if isinstance(container, dict):
|
||||
container = container.get('Id')
|
||||
return self._result(self.get(self._url("/containers/{0}/changes".
|
||||
format(container))), True)
|
||||
|
||||
def export(self, container):
|
||||
if isinstance(container, dict):
|
||||
container = container.get('Id')
|
||||
res = self.get(self._url("/containers/{0}/export".format(container)),
|
||||
stream=True)
|
||||
return res.raw
|
||||
|
|
@ -355,9 +361,11 @@ class Client(requests.Session):
|
|||
}
|
||||
return self._result(self.post(api_url, None, params=params))
|
||||
|
||||
def inspect_container(self, container_id):
|
||||
def inspect_container(self, container):
|
||||
if isinstance(container, dict):
|
||||
container = container.get('Id')
|
||||
return self._result(self.get(self._url("/containers/{0}/json".
|
||||
format(container_id))), True)
|
||||
format(container))), True)
|
||||
|
||||
def inspect_image(self, image_id):
|
||||
return self._result(self.get(self._url("/images/{0}/json".
|
||||
|
|
@ -365,6 +373,8 @@ class Client(requests.Session):
|
|||
|
||||
def kill(self, *args):
|
||||
for name in args:
|
||||
if isinstance(name, dict):
|
||||
name = name.get('Id')
|
||||
url = self._url("/containers/{0}/kill".format(name))
|
||||
self.post(url, None)
|
||||
|
||||
|
|
@ -386,6 +396,8 @@ class Client(requests.Session):
|
|||
return res
|
||||
|
||||
def logs(self, container):
|
||||
if isinstance(container, dict):
|
||||
container = container.get('Id')
|
||||
params = {
|
||||
'logs': 1,
|
||||
'stdout': 1,
|
||||
|
|
@ -395,6 +407,8 @@ class Client(requests.Session):
|
|||
return self._result(self.post(u, None, params=params))
|
||||
|
||||
def port(self, container, private_port):
|
||||
if isinstance(container, dict):
|
||||
container = container.get('Id')
|
||||
res = self.get(self._url("/containers/{0}/json".format(container)))
|
||||
json_ = res.json()
|
||||
s_port = str(private_port)
|
||||
|
|
@ -433,6 +447,8 @@ class Client(requests.Session):
|
|||
'v': 1 if kwargs.get('v', False) else 0
|
||||
}
|
||||
for container in args:
|
||||
if isinstance(container, dict):
|
||||
container = container.get('Id')
|
||||
res = self.delete(self._url("/containers/" + container), params=params)
|
||||
if res.status_code >= 400:
|
||||
raise RuntimeError(res.text)
|
||||
|
|
@ -446,6 +462,8 @@ class Client(requests.Session):
|
|||
't': kwargs.get('timeout', 10)
|
||||
}
|
||||
for name in args:
|
||||
if isinstance(name, dict):
|
||||
name = name.get('Id')
|
||||
url = self._url("/containers/{0}/restart".format(name))
|
||||
self.post(url, None, params=params)
|
||||
|
||||
|
|
@ -463,6 +481,8 @@ class Client(requests.Session):
|
|||
}
|
||||
|
||||
for name in args:
|
||||
if isinstance(name, dict):
|
||||
name = name.get('Id')
|
||||
url = self._url("/containers/{0}/start".format(name))
|
||||
self._post_json(url, start_config)
|
||||
|
||||
|
|
@ -471,6 +491,8 @@ class Client(requests.Session):
|
|||
't': kwargs.get('timeout', 10)
|
||||
}
|
||||
for name in args:
|
||||
if isinstance(name, dict):
|
||||
name = name.get('Id')
|
||||
url = self._url("/containers/{0}/stop".format(name))
|
||||
self.post(url, None, params=params)
|
||||
|
||||
|
|
@ -491,6 +513,8 @@ class Client(requests.Session):
|
|||
def wait(self, *args):
|
||||
result = []
|
||||
for name in args:
|
||||
if isinstance(name, dict):
|
||||
name = name.get('Id')
|
||||
url = self._url("/containers/{0}/wait".format(name))
|
||||
res = self.post(url, None, timeout=None)
|
||||
json_ = res.json()
|
||||
|
|
|
|||
121
tests/test.py
121
tests/test.py
|
|
@ -155,6 +155,23 @@ class TestStartContainer(BaseTestCase):
|
|||
self.assertIn('ExitCode', inspect['State'])
|
||||
self.assertEqual(inspect['State']['ExitCode'], 0)
|
||||
|
||||
class TestStartContainerWithDictInsteadOfId(BaseTestCase):
|
||||
def runTest(self):
|
||||
res = self.client.create_container('busybox', 'true')
|
||||
self.assertIn('Id', res)
|
||||
self.tmp_containers.append(res['Id'])
|
||||
self.client.start(res)
|
||||
inspect = self.client.inspect_container(res['Id'])
|
||||
self.assertIn('Config', inspect)
|
||||
self.assertIn('ID', inspect)
|
||||
self.assertTrue(inspect['ID'].startswith(res['Id']))
|
||||
self.assertIn('Image', inspect)
|
||||
self.assertIn('State', inspect)
|
||||
self.assertIn('Running', inspect['State'])
|
||||
if not inspect['State']['Running']:
|
||||
self.assertIn('ExitCode', inspect['State'])
|
||||
self.assertEqual(inspect['State']['ExitCode'], 0)
|
||||
|
||||
class TestWait(BaseTestCase):
|
||||
def runTest(self):
|
||||
res = self.client.create_container('busybox', ['sleep', '10'])
|
||||
|
|
@ -169,6 +186,20 @@ class TestWait(BaseTestCase):
|
|||
self.assertIn('ExitCode', inspect['State'])
|
||||
self.assertEqual(inspect['State']['ExitCode'], exitcode)
|
||||
|
||||
class TestWaitWithDictInsteadOfId(BaseTestCase):
|
||||
def runTest(self):
|
||||
res = self.client.create_container('busybox', ['sleep', '10'])
|
||||
id = res['Id']
|
||||
self.tmp_containers.append(id)
|
||||
self.client.start(res)
|
||||
exitcode = self.client.wait(res)
|
||||
self.assertEqual(exitcode, 0)
|
||||
inspect = self.client.inspect_container(res)
|
||||
self.assertIn('Running', inspect['State'])
|
||||
self.assertEqual(inspect['State']['Running'], False)
|
||||
self.assertIn('ExitCode', inspect['State'])
|
||||
self.assertEqual(inspect['State']['ExitCode'], exitcode)
|
||||
|
||||
class TestLogs(BaseTestCase):
|
||||
def runTest(self):
|
||||
snippet = 'Flowering Nights (Sakuya Iyazoi)'
|
||||
|
|
@ -182,6 +213,19 @@ class TestLogs(BaseTestCase):
|
|||
logs = self.client.logs(id)
|
||||
self.assertEqual(logs, snippet + '\n')
|
||||
|
||||
class TestLogsWithDictInsteadOfId(BaseTestCase):
|
||||
def runTest(self):
|
||||
snippet = 'Flowering Nights (Sakuya Iyazoi)'
|
||||
container = self.client.create_container('busybox',
|
||||
'echo {0}'.format(snippet))
|
||||
id = container['Id']
|
||||
self.client.start(id)
|
||||
self.tmp_containers.append(id)
|
||||
exitcode = self.client.wait(id)
|
||||
self.assertEqual(exitcode, 0)
|
||||
logs = self.client.logs(container)
|
||||
self.assertEqual(logs, snippet + '\n')
|
||||
|
||||
class TestDiff(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['touch', '/test'])
|
||||
|
|
@ -198,6 +242,20 @@ class TestDiff(BaseTestCase):
|
|||
# FIXME also test remove/modify
|
||||
# (need testcommit first)
|
||||
|
||||
class TestDiffWithDictInsteadOfId(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['touch', '/test'])
|
||||
id = container['Id']
|
||||
self.client.start(id)
|
||||
self.tmp_containers.append(id)
|
||||
exitcode = self.client.wait(id)
|
||||
self.assertEqual(exitcode, 0)
|
||||
diff = self.client.diff(container)
|
||||
test_diff = [x for x in diff if x.get('Path', None) == '/test']
|
||||
self.assertEqual(len(test_diff), 1)
|
||||
self.assertIn('Kind', test_diff[0])
|
||||
self.assertEqual(test_diff[0]['Kind'], 1)
|
||||
|
||||
class TestStop(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['sleep', '9999'])
|
||||
|
|
@ -213,6 +271,22 @@ class TestStop(BaseTestCase):
|
|||
self.assertIn('Running', state)
|
||||
self.assertEqual(state['Running'], False)
|
||||
|
||||
class TestStopWithDictInsteadOfId(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['sleep', '9999'])
|
||||
self.assertIn('Id', container)
|
||||
id = container['Id']
|
||||
self.client.start(container)
|
||||
self.tmp_containers.append(id)
|
||||
self.client.stop(container, timeout=2)
|
||||
container_info = self.client.inspect_container(id)
|
||||
self.assertIn('State', container_info)
|
||||
state = container_info['State']
|
||||
self.assertIn('ExitCode', state)
|
||||
self.assertNotEqual(state['ExitCode'], 0)
|
||||
self.assertIn('Running', state)
|
||||
self.assertEqual(state['Running'], False)
|
||||
|
||||
class TestKill(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['sleep', '9999'])
|
||||
|
|
@ -228,6 +302,21 @@ class TestKill(BaseTestCase):
|
|||
self.assertIn('Running', state)
|
||||
self.assertEqual(state['Running'], False)
|
||||
|
||||
class TestKillWithDictInsteadOfId(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['sleep', '9999'])
|
||||
id = container['Id']
|
||||
self.client.start(id)
|
||||
self.tmp_containers.append(id)
|
||||
self.client.kill(container)
|
||||
container_info = self.client.inspect_container(id)
|
||||
self.assertIn('State', container_info)
|
||||
state = container_info['State']
|
||||
self.assertIn('ExitCode', state)
|
||||
self.assertNotEqual(state['ExitCode'], 0)
|
||||
self.assertIn('Running', state)
|
||||
self.assertEqual(state['Running'], False)
|
||||
|
||||
class TestRestart(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['sleep', '9999'])
|
||||
|
|
@ -248,6 +337,27 @@ class TestRestart(BaseTestCase):
|
|||
self.assertEqual(info2['State']['Running'], True)
|
||||
self.client.kill(id)
|
||||
|
||||
class TestRestartWithDictInsteadOfId(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['sleep', '9999'])
|
||||
self.assertIn('Id', container)
|
||||
id = container['Id']
|
||||
self.client.start(container)
|
||||
self.tmp_containers.append(id)
|
||||
info = self.client.inspect_container(id)
|
||||
self.assertIn('State', info)
|
||||
self.assertIn('StartedAt', info['State'])
|
||||
start_time1 = info['State']['StartedAt']
|
||||
self.client.restart(container, timeout=2)
|
||||
info2 = self.client.inspect_container(id)
|
||||
self.assertIn('State', info2)
|
||||
self.assertIn('StartedAt', info2['State'])
|
||||
start_time2 = info2['State']['StartedAt']
|
||||
self.assertNotEqual(start_time1, start_time2)
|
||||
self.assertIn('Running', info2['State'])
|
||||
self.assertEqual(info2['State']['Running'], True)
|
||||
self.client.kill(id)
|
||||
|
||||
class TestRemoveContainer(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['true'])
|
||||
|
|
@ -259,6 +369,17 @@ class TestRemoveContainer(BaseTestCase):
|
|||
res = [x for x in containers if 'Id' in x and x['Id'].startswith(id)]
|
||||
self.assertEqual(len(res), 0)
|
||||
|
||||
class TestRemoveContainerWithDictInsteadOfId(BaseTestCase):
|
||||
def runTest(self):
|
||||
container = self.client.create_container('busybox', ['true'])
|
||||
id = container['Id']
|
||||
self.client.start(id)
|
||||
self.client.wait(id)
|
||||
self.client.remove_container(container)
|
||||
containers = self.client.containers(all=True)
|
||||
res = [x for x in containers if 'Id' in x and x['Id'].startswith(id)]
|
||||
self.assertEqual(len(res), 0)
|
||||
|
||||
##################
|
||||
## IMAGES TESTS ##
|
||||
##################
|
||||
|
|
|
|||
Loading…
Reference in New Issue