---
- name: Test create raid pool and remove
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    storage_use_partitions: true
    mount_location1: '/opt/test1'
    mount_location2: '/opt/test2'
    mount_location3: '/opt/test3'
    volume1_size: '2g'
    volume3_size: '3g'
    volume2_size: '3g'
  tags:
    - tests::lvm

  tasks:
    - name: Run the role
      include_role:
        name: linux-system-roles.storage

    - name: Mark tasks to be skipped
      set_fact:
        storage_skip_checks:
          - blivet_available
          - "{{ (lookup('env',
                        'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
                ternary('packages_installed', '') }}"
          - service_facts

    - name: Gather package facts
      package_facts:

    - name: Set blivet package name
      set_fact:
        blivet_pkg_name: "{{ ansible_facts.packages |
          select('search', 'blivet') | select('search', 'python') | list }}"

    - name: Set blivet package version
      set_fact:
        blivet_pkg_version: "{{
          ansible_facts.packages[blivet_pkg_name[0]][0]['version'] +
          '-' + ansible_facts.packages[blivet_pkg_name[0]][0]['release'] }}"

    - name: Set distribution version
      set_fact:
        is_rhel10: "{{ (ansible_facts['os_family'] == 'RedHat') and
          ansible_facts.distribution_major_version == '10' }}"
        is_rhel9: "{{ (ansible_facts['os_family'] == 'RedHat') and
          ansible_facts.distribution_major_version == '9' }}"
        is_rhel8: "{{ (ansible_facts['os_family'] == 'RedHat') and
          ansible_facts.distribution_major_version == '8' }}"
        is_fedora: "{{ ansible_facts.distribution == 'Fedora' }}"

    - name: Get unused disks
      include_tasks: get_unused_disk.yml
      vars:
        max_return: 2
        disks_needed: 2

    - name: Create a RAID0 device
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            raid_level: "raid0"
            state: present
            volumes:
              - name: lv1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
              - name: lv2
                size: "{{ volume2_size }}"
                mount_point: "{{ mount_location2 }}"
              - name: lv3
                size: "{{ volume3_size }}"
                mount_point: "{{ mount_location3 }}"

    - name: Verify role results
      include_tasks: verify-role-results.yml

    - name: Repeat the previous invocation to verify idempotence
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            raid_level: "raid0"
            state: present
            volumes:
              - name: lv1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
              - name: lv2
                size: "{{ volume2_size }}"
                mount_point: "{{ mount_location2 }}"
              - name: lv3
                size: "{{ volume3_size }}"
                mount_point: "{{ mount_location3 }}"

    - name: Verify role results
      include_tasks: verify-role-results.yml

    - name: Remove the device created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            raid_level: raid0
            state: absent
            volumes:
              - name: lv1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
              - name: lv2
                size: "{{ volume2_size }}"
                mount_point: "{{ mount_location2 }}"
              - name: lv3
                size: "{{ volume3_size }}"
                mount_point: "{{ mount_location3 }}"

    - name: Verify role results
      include_tasks: verify-role-results.yml

    - name: Create a RAID1 lvm raid device
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            state: present
            volumes:
              - name: lv1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
                raid_disks: "{{ [unused_disks[0], unused_disks[1]] }}"
                raid_level: raid1

    - name: Verify role results
      include_tasks: verify-role-results.yml

    - name: Repeat the previous invocation to verify idempotence
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            state: present
            volumes:
              - name: lv1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
                raid_level: raid1
                raid_disks: "{{ [unused_disks[0], unused_disks[1]] }}"

    - name: Verify role results
      include_tasks: verify-role-results.yml

    - name: Remove the device created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            state: absent
            volumes:
              - name: lv1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
                raid_level: raid1
                raid_disks: "{{ [unused_disks[0], unused_disks[1]] }}"

    - name: Verify role results
      include_tasks: verify-role-results.yml

    - name: Create a RAID0 lvm raid device
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            state: present
            volumes:
              - name: lv1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
                raid_disks: "{{ [unused_disks[0], unused_disks[1]] }}"
                raid_level: raid0

    - name: Verify role results
      include_tasks: verify-role-results.yml

    - name: Repeat the previous invocation to verify idempotence
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            state: present
            volumes:
              - name: lv1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
                raid_level: raid0
                raid_disks: "{{ [unused_disks[0], unused_disks[1]] }}"

    - name: Verify role results
      include_tasks: verify-role-results.yml

    - name: Remove the device created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            state: absent
            volumes:
              - name: lv1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
                raid_level: raid0
                raid_disks: "{{ [unused_disks[0], unused_disks[1]] }}"

    - name: Verify role results
      include_tasks: verify-role-results.yml

    - name: Run test on supported platforms
      when: ((is_fedora and blivet_pkg_version is version("3.7.1-2", ">=")) or
             (is_rhel8 and blivet_pkg_version is version("3.6.0-5", ">=")) or
             (is_rhel9 and blivet_pkg_version is version("3.6.0-6", ">=")) or
             is_rhel10)
      block:
        - name: Create a RAID0 lvm raid device with custom stripe size
          include_role:
            name: linux-system-roles.storage
          vars:
            storage_pools:
              - name: vg1
                disks: "{{ unused_disks }}"
                type: lvm
                state: present
                volumes:
                  - name: lv1
                    size: "{{ volume1_size }}"
                    mount_point: "{{ mount_location1 }}"
                    raid_disks: "{{ [unused_disks[0], unused_disks[1]] }}"
                    raid_level: raid0
                    raid_stripe_size: "256 KiB"

        - name: Verify role results
          include_tasks: verify-role-results.yml

        - name: Remove the device created above
          include_role:
            name: linux-system-roles.storage
          vars:
            storage_pools:
              - name: vg1
                disks: "{{ unused_disks }}"
                type: lvm
                state: absent
                volumes:
                  - name: lv1
                    size: "{{ volume1_size }}"
                    mount_point: "{{ mount_location1 }}"
                    raid_level: raid0
                    raid_disks: "{{ [unused_disks[0], unused_disks[1]] }}"

        - name: Verify role results
          include_tasks: verify-role-results.yml
