Skip to content

Commit

Permalink
Fix tests for "present"
Browse files Browse the repository at this point in the history
  • Loading branch information
lel-amri committed Apr 2, 2023
1 parent 54666b5 commit f55a3f8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 55 deletions.
45 changes: 27 additions & 18 deletions plugins/modules/docker_compose_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -435,6 +435,7 @@
'Stopped',
'Killed',
'Removed',
'Recreated',
}) # type: Final[FrozenSet[Text]]


Expand Down Expand Up @@ -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'(?P<resource_type>Network|Image|Volume|Container)'
r' '
r'(?P<resource_id>.+)'
r' '
r'(?P<status>%s)' % (
"|".join(STATUS_DONE | STATUS_WORKING | STATUS_ERROR)
r'\s+'
r'(?P<resource_id>[^\s]+)'
r'\s+'
r'(?P<status>%s)'
r'\s*'
r'$'
% (
"|".join(sorted(STATUS_DONE | STATUS_WORKING | STATUS_ERROR, key=lambda e: len(e), reverse=True))
)
)

Expand All @@ -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)
Expand All @@ -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']
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/targets/docker_compose_v2/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##########################################################
Expand Down Expand Up @@ -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"

0 comments on commit f55a3f8

Please sign in to comment.