Skip to content

Commit 30d16ce

Browse files
committed
Update DockerClient.images.pull to always stream response
Also raise a warning when users attempt to specify the "stream" parameter Signed-off-by: Joffrey F <[email protected]>
1 parent 7117855 commit 30d16ce

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

docker/models/images.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import itertools
22
import re
3+
import warnings
34

45
import six
56

@@ -425,8 +426,21 @@ def pull(self, repository, tag=None, **kwargs):
425426
if not tag:
426427
repository, tag = parse_repository_tag(repository)
427428

428-
kwargs['stream'] = False
429-
self.client.api.pull(repository, tag=tag, **kwargs)
429+
if 'stream' in kwargs:
430+
warnings.warn(
431+
'`stream` is not a valid parameter for this method'
432+
' and will be overridden'
433+
)
434+
del kwargs['stream']
435+
436+
pull_log = self.client.api.pull(
437+
repository, tag=tag, stream=True, **kwargs
438+
)
439+
for _ in pull_log:
440+
# We don't do anything with the logs, but we need
441+
# to keep the connection alive and wait for the image
442+
# to be pulled.
443+
pass
430444
if tag:
431445
return self.get('{0}{2}{1}'.format(
432446
repository, tag, '@' if tag.startswith('sha256:') else ':'

tests/unit/models_containers_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ def test_run_pull(self):
232232
container = client.containers.run('alpine', 'sleep 300', detach=True)
233233

234234
assert container.id == FAKE_CONTAINER_ID
235-
client.api.pull.assert_called_with('alpine', platform=None, tag=None,
236-
stream=False)
235+
client.api.pull.assert_called_with(
236+
'alpine', platform=None, tag=None, stream=True
237+
)
237238

238239
def test_run_with_error(self):
239240
client = make_fake_client()

tests/unit/models_images_test.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import unittest
2+
import warnings
3+
14
from docker.constants import DEFAULT_DATA_CHUNK_SIZE
25
from docker.models.images import Image
3-
import unittest
46

57
from .fake_api import FAKE_IMAGE_ID
68
from .fake_api_client import make_fake_client
@@ -43,17 +45,19 @@ def test_load(self):
4345
def test_pull(self):
4446
client = make_fake_client()
4547
image = client.images.pull('test_image:latest')
46-
client.api.pull.assert_called_with('test_image', tag='latest',
47-
stream=False)
48+
client.api.pull.assert_called_with(
49+
'test_image', tag='latest', stream=True
50+
)
4851
client.api.inspect_image.assert_called_with('test_image:latest')
4952
assert isinstance(image, Image)
5053
assert image.id == FAKE_IMAGE_ID
5154

5255
def test_pull_multiple(self):
5356
client = make_fake_client()
5457
images = client.images.pull('test_image')
55-
client.api.pull.assert_called_with('test_image', tag=None,
56-
stream=False)
58+
client.api.pull.assert_called_with(
59+
'test_image', tag=None, stream=True
60+
)
5761
client.api.images.assert_called_with(
5862
all=False, name='test_image', filters=None
5963
)
@@ -63,6 +67,16 @@ def test_pull_multiple(self):
6367
assert isinstance(image, Image)
6468
assert image.id == FAKE_IMAGE_ID
6569

70+
def test_pull_with_stream_param(self):
71+
client = make_fake_client()
72+
with warnings.catch_warnings(record=True) as w:
73+
client.images.pull('test_image', stream=True)
74+
75+
assert len(w) == 1
76+
assert str(w[0].message).startswith(
77+
'`stream` is not a valid parameter'
78+
)
79+
6680
def test_push(self):
6781
client = make_fake_client()
6882
client.images.push('foobar', insecure_registry=True)

0 commit comments

Comments
 (0)