---
- name: Basic extend snapshot test
  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: "15%"
          - name: lv6
            size: "15%"

  tasks:
    - name: Run tests
      block:
        - name: Setup
          include_tasks: tasks/setup.yml

        - name: Run the snapshot role to create snapshot LVs
          include_role:
            name: fedora.linux_system_roles.snapshot
          vars:
            snapshot_lvm_percent_space_required: 50
            snapshot_lvm_all_vgs: true
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: snapshot

        - name: Verify the snapshot LVs are created
          include_role:
            name: fedora.linux_system_roles.snapshot
          vars:
            snapshot_lvm_all_vgs: true
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_verify_only: true
            snapshot_lvm_action: check

        - name: Extend the snapshots size to 60 percent of the source
          include_role:
            name: fedora.linux_system_roles.snapshot
          vars:
            snapshot_lvm_percent_space_required: 60
            snapshot_lvm_all_vgs: true
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: extend

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

        - name: Use the snapshot_lvm_verify option to make sure extend is done
          include_role:
            name: fedora.linux_system_roles.snapshot
          vars:
            snapshot_lvm_percent_space_required: 60
            snapshot_lvm_all_vgs: true
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_verify_only: true
            snapshot_lvm_action: extend

      # 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 no changes for extend verify
          assert:
            that: not snapshot_cmd["changed"]

        - name: Extend again to check idempotence
          include_role:
            name: fedora.linux_system_roles.snapshot
          vars:
            snapshot_lvm_percent_space_required: 60
            snapshot_lvm_all_vgs: true
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: extend

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

        - name: Verify extend again to check idempotence
          include_role:
            name: fedora.linux_system_roles.snapshot
          vars:
            snapshot_lvm_percent_space_required: 60
            snapshot_lvm_all_vgs: true
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_verify_only: true
            snapshot_lvm_action: extend

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

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