- 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 == {}

    - name: Create volume with UID option
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        options:
          - "o=uid=1001"
        executable: "{{ podman_cmd | default(omit) }}"
      register: info11

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

    - name: Create volume with UID option - again
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        options:
          - "o=uid=1001"
        executable: "{{ podman_cmd | default(omit) }}"
      register: info12

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

    - name: Create volume with UID option - default
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        executable: "{{ podman_cmd | default(omit) }}"
      register: info13

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

    - name: Create volume with GID option
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        options:
          - "o=gid=1001"
        executable: "{{ podman_cmd | default(omit) }}"
      register: info14

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

    - name: Create volume with GID option - again
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        options:
          - "o=gid=1001"
        executable: "{{ podman_cmd | default(omit) }}"
      register: info15

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

    - name: Create volume with GID option - default
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        executable: "{{ podman_cmd | default(omit) }}"
      register: info16

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

    - name: Create volume with GID and UID option
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        options:
          - "o=gid=1002,uid=1002"
        executable: "{{ podman_cmd | default(omit) }}"
      register: info17

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

    - name: Create volume with GID and UID option - again
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        options:
          - "o=uid=1002"
          - "o=gid=1002"
        executable: "{{ podman_cmd | default(omit) }}"
      register: info18

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

    - name: Create volume with GID and UID option - default
      containers.podman.podman_volume:
        name: "{{ volume_name }}"
        state: present
        executable: "{{ podman_cmd | default(omit) }}"
      register: info19

    - name: Check info
      assert:
        that:
          - info19 is changed
  always:

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