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

Commit 3a3736e

Browse files
committed
Merge pull request #30 from frigg/return_code
Remove return code workaround and fix EOF newline behavior
2 parents cf78c74 + 23fe329 commit 3a3736e

3 files changed

Lines changed: 38 additions & 25 deletions

File tree

docker/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def execute(cmd, stdin=''):
3434
)
3535

3636
(stdout, stderr) = process.communicate(str.encode(stdin))
37-
result.out = stdout.decode('utf-8').strip() if stdout else ''
38-
result.err = stderr.decode('utf-8').strip() if stderr else ''
37+
result.out = stdout.decode('utf-8') if stdout else ''
38+
result.err = stderr.decode('utf-8') if stderr else ''
3939
result.return_code = process.returncode
4040
logger.debug('Finished running of: {0}'.format(result.__dict__))
4141
return result

docker/manager.py

Lines changed: 3 additions & 10 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):
@@ -194,7 +187,7 @@ def list_files(self, path):
194187

195188
raise errors.DockerWrapperBaseError(result.err)
196189

197-
return result.out.split('\n')
190+
return result.out.strip().split('\n')
198191

199192
def list_directories(self, path, include_trailing_slash=True):
200193
"""
@@ -218,7 +211,7 @@ def list_directories(self, path, include_trailing_slash=True):
218211

219212
raise errors.DockerWrapperBaseError(result.err)
220213

221-
for file_path in result.out.split(', '):
214+
for file_path in result.out.strip().split(', '):
222215
if include_trailing_slash:
223216
files.append(file_path)
224217
else:

tests/test_manager.py

Lines changed: 33 additions & 13 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):
@@ -206,9 +207,8 @@ def test_create_and_list_files_in_sub_directory(self):
206207

207208
def test_read_file_with_content(self):
208209
file_name = 'readme.txt'
209-
file_content = 'this is a test file {0}'.format(randint(5000, 5500))
210-
self.docker.run('echo \"{0}\" > ~/{1}; cat readme.txt'.format(file_content, file_name))
211-
210+
file_content = 'this is a test file {0}\n'.format(randint(5000, 5500))
211+
self.docker.write_file(file_name, file_content)
212212
self.assertEqual(self.docker.read_file(file_name), file_content)
213213

214214
def test_read_file_that_dont_exist(self):
@@ -220,6 +220,17 @@ def test_read_file_that_dont_exist(self):
220220
path
221221
)
222222

223+
def test_read_file_eof_newline(self):
224+
path = '/etc/hostname'
225+
content = self.docker.read_file(path)
226+
self.assertTrue(content.endswith('\n'))
227+
228+
def test_write_file_read_file(self):
229+
path = 'testfile'
230+
content = 'this is a nice file\n'
231+
self.docker.write_file(path, content)
232+
self.assertEqual(content, self.docker.read_file(path))
233+
223234
def test_directory_exist(self):
224235
self.assertTrue(self.docker.directory_exist('~/'))
225236
self.assertFalse(self.docker.directory_exist('does-not-exist'))
@@ -233,15 +244,16 @@ def test_combine_output(self):
233244
self.docker.combine_outputs = True
234245
result = self.docker.run('ls does-not-exist')
235246
self.assertEqual(result.err, '')
236-
self.assertEqual(result.out, 'ls: cannot access does-not-exist: No such file or directory')
247+
self.assertEqual(result.out,
248+
'ls: cannot access does-not-exist: No such file or directory\n')
237249

238250
def test_privilege(self):
239251
Docker(privilege=True).start()
240252

241253
def test_write_file_append(self):
242254
path = 'readme.txt'
243-
old_content = 'hi'
244-
content = 'this is a readme'
255+
old_content = 'hi\n'
256+
content = 'this is a readme\n'
245257
self.docker.run('echo "{0}" > {1}'.format(old_content, path))
246258

247259
self.docker.write_file(path, content, append=True)
@@ -251,7 +263,7 @@ def test_write_file_append(self):
251263
def test_write_file_no_append(self):
252264
path = 'readme.txt'
253265
old_content = 'hi'
254-
content = 'this is a readme'
266+
content = 'this is a readme\n'
255267
self.docker.run('echo "{0}" > {1}'.format(old_content, path))
256268

257269
self.docker.write_file(path, content, append=False)
@@ -260,8 +272,16 @@ def test_write_file_no_append(self):
260272

261273
def test_write_file_quotes(self):
262274
path = 'readme.txt'
263-
content = 'this is a "readme"'
275+
content = 'this is a "readme"\n'
264276

265277
self.docker.write_file(path, content, append=False)
266278
written_content = self.docker.read_file(path)
267279
self.assertEqual(written_content, content)
280+
281+
def test_run_return_code(self):
282+
code = 4
283+
path = 'testfile'
284+
content = 'exit {0}\n'.format(code)
285+
self.docker.write_file(path, content)
286+
result = self.docker.run('bash {0}'.format(path))
287+
self.assertEqual(code, result.return_code)

0 commit comments

Comments
 (0)