# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing ethtool coalesce settings
  hosts: all
  vars:
    interface: "{{ network_interface_name1 }}"
    type: "{{ network_interface_type1 }}"
  tasks:
    - name: "INIT: Ethtool coalesce tests"
      debug:
        msg: "##################################################"
    - name: Include the task 'show_interfaces.yml'
      include_tasks: tasks/show_interfaces.yml
    - name: Include the task 'manage_test_interface.yml'
      include_tasks: tasks/manage_test_interface.yml
      vars:
        state: present
    - name: Include the task 'assert_device_present.yml'
      include_tasks: tasks/assert_device_present.yml
    - name: Install ethtool (test dependency)
      package:
        name: ethtool
        state: present
        use: "{{ (__network_is_ostree | d(false)) |
          ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
    - name: Test ethtool coalesce settings
      block:
        - name: >-
            TEST: I can create a profile without changing the ethtool coalesce.
          debug:
            msg: "##################################################"
        - name: Get current device coalesce
          command: "ethtool --show-coalesce {{ interface }}"
          register: original_ethtool_coalesce
          changed_when: false
        - name: Import network role
          import_role:
            name: linux-system-roles.network
          vars:
            network_connections:
              - name: "{{ interface }}"
                state: up
                type: ethernet
                ip:
                  dhcp4: "no"
                  auto6: "no"
        - name: Get current device coalesce
          command: "ethtool --show-coalesce {{ interface }}"
          register: ethtool_coalesce
          changed_when: false
        - name: "ASSERT: The profile does not change the ethtool coalesce"
          assert:
            that:
              - original_ethtool_coalesce.stdout == ethtool_coalesce.stdout
        - name: >-
            TEST: I can set rx-frames and adaptive-tx.
          debug:
            msg: "##################################################"
        - name: Import network role
          import_role:
            name: linux-system-roles.network
          vars:
            network_connections:
              - name: "{{ interface }}"
                state: up
                type: ethernet
                ip:
                  dhcp4: "no"
                  auto6: "no"
                ethtool:
                  coalesce:
                    rx_frames: 1
                    tx_frames: 1
        - name: Get current device coalesce
          command: "ethtool --show-coalesce {{ interface }}"
          register: ethtool_coalesce
          changed_when: false
        - name: Show ethtool_coalesce output
          debug:
            var: ethtool_coalesce.stdout_lines
        - name: Assert device coalesce
          assert:
            that:
              - >-
                  'rx-frames: 1' in
                  ethtool_coalesce.stdout_lines
              - >-
                  'tx-frames: 1' in
                  ethtool_coalesce.stdout_lines
        - name: "TEST: I can reset coalesce to their original value."
          debug:
            msg: "##################################################"
        - name: Import network role
          import_role:
            name: linux-system-roles.network
          vars:
            network_connections:
              - name: "{{ interface }}"
                state: up
                type: ethernet
                ip:
                  dhcp4: "no"
                  auto6: "no"
        - name: Get current device coalesce
          command: "ethtool --show-coalesce {{ interface }}"
          register: ethtool_coalesce
          changed_when: false
        # Resetting the ethtools only works with NetworkManager
        - name: "ASSERT: The profile does not change the ethtool coalesce"
          assert:
            that:
              - original_ethtool_coalesce.stdout == ethtool_coalesce.stdout
          when:
            network_provider == 'nm'
      always:
        - name: Clean up the test device and the connection profile
          tags:
            - "tests::cleanup"
          block:
            - name: Deactivate the connection and remove the connection profile
              import_role:
                name: linux-system-roles.network
              vars:
                network_connections:
                  - name: "{{ interface }}"
                    persistent_state: absent
                    state: down
              failed_when: false
            - name: Include the task 'manage_test_interface.yml'
              include_tasks: tasks/manage_test_interface.yml
              vars:
                state: absent
