This commit is contained in:
Gavin D'mello 2025-01-23 10:19:59 +00:00 committed by GitHub
commit 5b95dd7582
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 1 deletions

View File

@ -898,7 +898,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()

View File

@ -34,6 +34,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):

View File

@ -270,6 +270,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)