---
- name: Test VDO creation and remove
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    storage_use_partitions: true
    mount_location: '/opt/test1'
    volume_group_size: '10g'
    volume_size: '12g'
    pool_size: '9g'
  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

    - name: Gather package facts
      package_facts:

    - name: Set blivet package name
      set_fact:
        blivet_pkg_name: "{{ ansible_facts.packages |
          select('search', 'blivet') | select('search', 'python') | list }}"

    - name: Set blivet package version
      set_fact:
        blivet_pkg_version: "{{
          ansible_facts.packages[blivet_pkg_name[0]][0]['version'] +
          '-' + ansible_facts.packages[blivet_pkg_name[0]][0]['release'] }}"

    - name: Set libblockdev package version
      set_fact:
        libblockdev_pkg_version: "{{
          ansible_facts.packages['libblockdev'][0]['version'] +
          '-' + ansible_facts.packages['libblockdev'][0]['release'] }}"

    - name: Check if kvdo is loadable
      command: modprobe --dry-run kvdo
      ignore_errors: true  # noqa ignore-errors
      changed_when: false
      register: __storage_kvdo_loadable

    - name: Check if dm-vdo is loadable
      command: modprobe --dry-run dm-vdo
      ignore_errors: true  # noqa ignore-errors
      changed_when: false
      register: __storage_dmvdo_loadable

    - name: Run tests if VDO is available
      when:
        - blivet_pkg_version is version("3.2.2-10", ">=")
        - ansible_facts["distribution"] != "Fedora" or
          libblockdev_pkg_version is version("3.1.1-2", ">=")
        - __storage_kvdo_loadable is success or __storage_dmvdo_loadable is success
      block:
        - name: Get unused disks
          include_tasks: get_unused_disk.yml
          vars:
            min_size: "{{ volume_group_size }}"
            max_return: 1

        - name: Create LVM VDO volume under volume group 'pool1'
          include_role:
            name: linux-system-roles.storage
          vars:
            storage_pools:
              - name: pool1
                disks: "{{ unused_disks }}"
                volumes:
                  - name: volume1
                    compression: true
                    deduplication: true
                    vdo_pool_size: "{{ pool_size }}"
                    size: "{{ volume_size }}"
                    mount_point: "{{ mount_location }}"

        - 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: pool1
                disks: "{{ unused_disks }}"
                volumes:
                  - name: volume1
                    compression: true
                    deduplication: true
                    vdo_pool_size: "{{ pool_size }}"
                    size: "{{ volume_size }}"
                    mount_point: "{{ mount_location }}"

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

        - name: Remove LVM VDO volume in 'pool1' created above
          include_role:
            name: linux-system-roles.storage
          vars:
            storage_pools:
              - name: pool1
                disks: "{{ unused_disks }}"
                state: absent
                volumes:
                  - name: volume1
                    compression: true
                    deduplication: true
                    vdo_pool_size: "{{ pool_size }}"
                    size: "{{ volume_size }}"
                    mount_point: "{{ mount_location }}"

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

        - name: >-
            Create LVM VDO volume under volume group 'pool1' (this time
            default size)
          include_role:
            name: linux-system-roles.storage
          vars:
            storage_pools:
              - name: pool1
                disks: "{{ unused_disks }}"
                volumes:
                  - name: volume1
                    compression: true
                    deduplication: true
                    size: "{{ volume_size }}"
                    mount_point: "{{ mount_location }}"

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

        - name: Remove LVM VDO volume in 'pool1' created above
          include_role:
            name: linux-system-roles.storage
          vars:
            storage_pools:
              - name: pool1
                disks: "{{ unused_disks }}"
                state: absent
                volumes:
                  - name: volume1
                    compression: true
                    deduplication: true
                    size: "{{ volume_size }}"
                    mount_point: "{{ mount_location }}"

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