- name: Test podman_volume_info
  block:

    - name: Print podman version
      command: "{{ podman_cmd | default('podman') }} version"

    - name: Generate random value for volume name
      set_fact:
        volume_name: "{{ 'ansible-test-podman-%0x' % ((2**32) | random) }}"

    - name: Make sure volume doesn't exist
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: absent
        executable: "{{ podman_cmd | default(omit) }}"

    - name: Get missing volume info
      containers.podman.podman_volume_info:
        name: "{{ volume_name }}"
        executable: "{{ podman_cmd | default(omit) }}"
      register: nonexist
      ignore_errors: true

    - name: Check results
      assert:
        that:
          - "'volumes' not in nonexist"
          - nonexist is failed

    - name: Make sure volume exists
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        executable: "{{ podman_cmd | default(omit) }}"

    - name: Get existing volume info
      containers.podman.podman_volume_info:
        name: "{{ volume_name }}"
        executable: "{{ podman_cmd | default(omit) }}"
      register: existing_volume

    - name: Dump podman volume inspect result
      debug: var=existing_volume

    - name: Comparison with 'podman volume inspect'
      command: "{{ podman_cmd | default('podman') }} volume inspect {{ volume_name }}"
      register: podman_inspect

    - name: Convert podman inspect output to JSON
      set_fact:
        podman_inspect_result: "{{ podman_inspect.stdout | from_json }}"

    - name: Cleanup
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: absent
        executable: "{{ podman_cmd | default(omit) }}"

    - name: Make checks
      assert:
        that:
          - "'volumes' in existing_volume"
          - existing_volume.volumes
          - "existing_volume.volumes == podman_inspect_result"
  always:

    - name: Cleanup
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: absent
        executable: "{{ podman_cmd | default(omit) }}"
