---
- name: Test raid volume options
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    storage_use_partitions: true
    mount_location: '/opt/test1'

  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 RAID0 device mounted on "{{ mount_location }}"
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_volumes:
          - name: test1
            type: raid
            raid_level: "raid1"
            raid_device_count: 2
            raid_spare_count: 1
            raid_metadata_version: "1.0"
            disks: "{{ unused_disks }}"
            mount_point: "{{ mount_location }}"
            state: present

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

    - name: Re-run the same invocation without the RAID params
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_volumes:
          - name: test1
            type: raid
            disks: "{{ unused_disks }}"
            mount_point: "{{ mount_location }}"
            state: present

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

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

    - name: Remove the disk device created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_volumes:
          - name: test1
            type: raid
            raid_level: "raid1"
            raid_device_count: 2
            raid_spare_count: 1
            raid_metadata_version: "1.0"
            disks: "{{ unused_disks }}"
            mount_point: "{{ mount_location }}"
            state: absent

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

    - name: Create a RAID0 device mounted on "{{ mount_location }}"
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_volumes:
          - name: test0
            type: raid
            raid_level: "raid0"
            raid_device_count: 3
            raid_metadata_version: "1.0"
            raid_chunk_size: "1024K"
            disks: "{{ unused_disks }}"
            mount_point: "{{ mount_location }}"
            state: present

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

    - name: Remove the disk device created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_volumes:
          - name: test0
            type: raid
            raid_level: "raid0"
            raid_device_count: 3
            raid_metadata_version: "1.0"
            raid_chunk_size: "1024K"
            disks: "{{ unused_disks }}"
            mount_point: "{{ mount_location }}"
            state: absent

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