---
- name: Test create lvm pool and remove
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    mount_location1: '/opt/test1'
    mount_location2: '/opt/test2'
    volume_group_size: '10g'
    volume1_size: '5g'
    volume2_size: '4g'
  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: Create two LVM logical volumes under volume group 'foo'
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
              - name: test2
                size: "{{ volume2_size }}"
                mount_point: "{{ mount_location2 }}"

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

    - name: Remove one of the LVM logical volumes in 'foo' created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            state: "present"
            volumes:
              - name: test1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
              - name: test2
                size: "{{ volume2_size }}"
                mount_point: "{{ mount_location2 }}"
                state: absent

    - 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: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
              - name: test2
                size: "{{ volume2_size }}"
                mount_point: "{{ mount_location2 }}"
                state: absent

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

    - name: Remove both of the LVM logical volumes in 'foo' created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            state: "absent"
            volumes:
              - name: test1
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location1 }}"
              - name: test2
                size: "{{ volume2_size }}"
                mount_point: "{{ mount_location2 }}"

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