---
- name: Test raid pool options
  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: Get unused disks
      include_tasks: get_unused_disk.yml
      vars:
        max_return: 3
        disks_needed: 3

    - name: Create a RAID1 device
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            raid_level: "raid1"
            raid_device_count: 2
            raid_spare_count: 1
            raid_metadata_version: "1.0"
            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 minus the pool raid options
      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 }}"
              - name: Lv2
                size: "{{ volume2_size }}"
                mount_point: "{{ mount_location2 }}"
              - name: Lv3
                size: "{{ volume3_size }}"
                mount_point: "{{ mount_location3 }}"

    - name: Assert to preserve RAID settings for preexisting pool
      assert:
        that: not blivet_output.changed and
              blivet_output.pools[0].raid_level == 'raid1' and
              blivet_output.pools[0].raid_device_count == 2 and
              blivet_output.pools[0].raid_spare_count == 1 and
              blivet_output.pools[0].raid_metadata_version == '1.0'
        msg: "Failure to preserve RAID settings for preexisting pool."

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

    - name: Remove the pool created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            raid_level: "raid1"
            raid_device_count: 2
            raid_spare_count: 1
            raid_metadata_version: "1.0"
            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 RAID0 device
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            raid_level: "raid0"
            raid_device_count: 3
            raid_metadata_version: "1.0"
            raid_chunk_size: "1024 KiB"
            state: present

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

    - name: Remove the pool created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: vg1
            disks: "{{ unused_disks }}"
            type: lvm
            raid_level: "raid0"
            raid_device_count: 3
            raid_metadata_version: "1.0"
            raid_chunk_size: "1024 KiB"
            state: absent

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