# SPDX-License-Identifier: MIT
---
- name: Test bootc integration - cluster
  hosts: all
  vars_files: vars/main.yml
  tasks:
    - name: Run test
      tags: tests::verify
      when: ansible_connection | d("") == "buildah" or __bootc_validation | d(false)
      block:
        - name: Set up test environment
          include_role:
            name: linux-system-roles.ha_cluster
            tasks_from: test_setup.yml

        - name: Do build time tasks and verify
          when: not __bootc_validation | d(false)
          block:
            - name: Run HA Cluster role
              include_role:
                name: linux-system-roles.ha_cluster
                public: true
              when: not __bootc_validation | d(false)

            - name: Create QEMU deployment during bootc end-to-end test
              delegate_to: localhost
              command: "{{ lsr_scriptdir | quote }}/bootc-buildah-qcow.sh {{ ansible_host | quote }}"
              changed_when: true

        - name: Do build time tasks and verify
          when: __bootc_validation | d(false)
          block:
            - name: Get package facts
              package_facts:

            - name: Check installed packages
              assert:
                that:
                  - "'corosync' in ansible_facts.packages"
                  - "'pacemaker' in ansible_facts.packages"
                  - "'pcs' in ansible_facts.packages"

            - name: Get services status
              include_tasks: tasks/handle_service_facts.yml

            - name: Check that services exist and are not running
              assert:
                that:
                  - __test_service_facts["pcsd.service"].status == "disabled"
                  - __test_service_facts["pcsd.service"].state == "inactive"
                  - __test_service_facts["corosync.service"].status == "disabled"
                  - __test_service_facts["corosync.service"].state == "inactive"
                  - __test_service_facts["pacemaker.service"].status == "disabled"
                  - __test_service_facts["pacemaker.service"].state == "inactive"

            - name: Stat corosync config
              stat:
                path: /etc/corosync/corosync.conf
              register: stat_corosync_conf

            - name: Stat corosync authkey
              stat:
                path: /etc/corosync/authkey
              register: stat_corosync_key

            - name: Stat pacemaker authkey
              stat:
                path: /etc/pacemaker/authkey
              register: stat_pacemaker_key

            - name: Stat fence-virt authkey
              stat:
                path: /etc/cluster/fence_xvm.key
              register: stat_fence_xvm_key

            - name: Stat pcsd TLS certificate
              stat:
                path: /var/lib/pcsd/pcsd.crt
              register: stat_pcsd_cert

            - name: Stat pcsd TLS key
              stat:
                path: /var/lib/pcsd/pcsd.key
              register: stat_pcsd_key

            - name: Check that config files, keys and certificates are not present
              assert:
                that:
                  - not stat_corosync_conf.stat.exists
                  - not stat_corosync_key.stat.exists
                  - not stat_pacemaker_key.stat.exists
                  - not stat_fence_xvm_key.stat.exists
                  - not stat_pcsd_cert.stat.exists
                  - not stat_pcsd_key.stat.exists
