Skip to content

Commit 63cda2e

Browse files
authored
Merge pull request #2326 from docker/fix_exec_demux_tests
Avoid demux test flakiness
2 parents 0287cd9 + 073a21c commit 63cda2e

File tree

1 file changed

+80
-72
lines changed

1 file changed

+80
-72
lines changed

tests/integration/api_exec_test.py

Lines changed: 80 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
from docker.utils.proxy import ProxyConfig
12
from docker.utils.socket import next_frame_header
23
from docker.utils.socket import read_exactly
3-
from docker.utils.proxy import ProxyConfig
44

5-
from .base import BaseAPIIntegrationTest, BUSYBOX
5+
from .base import BUSYBOX, BaseAPIIntegrationTest
66
from ..helpers import (
7-
requires_api_version, ctrl_with, assert_cat_socket_detached_with_keys
7+
assert_cat_socket_detached_with_keys, ctrl_with, requires_api_version,
88
)
99

1010

@@ -115,75 +115,6 @@ def test_exec_command_streaming(self):
115115
res += chunk
116116
assert res == b'hello\nworld\n'
117117

118-
def test_exec_command_demux(self):
119-
container = self.client.create_container(
120-
BUSYBOX, 'cat', detach=True, stdin_open=True)
121-
id = container['Id']
122-
self.client.start(id)
123-
self.tmp_containers.append(id)
124-
125-
script = ' ; '.join([
126-
# Write something on stdout
127-
'echo hello out',
128-
# Busybox's sleep does not handle sub-second times.
129-
# This loops takes ~0.3 second to execute on my machine.
130-
'for i in $(seq 1 50000); do echo $i>/dev/null; done',
131-
# Write something on stderr
132-
'echo hello err >&2'])
133-
cmd = 'sh -c "{}"'.format(script)
134-
135-
# tty=False, stream=False, demux=False
136-
res = self.client.exec_create(id, cmd)
137-
exec_log = self.client.exec_start(res)
138-
assert exec_log == b'hello out\nhello err\n'
139-
140-
# tty=False, stream=True, demux=False
141-
res = self.client.exec_create(id, cmd)
142-
exec_log = self.client.exec_start(res, stream=True)
143-
assert next(exec_log) == b'hello out\n'
144-
assert next(exec_log) == b'hello err\n'
145-
with self.assertRaises(StopIteration):
146-
next(exec_log)
147-
148-
# tty=False, stream=False, demux=True
149-
res = self.client.exec_create(id, cmd)
150-
exec_log = self.client.exec_start(res, demux=True)
151-
assert exec_log == (b'hello out\n', b'hello err\n')
152-
153-
# tty=False, stream=True, demux=True
154-
res = self.client.exec_create(id, cmd)
155-
exec_log = self.client.exec_start(res, demux=True, stream=True)
156-
assert next(exec_log) == (b'hello out\n', None)
157-
assert next(exec_log) == (None, b'hello err\n')
158-
with self.assertRaises(StopIteration):
159-
next(exec_log)
160-
161-
# tty=True, stream=False, demux=False
162-
res = self.client.exec_create(id, cmd, tty=True)
163-
exec_log = self.client.exec_start(res)
164-
assert exec_log == b'hello out\r\nhello err\r\n'
165-
166-
# tty=True, stream=True, demux=False
167-
res = self.client.exec_create(id, cmd, tty=True)
168-
exec_log = self.client.exec_start(res, stream=True)
169-
assert next(exec_log) == b'hello out\r\n'
170-
assert next(exec_log) == b'hello err\r\n'
171-
with self.assertRaises(StopIteration):
172-
next(exec_log)
173-
174-
# tty=True, stream=False, demux=True
175-
res = self.client.exec_create(id, cmd, tty=True)
176-
exec_log = self.client.exec_start(res, demux=True)
177-
assert exec_log == (b'hello out\r\nhello err\r\n', None)
178-
179-
# tty=True, stream=True, demux=True
180-
res = self.client.exec_create(id, cmd, tty=True)
181-
exec_log = self.client.exec_start(res, demux=True, stream=True)
182-
assert next(exec_log) == (b'hello out\r\n', None)
183-
assert next(exec_log) == (b'hello err\r\n', None)
184-
with self.assertRaises(StopIteration):
185-
next(exec_log)
186-
187118
def test_exec_start_socket(self):
188119
container = self.client.create_container(BUSYBOX, 'cat',
189120
detach=True, stdin_open=True)
@@ -313,3 +244,80 @@ def test_detach_with_arg(self):
313244
self.addCleanup(sock.close)
314245

