- name: Test podman_image
  block:
    - name: Pull image
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: quay.io/coreos/alpine-sh
      register: pull1

    - name: Pull image again
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: quay.io/coreos/alpine-sh
      register: pull2

    - name: Pull image from docker.io with short url
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: docker.io/alpine
      register: pull3

    - name: Pull image from docker.io with short url again
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: docker.io/alpine
      register: pull4

    - name: Pull image from docker.io with official/normalised url again
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: docker.io/library/alpine
      register: pull5

    - name: Pull image for testing image deletion with image id
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: docker.io/library/ubuntu
      register: pull6

    - name: List images
      command: podman image ls
      register: images

    - name: Ensure image were pulled properly
      assert:
        that:
          - pull1 is changed
          - pull1.podman_actions is defined
          - pull2 is not changed
          - pull3 is changed
          - pull4 is changed
          - pull5 is not changed
          - pull6 is changed
          - "'alpine-sh' in images.stdout"
          - "'library/alpine' in images.stdout"
          - "'library/ubuntu' in images.stdout"

    - name: add another tag (repository url)
      command:
        argv:
          - podman
          - tag
          - quay.io/coreos/alpine-sh
          - quay.io/coreos/library/alpine-sh

    - name: Remove image (tag)
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: quay.io/coreos/alpine-sh
        state: absent
      register: rmi1

    - name: Remove image again
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: quay.io/coreos/alpine-sh
        state: absent
      register: rmi2

    - name: Remove image using new repository url
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: quay.io/coreos/library/alpine-sh
        state: absent
      register: rmi3

    - name: Try to remove docker.io image using short url
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: docker.io/alpine
        state: absent
      register: rmi4

    - name: Remove docker.io image using normalised url
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: docker.io/library/alpine
        state: absent
      register: rmi5

    - name: Get image id of the target image
      containers.podman.podman_image_info:
        executable: "{{ test_executable | default('podman') }}"
        name: docker.io/library/ubuntu
      register: imageinfo

    - name: Remove an image with image id
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ item.Id }}"
        state: absent
      loop: "{{ imageinfo.images }}"
      register: rmi6

    - name: List images
      command: podman image ls
      register: images

    - name: Ensure image were removed properly
      assert:
        that:
          - rmi1 is changed
          - rmi2 is not changed
          - rmi3 is changed
          - rmi4 is not changed
          - rmi5 is changed
          - rmi6 is changed
          - "'alpine-sh' not in images.stdout"
          - "'library/ubuntu' not in images.stdout"

    - name: Pull a specific version of an image
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: quay.io/coreos/etcd
        tag: v3.3.11
      register: specific_image1

    - name: Pull a specific version of an image again
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: quay.io/coreos/etcd
        tag: v3.3.11
      register: specific_image2

    - name: List images
      command: podman image ls
      register: images

    - name: Ensure specific image was pulled properly
      assert:
        that:
          - specific_image1 is changed
          - specific_image2 is not changed
          - "'v3.3.11' in images.stdout"

    - name: Create a build directory with a subdirectory
      file:
        path: /var/tmp/build/subdir
        state: directory

    - name: Copy Containerfile
      copy:
        src: Containerfile
        dest: /var/tmp/build/Dockerfile

    - name: Build OCI image
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: testimage
        path: /var/tmp/build
      register: oci_build1

    - name: Build OCI image again
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: testimage
        path: /var/tmp/build
      register: oci_build2

    - name: Build OCI image from a directory without Containerfile (should fail)
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: testimage2
        path: /var/tmp/build/subdir
      register: oci_build3
      ignore_errors: true

    - name: Build OCI image, point to location of Containerfile
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: testimage2
        path: /var/tmp/build/subdir
        build:
          file: /var/tmp/build/Dockerfile
      register: oci_build4

    - name: Inspect first image
      containers.podman.podman_image_info:
        executable: "{{ test_executable | default('podman') }}"
        name: testimage
      register: testimage_info

    - name: Inspect second image
      containers.podman.podman_image_info:
        executable: "{{ test_executable | default('podman') }}"
        name: testimage2
      register: testimage2_info

    - name: Ensure OCI images were built properly
      assert:
        that:
          - oci_build1 is changed
          - oci_build2 is not changed
          - oci_build3 is not changed
          - oci_build3 is failed
          - oci_build4 is changed
          - "'localhost/testimage:latest' in testimage_info.images[0]['RepoTags'][0]"
          - "'localhost/testimage2:latest' in testimage2_info.images[0]['RepoTags'][0]"
          - "'no such file or directory' in oci_build3.msg"

    - name: Build Docker image
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: dockerimage
        path: /var/tmp/build
        build:
          format: docker
      register: docker_build1

    - name: Build Docker image again
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: dockerimage
        path: /var/tmp/build
        build:
          format: docker
      register: docker_build2

    - name: Inspect built image
      containers.podman.podman_image_info:
        executable: "{{ test_executable | default('podman') }}"
        name: dockerimage
      register: dockerimage_info

    - name: Ensure Docker image was built properly
      assert:
        that:
          - docker_build1 is changed
          - docker_build2 is not changed
          - "'localhost/dockerimage:latest' in dockerimage_info.images[0]['RepoTags'][0]"

    - name: push image that doesn't exit to nowhere
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: bad_image
        pull: false
        push: yes
      register: bad_push
      ignore_errors: true

    - name: Ensure that Image failed correctly.
      assert:
        that:
          - "bad_push is failed"
          - "bad_push is not changed"
          - "'Failed to find image bad_image' in bad_push.msg"
          - "'image pull set to False' in bad_push.msg"

  always:
    - name: Cleanup images
      containers.podman.podman_image:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ item }}"
        state: absent
      loop:
        - quay.io/coreos/alpine-sh
        - quay.io/coreos/etcd:v3.3.11
        - localhost/testimage
        - localhost/testimage2
        - localhost/dockerimage
