Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Apr 7, 2024
1 parent 0a4762d commit 71d43de
Showing 1 changed file with 264 additions and 0 deletions.
264 changes: 264 additions & 0 deletions tests/integration/targets/docker_compose_v2/tasks/tests/definition.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
---
# 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 }}-definition"
cname: "{{ name_prefix }}-container"
test_service: |
services:
{{ cname }}:
image: "{{ docker_test_image_alpine }}"
command: '/bin/sh -c "sleep 10m"'
stop_grace_period: 1s
test_service_mod: |
services:
{{ cname }}:
image: "{{ docker_test_image_alpine }}"
command: '/bin/sh -c "sleep 15m"'
stop_grace_period: 1s
block:
- name: Registering container name
set_fact:
cnames: "{{ cnames + [pname ~ '-' ~ cname ~ '-1'] }}"
dnetworks: "{{ dnetworks + [pname ~ '_default'] }}"

####################################################################
## Present #########################################################
####################################################################

- name: Present (check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: present
check_mode: true
register: present_1_check

- name: Present
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: present
register: present_1

- name: Present (idempotent check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: present
check_mode: true
register: present_2_check

- name: Present (idempotent)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: present
register: present_2

- name: Present (changed check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service_mod | from_yaml }}'
state: present
check_mode: true
register: present_3_check

- name: Present (changed)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service_mod | from_yaml }}'
state: present
register: present_3

- assert:
that:
- present_1_check is changed
- present_1 is changed
- present_1.containers | length == 1
- present_1.containers[0].Name == pname ~ '-' ~ cname ~ '-1'
- present_1.containers[0].Image == docker_test_image_alpine
- 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_2_check is not changed
- present_2 is not changed
- present_3_check is changed
- present_3 is changed

####################################################################
## Absent ##########################################################
####################################################################

- name: Absent (check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service_mod | from_yaml }}'
state: absent
check_mode: true
register: absent_1_check

- name: Absent
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service_mod | from_yaml }}'
state: absent
register: absent_1

- name: Absent (idempotent check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service_mod | from_yaml }}'
state: absent
check_mode: true
register: absent_2_check

- name: Absent (idempotent)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service_mod | from_yaml }}'
state: absent
register: absent_2

- assert:
that:
- absent_1_check is changed
- absent_1 is changed
- absent_2_check is not changed
- absent_2 is not changed

####################################################################
## Stopping and starting ###########################################
####################################################################

- name: Present stopped (check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: stopped
check_mode: true
register: present_1_check

- name: Present stopped
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: stopped
register: present_1

- name: Present stopped (idempotent check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: stopped
check_mode: true
register: present_2_check

- name: Present stopped (idempotent)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: stopped
register: present_2

- name: Started (check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: present
check_mode: true
register: present_3_check

- name: Started
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: present
register: present_3

- name: Started (idempotent check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: present
check_mode: true
register: present_4_check

- name: Started (idempotent)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: present
register: present_4

- name: Restarted (check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: restarted
check_mode: true
register: present_5_check

- name: Restarted
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: restarted
register: present_5

- name: Stopped (check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: stopped
check_mode: true
register: present_6_check

- name: Stopped
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: stopped
register: present_6

- name: Restarted (check)
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: restarted
check_mode: true
register: present_7_check

- name: Restarted
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: restarted
register: present_7

- name: Cleanup
docker_compose_v2:
project_name: '{{ pname }}'
definition: '{{ test_service | from_yaml }}'
state: absent

- assert:
that:
- present_1_check is changed
- present_1 is changed
- present_2_check is not changed
- present_2 is not changed
- present_3_check is changed
- present_3 is changed
- present_4_check is not changed
- present_4 is not changed
- present_5_check is changed
- present_5 is changed
- present_6_check is changed
- present_6 is changed
- present_7_check is changed
- present_7 is changed

0 comments on commit 71d43de

Please sign in to comment.