---
####################################################################
## basic ###########################################################
####################################################################

- name: Make sure image is not there
  docker_image:
    name: "{{ docker_test_image_hello_world }}"
    state: absent
    force_absent: yes
  register: absent_1

- name: Make sure image is not there (idempotency)
  docker_image:
    name: "{{ docker_test_image_hello_world }}"
    state: absent
  register: absent_2

- assert:
    that:
    - absent_2 is not changed

- name: Make sure image is there
  docker_image:
    name: "{{ docker_test_image_hello_world }}"
    state: present
    source: pull
    pull:
      platform: amd64
  register: present_1

- name: Make sure image is there (idempotent)
  docker_image:
    name: "{{ docker_test_image_hello_world }}"
    state: present
    source: pull
    pull:
      platform: amd64
  register: present_2

- assert:
    that:
    - present_1 is changed
    - present_2 is not changed

- name: Make sure tag is not there
  docker_image:
    name: "{{ docker_test_image_hello_world_base }}:alias"
    state: absent

- name: Tag image with alias
  docker_image:
    source: local
    name: "{{ docker_test_image_hello_world }}"
    repository: "{{ docker_test_image_hello_world_base }}:alias"
  register: tag_1

- name: Tag image with alias (idempotent)
  docker_image:
    source: local
    name: "{{ docker_test_image_hello_world }}"
    repository: "{{ docker_test_image_hello_world_base }}:alias"
  register: tag_2

- name: Tag image with alias (force, still idempotent)
  docker_image:
    source: local
    name: "{{ docker_test_image_hello_world }}"
    repository: "{{ docker_test_image_hello_world_base }}:alias"
    force_tag: yes
  register: tag_3

- name: Tag image with ID instead of name
  docker_image:
    source: local
    name: "{{ present_1.image.Id }}"
    repository: "{{ docker_test_image_hello_world_base }}:alias"
  register: tag_4

- assert:
    that:
    - tag_1 is changed
    - tag_2 is not changed
    - tag_3 is not changed
    - tag_4 is not changed

- name: Cleanup alias tag
  docker_image:
    name: "{{ docker_test_image_hello_world_base }}:alias"
    state: absent

- name: Tag image with ID instead of name (use ID for repository, must fail)
  docker_image:
    source: local
    name: "{{ docker_test_image_hello_world }}"
    repository: "{{ present_1.image.Id }}"
  register: fail_1
  ignore_errors: true

- name: Push image with ID (must fail)
  docker_image:
    source: local
    name: "{{ present_1.image.Id }}"
    push: true
  register: fail_2
  ignore_errors: true

- name: Pull image ID (must fail)
  docker_image:
    source: pull
    name: "{{ present_1.image.Id }}"
    force_source: true
  register: fail_3
  ignore_errors: true

- name: buildargs
  docker_image:
    source: build
    name: "{{ present_1.image.Id }}"
    build:
      path: "{{ remote_tmp_dir }}/files"
    force_source: true
  register: fail_4
  ignore_errors: yes

- assert:
    that:
    - fail_1 is failed
    - "'`repository` must not be an image ID' in fail_1.msg"
    - fail_2 is failed
    - "'Cannot push an image by ID' in fail_2.msg"
    - fail_3 is failed
    - "'Image name must not be an image ID for source=pull' in fail_3.msg"
    - fail_4 is failed
    - "'Image name must not be an image ID for source=build' in fail_4.msg"
