From 20042ea780740cc7dfa90c98dec55678f8f58b98 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 10 Feb 2025 23:19:54 +0100 Subject: [PATCH] Add basic podman tests to CI (#1040) * Setup podman and run some basic tests with it. * Clean up Docker setup. --- .../targets/docker_host_info/meta/main.yml | 1 + .../targets/docker_host_info/tasks/main.yml | 23 +++ .../targets/setup_docker/handlers/main.yml | 4 +- .../integration/targets/setup_podman/aliases | 5 + .../targets/setup_podman/defaults/main.yml | 14 ++ .../targets/setup_podman/handlers/main.yml | 12 ++ .../targets/setup_podman/meta/main.yml | 7 + .../targets/setup_podman/tasks/main.yml | 163 ++++++++++++++++++ .../targets/setup_podman/vars/Alpine.yml | 6 + .../targets/setup_podman/vars/RedHat-7.yml | 8 + .../targets/setup_podman/vars/Ubuntu-20.yml | 7 + .../targets/setup_podman/vars/default.yml | 4 + 12 files changed, 252 insertions(+), 2 deletions(-) create mode 100644 tests/integration/targets/setup_podman/aliases create mode 100644 tests/integration/targets/setup_podman/defaults/main.yml create mode 100644 tests/integration/targets/setup_podman/handlers/main.yml create mode 100644 tests/integration/targets/setup_podman/meta/main.yml create mode 100644 tests/integration/targets/setup_podman/tasks/main.yml create mode 100644 tests/integration/targets/setup_podman/vars/Alpine.yml create mode 100644 tests/integration/targets/setup_podman/vars/RedHat-7.yml create mode 100644 tests/integration/targets/setup_podman/vars/Ubuntu-20.yml create mode 100644 tests/integration/targets/setup_podman/vars/default.yml diff --git a/tests/integration/targets/docker_host_info/meta/main.yml b/tests/integration/targets/docker_host_info/meta/main.yml index 471ddd41..ca004bc0 100644 --- a/tests/integration/targets/docker_host_info/meta/main.yml +++ b/tests/integration/targets/docker_host_info/meta/main.yml @@ -6,3 +6,4 @@ dependencies: - setup_docker - setup_docker_python_deps + - setup_podman diff --git a/tests/integration/targets/docker_host_info/tasks/main.yml b/tests/integration/targets/docker_host_info/tasks/main.yml index e26790f3..d2bc012b 100644 --- a/tests/integration/targets/docker_host_info/tasks/main.yml +++ b/tests/integration/targets/docker_host_info/tasks/main.yml @@ -13,3 +13,26 @@ - fail: msg="Too old docker / docker-py version to run docker_host_info tests!" when: not(docker_api_version is version('1.25', '>=')) and (ansible_distribution != 'CentOS' or ansible_distribution_major_version|int > 6) + +- when: podman_cli_version is version('1.0.0', '>=') + block: + - name: Get Podman context + community.docker.docker_context_info: + name: podman + register: docker_podman_context + + - name: Run tests with Podman context + module_defaults: + group/community.docker.docker: "{{ docker_podman_context.contexts[0].config }}" + block: + + - name: Get info on Podman host + docker_host_info: + register: output + + - name: Check for some Podman specific values + assert: + that: + - output.host_info.ProductLicense == 'Apache-2.0' + - >- + "Rootless" in output.host_info diff --git a/tests/integration/targets/setup_docker/handlers/main.yml b/tests/integration/targets/setup_docker/handlers/main.yml index ec68f655..fc526152 100644 --- a/tests/integration/targets/setup_docker/handlers/main.yml +++ b/tests/integration/targets/setup_docker/handlers/main.yml @@ -3,10 +3,10 @@ # 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: Remove docker pagkages +- name: Remove docker packages action: "{{ ansible_facts.pkg_mgr }}" args: name: "{{ docker_cleanup_packages }}" state: absent listen: cleanup docker - when: not docker_skip_cleanup | bool + when: not docker_skip_cleanup diff --git a/tests/integration/targets/setup_podman/aliases b/tests/integration/targets/setup_podman/aliases new file mode 100644 index 00000000..0a430dff --- /dev/null +++ b/tests/integration/targets/setup_podman/aliases @@ -0,0 +1,5 @@ +# 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 + +needs/target/setup_epel diff --git a/tests/integration/targets/setup_podman/defaults/main.yml b/tests/integration/targets/setup_podman/defaults/main.yml new file mode 100644 index 00000000..5217a34b --- /dev/null +++ b/tests/integration/targets/setup_podman/defaults/main.yml @@ -0,0 +1,14 @@ +--- +# 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 + +has_podman: true +podman_cli_version: '0.0' +podman_skip_cleanup: true +podman_packages: + - podman +podman_socket_service: podman.socket + +podman_cleanup_packages: + - podman diff --git a/tests/integration/targets/setup_podman/handlers/main.yml b/tests/integration/targets/setup_podman/handlers/main.yml new file mode 100644 index 00000000..e7d6ca9c --- /dev/null +++ b/tests/integration/targets/setup_podman/handlers/main.yml @@ -0,0 +1,12 @@ +--- +# 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: Remove podman packages + action: "{{ ansible_facts.pkg_mgr }}" + args: + name: "{{ podman_cleanup_packages }}" + state: absent + listen: cleanup podman + when: not podman_skip_cleanup diff --git a/tests/integration/targets/setup_podman/meta/main.yml b/tests/integration/targets/setup_podman/meta/main.yml new file mode 100644 index 00000000..2fcd152f --- /dev/null +++ b/tests/integration/targets/setup_podman/meta/main.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 + +dependencies: + - setup_pkg_mgr diff --git a/tests/integration/targets/setup_podman/tasks/main.yml b/tests/integration/targets/setup_podman/tasks/main.yml new file mode 100644 index 00000000..5cba8e3b --- /dev/null +++ b/tests/integration/targets/setup_podman/tasks/main.yml @@ -0,0 +1,163 @@ +--- +# 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 # +#################################################################### + +- name: Setup Podman + when: ansible_facts.distribution ~ ansible_facts.distribution_major_version not in ['CentOS6', 'RedHat6'] + block: + - name: Detect whether we are running inside a container + current_container_facts: + + - name: Look for marker whether Podman was already set up + stat: + path: /root/community.docker-podman-is-set-up + register: podman_setup_marker + + - name: Figure out user ID + command: id -u + register: podman_user_id + + - when: not podman_setup_marker.stat.exists and not ansible_module_running_in_container + block: + - name: + debug: + msg: |- + OS family: {{ ansible_facts.os_family }} + Distribution: {{ ansible_facts.distribution }} + Distribution major version: {{ ansible_facts.distribution_major_version }} + Distribution full version: {{ ansible_facts.distribution_version }} + + - name: Include distribution specific variables + include_vars: "{{ lookup('first_found', params) }}" + vars: + params: + files: + - "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml" + - "{{ ansible_facts.os_family }}-{{ ansible_facts.distribution_major_version }}.yml" + - "{{ ansible_facts.distribution }}.yml" + - "{{ ansible_facts.os_family }}.yml" + - default.yml + paths: + - "{{ role_path }}/vars" + + - when: has_podman + block: + - name: Install podman + package: + name: "{{ podman_packages }}" + notify: cleanup podman + + - name: Start podman socket for this user + systemd_service: + name: "{{ podman_socket_service }}" + state: started + scope: "{{ 'global' if podman_user_id.stdout == '0' else 'user' }}" + environment: + XDG_RUNTIME_DIR: "{{ '/run' if podman_user_id.stdout == '0' else ('/run/user/' ~ podman_user_id.stdout) }}" + + - name: Set marker that Podman was already set up + file: + path: /root/community.docker-podman-is-set-up + state: touch + when: podman_skip_cleanup + + - when: not has_podman or ansible_module_running_in_container + block: + - set_fact: + podman_cli_version: "0.0" + podman_socket: "not available" + + - when: has_podman and not ansible_module_running_in_container + block: + - name: Check Podman CLI version + command: "podman version -f {% raw %}'{{.Client.Version}}'{% endraw %}" + register: podman_cli_version_stdout + ignore_errors: true + + - set_fact: + podman_cli_version: "{{ podman_cli_version_stdout.stdout | default('0.0', true) }}" + podman_socket: "unix://{{ '/run' if podman_user_id.stdout == '0' else ('/run/user/' ~ podman_user_id.stdout) }}/podman/podman.sock" + + - name: Create podman Docker context + command: + cmd: >- + docker context + create podman + --description "Podman" + --docker "host={{ podman_socket }}" + ignore_errors: true + + - debug: + msg: |- + Podman CLI version: {{ podman_cli_version }} + Podman socket: {{ podman_socket }} + + - when: podman_cli_version is version('0.0', '>') + block: + # Cleanup podman + - name: Show all containers + command: 'podman ps --no-trunc --format {% raw %}"{{.Names}}"{% endraw %}' + + - name: "Remove all ansible-docker-test-* podman containers" + shell: 'podman ps --no-trunc --format {% raw %}"{{.Names}}"{% endraw %} | grep "^ansible-docker-test-" | xargs -r podman container rm -f' + register: podman_containers + retries: 3 + delay: 3 + until: podman_containers is success + ignore_errors: true + + - name: "Remove all ansible-docker-test-* podman volumes" + shell: 'podman volume ls --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-docker-test-" | xargs -r podman volume rm -f' + register: podman_volumes + ignore_errors: true + + - name: "Remove all ansible-docker-test-* podman networks" + shell: 'podman network ls --no-trunc --format {% raw %}"{{.Name}}"{% endraw %} | grep "^ansible-docker-test-" | xargs -r podman network rm' + register: podman_networks + ignore_errors: true + + - name: Cleaned podman resources + debug: + var: podman_resources + vars: + podman_resources: + containers: "{{ podman_containers.stdout_lines | default([]) }}" + volumes: "{{ podman_volumes.stdout_lines | default([]) }}" + networks: "{{ podman_networks.stdout_lines | default([]) }}" + + # List all existing podman resources + - name: List all podman containers + command: podman ps --no-trunc -a + register: podman_containers + ignore_errors: true + + - name: List all podman volumes + command: podman volume ls + register: podman_volumes + ignore_errors: true + + - name: List all podman networks + command: podman network ls --no-trunc + register: podman_networks + ignore_errors: true + + - name: List all podman images + command: podman images --no-trunc -a + register: podman_images + ignore_errors: true + + - name: Still existing podman resources + debug: + var: podman_resources + vars: + podman_resources: + containers: "{{ podman_containers.stdout_lines | default([]) }}" + volumes: "{{ podman_volumes.stdout_lines | default([]) }}" + networks: "{{ podman_networks.stdout_lines | default([]) }}" + images: "{{ podman_images.stdout_lines | default([]) }}" diff --git a/tests/integration/targets/setup_podman/vars/Alpine.yml b/tests/integration/targets/setup_podman/vars/Alpine.yml new file mode 100644 index 00000000..c74a9b30 --- /dev/null +++ b/tests/integration/targets/setup_podman/vars/Alpine.yml @@ -0,0 +1,6 @@ +--- +# 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 + +podman_socket_service: podman diff --git a/tests/integration/targets/setup_podman/vars/RedHat-7.yml b/tests/integration/targets/setup_podman/vars/RedHat-7.yml new file mode 100644 index 00000000..cb149ddd --- /dev/null +++ b/tests/integration/targets/setup_podman/vars/RedHat-7.yml @@ -0,0 +1,8 @@ +--- +# 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 + +# While CentOS 7 has podman, it doesn't come with a podman socket, so we treat it as whether it doesn't exist at all... +has_podman: false +podman_packages: [] diff --git a/tests/integration/targets/setup_podman/vars/Ubuntu-20.yml b/tests/integration/targets/setup_podman/vars/Ubuntu-20.yml new file mode 100644 index 00000000..aee6a58b --- /dev/null +++ b/tests/integration/targets/setup_podman/vars/Ubuntu-20.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 + +has_podman: false +podman_packages: [] diff --git a/tests/integration/targets/setup_podman/vars/default.yml b/tests/integration/targets/setup_podman/vars/default.yml new file mode 100644 index 00000000..f55df21f --- /dev/null +++ b/tests/integration/targets/setup_podman/vars/default.yml @@ -0,0 +1,4 @@ +--- +# 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