---
- name: Basic mount verify 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%"
            fs_type: xfs
          - name: lv2
            size: "50%"
            fs_type: xfs
      - name: test_vg2
        disks: "{{ range(3, 6) | map('extract', unused_disks) | list }}"
        volumes:
          - name: lv3
            size: "10%"
            fs_type: xfs
          - name: lv4
            size: "20%"
            fs_type: xfs
      - name: test_vg3
        disks: "{{ range(6, 10) | map('extract', unused_disks) | list }}"
        volumes:
          - name: lv5
            size: "30%"
            fs_type: xfs
          - name: lv6
            size: "25%"
            fs_type: xfs
          - name: lv7
            size: "10%"
            fs_type: xfs
          - name: lv8
            size: "10%"
            fs_type: xfs
  tasks:
    - name: Run tests
      block:
        - name: Setup
          include_tasks: tasks/setup.yml

        - name: Run the snapshot role to create snapshot LVs
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_percent_space_required: 15
            snapshot_lvm_all_vgs: true
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: snapshot

        - name: Verify the snapshot LVs are created
          include_role:
            name: 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: Mount the snapshot for lv1
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: mount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv1
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv1_mp' }}"
            snapshot_lvm_mountpoint_create: true

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

        - name: Mount the snapshot for lv1 again for idempotence
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: mount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv1
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv1_mp' }}"
            snapshot_lvm_mountpoint_create: true

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

        - name: Mount the snapshot for lv2
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: mount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv2
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv2_mp' }}"
            snapshot_lvm_mountpoint_create: true

        - name: Mount the snapshot for lv7
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: mount
            snapshot_lvm_vg: test_vg3
            snapshot_lvm_lv: lv7
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv7_mp' }}"
            snapshot_lvm_mountpoint_create: true

        - name: Mount the origin for lv6
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: mount
            snapshot_lvm_vg: test_vg3
            snapshot_lvm_lv: lv6
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv6_mp' }}"
            snapshot_lvm_mountpoint_create: true
            snapshot_lvm_mount_origin: true

        - name: Verify snapshot is mounted for lv1
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: mount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv1
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv1_mp' }}"
            snapshot_lvm_verify_only: true

        - name: Verify snapshot is mounted for lv2
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: mount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv2
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv2_mp' }}"
            snapshot_lvm_verify_only: true

        - name: Verify snapshot is mounted for lv7
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: mount
            snapshot_lvm_vg: test_vg3
            snapshot_lvm_lv: lv7
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv7_mp' }}"
            snapshot_lvm_verify_only: true

        - name: Verify origin is mounted lv6
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: mount
            snapshot_lvm_vg: test_vg3
            snapshot_lvm_lv: lv6
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv6_mp' }}"
            snapshot_lvm_mountpoint_create: true
            snapshot_lvm_mount_origin: true
            snapshot_lvm_verify_only: true

        - name: Umount the snapshot for lv1
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: umount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv1
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv1_mp' }}"

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

        - name: Umount the snapshot for lv1 again for idempotence
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: umount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv1
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv1_mp' }}"

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

        - name: Umount the snapshot for lv2
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: umount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv2
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv2_mp' }}"

        - name: Umount the snapshot for lv7
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: umount
            snapshot_lvm_vg: test_vg3
            snapshot_lvm_lv: lv7
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv7_mp' }}"

        - name: Umount the origin for lv6
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: umount
            snapshot_lvm_vg: test_vg3
            snapshot_lvm_lv: lv6
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv6_mp' }}"

        - name: Verify umount of the for lv1
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: umount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv1
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv1_mp' }}"
            snapshot_lvm_verify_only: true

        - name: Verify umount of the for lv2
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: umount
            snapshot_lvm_vg: test_vg1
            snapshot_lvm_lv: lv2
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv2_mp' }}"
            snapshot_lvm_verify_only: true

        - name: Verify umount of the for lv7
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: umount
            snapshot_lvm_vg: test_vg3
            snapshot_lvm_lv: lv7
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv7_mp' }}"
            snapshot_lvm_verify_only: true

        - name: Verify umount of the origin for lv6
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: umount
            snapshot_lvm_vg: test_vg3
            snapshot_lvm_lv: lv6
            snapshot_lvm_mountpoint: "{{ test_mnt_parent ~ '/lv6_mp' }}"
            snapshot_lvm_verify_only: true

        - name: Run the snapshot role remove the snapshot LVs
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_action: remove

        - name: Use the snapshot_lvm_verify option to make sure remove is done
          include_role:
            name: linux-system-roles.snapshot
          vars:
            snapshot_lvm_snapset_name: snapset1
            snapshot_lvm_verify_only: true
            snapshot_lvm_action: remove
      always:
        - name: Cleanup
          include_tasks: tasks/cleanup.yml
          tags: tests::cleanup
