# SPDX-License-Identifier: MIT
# Example test inventory:
# all:
#   hosts:
#     managed_host1:
#       ansible_host: <ip_address1>
#       mssql_ha_replica_type: primary
#       mssql_version: <version_to_test>
#     managed_host2:
#       ansible_host: <ip_address2>
#       mssql_ha_replica_type: synchronous
#       mssql_version: <version_to_test>
#     managed_host3:
#       ansible_host: <ip_address3>
#       mssql_ha_replica_type: asynchronous
#       mssql_version: <version_to_test>
---
- name: Verify HA functionality with read-scale clusters
  hosts: all
  vars:
    mssql_debug: true
    mssql_accept_microsoft_odbc_driver_17_for_sql_server_eula: true
    mssql_accept_microsoft_cli_utilities_for_sql_server_eula: true
    mssql_accept_microsoft_sql_server_standard_eula: true
    mssql_password: "p@55w0rD"
    mssql_edition: Evaluation
    mssql_version: 2022
    mssql_manage_firewall: true
    __mssql_gather_facts_no_log: true
    mssql_manage_selinux: false
    mssql_run_selinux_confined: false
    mssql_ha_configure: true
    mssql_ha_endpoint_port: 5022
    mssql_ha_cert_name: ExampleCert
    mssql_ha_master_key_password: "p@55w0rD1"
    mssql_ha_private_key_password: "p@55w0rD2"
    mssql_ha_reset_cert: false
    mssql_ha_endpoint_name: Example_Endpoint
    mssql_ha_ag_name: ExampleAG
    mssql_ha_db_names:
      - ExampleDB1
      - ExampleDB2
    mssql_ha_ag_cluster_type: none
  tasks:
    - name: Run test in a block to clean up in always
      when: ansible_play_hosts_all | length >= 3
      block:
        - name: Clusters of a read_scale type must not have a witness node
          meta: end_play
          when: ansible_play_hosts_all |
            map('extract', hostvars, 'mssql_ha_replica_type') |
            select('match', '^witness$') |
            list |
            length > 0

        - name: Verify that by default the role fails on EL < 8
          when:
            - ansible_distribution in ['CentOS', 'RedHat']
            - ansible_distribution_version is version('8', '<')
          block:
            - name: Run the role
              vars:
                mssql_ha_configure: true
              include_role:
                name: linux-system-roles.mssql

            - name: Unreachable task
              fail:
                msg: The above task must fail
          rescue:
            - name: Assert that the role failed with EL 7 not supported
              assert:
                that: >-
                  'mssql_ha_configure=true does not support running against EL 7
                  hosts' in ansible_failed_result.msg
          always:
            - name: Clean up after the role invocation
              include_tasks: tasks/cleanup.yml
              when: ansible_play_hosts_all | length == 1

            # Putting end_host into a rescue block results in a failed task
            - name: End EL 7 host
              meta: end_host

        - name: Set facts to create test DBs on primary as a pre task
          set_fact:
            mssql_pre_input_sql_file:
              - create_ExampleDB1.sql
              - create_ExampleDB2.sql
          when: mssql_ha_replica_type == 'primary'

        - name: Run on all hosts to configure read-scale cluster
          include_role:
            name: linux-system-roles.mssql

        - name: Verify that the database exists on the secondary servers
          vars:
            __mssql_sql_content_to_input:
              - |-
                IF EXISTS(
                  SELECT name
                  FROM sys.databases
                  WHERE name = 'ExampleDB1'
                )
                BEGIN
                  PRINT 'SUCCESS, The database ExampleDB1 exists'
                END
                ELSE
                BEGIN
                  PRINT 'FAIL, The database ExampleDB1 exists'
                END
          include_role:
            name: linux-system-roles.mssql
            tasks_from: input_sql_files.yml

        - name: Assert that the template reported changed state
          assert:
            that: >-
              "SUCCESS, The database ExampleDB1 exists" in
              __mssql_sqlcmd_input.stdout
      always:
        - name: Clean up after the role invocation
          include_tasks: tasks/cleanup.yml
