---
- name: Verify role fails when expected
  block:
    - name: Show __snapshot_failed_params
      debug:
        var: __snapshot_failed_params
        verbosity: 1

    - name: Verify role returns error
      include_role:
        name: linux-system-roles.snapshot
      vars:
        snapshot_lvm_percent_space_required: "{{
          __snapshot_failed_params.get('snapshot_lvm_percent_space_required')
          }}"
        snapshot_lvm_all_vgs: "{{
          __snapshot_failed_params.get('snapshot_lvm_all_vgs', false)
          }}"
        snapshot_lvm_snapset_name: "{{
          __snapshot_failed_params.get('snapshot_lvm_snapset_name')
          }}"
        snapshot_lvm_set: "{{
          __snapshot_failed_params.get('__snapshot_lvm_set', {})
          }}"
        snapshot_lvm_action: "{{
          __snapshot_failed_params.get('snapshot_lvm_action')
          }}"
        snapshot_lvm_verify_only: "{{
          __snapshot_failed_params.get('snapshot_lvm_verify_only', false)
          }}"
        snapshot_lvm_vg: "{{
          __snapshot_failed_params.get('snapshot_lvm_vg')
          }}"
        snapshot_lvm_lv: "{{
          __snapshot_failed_params.get('snapshot_lvm_lv')
          }}"
        snapshot_lvm_mount_origin: "{{
          __snapshot_failed_params.get('snapshot_lvm_mount_origin', false)
          }}"
        snapshot_lvm_mountpoint_create: "{{
          __snapshot_failed_params.get('snapshot_lvm_mountpoint_create', false)
          }}"
        snapshot_lvm_unmount_all: "{{
          __snapshot_failed_params.get('snapshot_lvm_unmount_all', false)
          }}"
        snapshot_lvm_mountpoint: "{{
          __snapshot_failed_params.get('snapshot_lvm_mountpoint')
          }}"
        snapshot_lvm_mount_options: "{{
          __snapshot_failed_params.get('snapshot_lvm_mount_options')
          }}"

    - name: Unreachable task
      fail:
        msg: UNREACH

  rescue:
    - name: Debug
      debug:
        var: ansible_failed_result

    - name: Check that there was a failure in the role
      assert:
        that: ansible_failed_result.msg != 'UNREACH'
        msg: Role has not failed when it should have

    - name: Debug snapshot_cmd
      debug:
        var: snapshot_cmd
        verbosity: 1

    - name: Debug __snapshot_failed_regex
      debug:
        var: __snapshot_failed_regex
        verbosity: 1

    - name: Verify return code and message is correct
      assert:
        that:
          - snapshot_cmd["return_code"] != 0
          - snapshot_cmd["errors"] is search(__snapshot_failed_regex)
        msg: "{{ __snapshot_failed_msg }}"
      when:
        - __snapshot_failed_regex is defined
        - __snapshot_failed_msg is defined

    - name: Assert role reported changes correctly
      assert:
        that: snapshot_cmd["changed"] == __snapshot_failed_changed
