Skip to content

Commit 8975851

Browse files
committed
Update tests to avoid failures on Windows platforms
Signed-off-by: Joffrey F <[email protected]>
1 parent a6f1277 commit 8975851

File tree

9 files changed

+144
-106
lines changed

9 files changed

+144
-106
lines changed

tests/integration/build_test.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import io
2-
import json
32
import os
43
import shutil
54
import tempfile
@@ -22,14 +21,11 @@ def test_build_streaming(self):
2221
'ADD https://dl.dropboxusercontent.com/u/20637798/silence.tar.gz'
2322
' /tmp/silence.tar.gz'
2423
]).encode('ascii'))
25-
stream = self.client.build(fileobj=script, stream=True)
26-
logs = ''
24+
stream = self.client.build(fileobj=script, stream=True, decode=True)
25+
logs = []
2726
for chunk in stream:
28-
if six.PY3:
29-
chunk = chunk.decode('utf-8')
30-
json.loads(chunk) # ensure chunk is a single, valid JSON blob
31-
logs += chunk
32-
self.assertNotEqual(logs, '')
27+
logs.append(chunk)
28+
assert len(logs) > 0
3329

3430
def test_build_from_stringio(self):
3531
if six.PY3:

tests/integration/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import print_function
22

3-
import json
43
import sys
54
import warnings
65

@@ -19,8 +18,7 @@ def setup_test_session():
1918
c.inspect_image(BUSYBOX)
2019
except docker.errors.NotFound:
2120
print("\npulling {0}".format(BUSYBOX), file=sys.stderr)
22-
for data in c.pull(BUSYBOX, stream=True):
23-
data = json.loads(data.decode('utf-8'))
21+
for data in c.pull(BUSYBOX, stream=True, decode=True):
2422
status = data.get("status")
2523
progress = data.get("progress")
2624
detail = "{0} - {1}".format(status, progress)

tests/integration/container_test.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tempfile
44

55
import docker
6+
from docker.constants import IS_WINDOWS_PLATFORM
67
from docker.utils.socket import next_frame_size
78
from docker.utils.socket import read_exactly
89
import pytest
@@ -524,13 +525,13 @@ def test_get_file_stat_from_container(self):
524525

525526
def test_copy_file_to_container(self):
526527
data = b'Deaf To All But The Song'
527-
with tempfile.NamedTemporaryFile() as test_file:
528+
with tempfile.NamedTemporaryFile(delete=False) as test_file:
528529
test_file.write(data)
529530
test_file.seek(0)
530531
ctnr = self.client.create_container(
531532
BUSYBOX,
532533
'cat {0}'.format(
533-
os.path.join('/vol1', os.path.basename(test_file.name))
534+
os.path.join('/vol1/', os.path.basename(test_file.name))
534535
),
535536
volumes=['/vol1']
536537
)
@@ -822,11 +823,12 @@ def test_kill_with_dict_instead_of_id(self):
822823
self.assertEqual(state['Running'], False)
823824

824825
def test_kill_with_signal(self):
825-
container = self.client.create_container(BUSYBOX, ['sleep', '60'])
826-
id = container['Id']
827-
self.client.start(id)
826+
id = self.client.create_container(BUSYBOX, ['sleep', '60'])
828827
self.tmp_containers.append(id)
829-
self.client.kill(id, signal=signal.SIGKILL)
828+
self.client.start(id)
829+
self.client.kill(
830+
id, signal=signal.SIGKILL if not IS_WINDOWS_PLATFORM else 9
831+
)
830832
exitcode = self.client.wait(id)
831833
self.assertNotEqual(exitcode, 0)
832834
container_info = self.client.inspect_container(id)
@@ -902,36 +904,41 @@ def test_port(self):
902904
class ContainerTopTest(helpers.BaseTestCase):
903905
def test_top(self):
904906
container = self.client.create_container(
905-
BUSYBOX, ['sleep', '60'])
907+
BUSYBOX, ['sleep', '60']
908+
)
906909

907-
id = container['Id']
910+
self.tmp_containers.append(container)
908911

909912
self.client.start(container)
910-
res = self.client.top(container['Id'])
911-
self.assertEqual(
912-
res['Titles'],
913-
['UID', 'PID', 'PPID', 'C', 'STIME', 'TTY', 'TIME', 'CMD']
914-
)
915-
self.assertEqual(len(res['Processes']), 1)
916-
self.assertEqual(res['Processes'][0][7], 'sleep 60')
917-
self.client.kill(id)
913+
res = self.client.top(container)
914+
if IS_WINDOWS_PLATFORM:
915+
assert res['Titles'] == ['PID', 'USER', 'TIME', 'COMMAND']
916+
else:
917+
assert res['Titles'] == [
918+
'UID', 'PID', 'PPID', 'C', 'STIME', 'TTY', 'TIME', 'CMD'
919+
]
920+
assert len(res['Processes']) == 1
921+
assert res['Processes'][0][-1] == 'sleep 60'
922+
self.client.kill(container)
918923

924+
@pytest.mark.skipif(
925+
IS_WINDOWS_PLATFORM, reason='No psargs support on windows'
926+
)
919927
def test_top_with_psargs(self):
920928
container = self.client.create_container(
921929
BUSYBOX, ['sleep', '60'])
922930

