# SPDX-License-Identifier: MIT
---
- name: Cluster with knet transport, full configuration
  hosts: all
  vars_files: vars/main.yml
  vars:
    ha_cluster_export_configuration: true
    ha_cluster_cluster_name: test-cluster
    ha_cluster_transport:
      type: knet
      options:
        - name: ip_version
          value: ipv4-6
        - name: link_mode
          value: active
      # Links variable defines two links, one with implicit linknumber and the
      # other with specified linknumber. On top of that, the links are not
      # listed in order by linknumbers. This is intentional. It tests that the
      # role is capable of handling such links definitions, assigning correct
      # linknumbers and not mixing the links.
      links:
        -
          - name: transport  # yamllint disable-line rule:hyphens
            value: udp
          - name: link_priority
            value: 10
        -
          - name: linknumber  # yamllint disable-line rule:hyphens
            value: 0
          - name: link_priority
            value: 5
      compression:
        - name: level
          value: "5"
        - name: model
          value: zlib
      crypto:
        - name: cipher
          value: none
        - name: hash
          value: none
    ha_cluster_totem:
      options:
        - name: send_join
          value: "0"
        - name: token_retransmits_before_loss_const
          value: "5"
    ha_cluster_quorum:
      options:
        - name: auto_tie_breaker
          value: "1"
        - name: wait_for_all
          value: "1"

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

        - name: Set node addresses variable
          set_fact:
            ha_cluster:
              # Getent prints IP address and hostnames separated by spaces.
              # Split the line by spaces and use only the first two items using
              # batch, first and flatten.
              corosync_addresses: "{{
                lookup('pipe', 'getent hosts ' + inventory_hostname)
                | regex_findall('\\S+') | batch(2) | first | flatten }}"

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

        - name: Fetch cluster versions of cluster components
          include_tasks: tasks/fetch_versions.yml

        - name: Check corosync
          include_tasks: tasks/assert_corosync_config.yml
          vars:
            __test_expected_lines:
              - 'totem {'
              - '    version: 2'
              - '    cluster_name: {{ ha_cluster_cluster_name }}'
              - '    transport: knet'
              - '    send_join: 0'
              - '    token_retransmits_before_loss_const: 5'
              - '    ip_version: ipv4-6'
              - '    link_mode: active'
              - '    knet_compression_level: 5'
              - '    knet_compression_model: zlib'
              - '    crypto_cipher: none'
              - '    crypto_hash: none'
              - '    interface {'
              - '        knet_link_priority: 5'
              - '        linknumber: 0'
              - '    }'
              - '    interface {'
              - '        knet_link_priority: 10'
              - '        knet_transport: udp'
              - '        linknumber: 1'
              - '    }'
              - '}'
              - 'nodelist {'
              - '}'
              - 'quorum {'
              - '    provider: corosync_votequorum'
              - '    auto_tie_breaker: 1'
              - '    wait_for_all: 1'
              - '}'
              - 'logging {'
              - '    to_logfile: yes'
              - '    logfile: /var/log/cluster/corosync.log'
              - '    to_syslog: yes'
              - '    timestamp: on'
              - '}'

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

        - name: Check firewall and selinux state
          include_tasks: tasks/check_firewall_selinux.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_install_cloud_agents': 'it depends on test environment',
                  'ha_cluster_node_options': 'it depends on test environment'
                })
              }}
            __test_expected_config:
              ha_cluster_cluster_present: true
              ha_cluster_cluster_name: test-cluster
              ha_cluster_start_on_boot: true
              ha_cluster_transport:
                type: knet
                options: "{{ ha_cluster_transport.options }}"
                links:
                  -
                    - name: link_priority  # yamllint disable-line rule:hyphens
                      value: "5"
                    - name: linknumber
                      value: "0"
                  -
                    - name: link_priority  # yamllint disable-line rule:hyphens
                      value: "10"
                    - name: transport
                      value: udp
                    - name: linknumber
                      value: "1"
                compression: "{{ ha_cluster_transport.compression }}"
                crypto: "{{ ha_cluster_transport.crypto }}"
              ha_cluster_totem: "{{ ha_cluster_totem }}"
              ha_cluster_quorum: "{{ ha_cluster_quorum }}"
              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_install_cloud_agents: "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

      always:
        - name: Unset node addresses variable
          set_fact:
            ha_cluster:
