- name: Test podman rootful pod
  block:

    - name: Discover cgroups version
      shell: podman info | grep cgroupVersion | awk {'print $2'}
      register: cgroups

    - name: Set cgroups version
      set_fact:
        cgroups_version: "{{ cgroups.stdout }}"

    - name: Delete all pods leftovers from tests
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: absent

    - name: Create and start pod
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: started

    - name: Create and start pod
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: started
      register: pod1_info

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

    - name: Add DNS to pod
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: started
        dns:
          - 1.1.1.1
      register: pod2_info

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

    - name: Remove DNS from pod
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: started
      register: pod3_info

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

    - name: Create network newnet
      command: podman network create newnet --subnet 10.90.90.0/24

    - name: Create network net2
      command: podman network create net2 --subnet 10.70.70.0/24

    - name: Start pod with networks
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: started
        network:
          - net2
          - newnet
      register: pod4_info

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

    - name: Start pod with networks again
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: started
        network:
          - newnet
          - net2
      register: pod5_info

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

    - name: Start pod with one network
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: started
        network: net2
      register: pod6_info

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

    - name: Start pod without networks
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: started
      register: pod7_info

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

    - name: Start pod with ports for idempotency and ipv6
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: pod1
        state: started
        ports:
          - "4444:4444/tcp"
          - "8888:19191/udp"
          - "127.0.0.1:7671:7676/udp"
          - "127.0.0.2:7671:7676/udp"
          - "127.0.0.1:13122:8871/tcp"
          - "127.0.0.1:43423:8872"
          - "0.0.0.0:44444:44445"
          - "[::1]:8743:8745"
      register: pod8_info

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

    - name: Start pod with ports for idempotency and ipv6 again
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: pod1
        state: started
        ports:
          - "4444:4444/tcp"
          - "8888:19191/udp"
          - "127.0.0.1:7671:7676/udp"
          - "127.0.0.2:7671:7676/udp"
          - "127.0.0.1:13122:8871/tcp"
          - "127.0.0.1:43423:8872"
          - "0.0.0.0:44444:44445"
          - "[::1]:8743:8745"
      register: pod9_info

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

  always:

    - name: Delete all pods leftovers from tests
      containers.podman.podman_pod:
        executable: "{{ test_executable | default('podman') }}"
        name: "rootpod"
        state: absent

    - name: Delete all existing pods
      shell: |
        podman pod rm -fa;
      ignore_errors: true

    - name: Delete all created networks
      shell: |
        podman network rm -f newnet net2
      ignore_errors: true
