---
- name: Test lvm errors
  hosts: all
  become: true
  vars:
    mount_location1: '/opt/test1'
    mount_location2: '/opt/test2'
    volume_group_size: '10g'
    volume1_size: '5g'
    volume2_size: '2g'
    invalid_disks:
      - '/non/existent/disk'
    invalid_size: 'xyz GiB'
    unused_disk_subfact: '{{ ansible_devices[unused_disks[0]] }}'
  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:
        min_size: "{{ volume_group_size }}"
        max_return: 1

    - name: Test for correct handling of invalid disk specifications.
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: unable to resolve any disks specified for pool
        __storage_failed_msg: Role did not report error about invalid disks
        __storage_failed_params:
          storage_pools:
            - name: foo
              disks: "{{ invalid_disks }}"
              volumes:
                - name: test1
                  size: "{{ volume1_size }}"
                  mount_point: "{{ mount_location1 }}"

    - name: Test for correct handling of invalid size specification.
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: invalid size specification
        __storage_failed_msg: Role did not report that the size spec is invalid
        __storage_failed_params:
          storage_pools:
            - name: foo
              disks: "{{ unused_disks }}"
              volumes:
                - name: test1
                  size: "{{ invalid_size }}"
                  mount_point: "{{ mount_location1 }}"

    - name: Test for correct handling of missing disk specification.
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: no disks specified for pool
        __storage_failed_msg: Role did not report that no disks were specified
        __storage_failed_params:
          storage_pools:
            - name: foo
              disks: []
              volumes:
                - name: test1
                  size: "{{ volume1_size }}"
                  mount_point: "{{ mount_location1 }}"

    - name: Test for correct handling of LVM volume not defined within a pool.
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_exception: >-
          NoneType.* object has no attribute .*_device.*
        __storage_failed_params:
          storage_volumes:
            - name: test1
              type: lvm
              disks: []
              size: "{{ volume1_size }}"
              mount_point: "{{ mount_location1 }}"

    - name: Test for correct handling of duplicate pool names
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: multiple pools with the same name
        __storage_failed_msg: >-
          Unexpected behavior w/ multiple pools sharing one name
        __storage_failed_params:
          storage_pools:
            - name: pool1
              type: lvm
              disks: "{{ unused_disks }}"
            - name: pool1
              type: lvm
              disks: "{{ unused_disks }}"

    - name: Test for correct handling of duplicate volume names within a pool
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: multiple volumes in pool.*with the same name
        __storage_failed_msg: >-
          Unexpected behavior w/ multiple volumes within a pool sharing a name
        __storage_failed_params:
          storage_pools:
            - name: pool1
              type: lvm
              disks: "{{ unused_disks }}"
              volumes:
                - name: test1
                  type: lvm
                  size: "{{ volume1_size }}"
                  mount_point: "{{ mount_location1 }}"
                - name: test1
                  type: lvm
                  size: "{{ volume2_size }}"
                  mount_point: "{{ mount_location2 }}"

    - name: Create a pool
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: testpool1
            type: lvm
            disks: "{{ unused_disks }}"
            volumes:
              - name: testvol1
                fs_type: ext4
                size: '1g'

    - name: Test for correct handling of safe_mode
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: >-
          cannot remove existing formatting on volume.*in safe mode
        __storage_failed_msg: >-
          Unexpected behavior w/ existing data on specified disks
        __storage_failed_params:
          storage_pools:
            - name: testpool1
              type: lvm
              disks: "{{ unused_disks }}"
              volumes:
                - name: testvol1
                  fs_type: ext3
                  size: '1g'

    - name: Test for correct handling of safe_mode with existing pool
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: >-
          cannot remove existing formatting.*and/or devices on disk.*in
          safe mode
        __storage_failed_msg: >-
          Unexpected behavior w/ existing data on specified disks
        __storage_failed_params:
          storage_pools:
            - name: foo
              disks: "{{ unused_disks }}"
              type: lvm

    - name: Test for correct handling of safe_mode
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: >-
          cannot remove existing formatting on volume.* in safe mode
        __storage_failed_msg: >-
          Unexpected behavior w/ existing data on specified disks
        __storage_failed_params:
          storage_volumes:
            - name: test1
              type: disk
              fs_type: ext3
              disks:
                - "{{ unused_disks[0] }}"

    - name: Clean up
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_safe_mode: false
        storage_pools:
          - name: testpool1
            type: lvm
            disks: "{{ unused_disks }}"
            state: absent

    - name: Test for correct handling of pool and volume type mismatch
      include_tasks: verify-role-failed.yml
      vars:
        storage_safe_mode: false
        __storage_failed_regex: LVM volume can be placed only on LVM pool
        __storage_failed_msg: Role did not report that wrong pool type was selected
        __storage_failed_params:
          storage_pools:
            - name: foo
              disks: "{{ unused_disks }}"
              type: partition
              volumes:
                - name: test1
                  type: lvm
                  size: "{{ volume1_size }}"
                  mount_point: "{{ mount_location1 }}"

    - name: Clean up
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_safe_mode: false
        storage_pools:
          - name: testpool1
            type: lvm
            disks: "{{ unused_disks }}"
            state: absent
