# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing infiniband device
  hosts: all
  vars:
    interface: "{{ lookup('env', 'INFINIBAND_INTERFACE_NAME') |
      default('ib0', true) }}"
  tags:
    - "tests::infiniband"
  tasks:
    - name: Test infiniband device
      block:
        - name: TEST Configuring infiniband devices
          debug:
            msg: "##################################################"
        - name: Setup the IP over the infiniband device
          import_role:
            name: linux-system-roles.network
          vars:
            network_connections:
              - name: "{{ interface }}"
                type: infiniband
                interface_name: "{{ interface }}"

              - name: "{{ interface }}-10"
                type: infiniband
                autoconnect: true
                infiniband:
                  p_key: 10
                  transport_mode: datagram
                parent: "{{ interface }}"
                state: up
                ip:
                  dhcp4: false
                  auto6: false
                  address:
                    - 198.51.100.133/30
                    - 2001:db8::2/32

        - name: Get the details for the virtual device
          command: ip -d a s {{ interface }}.000a
          register: virtual_device_setting
          ignore_errors: true
          changed_when: false

        - name: Assert that the settings in the virtual device
            match the specified settings
          assert:
            that:
              - virtual_device_setting.stdout is search("ipoib pkey 0x800a mode
                datagram")
            msg: the settings in the virtual device do not
              match the specified settings

        - name: "** TEST check IPv4"
          command: ip -4 a s {{ interface }}.000a
          register: result
          until: "'198.51.100.133/30' in result.stdout"
          retries: 20
          delay: 2
          changed_when: false

        - name: "** TEST check IPv6"
          command: ip -6 a s {{ interface }}.000a
          register: result
          until: "'2001:db8::2/32' in result.stdout"
          retries: 20
          delay: 2
          changed_when: false

      always:
        - name: Clean up the test devices and the connection profiles
          tags:
            - "tests::cleanup"
          block:
            - name: Import network role
              import_role:
                name: linux-system-roles.network
              vars:
                network_connections:
                  - name: "{{ interface }}"
                    persistent_state: absent
                    state: down
                  - name: "{{ interface }}-10"
                    persistent_state: absent
                    state: down
              failed_when: false
            - name: Delete the virtual device
              command: ip link del {{ interface }}.000a
              failed_when: false
              changed_when: false
            - name: Verify network state restored to default
              include_tasks: tasks/check_network_dns.yml
