---

#
# MDRaid devices paths are returned as a symlinks.
# But sometimes we need their targets.
#
- name: Get expected mount device based on device type
  set_fact:
    storage_test_device_path: "{{ storage_test_volume._kernel_device if _storage_test_volume_present and not storage_test_volume.encryption and storage_test_volume.raid_level and (storage_test_pool is not defined or storage_test_pool is none) else storage_test_volume._device }}"

- name: Set some facts
  set_fact:
    storage_test_swap_expected_matches: "{{ 1 if _storage_test_volume_present and storage_test_volume.fs_type == 'swap' else 0 }}"
    storage_test_mount_expected_mount_point: "{{ '[SWAP]' if storage_test_volume.fs_type == 'swap' else '' if storage_test_volume.mount_point == 'none' else mount_prefix + storage_test_volume.mount_point if storage_test_volume.mount_point else '' }}"
  vars:
  # assumes /opt which is /var/opt in ostree
    mount_prefix: "{{ '/var' if __storage_is_ostree | d(false) and storage_test_volume.mount_point and storage_test_volume.mount_point.startswith('/opt') else '' }}"

- name: Get information about the mountpoint directory
  stat:
    path: "{{ storage_test_volume.mount_point }}"
  register: storage_test_found_mount_stat
  when:
    - _storage_test_volume_present | bool
    - not storage_test_volume.mount_point is none
    - storage_test_volume.mount_point | length > 0
    - (not storage_test_volume.mount_user is none and storage_test_volume.mount_user | length > 0) or (not storage_test_volume.mount_group is none and storage_test_volume.mount_group | length > 0) or (not storage_test_volume.mount_mode is none and storage_test_volume.mount_mode | length > 0)

#
# Verify mount presence.
#
- name: Verify the current mount state by device
  assert:
    that: storage_test_blkinfo.info[storage_test_volume._device].mountpoint == storage_test_mount_expected_mount_point
    msg: >-
      Found unexpected mount state for volume
      '{{ storage_test_volume.name }}' device
  when: _storage_test_volume_present

- name: Verify mount directory user
  assert:
    that: storage_test_volume.mount_user == storage_test_found_mount_stat.stat.pw_name
    msg: "Mount directory {{ storage_test_volume.mount_point }} of volume {{ storage_test_volume.name }}) has unexpected owner (expected: {{ storage_test_volume.mount_user }}, found: {{ storage_test_found_mount_stat.stat.pw_name }})"
  when:
    - _storage_test_volume_present
    - not storage_test_volume.mount_point is none
    - storage_test_volume.mount_point | length > 0
    - not storage_test_volume.mount_user is none
    - storage_test_volume.mount_user | length > 0

- name: Verify mount directory group
  assert:
    that: storage_test_volume.mount_group == storage_test_found_mount_stat.stat.gr_name
    msg: "Mount directory {{ storage_test_volume.mount_point }} of volume {{ storage_test_volume.name }}) has unexpected group (expected: {{ storage_test_volume.mount_group }}, found: {{ storage_test_found_mount_stat.stat.gr_name }})"
  when:
    - _storage_test_volume_present
    - not storage_test_volume.mount_point is none
    - storage_test_volume.mount_point | length > 0
    - not storage_test_volume.mount_group is none
    - storage_test_volume.mount_group | length > 0

- name: Verify mount directory permissions
  assert:
    that: storage_test_volume.mount_mode == storage_test_found_mount_stat.stat.mode
    msg: "Mount directory {{ storage_test_volume.mount_point }} of volume {{ storage_test_volume.name }}) has unexpected permissions (expected: {{ storage_test_volume.mount_mode }}, found: {{ storage_test_found_mount_stat.stat.mode }})"
  when:
    - _storage_test_volume_present
    - not storage_test_volume.mount_point is none
    - storage_test_volume.mount_point | length > 0
    - not storage_test_volume.mount_mode is none
    - storage_test_volume.mount_mode | length > 0

#
# Verify swap status.
#
- name: Get path of test volume device
  command: realpath {{ storage_test_volume._device | quote }}
  register: storage_test_sys_node
  changed_when: false
  when: storage_test_volume.fs_type == "swap"

- name: Gather swap info
  command: cat /proc/swaps
  register: storage_test_swaps
  changed_when: false
  when: storage_test_volume.fs_type == "swap"

- name: Verify swap status
  assert:
    that: storage_test_swaps.stdout | regex_findall('^' + storage_test_sys_node.stdout + ' ', multiline=True) | list | length | int == storage_test_swap_expected_matches | int
    msg: Unexpected number of matching active swaps
  when: storage_test_volume.fs_type == "swap"

#
# Verify mount options.
#

- name: Unset facts
  set_fact:
    storage_test_swap_expected_matches:
    storage_test_sys_node:
    storage_test_swaps:
    storage_test_found_mount_stat:
    storage_test_mount_expected_mount_point:
