---
- name: Test change fs
  hosts: all
  become: true
  vars:
    storage_safe_mode: false
    mount_location: '/opt/test1'
    volume_size: '5g'
    fs_after: "{{ (ansible_distribution == 'RedHat' and
                   ansible_distribution_major_version == '6') |
                  ternary('ext4', 'xfs') }}"
  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: Get unused disks
      include_tasks: get_unused_disk.yml
      vars:
        min_size: "{{ volume_size }}"
        max_return: 1

    - name: Create a LVM logical volume with default fs_type
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ volume_size }}"
                mount_point: "{{ mount_location }}"

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

    - name: Change the file system signature on the logical volume created above
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ volume_size }}"
                fs_type: "{{ fs_after }}"
                mount_point: "{{ mount_location }}"

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

    - name: Re-run the role on the same volume without specifying fs_type
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ volume_size }}"
                mount_point: "{{ mount_location }}"

    - name: Verify the output of the duplicate volumes test
      assert:
        that:
          - not blivet_output.changed
          - blivet_output.pools[0].volumes[0].fs_type == fs_after
        msg: Failed to preserve omitted fs_type on existing lvm volume

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

    - name: Repeat the previous invocation to verify idempotence
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ volume_size }}"
                fs_type: "{{ fs_after }}"
                mount_point: "{{ mount_location }}"

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

    - name: Remove the FS
      include_role:
        name: linux-system-roles.storage
      vars:
        storage_pools:
          - name: foo
            disks: "{{ unused_disks }}"
            volumes:
              - name: test1
                size: "{{ volume_size }}"
                fs_type: unformatted

    - 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: test1
                size: "{{ volume_size }}"
                fs_type: "{{ fs_after }}"
                mount_point: "{{ mount_location }}"

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