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

- name: Registering container name
  set_fact:
    cname: "{{ cname_prefix ~ '-hi' }}"
- name: Registering container name
  set_fact:
    cnames: "{{ cnames + [cname] }}"

- name: Get current context
  community.docker.docker_context_info:
    only_current: true
  register: docker_current_context

- assert:
    that:
      - docker_current_context is not changed
      # Some of the following tests will not be true on all machines, but they should be in CI:
      - docker_current_context.current_context_name == 'default'
      - docker_current_context.contexts | length == 1
      - docker_current_context.contexts[0].name == 'default'
      - docker_current_context.contexts[0].current == true
      - docker_current_context.contexts[0].description == 'Current DOCKER_HOST based configuration'
      - docker_current_context.contexts[0].meta_path is none
      - docker_current_context.contexts[0].tls_path is none
      - docker_current_context.contexts[0].config.docker_host == 'unix:///var/run/docker.sock'
      - docker_current_context.contexts[0].config.tls == false

- name: Run community.docker modules with current context
  module_defaults:
    group/community.docker.docker: "{{ docker_current_context.contexts[0].config }}"
  block:
    - name: Create container
      docker_container:
        image: "{{ docker_test_image_alpine }}"
        command: '/bin/sh -c "sleep 10m"'
        name: "{{ cname }}"
        state: present
      register: create_1

    - name: Create container (idempotent)
      docker_container:
        image: "{{ docker_test_image_alpine }}"
        command: '/bin/sh -c "sleep 10m"'
        name: "{{ cname }}"
        state: present
      register: create_2

    - assert:
        that:
        - create_1 is changed
        - create_2 is not changed

    - name: Inspect container with CLI tool
      ansible.builtin.command:
        cmd: docker inspect {{ cname }}
      register: result

    - assert:
        that:
          - (result.stdout | from_json) | length == 1
          - (result.stdout | from_json)[0].State.Status == "created"

    - name: Start container
      docker_container:
        name: "{{ cname }}"
        state: started
      register: start_1

    - name: Start container (idempotent)
      docker_container:
        name: "{{ cname }}"
        state: started
      register: start_2

    - assert:
        that:
        - start_1 is changed
        - start_2 is not changed

    - name: Inspect container with CLI tool
      ansible.builtin.command:
        cmd: docker inspect {{ cname }}
      register: result

    - assert:
        that:
          - (result.stdout | from_json) | length == 1
          - (result.stdout | from_json)[0].State.Status == "running"

    - name: Remove container
      docker_container:
        name: "{{ cname }}"
        state: absent
        force_kill: true
      register: remove_1

    - name: Remove container (idempotent)
      docker_container:
        name: "{{ cname }}"
        state: absent
        force_kill: true
      register: remove_2

    - assert:
        that:
        - remove_1 is changed
        - remove_2 is not changed

    - name: Inspect container with CLI tool
      ansible.builtin.command:
        cmd: docker inspect {{ cname }}
      register: result
      failed_when: result.rc != 1

    - assert:
        that:
          - (result.stdout | from_json) | length == 0
