---
# 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 ~ '-iid' }}"
- name: Registering container name
  set_fact:
    cnames: "{{ cnames + [cname] }}"

- name: Pull images
  docker_image_pull:
    name: "{{ image }}"
  loop:
    - "{{ docker_test_image_hello_world }}"
    - "{{ docker_test_image_alpine }}"
  loop_control:
    loop_var: image

- name: Get image ID of {{ docker_test_image_hello_world }} and {{ docker_test_image_alpine }} images
  docker_image_info:
    name:
    - "{{ docker_test_image_hello_world }}"
    - "{{ docker_test_image_alpine }}"
  register: image_info

- assert:
    that:
      - image_info.images | length == 2

- name: Print image IDs
  debug:
    msg: "{{ docker_test_image_hello_world }}: {{ image_info.images[0].Id }}; {{ docker_test_image_alpine }}: {{ image_info.images[1].Id }}"

- name: Create container with {{ docker_test_image_hello_world }} image via ID
  docker_container:
    image: "{{ image_info.images[0].Id }}"
    name: "{{ cname }}"
    state: present
    force_kill: true
  register: create_1

- name: Create container with {{ docker_test_image_hello_world }} image via ID (idempotent)
  docker_container:
    image: "{{ image_info.images[0].Id }}"
    name: "{{ cname }}"
    state: present
    force_kill: true
  register: create_2

- name: Create container with {{ docker_test_image_alpine }} image via ID
  docker_container:
    image: "{{ image_info.images[1].Id }}"
    name: "{{ cname }}"
    state: present
    force_kill: true
  register: create_3

- name: Create container with {{ docker_test_image_alpine }} image via ID (idempotent)
  docker_container:
    image: "{{ image_info.images[1].Id }}"
    name: "{{ cname }}"
    state: present
    force_kill: true
  register: create_4

- name: Untag image
  # Image will not be deleted since the container still uses it
  docker_image_remove:
    name: "{{ docker_test_image_alpine }}"
    force: true

- name: Create container with {{ docker_test_image_alpine }} image via name (check mode, will pull, same image)
  docker_container:
    image: "{{ docker_test_image_alpine }}"
    name: "{{ cname }}"
    state: present
    image_name_mismatch: ignore
  register: create_5
  check_mode: true

- name: Create container with {{ docker_test_image_alpine }} image via name (will pull, same image)
  docker_container:
    image: "{{ docker_test_image_alpine }}"
    name: "{{ cname }}"
    state: present
    image_name_mismatch: ignore
  register: create_6

- name: Cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: true
  diff: false

- assert:
    that:
      - create_1 is changed
      - create_2 is not changed
      - create_3 is changed
      - create_4 is not changed
      - create_5 is changed
      - create_6 is changed
      - create_6.container.Image == image_info.images[1].Id
      - create_6.container.Id == create_4.container.Id  # make sure container wasn't recreated

- name: Create container with {{ docker_test_image_digest_base }} image via old digest
  docker_container:
    image: "{{ docker_test_image_digest_base }}@sha256:{{ docker_test_image_digest_v1 }}"
    name: "{{ cname }}"
    state: present
    force_kill: true
  register: digest_1

- name: Create container with {{ docker_test_image_digest_base }} image via old digest (idempotent)
  docker_container:
    image: "{{ docker_test_image_digest_base }}@sha256:{{ docker_test_image_digest_v1 }}"
    name: "{{ cname }}"
    state: present
    force_kill: true
  register: digest_2

- name: Create container with {{ docker_test_image_digest_base }} image via old digest (idempotent, pull)
  docker_container:
    image: "{{ docker_test_image_digest_base }}@sha256:{{ docker_test_image_digest_v1 }}"
    name: "{{ cname }}"
    pull: true
    state: present
    force_kill: true
  register: digest_3

- name: Update container with {{ docker_test_image_digest_base }} image via new digest
  docker_container:
    image: "{{ docker_test_image_digest_base }}@sha256:{{ docker_test_image_digest_v2 }}"
    name: "{{ cname }}"
    state: present
    force_kill: true
  register: digest_4

- name: Cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: true
  diff: false

- assert:
    that:
      - digest_1 is changed
      - digest_2 is not changed
      - digest_3 is not changed
      - digest_4 is changed
