---
- name: Check cache options
  when: storage_test_volume.type == 'lvm' and _storage_test_volume_present
  block:
    - name: Get information about the LV
      command: >
          lvs --noheadings --nameprefixes --units=b --nosuffix --unquoted
          -o name,attr,cache_total_blocks,chunk_size,segtype
          {{ storage_test_pool.name }}/{{ storage_test_volume.name }}
      register: lvs
      changed_when: false

    - name: Set LV segment type
      set_fact:
        storage_test_lv_segtype: "{{ lvs.stdout |
          regex_search('LVM2_SEGTYPE=(\\S+)', '\\1') }}"

    - name: Check segment type
      assert:
        that: storage_test_volume.cached |
          ternary(storage_test_lv_segtype[0] == 'cache',
                  storage_test_lv_segtype[0] != 'cache')
        msg: >-
          Unexpected segtype {{ storage_test_lv_segtype }}
          for {{ storage_test_volume.name }}

    - name: Set LV cache size
      set_fact:
        storage_test_lv_cache_size: "{{ lvs.stdout |
          regex_search(__pattern, '\\1', '\\2') | map('int') | list }}"
      vars:
        __pattern: LVM2_CACHE_TOTAL_BLOCKS=([0-9]*) LVM2_CHUNK_SIZE=([0-9]+)
      when: storage_test_volume.cached | bool

    - name: Parse the requested cache size
      bsize:
        size: "{{ storage_test_volume.cache_size }}"
      register: storage_test_requested_cache_size
      when: storage_test_volume.cached | bool

    - name: Set expected cache size
      set_fact:
        storage_test_expected_cache_size: "{{
          storage_test_requested_cache_size.bytes }}"
      when: storage_test_volume.cached | bool

    - name: Check cache size
      assert:
        that: (storage_test_expected_cache_size | int - __actual_size | int) |
          abs / storage_test_expected_cache_size | int < 0.01
        msg: >
            Unexpected cache size, expected
            {{ storage_test_expected_cache_size }},
            got {{ __actual_size }}
      vars:
        __actual_size: "{{ storage_test_lv_cache_size[0] *
          storage_test_lv_cache_size[1] }}"
      when: storage_test_volume.cached | bool
