# SPDX-License-Identifier: MIT
---
- name: Test authentication
  hosts: all
  vars:
    __test_uname: pcptest
    metrics_username: "{{ __test_uname }}"
    metrics_password: "{{ pcptest_pw }}"
    # test to install firewall but not selinux
    metrics_manage_firewall: true
    metrics_manage_selinux: false

  pre_tasks:
    - name: Stop test
      meta: end_host
      when: (ansible_distribution in ['RedHat', 'CentOS'] and
             ansible_distribution_major_version | int < 8) or
             ansible_distribution not in ['Fedora', 'RedHat', 'CentOS']

    - name: Save state of services
      import_tasks: get_services_state.yml

  tasks:
    - name: Run test
      block:
        - name: Run role
          include_role:
            name: linux-system-roles.metrics
            public: true

        - name: Restart PMCD
          # noqa command-instead-of-module
          shell: systemctl restart pmcd && sleep 5
          changed_when: false
          when: __metrics_is_booted | bool

        - name: Check if SASL works
          include_tasks: "{{ item }}"
          loop:
            - check_sasl.yml
            - check_firewall_selinux.yml

        - name: Flush handlers
          meta: flush_handlers

      rescue:
        - name: Handle failure case
          include_tasks: handle_test_failure.yml

      always:
        - name: Restore state of services
          import_tasks: restore_services_state.yml