315246
assert_cat_socket_detached_with_keys(sock, [ctrl_with('x')])
247+
248+
249+
class ExecDemuxTest(BaseAPIIntegrationTest):
250+
cmd = 'sh -c "{}"'.format(' ; '.join([
251+
# Write something on stdout
252+
'echo hello out',
253+
# Busybox's sleep does not handle sub-second times.
254+
# This loops takes ~0.3 second to execute on my machine.
255+
'for i in $(seq 1 50000); do echo $i>/dev/null; done',
256+
# Write something on stderr
257+
'echo hello err >&2'])
258+
)
259+
260+
def setUp(self):
261+
super(ExecDemuxTest, self).setUp()
262+
self.container = self.client.create_container(
263+
BUSYBOX, 'cat', detach=True, stdin_open=True
264+
)
265+
self.client.start(self.container)
266+
self.tmp_containers.append(self.container)
267+
268+
def test_exec_command_no_stream_no_demux(self):
269+
# tty=False, stream=False, demux=False
270+
res = self.client.exec_create(self.container, self.cmd)
271+
exec_log = self.client.exec_start(res)
272+
assert b'hello out\n' in exec_log
273+
assert b'hello err\n' in exec_log
274+
275+
def test_exec_command_stream_no_demux(self):
276+
# tty=False, stream=True, demux=False
277+
res = self.client.exec_create(self.container, self.cmd)
278+
exec_log = list(self.client.exec_start(res, stream=True))
279+
assert len(exec_log) == 2
280+
assert b'hello out\n' in exec_log
281+
assert b'hello err\n' in exec_log
282+
283+
def test_exec_command_no_stream_demux(self):
284+
# tty=False, stream=False, demux=True
285+
res = self.client.exec_create(self.container, self.cmd)
286+
exec_log = self.client.exec_start(res, demux=True)
287+
assert exec_log == (b'hello out\n', b'hello err\n')
288+
289+
def test_exec_command_stream_demux(self):
290+
# tty=False, stream=True, demux=True
291+
res = self.client.exec_create(self.container, self.cmd)
292+
exec_log = list(self.client.exec_start(res, demux=True, stream=True))
293+
assert len(exec_log) == 2
294+
assert (b'hello out\n', None) in exec_log
295+
assert (None, b'hello err\n') in exec_log
296+
297+
def test_exec_command_tty_no_stream_no_demux(self):
298+
# tty=True, stream=False, demux=False
299+
res = self.client.exec_create(self.container, self.cmd, tty=True)
300+
exec_log = self.client.exec_start(res)
301+
assert exec_log == b'hello out\r\nhello err\r\n'
302+
303+
def test_exec_command_tty_stream_no_demux(self):
304+
# tty=True, stream=True, demux=False
305+
res = self.client.exec_create(self.container, self.cmd, tty=True)
306+
exec_log = list(self.client.exec_start(res, stream=True))
307+
assert len(exec_log) == 2
308+
assert b'hello out\r\n' in exec_log
309+
assert b'hello err\r\n' in exec_log
310+
311+
def test_exec_command_tty_no_stream_demux(self):
312+
# tty=True, stream=False, demux=True
313+
res = self.client.exec_create(self.container, self.cmd, tty=True)
314+
exec_log = self.client.exec_start(res, demux=True)
315+
assert exec_log == (b'hello out\r\nhello err\r\n', None)
316+
317+
def test_exec_command_tty_stream_demux(self):
318+
# tty=True, stream=True, demux=True
319+
res = self.client.exec_create(self.container, self.cmd, tty=True)
320+
exec_log = list(self.client.exec_start(res, demux=True, stream=True))
321+
assert len(exec_log) == 2
322+
assert (b'hello out\r\n', None) in exec_log
323+
assert (b'hello err\r\n', None) in exec_log

0 commit comments

Comments
 (0)