---
- name: Test create disk and remove
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    mount_location1: '/opt/test1'
    mount_location2: '/opt/test2'
    pv_size: '8g'
    volume1_size: '2g'
    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
          - service_facts
          - "{{ (lookup('env',
                        'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
                ternary('packages_installed', '') }}"

    - name: Get unused disks
      include_tasks: get_unused_disk.yml
      vars:
        max_return: 1
        min_size: "10g"

    - name: Create PV with a space to grow
      command: >-
        timeout 30s pvcreate -vvv -y --setphysicalvolumesize
        {{ pv_size | quote }} /dev/{{ unused_disks[0] | quote }}
      register: pvcreate_output
      changed_when: pvcreate_output.rc != 0

      # VG has to be present, the role otherwise automatically reformats empty PV,
      # taking all available space
    - name: Create VG
      command: "vgcreate foo /dev/{{ unused_disks[0] }}"
      register: vgcreate_output
      changed_when: vgcreate_output.rc != 0

    - name: Create LVM
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            grow_to_fill: true
            state: present
            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: Rerun the task to verify idempotence
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            grow_to_fill: true
            state: present
            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 'foo' pool created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            state: "absent"
            volumes:
              - name: test1
              - name: test2

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