---
- name: Extend snapshots of logical volumes across different volume groups
  hosts: all
  vars:
    # only use vgs matching this pattern
    snapshot_lvm_vg_include: "^test_"
    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: "15%"
      - name: test_vg2
        disks: "{{ range(3, 6) | map('extract', unused_disks) | list }}"
        volumes:
          - name: lv3
            size: "15%"
          - name: lv4
            size: "15%"
      - name: test_vg3
        disks: "{{ range(6, 10) | map('extract', unused_disks) | list }}"
        volumes:
          - name: lv5
            size: "30%"
          - name: lv6
            size: "25%"
    snapshot_test_set:
      name: snapset1
      volumes:
        - name: snapshot VG1 LV1
          vg: test_vg1
          lv: lv1
          percent_space_required: 50
        - name: snapshot VG2 LV3
          vg: test_vg2
          lv: lv3
          percent_space_required: 50
        - name: snapshot VG2 LV4
          vg: test_vg2
          lv: lv4
          percent_space_required: 50
        - name: snapshot VG3 LV6
          vg: test_vg3
          lv: lv6
          percent_space_required: 50
    snapshot_extend_set:
      name: snapset1
      volumes:
        - name: snapshot VG1 LV1
          vg: test_vg1
          lv: lv1
          percent_space_required: 60
        - name: snapshot VG2 LV3
          vg: test_vg2
          lv: lv3
          percent_space_required: 60
        - name: snapshot VG2 LV4
          vg: test_vg2
          lv: lv4
          percent_space_required: 60
        - name: snapshot VG3 LV6
          vg: test_vg3
          lv: lv6
          percent_space_required: 60
  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: Extend the set
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: extend
            snapshot_lvm_set: "{{ snapshot_extend_set }}"

        # Both LVM and Snapshot Manager have minimum sizes for snapshots.
        # extend may return changed=false if the original request was
        # smaller than the minimum snapshot size and the new percentage
        # is also smaller than the original.
        - name: Assert changes for extend
          assert:
            that: snapshot_cmd["changed"]

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

        - name: Extend the set again to check idempotence
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_action: extend
            snapshot_lvm_set: "{{ snapshot_extend_set }}"

        - name: Assert no changes for extend
          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_extend_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_extend_set }}"
            snapshot_lvm_verify_only: true

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