---
- name: Mount snapshots of logical volumes across different volume groups
  hosts: all
  vars:
    test_disk_min_size: "1g"
    test_disk_count: 10
    test_storage_pools:
      - name: test_vg1
        disks: "{{ range(0, 3) | map('extract', unused_disks) | list }}"
        volumes:
          - name: lv1
            size: "15%"
          - name: lv2
            size: "50%"
      - name: test_vg2
        disks: "{{ range(3, 6) | map('extract', unused_disks) | list }}"
        volumes:
          - name: lv3
            size: "10%"
          - name: lv4
            size: "20%"
      - name: test_vg3
        disks: "{{ range(6, 10) | map('extract', unused_disks) | list }}"
        volumes:
          - name: lv5
            size: "30%"
          - name: lv6
            size: "25%"
          - name: lv7
            size: "10%"
          - name: lv8
            size: "10%"
    snapshot_test_set:
      name: snapset1
      volumes:
        - name: snapshot VG1 LV1
          vg: test_vg1
          lv: lv1
          mountpoint: "{{ test_mnt_parent ~ '/lv1_mp' }}"
          percent_space_required: 15
          mountpoint_create: true
        - name: snapshot VG2 LV3
          vg: test_vg2
          lv: lv3
          mountpoint: "{{ test_mnt_parent ~ '/lv3_mp' }}"
          percent_space_required: 15
          mountpoint_create: true
        - name: snapshot VG2 LV4
          vg: test_vg2
          lv: lv4
          mountpoint: "{{ test_mnt_parent ~ '/lv4_mp' }}"
          percent_space_required: 15
          mountpoint_create: true
        - name: snapshot VG3 LV7
          vg: test_vg3
          lv: lv7
          mountpoint: "{{ test_mnt_parent ~ '/lv7_mp' }}"
          percent_space_required: 15
          mountpoint_create: true
  tasks:
    - name: Run tests
      block:
        - name: Setup
          include_tasks: tasks/setup.yml

        - name: Run the snapshot role to create a snapshot set of LVs
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: snapshot
            snapshot_lvm_set: "{{ snapshot_test_set }}"

        - name: Verify the set of snapshots for the LVs
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: check
            snapshot_lvm_set: "{{ snapshot_test_set }}"
            snapshot_lvm_verify_only: true

        - name: Mount the set
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: mount
            snapshot_lvm_mountpoint_create: true
            snapshot_lvm_set: "{{ snapshot_test_set }}"

        - name: Assert changes for mount
          assert:
            that: snapshot_cmd["changed"]

        - name: Verify the mount is done
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_verify_only: true
            snapshot_lvm_action: mount
            snapshot_lvm_set: "{{ snapshot_test_set }}"

        - name: Mount the set again to check idempotence
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: mount
            snapshot_lvm_mountpoint_create: true
            snapshot_lvm_set: "{{ snapshot_test_set }}"

        - name: Assert no changes for mount
          assert:
            that: not snapshot_cmd["changed"]

        - name: Umount the set
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: umount
            snapshot_lvm_set: "{{ snapshot_test_set }}"

        - name: Assert changes for umount
          assert:
            that: snapshot_cmd["changed"]

        - name: Verify the umount is done
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_verify_only: true
            snapshot_lvm_action: umount
            snapshot_lvm_set: "{{ snapshot_test_set }}"

        - name: Umount the set again to check idempotence
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: umount
            snapshot_lvm_set: "{{ snapshot_test_set }}"

        - name: Assert no changes for umount
          assert:
            that: not snapshot_cmd["changed"]

        - name: Run the snapshot role remove the set
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: remove
            snapshot_lvm_set: "{{ snapshot_test_set }}"

        - name: Run the snapshot role to verify the set is removed
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: remove
            snapshot_lvm_set: "{{ snapshot_test_set }}"
            snapshot_lvm_verify_only: true

      always:
        - name: Cleanup
          include_tasks: tasks/cleanup.yml
          tags: tests::cleanup
