# SPDX-License-Identifier: MIT
---
# All nodes must have the same number of SBD devices. The role is supposed to
# exit with an error if that's not the case. This test verifies, that the check
# works, when SBD devices are defined in 'ha_cluster' variable (inventory
# style).
- name: Test a check SBD devices count matches for all nodes (inventory)
  hosts: all
  vars_files: vars/main.yml
  vars:
    ha_cluster_sbd_enabled: true

  tasks:
    - name: Run test
      when: ansible_play_hosts_all | length > 1
      tags: tests::verify
      block:
        - name: Set up test environment
          include_role:
            name: linux-system-roles.ha_cluster
            tasks_from: test_setup.yml

        # Generate an "inventory" variable for each node
        - name: Set SBD devices variable
          set_fact:
            ha_cluster:
              sbd_devices: "{{
                  range(1, ansible_play_hosts_all.index(inventory_hostname) + 2)
                  | product(['/tmp/dev/sdx']) | map('reverse') | map('join')
                  | list }}"

        - name: Print set SBD devices
          debug:
            var: ha_cluster.sbd_devices

        - name: Run the role and check for errors
          block:
            - name: Run the role
              include_role:
                name: linux-system-roles.ha_cluster
          rescue:
            - name: Check errors
              assert:
                that: ansible_failed_result.msg == expected_msg
              run_once: true  # noqa: run_once[task]
              vars:
                expected_msg: >-
                  All nodes must have the same number of SBD devices specified
      always:
        - name: Unset SBD devices variable
          set_fact:
            ha_cluster:

    - name: Message
      debug:
        msg: This test needs two or more nodes
      when: ansible_play_hosts_all | length <= 1
