- 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:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ volume_name }}"
        state: absent

    - name: Get missing volume info
      containers.podman.podman_volume_info:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ volume_name }}"
      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:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ volume_name }}"
        state: present

    - name: Get existing volume info
      containers.podman.podman_volume_info:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ volume_name }}"
      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:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ volume_name }}"
        state: absent

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

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