Files
kubevirt.core/tests/unit/utils/ansible_module_mock.py
Felix Matouschek 5d31db3ea2 chore: Change license Apache 2.0
Change the license of the collection to Apache 2.0 to be compliant with
the CNCF licensing requirements.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-06-11 11:39:32 +02:00

48 lines
1.4 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2024 Red Hat, Inc.
# Apache License 2.0 (see LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
# This file allows to run modules in unit tests.
# It was taken from:
# https://docs.ansible.com/ansible/latest/dev_guide/testing_units_modules.html#module-argument-processing
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import json
from ansible.module_utils import basic
from ansible.module_utils.common.text.converters import to_bytes
def set_module_args(args):
"""prepare arguments so that they will be picked up during module creation"""
args = json.dumps({"ANSIBLE_MODULE_ARGS": args})
basic._ANSIBLE_ARGS = to_bytes(args)
class AnsibleExitJson(Exception):
"""Exception class to be raised by module.exit_json and caught by the test case"""
pass
class AnsibleFailJson(Exception):
"""Exception class to be raised by module.fail_json and caught by the test case"""
pass
def exit_json(*args, **kwargs):
"""function to patch over exit_json; package return data into an exception"""
if "changed" not in kwargs:
kwargs["changed"] = False
raise AnsibleExitJson(kwargs)
def fail_json(*args, **kwargs):
"""function to patch over fail_json; package return data into an exception"""
kwargs["failed"] = True
raise AnsibleFailJson(kwargs)