---
# 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 #
####################################################################

# Create random name prefix (for services, ...)
- name: Create random container name prefix
  set_fact:
    name_prefix: "{{ 'ansible-docker-test-%0x' % ((2**32) | random) }}"
    cnames: []
    dnetworks: []
    images: []

- debug:
    msg: "Using name prefix {{ name_prefix }}"

# Run the tests
- block:
    - name: Show docker compose --help output
      command: docker compose --help

    - include_tasks: run-test.yml
      with_fileglob:
        - "tests/*.yml"
      loop_control:
        loop_var: test_name

  always:
    - name: "Make sure all containers are removed"
      docker_container:
        name: "{{ item }}"
        state: absent
        force_kill: true
      loop: "{{ cnames }}"
      diff: false

    - name: "Make sure all networks are removed"
      docker_network:
        name: "{{ item }}"
        state: absent
        force: true
      loop: "{{ dnetworks }}"
      diff: false

    - name: "Make sure all images are removed"
      docker_image_remove:
        name: "{{ item }}"
      loop: "{{ images }}"
      diff: false

  when: docker_has_compose and docker_compose_version is version('2.18.0', '>=')
