---
- name: Test create disk and remove
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    mount_location: '/opt/test1'

  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
          - "{{ (lookup('env',
                        'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
                ternary('packages_installed', '') }}"

    - name: Get unused disks
      include_tasks: get_unused_disk.yml
      vars:
        max_return: 1

    - name: >-
        Create a disk device; specify disks as non-list mounted on
        {{ mount_location }}
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_volumes:
          - name: test1
            type: disk
            disks: "{{ unused_disks[0] }}"
            fs_type: ext4
            mount_point: "{{ mount_location }}"
            mount_user: "nobody"
            mount_group: "nobody"
            mount_mode: "0777"

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

    - name: Repeat the previous invocation minus fs_type to verify idempotence
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_volumes:
          - name: test1
            type: disk
            disks: "{{ unused_disks }}"
            mount_point: "{{ mount_location }}"
            mount_user: "root"
            mount_group: "root"
            mount_mode: "0755"

    - name: Assert file system is preserved on existing partition volume
      assert:
        that:
          - not blivet_output.changed
          - blivet_output.volumes[0].fs_type == 'ext4'
        msg: File system not preserved on existing partition volume.

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

    - name: Remove the disk device created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_volumes:
          - name: test1
            type: disk
            disks: "{{ unused_disks }}"
            mount_point: "{{ mount_location }}"
            state: absent

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