---
- name: Test stratis pool management
  hosts: all
  become: true
  vars:
    mount_location: '/opt/test1'
    mount_location_2: '/opt/test2'
    volume_group_size: '5g'
    volume_size: '4g'
  tags:
    - tests::stratis

  tasks:
    - name: Run the role
      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: Gather package facts
      package_facts:

    - name: Set blivet package name
      set_fact:
        blivet_pkg_name: "{{ ansible_facts.packages | select('search', 'blivet') | select('search', 'python') | list }}"

    - name: Set blivet package version
      set_fact:
        blivet_pkg_version: "{{ ansible_facts.packages[blivet_pkg_name[0]][0]['version'] + '-' + ansible_facts.packages[blivet_pkg_name[0]][0]['release'] }}"

    - name: Set distribution version
      set_fact:
        is_rhel78: "{{ (ansible_facts['os_family'] == 'RedHat') and (ansible_facts.distribution_major_version == '7' or ansible_facts.distribution_major_version == '8') }}"
        is_rhel9: "{{ (ansible_facts['os_family'] == 'RedHat') and ansible_facts.distribution_major_version == '9' }}"
        is_rhel10: "{{ (ansible_facts['os_family'] == 'RedHat') and ansible_facts.distribution_major_version == '10' }}"
        is_fedora: "{{ ansible_facts.distribution == 'Fedora' }}"


    - name: Completely skip this on RHEL/CentOS 7 and 8 where Stratis isn't supported by blivet
      when: not is_rhel78
      block:
        - name: Get unused disks
          include_tasks: get_unused_disk.yml
          vars:
            min_size: "{{ volume_group_size }}"
            disks_needed: 2

      # stratisd is not started automatically and doesn't support DBus activation
      # this will be covered by Blivet in the next build
        - name: Start stratisd service
          service:
            name: stratisd
            state: started

        - name: One pool/volume test
          block:
            - name: Create one Stratis pool with one volume
              include_role:
                name: fedora.linux_system_roles.storage
              vars:
                storage_pools:
                  - name: foo
                    disks: "{{ unused_disks }}"
                    type: stratis
                    volumes:
                      - name: test1
                        size: "{{ volume_size }}"
                        mount_point: "{{ mount_location }}"

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

            - name: Repeat the previous invocation to verify idempotence
              include_role:
                name: fedora.linux_system_roles.storage
              vars:
                storage_pools:
                  - name: foo
                    disks: "{{ unused_disks }}"
                    type: stratis
                    volumes:
                      - name: test1
                        size: "{{ volume_size }}"
                        mount_point: "{{ mount_location }}"

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

            - name: Add second filesystem to the pool
              include_role:
                name: fedora.linux_system_roles.storage
              vars:
                storage_pools:
                  - name: foo
                    disks: "{{ unused_disks }}"
                    type: stratis
                    volumes:
                      - name: test1
                        size: "{{ volume_size }}"
                        mount_point: "{{ mount_location }}"
                      - name: test2
                        size: "{{ volume_size }}"
                        mount_point: "{{ mount_location_2 }}"

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

          always:
            - name: Clean up
              include_role:
                name: fedora.linux_system_roles.storage
              vars:
                storage_pools:
                  - name: foo
                    disks: "{{ unused_disks }}"
                    type: stratis
                    state: absent
                    volumes:
                      - name: test1
                        size: "{{ volume_size }}"
                        mount_point: "{{ mount_location }}"
                        state: absent
                      - name: test2
                        size: "{{ volume_size }}"
                        mount_point: "{{ mount_location_2 }}"
                        state: absent

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

        - name: Run test only if blivet supports this functionality
          when: ((is_fedora and blivet_pkg_version is version("3.10.0-1", ">=")) or (is_rhel10 and blivet_pkg_version is version("3.10.0-1", ">=")) or (is_rhel9 and blivet_pkg_version is version("3.6.0-15", ">=")))
          block:
            - name: Test encrypted pool
              block:
                - name: Create encrypted Stratis pool
                  include_role:
                    name: fedora.linux_system_roles.storage
                  vars:
                    storage_pools:
                      - name: foo
                        disks: "{{ unused_disks }}"
                        type: stratis
                        encryption: true
                        encryption_password: yabbadabbadoo
                        volumes:
                          - name: test1
                            size: "{{ volume_size }}"
                            mount_point: "{{ mount_location }}"

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

                - name: Repeat the previous invocation to verify idempotence
                  include_role:
                    name: fedora.linux_system_roles.storage
                  vars:
                    storage_pools:
                      - name: foo
                        disks: "{{ unused_disks }}"
                        type: stratis
                        encryption: true
                        encryption_password: yabbadabbadoo
                        volumes:
                          - name: test1
                            size: "{{ volume_size }}"
                            mount_point: "{{ mount_location }}"

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

              always:
                - name: Clean up
                  include_role:
                    name: fedora.linux_system_roles.storage
                  vars:
                    storage_pools:
                      - name: foo
                        disks: "{{ unused_disks }}"
                        type: stratis
                        state: absent
                        volumes:
                          - name: test1
                            size: "{{ volume_size }}"
                            mount_point: "{{ mount_location }}"
                            state: absent

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

            - name: Test one disk pool
              block:
                - name: Create one Stratis pool on one disk
                  include_role:
                    name: fedora.linux_system_roles.storage
                  vars:
                    storage_pools:
                      - name: foo
                        disks: "{{ unused_disks[0] }}"
                        type: stratis

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

                - name: Add the second disk to the pool
                  include_role:
                    name: fedora.linux_system_roles.storage
                  vars:
                    storage_pools:
                      - name: foo
                        disks: "{{ [unused_disks[0], unused_disks[1]] }}"
                        type: stratis

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

              always:
                - name: Clean up
                  include_role:
                    name: fedora.linux_system_roles.storage
                  vars:
                    storage_pools:
                      - name: foo
                        disks: "{{ unused_disks }}"
                        type: stratis
                        state: absent
                        volumes:
                          - name: test1
                            size: "{{ volume_size }}"
                            mount_point: "{{ mount_location }}"
                            state: absent

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

            - name: Setup Tang server on localhost for testing
              ansible.builtin.include_role:
                name: fedora.linux_system_roles.nbde_server
              vars:
                nbde_server_manage_firewall: true
                nbde_server_manage_selinux: true
                nbde_server_port: 7500

            - name: Clevis/Tang test
              block:
                - name: Create encrypted Stratis pool with Clevis/Tang
                  include_role:
                    name: fedora.linux_system_roles.storage
                  vars:
                    storage_pools:
                      - name: foo
                        disks: "{{ unused_disks[0] }}"
                        type: stratis
                        encryption: true
                        encryption_password: yabbadabbadoo
                        encryption_clevis_pin: tang
                        encryption_tang_url: localhost:7500

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

              always:
                - name: Clean up
                  include_role:
                    name: fedora.linux_system_roles.storage
                  vars:
                    storage_pools:
                      - name: foo
                        disks: "{{ unused_disks[0] }}"
                        type: stratis
                        state: absent

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