Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docker/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ class BuildError(Exception):
pass


class ImageLoadError(DockerException):
pass


def create_unexpected_kwargs_error(name, kwargs):
quoted_kwargs = ["'{}'".format(k) for k in sorted(kwargs)]
text = ["{}() ".format(name)]
Expand Down
20 changes: 17 additions & 3 deletions docker/models/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import six

from ..api import APIClient
from ..errors import BuildError
from ..errors import BuildError, ImageLoadError
from ..utils.json_stream import json_stream
from .resource import Collection, Model

Expand Down Expand Up @@ -241,13 +241,27 @@ def load(self, data):
data (binary): Image data to be loaded.

Returns:
(generator): Progress output as JSON objects
(list of :py:class:`Image`): The images.

Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.
"""
return self.client.api.load_image(data)
resp = self.client.api.load_image(data)
images = []
for chunk in resp:
if 'stream' in chunk:
match = re.search(
r'(^Loaded image ID: |^Loaded image: )(.+)$',
chunk['stream']
)
if match:
image_id = match.group(2)
images.append(image_id)
if 'error' in chunk:
raise ImageLoadError(chunk['error'])

return [self.get(i) for i in images]

def pull(self, name, tag=None, **kwargs):
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/models_images_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def test_pull_with_tag(self):
image = client.images.pull('alpine', tag='3.3')
assert 'alpine:3.3' in image.attrs['RepoTags']

def test_load_error(self):
client = docker.from_env(version=TEST_API_VERSION)
with pytest.raises(docker.errors.ImageLoadError):
client.images.load('abc')


class ImageTest(BaseIntegrationTest):

Expand Down