# SPDX-License-Identifier: MIT
# Example test inventory:
# all:
#   hosts:
#     managed_host1:
#       ansible_host: <ip_address1>
#       mssql_ha_replica_type: primary
#       mssql_ha_ag_secondary_role_allow_connections: ALL
#       mssql_ha_ag_read_only_routing_list: ('managed_host2')
#     managed_host2:
#       ansible_host: <ip_address2>
#       mssql_ha_replica_type: synchronous
#       mssql_ha_ag_secondary_role_allow_connections: READ_ONLY
#     managed_host3:
#       ansible_host: <ip_address3>
#       mssql_ha_replica_type: witness
---
- name: Verify HA functionality with external clusters and templates
  hosts: all
  tags:
    # this test doesn't work in container builds
    - tests::booted
  vars:
    mssql_debug: true
    mssql_accept_microsoft_odbc_driver_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: external
    mssql_ha_login: ExampleLogin
    mssql_ha_login_password: "p@55w0rD3"
    mssql_ha_virtual_ip: 192.168.124.158
    # ha_cluster doesn't support running against a single host
    mssql_manage_ha_cluster: "{{ ansible_play_hosts_all | length > 1 }}"
    ha_cluster_cluster_name: "{{ mssql_ha_ag_name }}"
    ha_cluster_hacluster_password: "p@55w0rD4"
    ha_cluster_sbd_enabled: true
    ha_cluster_cluster_properties:
      - attrs:
          - name: cluster-recheck-interval
            value: 2min
          - name: start-failure-is-fatal
            value: true
          - name: stonith-enabled
            value: true
          - name: stonith-watchdog-timeout
            value: 10
    ha_cluster_resource_primitives:
      - id: ag_cluster
        agent: ocf:mssql:ag
        instance_attrs:
          - attrs:
              - name: ag_name
                value: "{{ mssql_ha_ag_name }}"
        meta_attrs:
          - attrs:
              - name: failure-timeout
                value: 60s
      - id: virtualip
        agent: ocf:heartbeat:IPaddr2
        instance_attrs:
          - attrs:
              - name: ip
                value: "{{ mssql_ha_virtual_ip }}"
        operations:
          - action: monitor
            attrs:
              - name: interval
                value: 30s
    ha_cluster_resource_clones:
      - resource_id: ag_cluster
        promotable: true
        meta_attrs:
          - attrs:
              - name: notify
                value: true
    ha_cluster_constraints_colocation:
      - resource_leader:
          id: ag_cluster-clone
          role: Promoted
        resource_follower:
          id: virtualip
        options:
          - name: score
            value: INFINITY
    ha_cluster_constraints_order:
      - resource_first:
          id: ag_cluster-clone
          action: promote
        resource_then:
          id: virtualip
          action: start
  tasks:
    - name: Run test in a block to clean up in always
      block:
        - name: This test is applicable in multi-host scenario only
          meta: end_play
          when: ansible_play_hosts_all | length < 3

        - name: Load softdog module for stonith to have at least one watchdog
          command: modprobe softdog
          changed_when: true

        - 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: Set up test environment for the ha_cluster role
          include_role:
            name: fedora.linux_system_roles.ha_cluster
            tasks_from: test_setup.yml

        - name: Run to configure AG
          include_role:
            name: linux-system-roles.mssql

        - name: Wait for the cluster to finish configuration
          command: crm_resource --wait
          changed_when: false
          register: __mssql_crm_resource_wait
          until: __mssql_crm_resource_wait is success

        # Run sqlcmd against virtual_ip with ReadOnly application intent and
        # with one of our HA DBs. Running this should route to READ_ONLY node,
        # and hence should return the "allow_connections: READ_ONLY" node name.
        - name: Run with ReadOnly intent and check that routing worked
          command: >-
            {{ __mssql_sqlcmd_login_cmd }}
            -K ReadOnly
            -S {{ mssql_ha_virtual_ip }}
            -d {{ mssql_ha_db_names[0] }}
            -Q "SELECT @@SERVERNAME"
          register: __mssql_sqlcmd_input
          changed_when: false
          failed_when: '"managed-node2" not in __mssql_sqlcmd_input.stdout'
          when: mssql_ha_replica_type == 'primary'
          until: __mssql_sqlcmd_input is success
      always:
        - name: Clean up after the role invocation
          include_tasks: tasks/cleanup.yml
