Kaydet (Commit) b0cc4b55 authored tarafından Joffrey F's avatar Joffrey F

Merge branch 'add_exit_code_to_exec_run' of https://github.com/hnq90/docker-py…

Merge branch 'add_exit_code_to_exec_run' of https://github.com/hnq90/docker-py into hnq90-add_exit_code_to_exec_run
......@@ -150,10 +150,14 @@ class Container(Model):
workdir (str): Path to working directory for this exec session
Returns:
(generator or str):
If ``stream=True``, a generator yielding response chunks.
If ``socket=True``, a socket object for the connection.
A string containing response data otherwise.
dict:
output: (generator or str):
If ``stream=True``, a generator yielding response chunks.
If ``socket=True``, a socket object for the connection.
A string containing response data otherwise.
exit_code: (int):
Exited code of execution
Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.
......@@ -163,9 +167,16 @@ class Container(Model):
privileged=privileged, user=user, environment=environment,
workdir=workdir
)
return self.client.api.exec_start(
exec_output = self.client.api.exec_start(
resp['Id'], detach=detach, tty=tty, stream=stream, socket=socket
)
exit_code = 0
if stream is False:
exit_code = self.client.api.exec_inspect(resp['Id'])['ExitCode']
return {
'exit_code': exit_code,
'output': exec_output
}
def export(self):
"""
......
......@@ -182,13 +182,24 @@ class ContainerTest(BaseIntegrationTest):
container.wait()
assert container.diff() == [{'Path': '/test', 'Kind': 1}]
def test_exec_run(self):
def test_exec_run_success(self):
client = docker.from_env(version=TEST_API_VERSION)
container = client.containers.run(
"alpine", "sh -c 'echo \"hello\" > /test; sleep 60'", detach=True
)
self.tmp_containers.append(container.id)
assert container.exec_run("cat /test") == b"hello\n"
exec_output = container.exec_run("cat /test")
assert exec_output["output"] == b"hello\n"
assert exec_output["exit_code"] == 0
def test_exec_run_failed(self):
client = docker.from_env(version=TEST_API_VERSION)
container = client.containers.run(
"alpine", "sh -c 'sleep 60'", detach=True
)
self.tmp_containers.append(container.id)
exec_output = container.exec_run("docker ps")
assert exec_output["exit_code"] == 126
def test_kill(self):
client = docker.from_env(version=TEST_API_VERSION)
......
......@@ -400,6 +400,17 @@ class ContainerTest(unittest.TestCase):
client.api.exec_start.assert_called_with(
FAKE_EXEC_ID, detach=False, tty=False, stream=True, socket=False
)
container.exec_run("docker ps", privileged=True, stream=False)
client.api.exec_create.assert_called_with(
FAKE_CONTAINER_ID, "docker ps", stdout=True, stderr=True,
stdin=False, tty=False, privileged=True, user='', environment=None
)
client.api.exec_start.assert_called_with(
FAKE_EXEC_ID, detach=False, tty=False, stream=False, socket=False
)
client.api.exec_start.assert_called_with(
FAKE_EXEC_ID, detach=False, tty=False, stream=False, socket=False
)
def test_export(self):
client = make_fake_client()
......
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