---
- name: Test misc features of the storage role
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    mount_location: /opt/test1
    volume_group_size: "5g"
    volume1_size: "4g"
    unused_disk_subfact: "{{ ansible_devices[unused_disks[0]] }}"
    too_large_size: "{{ (unused_disk_subfact.sectors | int * 1.2) * 512 }}"
  tags:
    - tests::lvm
  tasks:
    - name: Include the role to ensure packages are installed
      include_role:
        name: fedora.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 for test
      include_tasks: get_unused_disk.yml
      vars:
        min_size: "{{ volume_group_size }}"
        max_return: 1

    - name: Test creating ext4 filesystem with valid parameter "-Fb 4096"
      include_role:
        name: fedora.linux_system_roles.storage
      vars:
        storage_pools:
          - name: foo
            type: lvm
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                fs_type: ext4
                size: "{{ volume1_size }}"
                fs_create_options: -Fb 4096
                mount_point: "{{ mount_location }}"

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

    - name: Remove the volume group created above
      include_role:
        name: fedora.linux_system_roles.storage
      vars:
        storage_pools:
          - name: foo
            type: lvm
            disks: "{{ unused_disks }}"
            state: absent

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

    - name: >-
        Test for correct handling of invalid parameter when creating ext4
        filesystem
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: >-
          Failed to commit changes to disk.*(FSError.*format failed:
          1.*/dev/mapper/foo-test1|
          Process reported exit code 1: mke2fs: invalid block size - 512)
        __storage_failed_msg: >-
          Unexpected behavior when creating ext4 filesystem with invalid
          parameter
        __storage_failed_params:
          storage_pools:
            - name: foo
              type: lvm
              disks: "{{ unused_disks }}"
              volumes:
                - name: test1
                  fs_type: ext4
                  size: "{{ volume1_size }}"
                  fs_create_options: -Fb 512
                  mount_point: "{{ mount_location }}"

    - name: Remove the volume group created above
      include_role:
        name: fedora.linux_system_roles.storage
      vars:
        storage_pools:
          - name: foo
            type: lvm
            disks: "{{ unused_disks }}"
            state: absent

    - name: >-
        Create one LVM logical volume under one volume group, size
        {{ volume1_size }}
      include_role:
        name: fedora.linux_system_roles.storage
      vars:
        storage_pools:
          - name: foo
            type: lvm
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                fs_type: ext4
                size: "{{ volume1_size }}"
                mount_point: "{{ mount_location }}"

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

    - name: Test for correct handling resize large size
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: >-
          volume.*test1.*cannot be resized to.*
        __storage_failed_msg: >-
          Unexpected behavior when resizing with large size
        __storage_failed_params:
          storage_pools:
            - name: foo
              type: lvm
              disks: "{{ unused_disks }}"
              volumes:
                - name: test1
                  fs_type: ext4
                  size: "{{ too_large_size }}"
                  mount_point: "{{ mount_location }}"

    - name: Remove the volume group created above
      include_role:
        name: fedora.linux_system_roles.storage
      vars:
        storage_pools:
          - name: foo
            type: lvm
            disks: "{{ unused_disks }}"
            state: absent

    - name: Create one partition on one disk
      include_role:
        name: fedora.linux_system_roles.storage
      vars:
        storage_pools:
          - name: foo
            type: partition
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                type: partition
                fs_type: ext4
                mount_point: "{{ mount_location }}"

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

    - name: Test setting up disk volume will remove the partition create above
      include_role:
        name: fedora.linux_system_roles.storage
      vars:
        storage_volumes:
          - name: foo
            type: disk
            disks: "{{ unused_disks }}"
            fs_type: ext4
            fs_create_options: -F
            mount_point: "{{ mount_location }}"
            mount_options: rw,noatime,defaults

    - name: Verify results
      include_tasks: verify-role-results.yml
      vars:
        __storage_verify_mount_options: true

    - name: Remove the disk volume created above
      include_role:
        name: fedora.linux_system_roles.storage
      vars:
        storage_volumes:
          - name: foo
            type: disk
            disks: "{{ unused_disks }}"
            state: absent

  # rhel7 has a limitation of 128g swap size
    - name: Get unused disks for swap
      include_tasks: get_unused_disk.yml
      vars:
        min_size: "{{ volume_group_size }}"
        max_return: 1
        disks_needed: 1
        max_size: "{{ '127g' if (ansible_facts['os_family'] == 'RedHat' and ansible_facts['distribution_major_version'] is version('8', '<')) else '0' }}"

    - name: Save disk used for swap
      set_fact:
        __swap_disk: "{{ unused_disks[0] }}"

    - name: >-
        Test for correct handling of mounting a non-mountable formatting type
      include_tasks: verify-role-failed.yml
      vars:
        __storage_failed_regex: >-
          volume.*test1.*has a mount point but no mountable file system
        __storage_failed_msg: >-
          Unexpected behavior when mount swap filesystem
        __storage_failed_params:
          storage_volumes:
            - name: test1
              type: disk
              disks: "{{ [__swap_disk] }}"
              fs_type: swap
              mount_point: "{{ mount_location }}"

    - name: Remove the disk volume created above
      include_role:
        name: fedora.linux_system_roles.storage
      vars:
        storage_volumes:
          - name: test1
            type: disk
            disks: "{{ [__swap_disk] }}"
            state: absent
