Files
kubevirt.core/tests/unit/utils/ansible_module_mock.py
Felix Matouschek 93473cdd47 fix: Ensure compatibility with ansible-core >= 2.19
ansible-core 2.19 changes the way templates are trusted and provides a
new way of patching module args in unit tests.

With this commit the following changes are made to ensure compatibility
with ansible-core >= 2.19:

- Mark inputs to composable as trusted to align with the new template
  trust model.
- Utilize the updated method for patching module arguments in unit tests
  if available.
- Replace direct access to the self._cache attribute with the inventory's
  cache property.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2025-04-23 11:25:51 +02:00

56 lines
1.5 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
from contextlib import contextmanager
from json import dumps
from typing import (
Any,
Dict,
)
from unittest import mock
from ansible.module_utils import basic
from ansible.module_utils.common.text.converters import to_bytes
@contextmanager
def patch_module_args(args: Dict[str, Any] | None = None):
"""prepare arguments so that they will be picked up during module creation"""
args = dumps({"ANSIBLE_MODULE_ARGS": args})
with mock.patch.object(basic, "_ANSIBLE_ARGS", to_bytes(args)):
yield
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)