---
# 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: Image ID  pull tests
  block:
    - name: Make sure images are not there
      docker_image_remove:
        name: "{{ item }}"
        force: true
      loop:
        - "sha256:{{ docker_test_image_digest_v1_image_id }}"
        - "sha256:{{ docker_test_image_digest_v2_image_id }}"

    - name: Pull image 1
      docker_image_pull:
        name: "{{ docker_test_image_digest_base }}@sha256:{{ docker_test_image_digest_v1 }}"
      register: present_1
      diff: true

    - name: Pull image 1 (idempotent, do pull)
      docker_image_pull:
        name: "{{ docker_test_image_digest_base }}@sha256:{{ docker_test_image_digest_v1 }}"
        pull: always
      register: present_2
      diff: true

    - name: Pull image 1 (idempotent, do not pull)
      docker_image_pull:
        name: "{{ docker_test_image_digest_base }}@sha256:{{ docker_test_image_digest_v1 }}"
        pull: not_present
      register: present_3
      diff: true

    - assert:
        that:
        - present_1 is changed
        - present_1.actions | length == 1
        - present_1.actions[0] == ('Pulled image ' ~ docker_test_image_digest_base ~ ':sha256:' ~ docker_test_image_digest_v1)
        - present_1.diff.before.exists is false
        - present_1.diff.after.id is string
        - present_2 is not changed
        - present_2.actions | length == 1
        - present_2.actions[0] == ('Pulled image ' ~ docker_test_image_digest_base ~ ':sha256:' ~ docker_test_image_digest_v1)
        - present_2.diff.before.id == present_1.diff.after.id
        - present_2.diff.after.id == present_1.diff.after.id
        - present_3 is not changed
        - present_3.actions | length == 0
        - present_3.diff.before.id == present_1.diff.after.id
        - present_3.diff.after.id == present_1.diff.after.id

    - name: Pull image 2 (check mode)
      docker_image_pull:
        name: "{{ docker_test_image_digest_base }}@sha256:{{ docker_test_image_digest_v2 }}"
        pull: always
      register: present_4
      diff: true
      check_mode: true

    - name: Pull image 2
      docker_image_pull:
        name: "{{ docker_test_image_digest_base }}@sha256:{{ docker_test_image_digest_v2 }}"
        pull: always
      register: present_5
      diff: true

    - assert:
        that:
        - present_4 is changed
        - present_4.actions | length == 1
        - present_4.actions[0] == ('Pulled image ' ~ docker_test_image_digest_base ~ ':sha256:' ~ docker_test_image_digest_v2)
        - present_4.diff.before.exists is false
        - present_4.diff.after.id == 'unknown'
        - present_5 is changed
        - present_5.actions | length == 1
        - present_5.actions[0] == ('Pulled image ' ~ docker_test_image_digest_base ~ ':sha256:' ~ docker_test_image_digest_v2)
        - present_5.diff.before.exists is false
        - present_5.diff.after.id != present_1.diff.after.id
        - present_5.diff.after.id is string

  always:
    - name: cleanup
      docker_image_remove:
        name: "{{ item }}"
        force: true
      loop:
        - "sha256:{{ docker_test_image_digest_v1_image_id }}"
        - "sha256:{{ docker_test_image_digest_v2_image_id }}"
