# SPDX-License-Identifier: BSD-3-Clause
# This test should check whether the NMDevice.reapply method is called by the
# role. This is probably a good candidate to test with pytest directly instead
# of via Ansible. Until there is better test support for this, just check the
# log output for the respective log message.
---
- name: Play for testing reapplying the connection
  hosts: all
  vars:
    interface: rpltstbr
    profile: "{{ interface }}"
    network_provider: nm
  pre_tasks:
    - name: Show test banner
      debug:
        msg: Inside states tests
    - name: Include the task 'show_interfaces.yml'
      include_tasks: tasks/show_interfaces.yml
    - name: Include the task 'assert_device_absent.yml'
      include_tasks: tasks/assert_device_absent.yml
  roles:
    - linux-system-roles.network
  tasks:
    - name: Test reapply the connection
      block:
        # create test profile
        - name: Include network role
          include_role:
            name: linux-system-roles.network
          vars:
            network_connections:
              - name: "{{ interface }}"
                state: up
                type: bridge
                ip:
                  dhcp4: false
                  auto6: false
        - name: Include the task 'assert_device_present.yml'
          include_tasks: tasks/assert_device_present.yml
        - name: Include the task 'assert_profile_present.yml'
          include_tasks: tasks/assert_profile_present.yml
        # Use internal module to get output
        - name: Get network_connections output
          network_connections:
            provider: nm
            connections:
              - name: "{{ interface }}"
                state: up
                type: bridge
                ip:
                  address:
                    - 192.0.2.72/31
                  dhcp4: false
                  auto6: false
            __header: "# Ansible managed test header"
          ignore_errors: true
          register: test_module_run
        - name: Show test_module_run
          debug:
            var: test_module_run
        - name: Assert that reapply is found in log output
          assert:
            fail_msg: Reapply not found in log output
            that: "'connection reapplied' in test_module_run.stderr"
      always:
        - name: Clean up the test device and the connection profile
          tags:
            - "tests::cleanup"
          ignore_errors: true  # noqa ignore-errors
          block:
            # Use internal module directly for speedup
            - name: Deactivate the connection and remove the connection profile
              network_connections:
                provider: nm
                connections:
                  - name: "{{ interface }}"
                    persistent_state: absent
                    state: down
            - name: Delete the device '{{ interface }}'
              command: ip link del "{{ interface }}"
              changed_when: false
            - name: Verify network state restored to default
              include_tasks: tasks/check_network_dns.yml
