# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing bond cloned MAC address
  hosts: all
  vars:
    controller_profile: bond0
    controller_device: nm-bond
    port1_profile: bond0.0
    dhcp_interface1: test1
    port2_profile: bond0.1
    dhcp_interface2: test2
  tasks:
    - name: Test bond cloned MAC address
      block:
        - name: INIT Prepare setup
          debug:
            msg: "##################################################"
        - name: Import the task 'create_test_interfaces_with_dhcp.yml'
          import_tasks: tasks/create_test_interfaces_with_dhcp.yml
        - name: Backup the /etc/resolv.conf for initscript
          command: cp -vf /etc/resolv.conf /etc/resolv.conf.bak
          when: network_provider == "initscripts"
          changed_when: false
        - name: TEST Add Bond with 2 ports
          debug:
            msg: "##################################################"
        - name: Import network role
          import_role:
            name: linux-system-roles.network
          vars:
            network_connections:
              # Create a bond controller
              - name: "{{ controller_profile }}"
                state: up
                type: bond
                interface_name: "{{ controller_device }}"
                bond:
                  mode: active-backup
                  miimon: 110
                ip:
                  route_metric4: 65535
                cloned_mac: "12:23:34:45:56:60"
              # add an ethernet to the bond
              - name: "{{ port1_profile }}"
                state: up
                type: ethernet
                interface_name: "{{ dhcp_interface1 }}"
                controller: "{{ controller_profile }}"
                cloned_mac: "12:23:34:45:56:61"
              # add a second ethernet to the bond
              - name: "{{ port2_profile }}"
                state: up
                type: ethernet
                interface_name: "{{ dhcp_interface2 }}"
                controller: "{{ controller_profile }}"

        - name: Verify nmcli cloned-mac-address entry
          command: >-
            nmcli -f 802-3-ethernet.cloned-mac-address con show {{ item.name }}
          register: cloned_mac_address
          ignore_errors: true
          changed_when: false
          loop:
            - {name: "{{ controller_profile }}", mac: "12:23:34:45:56:60"}
            - {name: "{{ port1_profile }}", mac: "12:23:34:45:56:61"}
            - {name: "{{ port2_profile }}", mac: "--"}
          when: network_provider == 'nm'

        - name: >
            Assert that cloned-mac-address addresses are configured correctly
          assert:
            that:
              - item.stdout.endswith(item.item.mac)
            msg: cloned-mac-address is configured incorrectly
          loop: "{{ cloned_mac_address.results }}"
          when: network_provider == 'nm'

        - name: Verify the MAC address in {{ controller_profile }}
          command: >-
            grep 'MACADDR'
            /etc/sysconfig/network-scripts/ifcfg-{{ controller_profile }}
          register: mac_address_controller
          ignore_errors: true
          changed_when: false
          when: network_provider == 'initscripts'

        - name: Verify the MAC address in {{ port1_profile }}
          command: >-
            grep 'MACADDR'
            /etc/sysconfig/network-scripts/ifcfg-{{ port1_profile }}
          register: mac_address_port1
          ignore_errors: true
          changed_when: false
          when: network_provider == 'initscripts'

        - name: Assert that MAC addresses are configured correctly for bonding
            interface
          assert:
            that:
              - mac_address_controller.stdout is search("12:23:34:45:56:60")
              - mac_address_port1.stdout is search("12:23:34:45:56:61")
            msg: the MAC addresses are configured incorrectly for bonding
              interface
          when: network_provider == 'initscripts'

      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: "{{ port2_profile }}"
                    persistent_state: absent
                    state: down
                  - name: "{{ port1_profile }}"
                    persistent_state: absent
                    state: down
                  - name: "{{ controller_profile }}"
                    persistent_state: absent
                    state: down
              failed_when: false
            - name: Delete the device '{{ controller_device }}'
              command: ip link del {{ controller_device }}
              failed_when: false
              changed_when: false
            - name: Import the task 'remove_test_interfaces_with_dhcp.yml'
              import_tasks: tasks/remove_test_interfaces_with_dhcp.yml
            - name: Restore the /etc/resolv.conf for initscript
              command: mv -vf /etc/resolv.conf.bak /etc/resolv.conf
              when:
                - network_provider == "initscripts"
              changed_when: false
            - name: Verify network state restored to default
              include_tasks: tasks/check_network_dns.yml
