---
- name: Registering image name
  set_fact:
    iname: "{{ name_prefix ~ '-options' }}"

- name: Determining pushed image names
  set_fact:
    hello_world_image_base: "{{ registry_address }}/test/hello-world"
    test_image_base: "{{ registry_address }}/test/{{ iname }}"

- name: Registering image name
  set_fact:
    inames: "{{ inames + [iname, test_image_base ~ ':latest', hello_world_image_base ~ ':latest', hello_world_image_base ~ ':newtag', hello_world_image_base ~ ':newtag2'] }}"

####################################################################
## interact with test registry #####################################
####################################################################

- name: Make sure image is not there
  docker_image:
    name: "{{ hello_world_image_base }}:latest"
    state: absent
    force_absent: yes

- name: Make sure we have quay.io/ansible/docker-test-containers:hello-world
  docker_image:
    name: quay.io/ansible/docker-test-containers:hello-world
    source: pull

- name: Push image to test registry
  docker_image:
    name: "quay.io/ansible/docker-test-containers:hello-world"
    repository: "{{ hello_world_image_base }}:latest"
    push: yes
    source: local
  register: push_1

- name: Push image to test registry (idempotent)
  docker_image:
    name: "quay.io/ansible/docker-test-containers:hello-world"
    repository: "{{ hello_world_image_base }}:latest"
    push: yes
    source: local
  register: push_2

- name: Push image to test registry (force, still idempotent)
  docker_image:
    name: "quay.io/ansible/docker-test-containers:hello-world"
    repository: "{{ hello_world_image_base }}:latest"
    push: yes
    source: local
    force_tag: yes
  register: push_3

- assert:
    that:
    - push_1 is changed
    - push_2 is not changed
    - push_3 is not changed

- name: Get facts of local image
  docker_image_info:
    name: "{{ hello_world_image_base }}:latest"
  register: facts_1

- name: Make sure image is not there
  docker_image:
    name: "{{ hello_world_image_base }}:latest"
    state: absent
    force_absent: yes

- name: Get facts of local image (absent)
  docker_image_info:
    name: "{{ hello_world_image_base }}:latest"
  register: facts_2

- name: Pull image from test registry
  docker_image:
    name: "{{ hello_world_image_base }}:latest"
    state: present
    source: pull
  register: pull_1

- name: Pull image from test registry (idempotency)
  docker_image:
    name: "{{ hello_world_image_base }}:latest"
    state: present
    source: pull
  register: pull_2

- name: Get facts of local image (present)
  docker_image_info:
    name: "{{ hello_world_image_base }}:latest"
  register: facts_3

- assert:
    that:
    - pull_1 is changed
    - pull_2 is not changed
    - facts_1.images | length == 1
    - facts_2.images | length == 0
    - facts_3.images | length == 1

- name: Tag different image with new tag
  docker_image:
    name: quay.io/ansible/docker-test-containers:alpine3.8
    repository: "{{ hello_world_image_base }}:newtag"
    push: no
    source: pull

- name: Push different image with new tag
  docker_image:
    name: "{{ hello_world_image_base }}"
    repository: "{{ hello_world_image_base }}"
    tag: newtag
    push: yes
    source: local
  register: push_1_different

- name: Push different image with new tag (idempotent)
  docker_image:
    name: "{{ hello_world_image_base }}"
    repository: "{{ hello_world_image_base }}"
    tag: newtag
    push: yes
    source: local
  register: push_2_different

- assert:
    that:
    - push_1_different is changed
    - push_2_different is not changed

- name: Tag same image with new tag
  docker_image:
    name: quay.io/ansible/docker-test-containers:alpine3.8
    repository: "{{ hello_world_image_base }}:newtag2"
    push: no
    source: pull

- name: Push same image with new tag
  docker_image:
    name: "{{ hello_world_image_base }}"
    repository: "{{ hello_world_image_base }}"
    tag: newtag2
    push: yes
    source: local
  register: push_1_same

- name: Push same image with new tag (idempotent)
  docker_image:
    name: "{{ hello_world_image_base }}"
    repository: "{{ hello_world_image_base }}"
    tag: newtag2
    push: yes
    source: local
  register: push_2_same

- assert:
    that:
    # NOTE: This should be:
    #   - push_1_same is changed
    # Unfortunately docker does *NOT* report whether the tag already existed or not.
    # Here are the logs returned by client.push() for both tasks (which are exactly the same):
    #   push_1_same:
    #     {"status": "The push refers to repository [localhost:32796/test/hello-world]"},
    #     {"id": "3fc64803ca2d", "progressDetail": {}, "status": "Preparing"},
    #     {"id": "3fc64803ca2d", "progressDetail": {}, "status": "Layer already exists"},
    #     {"status": "newtag2: digest: sha256:92251458088c638061cda8fd8b403b76d661a4dc6b7ee71b6affcf1872557b2b size: 528"},
    #     {"aux": {"Digest": "sha256:92251458088c638061cda8fd8b403b76d661a4dc6b7ee71b6affcf1872557b2b", "Size": 528, "Tag": "newtag2"}, "progressDetail": {}}
    #   push_2_same:
    #     {"status": "The push refers to repository [localhost:32796/test/hello-world]"},
    #     {"id": "3fc64803ca2d", "progressDetail": {}, "status": "Preparing"},
    #     {"id": "3fc64803ca2d", "progressDetail": {}, "status": "Layer already exists"},
    #     {"status": "newtag2: digest: sha256:92251458088c638061cda8fd8b403b76d661a4dc6b7ee71b6affcf1872557b2b size: 528"},
    #     {"aux": {"Digest": "sha256:92251458088c638061cda8fd8b403b76d661a4dc6b7ee71b6affcf1872557b2b", "Size": 528, "Tag": "newtag2"}, "progressDetail": {}}
    - push_1_same is not changed
    - push_2_same is not changed

####################################################################
## repository ######################################################
####################################################################

- name: Make sure image is not there
  docker_image:
    name: "{{ test_image_base }}:latest"
    state: absent
    force_absent: yes

- name: repository
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      pull: no
    repository: "{{ test_image_base }}"
    source: build
  register: repository_1

- name: repository (idempotent)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      pull: no
    repository: "{{ test_image_base }}"
    source: build
  register: repository_2

- assert:
    that:
    - repository_1 is changed
    - repository_2 is not changed

- name: Get facts of image
  docker_image_info:
    name: "{{ test_image_base }}:latest"
  register: facts_1

- name: cleanup
  docker_image:
    name: "{{ test_image_base }}:latest"
    state: absent
    force_absent: yes

- assert:
    that:
    - facts_1.images | length == 1
