- name: Test podman_volume
  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: info
      ignore_errors: true

    - name: Check results
      assert:
        that:
          - info is failed

    - name: Create volume
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        executable: "{{ podman_cmd | default(omit) }}"
      register: vol

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

    - name: Check info
      assert:
        that:
          - info1 | length > 1
          - info1.volumes.0.Name == volume_name
          - vol is changed

    - name: Create volume again
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        executable: "{{ podman_cmd | default(omit) }}"
      register: info2

    - name: Check info
      assert:
        that:
          - info2 is not changed

    - name: Create volume with labels
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        label:
          key: val
          nokey: noval
        executable: "{{ podman_cmd | default(omit) }}"
      register: info3

    - name: Check info
      assert:
        that:
          - info3 is changed

    - name: Create volume with labels again
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        label:
          key: val
          nokey: noval
        executable: "{{ podman_cmd | default(omit) }}"
      register: info4

    - name: Check info
      assert:
        that:
          - info4 is not changed

    - name: Create volume w/o labels
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        executable: "{{ podman_cmd | default(omit) }}"
      register: info5

    - name: Check info
      assert:
        that:
          - info5 is changed

    - name: Create volume with options
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        options:
          - "device=/dev/something"
          - "type=ext4"
        executable: "{{ podman_cmd | default(omit) }}"
      register: info6

    - name: Check info
      assert:
        that:
          - info6 is changed

    - name: Create volume with options again
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        options:
          - "device=/dev/something"
          - "type=ext4"
        executable: "{{ podman_cmd | default(omit) }}"
      register: info7

    - name: Check info
      assert:
        that:
          - info7 is not changed

    - name: Create volume w/o options
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        executable: "{{ podman_cmd | default(omit) }}"
      register: info8

    - name: Check info
      assert:
        that:
          - info8 is changed

    - name: Make sure volume doesn't exist
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: absent
      register: delete

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

    - name: Check results
      assert:
        that:
          - info10 is failed
          - delete.volume == {}

  always:

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