- name: Test podman_image
  block:
    - name: Pull image
      containers.podman.podman_image:
        name: quay.io/coreos/alpine-sh
      register: pull1

    - name: Pull image again
      containers.podman.podman_image:
        name: quay.io/coreos/alpine-sh
      register: pull2

    - name: Pull image from docker.io with short url
      containers.podman.podman_image:
        name: docker.io/alpine
      register: pull3

    - name: Pull image from docker.io with short url again
      containers.podman.podman_image:
        name: docker.io/alpine
      register: pull4

    - name: Pull image from docker.io with official/normalised url again
      containers.podman.podman_image:
        name: docker.io/library/alpine
      register: pull5

    - 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
          - "'alpine-sh' in images.stdout"
          - "'library/alpine' 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:
        name: quay.io/coreos/alpine-sh
        state: absent
      register: rmi1

    - name: Remove image again
      containers.podman.podman_image:
        name: quay.io/coreos/alpine-sh
        state: absent
      register: rmi2

    - name: Remove image using new repository url
      containers.podman.podman_image:
        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:
        name: docker.io/alpine
        state: absent
      register: rmi4

    - name: Remove docker.io image using normalised url
      containers.podman.podman_image:
        name: docker.io/library/alpine
        state: absent
      register: rmi5

    - 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
          - "'alpine-sh' not in images.stdout"
          - "'library/alpine' not in images.stdout"

    - name: Pull a specific version of an image
      containers.podman.podman_image:
        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:
        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 dir
      file:
        path: /var/tmp/build
        state: directory

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

    - name: Build OCI image
      containers.podman.podman_image:
        name: testimage
        path: /var/tmp/build
      register: oci_build1

    - name: Build OCI image again
      containers.podman.podman_image:
        name: testimage
        path: /var/tmp/build
      register: oci_build2

    - name: Inspect build image
      containers.podman.podman_image_info:
        name: testimage
      register: testimage_info

    - name: Ensure OCI image was built properly
      assert:
        that:
          - oci_build1 is changed
          - oci_build2 is not changed
          - "'localhost/testimage:latest' in testimage_info.images[0]['RepoTags'][0]"

    - name: Build Docker image
      containers.podman.podman_image:
        name: dockerimage
        path: /var/tmp/build
        build:
          format: docker
      register: docker_build1

    - name: Build Docker image again
      containers.podman.podman_image:
        name: dockerimage
        path: /var/tmp/build
        build:
          format: docker
      register: docker_build2

    - name: Inspect build image
      containers.podman.podman_image_info:
        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]"

  always:
    - name: Cleanup images
      containers.podman.podman_image:
        name: "{{ item }}"
        state: absent
      loop:
        - quay.io/coreos/alpine-sh
        - quay.io/coreos/etcd:v3.3.11
        - localhost/testimage
        - localhost/dockerimage
