---
- name: Test lvm auto size
  hosts: all
  become: true
  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: 10g
        max_return: 1

    - name: Run lsblk -b -l --noheadings -o NAME,SIZE
      command: lsblk -b -l --noheadings -o NAME,SIZE
      register: storage_test_lsblk
      changed_when: false

    - name: Set test_disk_size
      set_fact:
        test_disk_size: "{{ storage_test_lsblk.stdout_lines |
          map('regex_search', '^' + unused_disks[0] + '\\s+\\d+$') |
          select('string') | first | regex_replace('^\\w+\\s+', '') }}"

    - name: Ensure bc is installed
      package:
        name: bc
        state: present
        use: "{{ (__storage_is_ostree | d(false)) |
                 ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

    - name: Run bc 2 * {{ test_disk_size }}
      command:
        cmd: bc
        stdin: "{{ test_disk_size }} *2"
      register: doubled_size
      changed_when: false

    - name: Test handling of too-large LVM volume size
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: specified size for volume.+exceeds available
        __storage_failed_msg: >-
          Role did not report error about size too large
        __storage_failed_params:
          storage_pools:
            - name: foo
              type: lvm
              disks: "{{ unused_disks }}"
              volumes:
                - name: test1
                  size: "{{ doubled_size.stdout | trim }}"

    - name: >-
        Create a pool containing one volume the same size as the backing disk
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ test_disk_size }}"

    - 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
            type: lvm
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ test_disk_size }}"

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

    - name: Clean up
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            state: absent
            volumes: []
