# issue #527: catch exceptions from crashy modules.

- name: integration/runner/crashy_new_style_module.yml
  hosts: test-targets
  tasks:
    - custom_python_run_script:
        script: kaboom
      register: out
      ignore_errors: true

    - name: Check error report
      vars:
        msg_pattern: "MODULE FAILURE(?:\nSee stdout/stderr for the exact error)?"
        # (?s) -> . matches any character, even newlines
        tb_pattern:  "(?s)Traceback \\(most recent call last\\).+NameError: name 'kaboom' is not defined"
      assert:
        that:
        - not out.changed
        - out is failed
        # https://github.com/ansible/ansible/commit/62d8c8fde6a76d9c567ded381e9b34dad69afcd6
        - |
          out.msg is match(msg_pattern)
          or out.msg in (
             "Task failed: Module failed: name 'kaboom' is not defined",
             'Module result deserialization failed: No start of json char found',
          )
        # - out.exception is undefined
        #   or out.exception | default('') is match(tb_pattern)
        #   or out.module_stderr is search(tb_pattern)
        # - out.module_stdout == ''
        # - out.module_stderr is search(tb_pattern)
        fail_msg: |
          out={{ out }}
  tags:
    - crashy_new_style_module
