# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing states
  hosts: all
  vars:
    interface: statebr
    profile: "{{ interface }}"
    lsr_fail_debug:
      - __network_connections_result
  tasks:
    - name: Show playbook name
      debug:
        msg: "this is: playbooks/tests_states.yml"
      tags:
        - always

    - name: Test creating the bridge connection
      tags:
        - tests::states:create
      block:
        - name: Include the task 'run_test.yml'
          include_tasks: tasks/run_test.yml
          vars:
            lsr_description: I can create a profile
            lsr_setup:
              - tasks/delete_interface.yml
              - tasks/assert_device_absent.yml
            lsr_test:
              - tasks/create_bridge_profile.yml
            lsr_assert:
              - tasks/assert_profile_present.yml
            lsr_assert_when:
              # Device should be present because of autoconnect: true by
              # default for NM (this might be considered a bug)
              - what: tasks/assert_device_present.yml
                condition: "{{ network_provider == 'nm' }}"
            lsr_cleanup:
              - tasks/cleanup_profile+device.yml

    - name: Test creating the bridge connection without autoconnect
      tags:
        - tests::states:create_without_autoconnect
      block:
        - name: Include the task 'run_test.yml'
          include_tasks: tasks/run_test.yml
          vars:
            lsr_description: I can create a profile without autoconnect
            lsr_setup:
              - tasks/delete_interface.yml
              - tasks/assert_device_absent.yml
            lsr_test:
              - tasks/create_bridge_profile_no_autoconnect.yml
            lsr_assert:
              # Device should be absent because of autoconnect: false
              - tasks/assert_device_absent.yml
              - tasks/assert_profile_present.yml
            lsr_cleanup:
              - tasks/cleanup_profile+device.yml

    - name: Test activating the bridge connection
      tags:
        - tests::states:activate
      block:
        - name: Include the task 'run_test.yml'
          include_tasks: tasks/run_test.yml
          vars:
            lsr_description: I can activate an existing profile
            lsr_setup:
              - tasks/create_bridge_profile.yml
            lsr_test:
              - tasks/activate_profile.yml
            lsr_assert:
              - tasks/assert_device_present.yml
              - tasks/assert_profile_present.yml
            lsr_cleanup:
              - tasks/cleanup_profile+device.yml

    - name: Test removing the bridge connection profile without taking it down
      tags:
        - tests::states:remove_up
      block:
        - name: Include the task 'run_test.yml'
          include_tasks: tasks/run_test.yml
          vars:
            lsr_description: I can remove an existing profile without taking it
              down
            lsr_setup:
              - tasks/create_bridge_profile.yml
              - tasks/activate_profile.yml
            lsr_test:
              - tasks/remove_profile.yml
            lsr_assert:
              - tasks/assert_device_present.yml
              - tasks/assert_profile_absent.yml
            lsr_cleanup:
              - tasks/cleanup_profile+device.yml

    - name: Test removing the bridge connection profile and taking it down
      tags:
        - tests::states:remove_down
      block:
        - name: Include the task 'run_test.yml'
          include_tasks: tasks/run_test.yml
          vars:
            lsr_description: I can take a profile down that is absent
            lsr_setup:
              - tasks/create_bridge_profile.yml
              - tasks/activate_profile.yml
              - tasks/remove_profile.yml
            lsr_test:
              - tasks/remove+down_profile.yml
            lsr_assert:
              - tasks/assert_profile_absent.yml
            lsr_assert_when:
              - what: tasks/assert_device_absent.yml
                condition: "{{ network_provider == 'nm' }}"
            lsr_cleanup:
              - tasks/cleanup_profile+device.yml

    - name: Test removing the bridge connection profile and taking it down
        twice
      tags:
        - tests::states:remove_down_twice
      block:
        - name: Include the task 'run_test.yml'
          include_tasks: tasks/run_test.yml
          vars:
            lsr_description: I will not get an error when I try to
              remove an absent profile
            lsr_setup:
              - tasks/create_bridge_profile.yml
              - tasks/activate_profile.yml
              - tasks/remove+down_profile.yml
            lsr_test:
              - tasks/remove+down_profile.yml
            lsr_assert:
              - tasks/assert_profile_absent.yml
              # FIXME: This needs to be included before lsr_assert_when but
              # after the role ran to ensure that NetworkManager is actually
              # installed but it is not an assert.
              - tasks/get_NetworkManager_NVR.yml
            lsr_assert_when:
              - what: tasks/assert_device_absent.yml
                condition: "{{ network_provider == 'nm' }}"
            lsr_cleanup:
              - tasks/cleanup_profile+device.yml
              - tasks/check_network_dns.yml
