From a494201e6ff908dc27d67f6f361eacda37906144 Mon Sep 17 00:00:00 2001 From: Gavin D'mello Date: Thu, 14 Nov 2019 01:10:00 +0000 Subject: [PATCH] Display stack trace when tty is enabled Signed-off-by: Gavin D'mello --- docker/models/containers.py | 5 ++++- tests/integration/models_containers_test.py | 9 +++++++++ tests/unit/models_containers_test.py | 10 ++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/docker/models/containers.py b/docker/models/containers.py index d1f275f7..fcf602e6 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -823,7 +823,10 @@ class ContainerCollection(Collection): if exit_status != 0: out = None if not kwargs.get('auto_remove'): - out = container.logs(stdout=False, stderr=True) + if not kwargs.get('tty'): + out = container.logs(stdout=False, stderr=True) + else: + out = container.logs(stdout=True, stderr=True) if remove: container.remove() diff --git a/tests/integration/models_containers_test.py b/tests/integration/models_containers_test.py index eac4c979..b933db9f 100644 --- a/tests/integration/models_containers_test.py +++ b/tests/integration/models_containers_test.py @@ -35,6 +35,15 @@ class ContainerCollectionTest(BaseIntegrationTest): assert "alpine" in cm.exconly() assert "No such file or directory" in cm.exconly() + def test_run_with_error_with_tty(self): + client = docker.from_env(version=TEST_API_VERSION) + with pytest.raises(docker.errors.ContainerError) as cm: + client.containers.run("alpine", "cat /test", remove=True, tty=True) + assert cm.value.exit_status == 1 + assert "cat /test" in cm.exconly() + assert "alpine" in cm.exconly() + assert "No such file or directory" in cm.exconly() + def test_run_with_image_that_does_not_exist(self): client = docker.from_env(version=TEST_API_VERSION) with pytest.raises(docker.errors.ImageNotFound): diff --git a/tests/unit/models_containers_test.py b/tests/unit/models_containers_test.py index da5f0ab9..8f64b345 100644 --- a/tests/unit/models_containers_test.py +++ b/tests/unit/models_containers_test.py @@ -246,6 +246,16 @@ class ContainerCollectionTest(unittest.TestCase): assert cm.value.exit_status == 1 assert "some error" in cm.exconly() + def test_run_with_error_and_tty(self): + client = make_fake_client() + client.api.logs.return_value = "some error" + client.api.wait.return_value = {'StatusCode': 1} + + with pytest.raises(docker.errors.ContainerError) as cm: + client.containers.run('alpine', 'echo hello world', tty=True) + assert cm.value.exit_status == 1 + assert "some error" in cm.exconly() + def test_run_with_image_object(self): client = make_fake_client() image = client.images.get(FAKE_IMAGE_ID)