# SPDX-License-Identifier: MIT
---
- name: Minimal cluster configuration, install cloud agents
  hosts: all
  gather_facts: true  # needs facts
  vars_files: vars/main.yml
  vars:
    ha_cluster_cluster_name: test-cluster
    ha_cluster_install_cloud_agents: true
    ha_cluster_export_configuration: true
    # Only agents available on all architectures are listed so that we don't
    # need a special case for each architecture.
    __test_agents_rhel_8:
      - fence-agents-aws
      - fence-agents-azure-arm
      - fence-agents-gce
    # RHEL 10 has the same agents as RHEL 9
    __test_agents_rhel_9:
      - fence-agents-ibm-powervs
      - fence-agents-ibm-vpc
    __test_agents: "{{ (ansible_facts['distribution_major_version'] == '8') |
      ternary(__test_agents_rhel_8, __test_agents_rhel_9) }}"
    __test_eligible: "{{
      ansible_facts['distribution'] in ['RedHat', 'CentOS'] }}"

  tasks:
    - name: Run test
      tags: tests::verify
      when: __test_eligible
      block:
        - name: Set up test environment
          include_role:
            name: linux-system-roles.ha_cluster
            tasks_from: test_setup.yml

        - name: Skip test on ostree systems
          meta: end_host
          when: __ha_cluster_is_ostree | d(false)

        - name: Ensure cloud agents are not installed
          package:
            name: "{{ __test_agents }}"
            state: absent

        - name: Run HA Cluster role
          include_role:
            name: linux-system-roles.ha_cluster
            public: true

        - name: Get packages status
          package_facts:

        - name: Check installed packages
          assert:
            that:
              - "item in ansible_facts.packages"
          loop: "{{ __test_agents }}"

        - name: Check cluster status
          include_tasks: tasks/assert_cluster_running.yml

        - name: Check exported configuration
          vars:
            __test_exported_config: >
              {{
                ha_cluster_facts | combine({
                  'ha_cluster_enable_repos': 'it depends on test environment',
                  'ha_cluster_enable_repos_resilient_storage': 'it depends on test environment',
                  'ha_cluster_manage_firewall': 'it depends on test environment',
                  'ha_cluster_manage_selinux': 'it depends on test environment',
                  'ha_cluster_node_options': 'it depends on test environment'
                })
              }}
            __test_expected_config:
              ha_cluster_install_cloud_agents: true
              ha_cluster_cluster_present: true
              ha_cluster_cluster_name: test-cluster
              ha_cluster_start_on_boot: true
              ha_cluster_transport:
                type: knet
                crypto:
                  - name: cipher
                    value: aes256
                  - name: hash
                    value: sha256
              ha_cluster_pcs_permission_list:
                - name: haclient
                  type: group
                  allow_list: ["grant", "read", "write"]
              ha_cluster_enable_repos: "it depends on test environment"
              ha_cluster_enable_repos_resilient_storage: "it depends on test environment"
              ha_cluster_manage_firewall: "it depends on test environment"
              ha_cluster_manage_selinux: "it depends on test environment"
              ha_cluster_node_options: "it depends on test environment"
          block:
            - name: Print exported configuration
              debug:
                var: __test_exported_config

            - name: Print expected configuration
              debug:
                var: __test_expected_config

            - name: Compare expected and exported configuration
              assert:
                that:
                  - __test_exported_config == __test_expected_config

    - name: Message
      debug:
        msg: This test is RHEL / CentOS specific
      when: not __test_eligible
