Skip to content

Commit e304f91

Browse files
committed
Update detach tests to work with AF_INET as well
Signed-off-by: Joffrey F <[email protected]>
1 parent dd85864 commit e304f91

File tree

4 files changed

+54
-36
lines changed

4 files changed

+54
-36
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ integration-dind-py2: build
5454
-H tcp://0.0.0.0:2375 --experimental
5555
docker run -t --rm --env="DOCKER_HOST=tcp://docker:2375" --env="DOCKER_TEST_API_VERSION=${TEST_API_VERSION}"\
5656
--link=dpy-dind-py2:docker docker-sdk-python py.test tests/integration
57-
docker rm -vf dpy-dind-py3
57+
docker rm -vf dpy-dind-py2
5858

5959
.PHONY: integration-dind-py3
6060
integration-dind-py3: build-py3

tests/helpers.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import tempfile
77
import time
88
import re
9-
import socket
109
import six
10+
import socket
1111

1212
import docker
1313
import pytest
@@ -107,16 +107,23 @@ def swarm_listen_addr():
107107
return '0.0.0.0:{0}'.format(random.randrange(10000, 25000))
108108

109109

110-
def assert_socket_closed_with_keys(sock, inputs):
110+
def assert_cat_socket_detached_with_keys(sock, inputs):
111111
if six.PY3:
112112
sock = sock._sock
113113

114114
for i in inputs:
115115
sock.send(i)
116-
time.sleep(1)
117-
118-
with pytest.raises(socket.error):
116+
time.sleep(0.5)
117+
118+
# If we're using a Unix socket, the sock.send call will fail with a
119+
# BrokenPipeError ; INET sockets will just stop receiving / sending data
120+
# but will not raise an error
121+
if sock.family == getattr(socket, 'AF_UNIX', -1):
122+
with pytest.raises(socket.error):
123+
sock.send(b'make sure the socket is closed\n')
124+
else:
119125
sock.send(b"make sure the socket is closed\n")
126+
assert sock.recv(32) == b''
120127

121128

122129
def ctrl_with(char):

tests/integration/api_container_test.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .base import BUSYBOX, BaseAPIIntegrationTest
1818
from .. import helpers
1919
from ..helpers import (
20-
requires_api_version, ctrl_with, assert_socket_closed_with_keys
20+
requires_api_version, ctrl_with, assert_cat_socket_detached_with_keys
2121
)
2222

2323

@@ -1227,55 +1227,54 @@ def test_attach_no_stream(self):
12271227

12281228
def test_detach_with_default(self):
12291229
container = self.client.create_container(
1230-
BUSYBOX, '/bin/sh',
1230+
BUSYBOX, 'cat',
12311231
detach=True, stdin_open=True, tty=True
12321232
)
1233-
id = container['Id']
1234-
self.tmp_containers.append(id)
1235-
self.client.start(id)
1233+
self.tmp_containers.append(container)
1234+
self.client.start(container)
12361235

12371236
sock = self.client.attach_socket(
12381237
container,
12391238
{'stdin': True, 'stream': True}
12401239
)
12411240

1242-
assert_socket_closed_with_keys(sock, [ctrl_with('p'), ctrl_with('q')])
1241+
assert_cat_socket_detached_with_keys(
1242+
sock, [ctrl_with('p'), ctrl_with('q')]
1243+
)
12431244

12441245
def test_detach_with_config_file(self):
12451246
self.client._general_configs['detachKeys'] = 'ctrl-p'
12461247

12471248
container = self.client.create_container(
1248-
BUSYBOX, '/bin/sh',
1249+
BUSYBOX, 'cat',
12491250
detach=True, stdin_open=True, tty=True
12501251
)
1251-
id = container['Id']
1252-
self.tmp_containers.append(id)
1253-
self.client.start(id)
1252+
self.tmp_containers.append(container)
1253+
self.client.start(container)
12541254

12551255
sock = self.client.attach_socket(
12561256
container,
12571257
{'stdin': True, 'stream': True}
12581258
)
12591259

1260-
assert_socket_closed_with_keys(sock, [ctrl_with('p')])
1260+
assert_cat_socket_detached_with_keys(sock, [ctrl_with('p')])
12611261

12621262
def test_detach_with_arg(self):
12631263
self.client._general_configs['detachKeys'] = 'ctrl-p'
12641264

