# SPDX-License-Identifier: MIT
---
- name: Basic usability test
  hosts: all
  # argument validation requires facts
  gather_facts: true
  vars:
    common_cluster_name: testcluster
    mount_point: /mnt/test
    gfs2_enable_repos: "{{ ansible_facts['distribution'] != 'RedHat' }}"
  tasks:
    - name: Skip this test if test system does not support
      meta: end_host
      when: lookup("env", "SYSTEM_ROLES_GFS2_CLUSTER_TEST") != "true"

    - name: Run the test
      block:
        # Make the host alias we specified in LSR_QEMU_IMAGE_ALIAS (tox.ini)
        # resolve
        - name: Update /etc/hosts
          ansible.builtin.lineinfile:
            path: /etc/hosts
            line: 127.0.0.1 testnode

        - name: Get unused disks
          ansible.builtin.include_tasks: get_unused_disk.yml
          vars:
            min_size: "5g"
            max_return: 1
            disks_needed: 1

        - name: Create cluster
          ansible.builtin.include_role:
            name: fedora.linux_system_roles.ha_cluster
          vars:
            ha_cluster_enable_repos: false
            ha_cluster_cluster_name: "{{ common_cluster_name }}"
            ha_cluster_hacluster_password: insecure
            ha_cluster_extra_packages:
              - dlm
              - lvm2-lockd
            ha_cluster_cluster_properties:
              - attrs:
                  # Don't do this in production
                  - name: stonith-enabled
                    value: "false"
            ha_cluster_resource_primitives:
              - id: dlm
                agent: "ocf:pacemaker:controld"
                instance_attrs:
                  - attrs:
                      # Don't do this in production
                      - name: allow_stonith_disabled
                        value: "true"
              - id: lvmlockd
                agent: "ocf:heartbeat:lvmlockd"
            ha_cluster_resource_groups:
              - id: locking
                resource_ids:
                  - dlm
                  - lvmlockd

        - name: Create gfs2 filesystem and cluster resources
          ansible.builtin.include_role:
            name: linux-system-roles.gfs2
          vars:
            # noqa var-naming[no-role-prefix]
            _gfs2_test_allow_stonith_disabled: true
            gfs2_cluster_name: "{{ common_cluster_name }}"
            gfs2_file_systems:
              - name: fs1
                pvs:
                  - "{{ unused.disks[0] }}"
                vg: vg_gfs2_1
                lv: lv_gfs2_1
                lv_size: 4G
                mount_point: "{{ mount_point }}"

        - name: Create a file on the gfs2 filesystem
          ansible.builtin.file:
            path: "{{ mount_point }}/boop"
            state: touch
            mode: "0644"

        - name: Run stat
          ansible.builtin.command:
            argv:
              - stat
              - -f
              - "{{ mount_point }}/boop"
          register: stat_result
          failed_when: "'gfs/gfs2' not in stat_result.stdout"
          changed_when: true

        - name: Re-run the role to test idempotency
          ansible.builtin.include_role:
            name: linux-system-roles.gfs2
          vars:
            # noqa var-naming[no-role-prefix]
            _gfs2_test_allow_stonith_disabled: true
            gfs2_cluster_name: "{{ common_cluster_name }}"
            gfs2_file_systems:
              - name: fs1
                pvs:
                  - "{{ unused.disks[0] }}"
                vg: vg_gfs2_1
                lv: lv_gfs2_1
                lv_size: 4G
                mount_point: "{{ mount_point }}"

      always:
        - name: Remove cluster
          ansible.builtin.include_role:
            name: fedora.linux_system_roles.ha_cluster
          vars:
            ha_cluster_enable_repos: false
            ha_cluster_cluster_name: "{{ common_cluster_name }}"
            ha_cluster_cluster_present: false

        - name: Update /etc/hosts
          ansible.builtin.lineinfile:
            path: /etc/hosts
            line: 127.0.0.1 testnode
            state: absent
