- name: Test podman_network
  become: true
  block:

    - name: Print podman version
      command: podman version

    - name: Check if dnsname plugin is installed
      block:

        - name: Check if plugin is installed
          stat:
            path: "{{ item }}"
          loop:
            - /usr/libexec/cni/dnsname
            - /usr/lib/cni/dnsname
            - /opt/cni/bin/dnsname
            - /opt/bridge/bin/dnsname
          register: plugin_results

        - name: Set plugin fact
          set_fact:
            dns_plugin: "{{ true in plugin_results.results|map(attribute='stat.exists') }}"

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

    - name: Make sure network doesn't exist
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: absent

    - name: Get missing network info
      containers.podman.podman_network_info:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
      register: info
      ignore_errors: true

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

    - name: Create network
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
      register: net

    - name: Get existing network info
      containers.podman.podman_network_info:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
      register: info1

    - name: Check info
      assert:
        that:
          - info1 | length > 1
          - info1.networks.0.name == network_name
          - net is changed

    - name: Create network again
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
      register: info2

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

    # TODO: Enable it when podman v4 is available in CI

    # - name: Create network with disable DNS
    #   containers.podman.podman_network:
    #     executable: "{{ test_executable | default('podman') }}"
    #     name: "{{ network_name }}"
    #     state: present
    #     disable_dns: true
    #   register: info3

    # - name: Check info
    #   assert:
    #     that:
    #       - >-
    #         info3 is changed and dns_plugin|bool or
    #         info3 is not changed and not dns_plugin|bool

    # - name: Create network with disable DNS again
    #   containers.podman.podman_network:
    #     executable: "{{ test_executable | default('podman') }}"
    #     name: "{{ network_name }}"
    #     state: present
    #     disable_dns: true
    #   register: info4

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

    # - name: Create network w/o disable DNS
    #   containers.podman.podman_network:
    #     executable: "{{ test_executable | default('podman') }}"
    #     name: "{{ network_name }}"
    #     state: present
    #   register: info5

    # - name: Check info
    #   assert:
    #     that:
    #       - >-
    #         info5 is changed and dns_plugin|bool or
    #         info5 is not changed and not dns_plugin|bool

    - name: Create network with custom gateway
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        gateway: 10.100.100.100
        subnet: 10.100.100.0/24
      register: info6

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

    - name: Create network with custom gateway again
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        gateway: 10.100.100.100
        subnet: 10.100.100.0/24
      register: info7

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

    - name: Create internal network
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        internal: true
      register: info9

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

    - name: Create internal network again
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        internal: true
      register: info10

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

    - name: Create a regular external network
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
      register: info11

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

    - name: Create network with subnet
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        subnet: 10.200.200.0/24
      register: info12

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

    - name: Create network with subnet again
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        subnet: 10.200.200.0/24
      register: info13

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

    - name: Create network with ipv6 subnet
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        subnet: 2001:cafe::/64
        ipv6: true

    - name: Make sure network doesn't exist
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: absent

    - name: Get existing network info
      containers.podman.podman_network_info:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
      register: info100
      ignore_errors: true

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

    - name: Create network with opts MTU
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        opt:
          mtu: 1311
      register: opt1

    - name: Create network with opts VLAN
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        opt:
          vlan: 5555
      register: opt2

    - name: Create network with opts MTU and VLAN
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        opt:
          mtu: 1311
          vlan: 5555
      register: opt3

    - name: Create network with opts MTU and VLAN again
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
        opt:
          mtu: 1311
          vlan: 5555
      register: opt4

    - name: Check results for network opts
      assert:
        that:
          - opt1 is changed
          - opt2 is changed
          - opt3 is changed
          - opt4 is not changed

  always:

    - name: Cleanup
      command: podman network rm -f {{ network_name }}
      ignore_errors: true

- name: Test podman_network rootless
  become: false
  block:

    - name: Create network
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
      register: net1

    - name: Get existing network info
      containers.podman.podman_network_info:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
      register: info15

    - name: Check info
      assert:
        that:
          - info15 | length > 1
          - info15.networks.0.name == network_name
          - net1 is changed

    - name: Create network again
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        state: present
      register: info16

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

    - name: Create network with IPv6 'exploded'
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        ipv6: yes
        subnet: fd4f:552c:830f:0000::/64
        state: present

    - name: Create network with IPv6 'exploded' again
      containers.podman.podman_network:
        executable: "{{ test_executable | default('podman') }}"
        name: "{{ network_name }}"
        ipv6: yes
        subnet: fd4f:552c:830f:0000::/64
        state: present
      register: info17

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

  always:

    - name: Cleanup
      command: podman network rm -f {{ network_name }}
      ignore_errors: true