923-
id = container['Id']
931+
self.tmp_containers.append(container)
924932

925933
self.client.start(container)
926-
res = self.client.top(container['Id'], 'waux')
934+
res = self.client.top(container, 'waux')
927935
self.assertEqual(
928936
res['Titles'],
929937
['USER', 'PID', '%CPU', '%MEM', 'VSZ', 'RSS',
930938
'TTY', 'STAT', 'START', 'TIME', 'COMMAND'],
931939
)
932940
self.assertEqual(len(res['Processes']), 1)
933941
self.assertEqual(res['Processes'][0][10], 'sleep 60')
934-
self.client.kill(id)
935942

936943

937944
class RestartContainerTest(helpers.BaseTestCase):

tests/integration/image_test.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ def test_pull_streaming(self):
5757
self.client.remove_image('hello-world')
5858
except docker.errors.APIError:
5959
pass
60-
stream = self.client.pull('hello-world', stream=True)
60+
stream = self.client.pull('hello-world', stream=True, decode=True)
6161
self.tmp_imgs.append('hello-world')
6262
for chunk in stream:
63-
if six.PY3:
64-
chunk = chunk.decode('utf-8')
65-
json.loads(chunk) # ensure chunk is a single, valid JSON blob
63+
assert isinstance(chunk, dict)
6664
self.assertGreaterEqual(
6765
len(self.client.images('hello-world')), 1
6866
)
@@ -152,7 +150,7 @@ def dummy_tar_stream(self, n_bytes):
152150
@contextlib.contextmanager
153151
def dummy_tar_file(self, n_bytes):
154152
'''Yields the name of a valid tar file of size n_bytes.'''
155-
with tempfile.NamedTemporaryFile() as tar_file:
153+
with tempfile.NamedTemporaryFile(delete=False) as tar_file:
156154
self.write_dummy_tar_content(n_bytes, tar_file)
157155
tar_file.seek(0)
158156
yield tar_file.name

tests/integration/network_test.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,18 @@ def test_create_network_with_ipam_config(self):
6969

7070
assert ipam.pop('Options', None) is None
7171

72-
assert ipam == {
73-
'Driver': 'default',
74-
'Config': [{
75-
'Subnet': "172.28.0.0/16",
76-
'IPRange': "172.28.5.0/24",
77-
'Gateway': "172.28.5.254",
78-
'AuxiliaryAddresses': {
79-
"a": "172.28.1.5",
80-
"b": "172.28.1.6",
81-
"c": "172.28.1.7",
82-
},
83-
}],
84-
}
72+
assert ipam['Driver'] == 'default'
73+
74+
assert ipam['Config'] == [{
75+
'Subnet': "172.28.0.0/16",
76+
'IPRange': "172.28.5.0/24",
77+
'Gateway': "172.28.5.254",
78+
'AuxiliaryAddresses': {
79+
"a": "172.28.1.5",
80+
"b": "172.28.1.6",
81+
"c": "172.28.1.7",
82+
},
83+
}]
8584

8685
@requires_api_version('1.21')
8786
def test_create_network_with_host_driver_fails(self):

tests/unit/api_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def fake_delete(self, url, *args, **kwargs):
8686
def fake_read_from_socket(self, response, stream):
8787
return six.binary_type()
8888

89-
url_base = 'http+docker://localunixsocket/'
89+
url_base = '{0}/'.format(fake_api.prefix)
9090
url_prefix = '{0}v{1}/'.format(
9191
url_base,
9292
docker.constants.DEFAULT_DOCKER_API_VERSION)
@@ -422,6 +422,9 @@ def early_response_sending_handler(self, connection):
422422

423423
data += connection.recv(2048)
424424

425+
@pytest.mark.skipif(
426+
docker.constants.IS_WINDOWS_PLATFORM, reason='Unix only'
427+
)
425428
def test_early_stream_response(self):
426429
self.request_handler = self.early_response_sending_handler
427430
lines = []

tests/unit/container_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ def test_create_container_with_entrypoint(self):
270270
{'Content-Type': 'application/json'})
271271

272272
def test_create_container_with_cpu_shares(self):
273-
self.client.create_container('busybox', 'ls',
274-
cpu_shares=5)
273+
with pytest.deprecated_call():
274+
self.client.create_container('busybox', 'ls', cpu_shares=5)
275275

276276
args = fake_request.call_args
277277
self.assertEqual(args[0][1],
@@ -316,8 +316,8 @@ def test_create_container_with_host_config_cpu_shares(self):
316316
{'Content-Type': 'application/json'})
317317

318318
def test_create_container_with_cpuset(self):
319-
self.client.create_container('busybox', 'ls',
320-
cpuset='0,1')
319+
with pytest.deprecated_call():
320+
self.client.create_container('busybox', 'ls', cpuset='0,1')
321321

322322
args = fake_request.call_args
323323
self.assertEqual(args[0][1],

tests/unit/fake_api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ def post_fake_update_container():
408408

409409
# Maps real api url to fake response callback
410410
prefix = 'http+docker://localunixsocket'
411+
if constants.IS_WINDOWS_PLATFORM:
412+
prefix = 'http+docker://localnpipe'
413+
411414
fake_responses = {
412415
'{0}/version'.format(prefix):
413416
get_fake_raw_version,

0 commit comments

Comments
 (0)