Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit e3cd556

Browse files
author
ekmartin
committed
fix(run): Use docker exec's return code
BREAKING CHANGE: As this relies on moby/moby@00c2a8f it won't work with older Docker versions than 1.3.2.
1 parent cf78c74 commit e3cd556

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

docker/manager.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import re
32
import uuid
43
from collections import OrderedDict
54
from time import sleep
@@ -88,7 +87,7 @@ def run(self, command, working_directory='', stdin=''):
8887
])
8988

9089
result = execute(
91-
'docker exec -i {container} bash -c \'{command} ; echo "--return-$?--"\''.format(
90+
'docker exec -i {container} bash -c \'{command}\''.format(
9291
envs=env_string,
9392
container=self.container_name,
9493
command=command_string.format(
@@ -100,12 +99,6 @@ def run(self, command, working_directory='', stdin=''):
10099
stdin
101100
)
102101

103-
return_code_match = re.search(r'(?:\\n)?--return-(\d+)--$', result.out)
104-
if return_code_match:
105-
result.return_code = int(return_code_match.group(1))
106-
result.out = result.out.replace(return_code_match.group(0), '')
107-
if result.out.endswith('\n'):
108-
result.out = result.out[:len(result.out) - 1]
109102
return result
110103

111104
def read_file(self, path):

tests/test_manager.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def test_quotation_mark_handling(self, mock_run):
6969
docker = Docker()
7070
docker.run('echo "hi there"')
7171
docker.run("echo 'hi there'")
72-
expected = ('docker exec -i {} bash -c \'cd ~/ && echo "hi there" ; '
73-
'echo "--return-$?--"\''.format(docker.container_name), '')
72+
expected = (
73+
'docker exec -i {} bash -c \'cd ~/ && echo "hi there"\''.format(docker.container_name),
74+
'')
7475

7576
mock_run.assert_has_calls([mock.call(*expected), mock.call(*expected)])
7677

@@ -79,9 +80,9 @@ def test_quotation_mark_handling(self, mock_run):
7980
def test_env_variables(self, mock_run):
8081
docker = Docker(env_variables={'CI': 1, 'FRIGG': 1})
8182
docker.run('ls')
82-
mock_run.assert_called_once_with('docker exec -i {} bash -c \'cd ~/ && CI=1 FRIGG=1 ls ;'
83-
' echo "--return-$?--"\''.format(docker.container_name),
84-
'')
83+
mock_run.assert_called_once_with(
84+
'docker exec -i {} bash -c \'cd ~/ && CI=1 FRIGG=1 ls\''.format(docker.container_name),
85+
'')
8586

8687
@mock.patch('docker.manager.execute')
8788
def test_single_port_mappping(self, mock_run):
@@ -265,3 +266,11 @@ def test_write_file_quotes(self):
265266
self.docker.write_file(path, content, append=False)
266267
written_content = self.docker.read_file(path)
267268
self.assertEqual(written_content, content)
269+
270+
def test_run_return_code(self):
271+
code = 4
272+
path = 'testfile'
273+
content = 'exit {0}\n'.format(code)
274+
self.docker.write_file(path, content)
275+
result = self.docker.run('bash {0}'.format(path))
276+
self.assertEqual(code, result.return_code)

0 commit comments

Comments
 (0)