12651265
container = self.client.create_container(
1266-
BUSYBOX, '/bin/sh',
1266+
BUSYBOX, 'cat',
12671267
detach=True, stdin_open=True, tty=True
12681268
)
1269-
id = container['Id']
1270-
self.tmp_containers.append(id)
1271-
self.client.start(id)
1269+
self.tmp_containers.append(container)
1270+
self.client.start(container)
12721271

12731272
sock = self.client.attach_socket(
12741273
container,
12751274
{'stdin': True, 'stream': True, 'detachKeys': 'ctrl-x'}
12761275
)
12771276

1278-
assert_socket_closed_with_keys(sock, [ctrl_with('x')])
1277+
assert_cat_socket_detached_with_keys(sock, [ctrl_with('x')])
12791278

12801279

12811280
class PauseTest(BaseAPIIntegrationTest):

tests/integration/api_exec_test.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from .base import BaseAPIIntegrationTest, BUSYBOX
55
from ..helpers import (
6-
requires_api_version, ctrl_with, assert_socket_closed_with_keys
6+
requires_api_version, ctrl_with, assert_cat_socket_detached_with_keys
77
)
88

99

@@ -152,42 +152,54 @@ def test_exec_command_with_workdir(self):
152152
assert exec_log == b'/var/www\n'
153153

154154
def test_detach_with_default(self):
155-
container = self.client.create_container(BUSYBOX, 'cat',
156-
detach=True, stdin_open=True)
155+
container = self.client.create_container(
156+
BUSYBOX, 'cat', detach=True, stdin_open=True
157+
)
157158
id = container['Id']
158159
self.client.start(id)
159160
self.tmp_containers.append(id)
160161

161-
exec_id = self.client.exec_create(id, '/bin/sh', stdin=True, tty=True)
162+
exec_id = self.client.exec_create(
163+
id, 'cat', stdin=True, tty=True, stdout=True
164+
)
162165
sock = self.client.exec_start(exec_id, tty=True, socket=True)
166+
self.addCleanup(sock.close)
163167

164-
assert_socket_closed_with_keys(sock, [ctrl_with('p'), ctrl_with('q')])
168+
assert_cat_socket_detached_with_keys(
169+
sock, [ctrl_with('p'), ctrl_with('q')]
170+
)
165171

166172
def test_detach_with_config_file(self):
167173
self.client._general_configs['detachKeys'] = 'ctrl-p'
168-
container = self.client.create_container(BUSYBOX, 'cat',
169-
detach=True, stdin_open=True)
174+
container = self.client.create_container(
175+
BUSYBOX, 'cat', detach=True, stdin_open=True
176+
)
170177
id = container['Id']
171178
self.client.start(id)
172179
self.tmp_containers.append(id)
173180

174-
exec_id = self.client.exec_create(id, '/bin/sh', stdin=True, tty=True)
181+
exec_id = self.client.exec_create(
182+
id, 'cat', stdin=True, tty=True, stdout=True
183+
)
175184
sock = self.client.exec_start(exec_id, tty=True, socket=True)
185+
self.addCleanup(sock.close)
176186

177-
assert_socket_closed_with_keys(sock, [ctrl_with('p')])
187+
assert_cat_socket_detached_with_keys(sock, [ctrl_with('p')])
178188

179189
def test_detach_with_arg(self):
180190
self.client._general_configs['detachKeys'] = 'ctrl-p'
181-
container = self.client.create_container(BUSYBOX, 'cat',
182-
detach=True, stdin_open=True)
191+
container = self.client.create_container(
192+
BUSYBOX, 'cat', detach=True, stdin_open=True
193+
)
183194
id = container['Id']
184195
self.client.start(id)
185196
self.tmp_containers.append(id)
186197

187198
exec_id = self.client.exec_create(
188-
id, '/bin/sh',
189-
stdin=True, tty=True, detach_keys='ctrl-x'
199+
id, 'cat',
200+
stdin=True, tty=True, detach_keys='ctrl-x', stdout=True
190201
)
191202
sock = self.client.exec_start(exec_id, tty=True, socket=True)
203+
self.addCleanup(sock.close)
192204

193-
assert_socket_closed_with_keys(sock, [ctrl_with('x')])
205+
assert_cat_socket_detached_with_keys(sock, [ctrl_with('x')])

0 commit comments

Comments
 (0)