From a740cfa0c4b11798a6211d2c5e58a5b1bdf9c3aa Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 19 Oct 2024 22:07:06 +0200 Subject: [PATCH] Add more tests. (#980) --- .../tasks/tests/container-exit.yml | 70 +++++++++++++++++++ .../plugins/module_utils/test_compose_v2.py | 66 +++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 tests/integration/targets/docker_compose_v2/tasks/tests/container-exit.yml diff --git a/tests/integration/targets/docker_compose_v2/tasks/tests/container-exit.yml b/tests/integration/targets/docker_compose_v2/tasks/tests/container-exit.yml new file mode 100644 index 000000000..bdf0c0c25 --- /dev/null +++ b/tests/integration/targets/docker_compose_v2/tasks/tests/container-exit.yml @@ -0,0 +1,70 @@ +--- +# 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 + +- vars: + pname: "{{ name_prefix }}-exit" + cname: "{{ name_prefix }}-container" + project_src: "{{ remote_tmp_dir }}/{{ pname }}" + test_service: | + services: + {{ cname }}: + image: "{{ docker_test_image_alpine }}" + command: '/bin/sh -c "exit 0"' + stop_grace_period: 1s + + block: + - name: Registering container name + set_fact: + cnames: "{{ cnames + [pname ~ '-' ~ cname ~ '-1'] }}" + dnetworks: "{{ dnetworks + [pname ~ '_default'] }}" + + - name: Create project directory + file: + path: '{{ project_src }}' + state: directory + + - name: Template default project file + copy: + dest: '{{ project_src }}/docker-compose.yml' + content: '{{ test_service }}' + + - name: Present (check) + docker_compose_v2: + project_src: '{{ project_src }}' + state: present + wait: true + check_mode: true + register: present_1_check + + - name: Present + docker_compose_v2: + project_src: '{{ project_src }}' + state: present + wait: true + register: present_1 + ignore_errors: true + + - name: Cleanup + docker_compose_v2: + project_src: '{{ project_src }}' + state: absent + + - assert: + that: + - present_1_check is changed + - present_1_check.warnings | default([]) | select('regex', ' Please report this at ') | length == 0 + - present_1 is failed + - present_1.containers | length == 1 + - present_1.containers[0].Name == pname ~ '-' ~ cname ~ '-1' + - present_1.containers[0].Image == docker_test_image_alpine + - present_1.containers[0].State == 'exited' + - present_1.containers[0].ExitCode == 0 + - present_1.images | length == 1 + - present_1.images[0].ContainerName == pname ~ '-' ~ cname ~ '-1' + - present_1.images[0].Repository == (docker_test_image_alpine | split(':') | first) + - present_1.images[0].Tag == (docker_test_image_alpine | split(':') | last) + - present_1.warnings | default([]) | select('regex', ' Please report this at ') | length == 0 + - >- + ("container " ~ pname ~ '-' ~ cname ~ "-1 exited (0)") in present_1.msg diff --git a/tests/unit/plugins/module_utils/test_compose_v2.py b/tests/unit/plugins/module_utils/test_compose_v2.py index 186eabf87..809999272 100644 --- a/tests/unit/plugins/module_utils/test_compose_v2.py +++ b/tests/unit/plugins/module_utils/test_compose_v2.py @@ -283,6 +283,72 @@ ], [], ), + ( + # https://github.com/ansible-collections/community.docker/issues/978 + '2.28.1-unknown', # TODO: find out actual version! + '2.28.1', # TODO: find out actual version! + False, + True, + " Network create_users_db_default Creating\n" + " Network create_users_db_default Created\n" + " Container create_users_db-init Creating\n" + " Container create_users_db-init Created\n" + " Container create_users_db-init Starting\n" + " Container create_users_db-init Started\n" + " Container create_users_db-init Waiting\n" + "container create_users_db-init exited (0)\n", + [ + Event( + 'network', + 'create_users_db_default', + 'Creating', + None, + ), + Event( + 'network', + 'create_users_db_default', + 'Created', + None, + ), + Event( + 'container', + 'create_users_db-init', + 'Creating', + None, + ), + Event( + 'container', + 'create_users_db-init', + 'Created', + None, + ), + Event( + 'container', + 'create_users_db-init', + 'Starting', + None, + ), + Event( + 'container', + 'create_users_db-init', + 'Started', + None, + ), + Event( + 'container', + 'create_users_db-init', + 'Waiting', + None, + ), + Event( + 'unknown', + '', + 'Error', + 'container create_users_db-init exited (0)', + ), + ], + [], + ), ] _ALL_TEST_CASES = EVENT_TEST_CASES + EXTRA_TEST_CASES