Add docker_image_export module (#774)

* Add docker_image_export module.

* Add basic tests.

* Add more seealsos.
This commit is contained in:
Felix Fontein
2024-01-22 22:03:38 +01:00
committed by GitHub
parent a53ecb6e66
commit b2a79d9eb7
14 changed files with 509 additions and 43 deletions
@@ -0,0 +1,13 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
####################################################################
# WARNING: These are designed specifically for Ansible tests #
# and should not be used as examples of how to write Ansible roles #
####################################################################
- when: ansible_facts.distribution ~ ansible_facts.distribution_major_version not in ['CentOS6', 'RedHat6']
include_tasks:
file: test.yml
@@ -0,0 +1,7 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: "Loading tasks from {{ test_name }}"
include_tasks: "{{ test_name }}"
@@ -0,0 +1,39 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: Create random name prefix
set_fact:
name_prefix: "{{ 'ansible-docker-test-%0x' % ((2**32) | random) }}"
- name: Create image and container list
set_fact:
inames: []
cnames: []
- debug:
msg: "Using name prefix {{ name_prefix }}"
- block:
- include_tasks: run-test.yml
with_fileglob:
- "tests/*.yml"
loop_control:
loop_var: test_name
always:
- name: "Make sure all images are removed"
docker_image_remove:
name: "{{ item }}"
with_items: "{{ inames }}"
- name: "Make sure all containers are removed"
docker_container:
name: "{{ item }}"
state: absent
force_kill: true
with_items: "{{ cnames }}"
when: docker_api_version is version('1.25', '>=')
- fail: msg="Too old docker / docker-py version to run docker_image tests!"
when: not(docker_api_version is version('1.25', '>=')) and (ansible_distribution != 'CentOS' or ansible_distribution_major_version|int > 6)
@@ -0,0 +1,69 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- set_fact:
image_names:
- "{{ docker_test_image_hello_world }}"
- "{{ docker_test_image_alpine_different }}"
- "{{ docker_test_image_alpine }}"
- name: Make sure images are there
docker_image_pull:
name: "{{ item }}"
register: images
loop: "{{ image_names }}"
- vars:
image_ids: "{{ images.results | map(attribute='image') | map(attribute='Id') | list }}"
all_images: "{{ image_names + (images.results | map(attribute='image') | map(attribute='Id') | list) }}"
image_tasks:
- file: archive-1.tar
images: "{{ image_names }}"
- file: archive-2.tar
images: "{{ image_ids }}"
- file: archive-3.tar
images:
- "{{ image_names[0] }}"
- "{{ image_ids[1] }}"
- file: archive-4.tar
images:
- "{{ image_ids[0] }}"
- "{{ image_names[0] }}"
- file: archive-5.tar
images:
- "{{ image_ids[0] }}"
block:
- name: Create archives
docker_image_export:
names: "{{ item.images }}"
path: "{{ remote_tmp_dir }}/{{ item.file }}"
loop: "{{ image_tasks }}"
loop_control:
label: "{{ item.file }}"
register: result
- name: Extract manifest.json files
command: tar xvf "{{ remote_tmp_dir }}/{{ item.file }}" manifest.json --to-stdout
loop: "{{ image_tasks }}"
loop_control:
label: "{{ item.file }}"
register: manifests
- name: Do basic tests
assert:
that:
- item.0.images | length == item.1 | length
- item.1 | unique | length == item.2 | length
- manifest_json_images == export_image_ids
loop: "{{ image_tasks | zip(export_images, manifests_json) }}"
loop_control:
label: "{{ item.0.file }}"
vars:
filenames: "{{ image_tasks | map(attribute='file') }}"
export_images: "{{ result.results | map(attribute='images') | map('map', attribute='Id') }}"
manifests_json: "{{ manifests.results | map(attribute='stdout') | map('from_json') }}"
manifest_json_images: "{{ item.2 | map(attribute='Config') | map('regex_replace', '.json$', '') | map('regex_replace', '^blobs/sha256/', '') | sort }}"
export_image_ids: "{{ item.1 | map('regex_replace', '^sha256:', '') | unique | sort }}"