# SPDX-License-Identifier: MIT
---
- name: Test installation and configuration of MSSQL agent
  hosts: all

  roles:
    - role: performancecopilot.metrics.keyserver
    - role: performancecopilot.metrics.mssql
    - role: performancecopilot.metrics.pcp
      vars:
        pcp_optional_agents: ['mssql']

  pre_tasks:
    - name: Skip test if not supported by platform
      meta: end_host
      when: (ansible_distribution in ['RedHat'] and
             (ansible_facts['distribution_version'] is version('8.4', '<'))) or
             ansible_distribution not in ['Fedora', 'RedHat'] or
             ansible_architecture not in ['x86_64']

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

    - name: Determine if system is ostree and set flag
      when: not __ansible_pcp_is_ostree is defined
      block:
        - name: Check if system is ostree
          stat:
            path: /run/ostree-booted
          register: __ostree_booted_stat

        - name: Set flag to indicate system is ostree
          set_fact:
            __ansible_pcp_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"

    - name: Ensure python3-pyodbc is installed
      package:
        name: python3-pyodbc
        state: present
        use: "{{ (__ansible_pcp_is_ostree | d(false)) |
                ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

  tasks:
    - name: Check MSSQL functionality
      include_tasks: "{{ item }}"
      loop:
        - check_mssql.yml

    - name: Check headers for ansible_managed, fingerprint
      include_tasks: check_header.yml
      vars:
        __fingerprint: "performancecopilot:ansible-pcp"
        __file: "{{ __mssql_conf }}"

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