# SPDX-License-Identifier: BSD-3-Clause
# This test is supposed to check that checkpoints are properly cleaned-up after
# failures in the module. This test currently uses the initscripts provider to
# mark a device as unmanaged for NM and then tries to activate it using NM.
# This failed without removing the checkpoint.
---
- name: Play for testing checkpoint cleanup
  hosts: all
  vars:
    interface: cptstbr
    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 checkpoint cleanup
      block:
        # Workaround for https://bugzilla.redhat.com/show_bug.cgi?id=1832897
        - name: Install dbus-tools
          package:
            name: dbus-tools
            state: present
            use: "{{ (__network_is_ostree | d(false)) |
              ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
        # create test profile
        - name: Include network role
          include_role:
            name: linux-system-roles.network
          vars:
            network_provider: initscripts
            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 directly for speedup
        - name: Use network_connections
          network_connections:
            provider: nm
            connections:
              - name: "{{ interface }}"
                state: up
                type: bridge
                ip:
                  dhcp4: false
                  auto6: false
          ignore_errors: true
          register: error_trigger
        - name: Assert that the module call did not fail
          assert:
            fail_msg: The module call did not fail. Therefore the test
              condition was not triggered. This test needs to be adjusted or
              dropped.
            that: error_trigger.failed
        # yamllint disable-line rule:line-length
        - name: Get NM dbus objects
          command: busctl --system tree --list org.freedesktop.NetworkManager
          register: nm_dbus_objects
          changed_when: false
        - name: Show nm_dbus_objects
          debug:
            var: nm_dbus_objects
        - name: Assert that no checkpoints are left
          assert:
            fail_msg: Checkpoints not cleaned up
            that: >
              '/org/freedesktop/NetworkManager/Checkpoint/' not in
              nm_dbus_objects.stdout_lines
      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: Remove the connection profile
              file:
                dest: "/etc/sysconfig/network-scripts/ifcfg-{{ interface }}"
                state: absent
            - 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
