Backport of 5eef093e99
(#1548)
parent
54754f7e81
commit
ecbdaca971
|
@ -0,0 +1,3 @@
|
||||||
|
bugfixes:
|
||||||
|
- "docker_image - report error when loading a broken archive that contains no image (https://github.com/ansible-collections/community.docker/issues/46, https://github.com/ansible-collections/community.docker/pull/55)."
|
||||||
|
- "docker_image - report error when the loaded archive does not contain the specified image (https://github.com/ansible-collections/community.docker/issues/41, https://github.com/ansible-collections/community.docker/pull/55)."
|
|
@ -749,7 +749,7 @@ class ImageManager(DockerBaseClass):
|
||||||
# line = json.loads(line)
|
# line = json.loads(line)
|
||||||
self.log(line, pretty_print=True)
|
self.log(line, pretty_print=True)
|
||||||
if "stream" in line or "status" in line:
|
if "stream" in line or "status" in line:
|
||||||
build_line = line.get("stream") or line.get("status")
|
build_line = line.get("stream") or line.get("status") or ''
|
||||||
build_output.append(build_line)
|
build_output.append(build_line)
|
||||||
|
|
||||||
if line.get('error'):
|
if line.get('error'):
|
||||||
|
@ -774,17 +774,45 @@ class ImageManager(DockerBaseClass):
|
||||||
|
|
||||||
:return: image dict
|
:return: image dict
|
||||||
'''
|
'''
|
||||||
|
# Load image(s) from file
|
||||||
|
load_output = []
|
||||||
try:
|
try:
|
||||||
self.log("Opening image %s" % self.load_path)
|
self.log("Opening image %s" % self.load_path)
|
||||||
with open(self.load_path, 'rb') as image_tar:
|
with open(self.load_path, 'rb') as image_tar:
|
||||||
self.log("Loading image from %s" % self.load_path)
|
self.log("Loading image from %s" % self.load_path)
|
||||||
self.client.load_image(image_tar)
|
for line in self.client.load_image(image_tar):
|
||||||
|
self.log(line, pretty_print=True)
|
||||||
|
if "stream" in line or "status" in line:
|
||||||
|
load_line = line.get("stream") or line.get("status") or ''
|
||||||
|
load_output.append(load_line)
|
||||||
except EnvironmentError as exc:
|
except EnvironmentError as exc:
|
||||||
if exc.errno == errno.ENOENT:
|
if exc.errno == errno.ENOENT:
|
||||||
self.fail("Error opening image %s - %s" % (self.load_path, str(exc)))
|
self.client.fail("Error opening image %s - %s" % (self.load_path, str(exc)))
|
||||||
self.fail("Error loading image %s - %s" % (self.name, str(exc)))
|
self.client.fail("Error loading image %s - %s" % (self.name, str(exc)), stdout='\n'.join(load_output))
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.fail("Error loading image %s - %s" % (self.name, str(exc)))
|
self.client.fail("Error loading image %s - %s" % (self.name, str(exc)), stdout='\n'.join(load_output))
|
||||||
|
|
||||||
|
# Collect loaded images
|
||||||
|
loaded_images = set()
|
||||||
|
for line in load_output:
|
||||||
|
if line.startswith('Loaded image:'):
|
||||||
|
loaded_images.add(line[len('Loaded image:'):].strip())
|
||||||
|
|
||||||
|
if not loaded_images:
|
||||||
|
self.client.fail("Detected no loaded images. Archive potentially corrupt?", stdout='\n'.join(load_output))
|
||||||
|
|
||||||
|
expected_image = '%s:%s' % (self.name, self.tag)
|
||||||
|
if expected_image not in loaded_images:
|
||||||
|
self.client.fail(
|
||||||
|
"The archive did not contain image '%s'. Instead, found %s." % (
|
||||||
|
expected_image, ', '.join(["'%s'" % image for image in sorted(loaded_images)])),
|
||||||
|
stdout='\n'.join(load_output))
|
||||||
|
loaded_images.remove(expected_image)
|
||||||
|
|
||||||
|
if loaded_images:
|
||||||
|
self.client.module.warn(
|
||||||
|
"The archive contained more images than specified: %s" % (
|
||||||
|
', '.join(["'%s'" % image for image in sorted(loaded_images)]), ))
|
||||||
|
|
||||||
return self.client.find_image(self.name, self.tag)
|
return self.client.find_image(self.name, self.tag)
|
||||||
|
|
||||||
|
|
|
@ -189,6 +189,11 @@
|
||||||
source: pull
|
source: pull
|
||||||
register: archive_image
|
register: archive_image
|
||||||
|
|
||||||
|
- name: Create invalid archive
|
||||||
|
copy:
|
||||||
|
dest: "{{ output_dir }}/image-invalid.tar"
|
||||||
|
content: "this is not a valid image"
|
||||||
|
|
||||||
- name: remove image
|
- name: remove image
|
||||||
docker_image:
|
docker_image:
|
||||||
name: "{{ docker_test_image_hello_world }}"
|
name: "{{ docker_test_image_hello_world }}"
|
||||||
|
@ -209,11 +214,32 @@
|
||||||
source: load
|
source: load
|
||||||
register: load_image_1
|
register: load_image_1
|
||||||
|
|
||||||
|
- name: load image (wrong name)
|
||||||
|
docker_image:
|
||||||
|
name: foo:bar
|
||||||
|
load_path: "{{ output_dir }}/image.tar"
|
||||||
|
source: load
|
||||||
|
register: load_image_2
|
||||||
|
ignore_errors: true
|
||||||
|
|
||||||
|
- name: load image (invalid image)
|
||||||
|
docker_image:
|
||||||
|
name: foo:bar
|
||||||
|
load_path: "{{ output_dir }}/image-invalid.tar"
|
||||||
|
source: load
|
||||||
|
register: load_image_3
|
||||||
|
ignore_errors: true
|
||||||
|
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- load_image is changed
|
- load_image is changed
|
||||||
- load_image_1 is not changed
|
|
||||||
- archive_image['image']['Id'] == load_image['image']['Id']
|
- archive_image['image']['Id'] == load_image['image']['Id']
|
||||||
|
- load_image_1 is not changed
|
||||||
|
- load_image_2 is failed
|
||||||
|
- >-
|
||||||
|
"The archive did not contain image 'foo:bar'. Instead, found '" ~ docker_test_image_hello_world ~ "'." == load_image_2.msg
|
||||||
|
- load_image_3 is failed
|
||||||
|
- '"Detected no loaded images. Archive potentially corrupt?" == load_image_3.msg'
|
||||||
|
|
||||||
####################################################################
|
####################################################################
|
||||||
## path ############################################################
|
## path ############################################################
|
||||||
|
|
Loading…
Reference in New Issue