---
- name: Test lvm cache create and remove
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    storage_use_partitions: true
    volume_group_size: '10g'
    volume_size: '5g'
    cache_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: 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 distribution version
      set_fact:
        is_rhel10: "{{ (ansible_facts['os_family'] == 'RedHat') and
          ansible_facts.distribution_major_version == '10' }}"
        is_rhel9: "{{ (ansible_facts['os_family'] == 'RedHat') and
          ansible_facts.distribution_major_version == '9' }}"
        is_rhel8: "{{ (ansible_facts['os_family'] == 'RedHat') and
          ansible_facts.distribution_major_version == '8' }}"
        is_fedora: "{{ ansible_facts.distribution == 'Fedora' }}"

    - name: Get unused disks
      include_tasks: get_unused_disk.yml
      vars:
        min_size: "{{ volume_group_size }}"
        max_return: 2
        disks_needed: 2
        match_sector_size: true

    - name: Create a cached LVM logical volume under volume group 'foo'
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test
                size: "{{ volume_size }}"
                cached: true
                cache_size: "{{ cache_size }}"
                cache_devices: "{{ [unused_disks[1]] }}"

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

    - name: Remove (detach) cache from the 'test' LV created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test
                size: "{{ volume_size }}"
                cached: false

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

    - name: Run test on supported platforms
      when: ((is_fedora and blivet_pkg_version is version("3.5.0-1", ">=")) or
             (is_rhel8 and blivet_pkg_version is version("3.4.0-10", ">=")) or
             (is_rhel9 and blivet_pkg_version is version("3.4.0-14", ">=")) or
             is_rhel10)
      block:
        - name: Attach the cache to the 'test' LV created above
          include_role:
            name: linux-system-roles.storage
          vars:
            storage_pools:
              - name: foo
                disks: "{{ unused_disks }}"
                volumes:
                  - name: test
                    size: "{{ volume_size }}"
                    cached: true
                    cache_size: "{{ cache_size }}"
                    cache_devices: "{{ [unused_disks[1]] }}"

        - 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:
              - name: test
                size: "{{ volume_size }}"

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