---
- name: Test specifying size as a percentage
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    mount_location1: '/opt/test1'
    mount_location2: '/opt/test2'
    volume_group_size: '10g'
    size1: "60%"
    size2: "40%"
    size3: "25%"
    size4: "50%"
  tags:
    - tests::lvm
  tasks:
    - name: Run the role
      include_role:
        name: linux-system-roles.storage

    - 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 percentage-based size
        specification.
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: invalid percentage
        __storage_failed_msg: >-
          Unexpected error message output from invalid percentage size input
        __storage_failed_params:
          storage_pools:
            - name: foo
              disks: "{{ unused_disks }}"
              volumes:
                - name: test1
                  size: "2x%"
                  mount_point: "{{ mount_location1 }}"

    - name: >-
        Create two LVM logical volumes under volume group 'foo' using percentage
        sizes
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ size1 }}"
                mount_point: "{{ mount_location1 }}"
              - name: test2
                size: "{{ size2 }}"
                mount_point: "{{ mount_location2 }}"
                fs_type: ext4

    - 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: "{{ size1 }}"
                mount_point: "{{ mount_location1 }}"
              - name: test2
                size: "{{ size2 }}"
                mount_point: "{{ mount_location2 }}"

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

    - name: Shrink test2 volume via percentage-based size spec
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ size1 }}"
                mount_point: "{{ mount_location1 }}"
              - name: test2
                size: "{{ size3 }}"
                mount_point: "{{ mount_location2 }}"

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

    - name: Get the size of test2 volume
      command: lsblk --noheadings -o SIZE /dev/mapper/foo-test2
      register: storage_test_test1_size_1
      changed_when: false

    - name: Remove the test1 volume without changing its size
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            state: present
            volumes:
              - name: test1
                size: "{{ size1 }}"
                mount_point: "{{ mount_location1 }}"
                state: absent
              - name: test2
                size: "{{ size3 }}"
                mount_point: "{{ mount_location2 }}"

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

    - name: Get the size of test2 volume again
      command: lsblk --noheadings -o SIZE /dev/mapper/foo-test2
      register: storage_test_test1_size_2
      changed_when: false

    - name: Verify that removing test1 didn't cause a change in test2 size
      assert:
        that: storage_test_test1_size_1.stdout ==
          storage_test_test1_size_2.stdout

    - name: Grow test2 using a percentage-based size spec
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            state: "present"
            volumes:
              - name: test2
                size: "{{ size4 }}"
                mount_point: "{{ mount_location2 }}"

    - 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

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