# SPDX-License-Identifier: MIT
---
- name: Run test
  vars:
    ha_cluster_cluster_name: test-cluster
    ha_cluster_sbd_enabled: true
    ha_cluster_sbd_options:
      - name: delay-start
        value: 2
      - name: startmode
        value: clean
      - name: timeout-action
        value: reboot,flush
      - name: watchdog-timeout
        value: 10
  block:
    - name: Ensure modprobe files are not present
      file:
        state: absent
        path: "{{ item }}"
      loop:
        - /etc/modprobe.d/iTCO_wdt.conf
        - /etc/modules-load.d/softdog.conf

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

    - name: Slurp generated SBD watchdog blocklist file
      slurp:
        src: /etc/modprobe.d/iTCO_wdt.conf
      register: __test_sbd_watchdog_blocklist_file

    - name: Decode SBD watchdog blocklist file
      set_fact:
        __test_sbd_watchdog_blocklist_file_lines: "{{
            (__test_sbd_watchdog_blocklist_file.content |
            b64decode).splitlines()
          }}"

    - name: Print SBD watchdog blocklist file lines
      debug:
        var: __test_sbd_watchdog_blocklist_file_lines

    - name: Check SBD watchdog blocklist file
      assert:
        that:
          - __blockstr in __test_sbd_watchdog_blocklist_file_lines
      vars:
        # wokeignore:rule=blacklist
        __blockstr: blacklist iTCO_wdt

    - name: Slurp generated SBD watchdog modprobe file
      slurp:
        src: /etc/modules-load.d/softdog.conf
      register: __test_sbd_watchdog_modprobe_file

    - name: Decode SBD watchdog modprobe file
      set_fact:
        __test_sbd_watchdog_modprobe_file_lines: "{{
            (__test_sbd_watchdog_modprobe_file.content |
            b64decode).splitlines()
          }}"

    - name: Print SBD watchdog modprobe file lines
      debug:
        var: __test_sbd_watchdog_modprobe_file_lines

    - name: Check SBD watchdog modprobe file
      assert:
        that:
          - __modulestr in __test_sbd_watchdog_modprobe_file_lines
      vars:
        __modulestr: softdog

    - name: Run lsmod for SBD watchdog module
      command: lsmod
      changed_when: false
      register: __test_sbd_watchdog_sbd_lsmod

    - name: Print lsmod
      debug:
        var: __test_sbd_watchdog_sbd_lsmod

    - name: Check lsmod output for absence of SBD watchdog module blocklist
      assert:
        that:
          - "'iTCO_wdt' not in __test_sbd_watchdog_sbd_lsmod.stdout"

    - name: Check lsmod output for SBD watchdog module
      assert:
        that:
          - "'softdog' in __test_sbd_watchdog_sbd_lsmod.stdout"

    - name: Slurp SBD config file
      slurp:
        src: /etc/sysconfig/sbd
      register: __test_sbd_config

    - name: Decode SBD config
      set_fact:
        __test_sbd_config_lines: "{{
            (__test_sbd_config.content | b64decode).splitlines()
          }}"

    - name: Print SBD config lines
      debug:
        var: __test_sbd_config_lines

    - name: Check SBD config
      assert:
        that:
          - "'SBD_DELAY_START=\"2\"' in __test_sbd_config_lines"
          - "'SBD_DEVICE=\"' ~ __test_sbd_mount.stdout ~ '\"'
            in __test_sbd_config_lines"
          - "'SBD_STARTMODE=\"clean\"' in __test_sbd_config_lines"
          - "'SBD_TIMEOUT_ACTION=\"reboot,flush\"'
            in __test_sbd_config_lines"
          - "'SBD_WATCHDOG_DEV=\"/dev/null\"' in __test_sbd_config_lines"
          - "'SBD_WATCHDOG_TIMEOUT=\"10\"' in __test_sbd_config_lines"
          - >
            __test_sbd_config_lines[-1]
            == 'SBD_OPTS="-n ' ~ __ha_cluster_node_name ~ '"'

    - name: Check firewall and selinux state
      include_tasks: tasks/check_firewall_selinux.yml

    - name: Check header for ansible_managed, fingerprint
      include_tasks: tasks/check_header.yml
      vars:
        __file_content: "{{ __test_sbd_config }}"
        __fingerprint: "system_role:ha_cluster"
