From f55a3f8154607a8913991f4d8f316b1f3ff02f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20El=20Amri?= Date: Sun, 2 Apr 2023 15:43:43 +0200 Subject: [PATCH] Fix tests for "present" --- plugins/modules/docker_compose_v2.py | 45 +++++++++------ .../targets/docker_compose_v2/tasks/main.yml | 5 +- .../tasks/tests/start-stop.yml | 57 ++++++++----------- 3 files changed, 52 insertions(+), 55 deletions(-) diff --git a/plugins/modules/docker_compose_v2.py b/plugins/modules/docker_compose_v2.py index e6a3461f5..37f8eae1e 100644 --- a/plugins/modules/docker_compose_v2.py +++ b/plugins/modules/docker_compose_v2.py @@ -369,7 +369,7 @@ import sys if sys.version_info[0] == 3 and sys.version_info[1] >= 5: - from typing import List, Optional, Tuple, Union, FrozenSet, Dict, Type, TYPE_CHECKING, Any + from typing import List, Optional, Tuple, Union, FrozenSet, Dict, Type, TYPE_CHECKING, Any, NamedTuple if sys.version_info[1] >= 8: from typing import Literal, Final else: @@ -435,6 +435,7 @@ 'Stopped', 'Killed', 'Removed', + 'Recreated', }) # type: Final[FrozenSet[Text]] @@ -469,23 +470,31 @@ def from_docker_compose_event(cls, resource_type): }[resource_type] -if TYPE_CHECKING: - EVENT = Tuple[Any, Text, Text] - ResourceEvent = namedtuple( 'ResourceEvent', ['resource_type', 'resource_id', 'status'] ) +if TYPE_CHECKING: + ResourceEvent = NamedTuple( + 'ResourceEvent', + [('resource_type', ResourceType), ('resource_id', Text), ('status', Text)] + ) + + _re_resource_event = re.compile( r'^' + r'\s*' r'(?PNetwork|Image|Volume|Container)' - r' ' - r'(?P.+)' - r' ' - r'(?P%s)' % ( - "|".join(STATUS_DONE | STATUS_WORKING | STATUS_ERROR) + r'\s+' + r'(?P[^\s]+)' + r'\s+' + r'(?P%s)' + r'\s*' + r'$' + % ( + "|".join(sorted(STATUS_DONE | STATUS_WORKING | STATUS_ERROR, key=lambda e: len(e), reverse=True)) ) ) @@ -501,8 +510,8 @@ def __init__(self, module, docker_host): @staticmethod def _parse_stderr(stderr): - # type: (Text) -> List[EVENT] - events = [] # type: List[EVENT] + # type: (Text) -> List[ResourceEvent] + events = [] # type: List[ResourceEvent] for line in stderr.splitlines(): line = line.strip() match = _re_resource_event.match(line) @@ -524,7 +533,7 @@ def _run_subcommand( profiles=None, # type: Optional[List[Text]] env_file=None, # type: Optional[Text] ): - # type: (...) -> Tuple[int, Text, Text, List[EVENT]] + # type: (...) -> Tuple[int, Text, Text, List[ResourceEvent]] if profiles is None: profiles = [] command = [DOCKER_COMPOSE_EXECUTABLE, '--ansi', 'never'] @@ -578,7 +587,7 @@ def up( remove_orphans=False, # type: bool timeout=None, # type: Optional[int] ): - # type: (...) -> Tuple[int, Text, Text, List[EVENT]] + # type: (...) -> Tuple[int, Text, Text, List[ResourceEvent]] if profiles is None: profiles = [] if services is None: @@ -625,7 +634,7 @@ def down( volumes=False, # type: bool timeout=None, # type: Optional[int] ): - # type: (...) -> Tuple[int, Text, Text, List[EVENT]] + # type: (...) -> Tuple[int, Text, Text, List[ResourceEvent]] if profiles is None: profiles = [] subcommand = ['down'] @@ -659,7 +668,7 @@ def stop( # Specific arguments timeout=None, # type: Optional[int] ): - # type: (...) -> Tuple[int, Text, Text, List[EVENT]] + # type: (...) -> Tuple[int, Text, Text, List[ResourceEvent]] if profiles is None: profiles = [] subcommand = ['stop'] @@ -688,7 +697,7 @@ def restart( services=None, # type: Optional[List[Text]] timeout=None, # type: Optional[int] ): - # type: (...) -> Tuple[int, Text, Text, List[EVENT]] + # type: (...) -> Tuple[int, Text, Text, List[ResourceEvent]] if profiles is None: profiles = [] if services is None: @@ -722,7 +731,7 @@ def build( no_cache=False, # type: bool pull=False, # type: bool ): - # type: (...) -> Tuple[int, Text, Text, List[EVENT]] + # type: (...) -> Tuple[int, Text, Text, List[ResourceEvent]] if profiles is None: profiles = [] if services is None: @@ -757,7 +766,7 @@ def pull( services=None, # type: Optional[List[Text]] include_deps=False, # type: bool ): - # type: (...) -> Tuple[int, Text, Text, List[EVENT]] + # type: (...) -> Tuple[int, Text, Text, List[ResourceEvent]] if profiles is None: profiles = [] if services is None: diff --git a/tests/integration/targets/docker_compose_v2/tasks/main.yml b/tests/integration/targets/docker_compose_v2/tasks/main.yml index 5c69add13..e4170b9ff 100644 --- a/tests/integration/targets/docker_compose_v2/tasks/main.yml +++ b/tests/integration/targets/docker_compose_v2/tasks/main.yml @@ -42,7 +42,4 @@ with_items: "{{ dnetworks }}" diff: false - when: has_docker_compose and docker_api_version is version('1.25', '>=') - -- fail: msg="Too old docker / docker-py version to run all docker_container tests!" - when: has_docker_compose and not(docker_api_version is version('1.25', '>=')) and (ansible_distribution != 'CentOS' or ansible_distribution_major_version|int > 6) + when: has_docker_compose diff --git a/tests/integration/targets/docker_compose_v2/tasks/tests/start-stop.yml b/tests/integration/targets/docker_compose_v2/tasks/tests/start-stop.yml index 6d62d25eb..84eeca0c4 100644 --- a/tests/integration/targets/docker_compose_v2/tasks/tests/start-stop.yml +++ b/tests/integration/targets/docker_compose_v2/tasks/tests/start-stop.yml @@ -46,58 +46,48 @@ ## Present ######################################################### #################################################################### - - name: Present (check) - docker_compose_v2: - state: present - project_src: "{{ remote_tmp_dir }}/{{ cname_prefix }}" - check_mode: true - register: present_1 - - name: Present docker_compose_v2: state: present - project_src: "{{ remote_tmp_dir }}/{{ cname_prefix }}" - register: present_2 + files: + - "{{ remote_tmp_dir }}/{{ cname_prefix }}/docker-compose.yaml" + register: present_1 - name: Present (idempotent) docker_compose_v2: state: present - project_src: "{{ remote_tmp_dir }}/{{ cname_prefix }}" - register: present_3 - - - name: Present (idempotent check) - docker_compose_v2: - state: present - project_src: "{{ remote_tmp_dir }}/{{ cname_prefix }}" - check_mode: true - register: present_4 + files: + - "{{ remote_tmp_dir }}/{{ cname_prefix }}/docker-compose.yaml" + register: present_2 - name: Update docker-compose.yml file copy: content: '{{ test_service_mod | string }}' dest: '{{ remote_tmp_dir }}/{{ pname }}/docker-compose.yaml' - - name: Present (changed check) - docker_compose_v2: - state: present - project_src: "{{ remote_tmp_dir }}/{{ cname_prefix }}" - check_mode: true - register: present_5 - - name: Present (changed) docker_compose_v2: state: present - project_src: "{{ remote_tmp_dir }}/{{ cname_prefix }}" - register: present_6 + files: + - "{{ remote_tmp_dir }}/{{ cname_prefix }}/docker-compose.yaml" + register: present_3 - assert: that: - present_1 is changed - - present_2 is changed - - present_3 is not changed - - present_4 is not changed - - present_5 is changed - - present_6 is changed + - "cname in present_1.containers" + - "present_1.containers[cname] | length == 2" + - "'created' in present_1.containers[cname]" + - "'started' in present_1.containers[cname]" + - present_2 is not changed + - "cname in present_2.containers" + - "present_2.containers[cname] | length == 1" + - "'running' in present_2.containers[cname]" + - present_3 is changed + - "cname in present_3.containers" + - "present_3.containers[cname] | length == 2" + - "'recreated' in present_3.containers[cname]" + - "'started' in present_3.containers[cname]" #################################################################### ## Absent ########################################################## @@ -238,4 +228,5 @@ - name: Clean up docker_compose_v2: state: absent - project_src: "{{ remote_tmp_dir }}/{{ cname_prefix }}" + files: + - "{{ remote_tmp_dir }}/{{ cname_prefix }}/docker-compose.yaml"