#!/usr/bin/env python # Copyright (c) 2022-2026 The Project Contributors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # For a detailed list of copyright holders and contribution history, # please refer to the CONTRIBUTORS.md file in the project root. from __future__ import (absolute_import, division, print_function) __metaclass__ = type import json import contextlib from unittest import TestCase from unittest.mock import patch from ansible.module_utils import basic from ansible.module_utils.common.text.converters import to_bytes @contextlib.contextmanager def set_module_args(args): """ Context manager that sets module arguments for AnsibleModule """ if '_ansible_remote_tmp' not in args: args['_ansible_remote_tmp'] = '/tmp' if '_ansible_keep_remote_files' not in args: args['_ansible_keep_remote_files'] = False try: from ansible.module_utils.testing import patch_module_args except ImportError: # Before ansible-core 2.19, this was the way to go: serialized_args = to_bytes(json.dumps({'ANSIBLE_MODULE_ARGS': args})) with patch.object(basic, '_ANSIBLE_ARGS', serialized_args): yield else: with patch_module_args(args): yield class AnsibleExitJson(Exception): pass class AnsibleFailJson(Exception): pass def exit_json(*args, **kwargs): if 'changed' not in kwargs: kwargs['changed'] = False raise AnsibleExitJson(kwargs) def fail_json(*args, **kwargs): kwargs['failed'] = True raise AnsibleFailJson(kwargs) class ModuleTestCase(TestCase): def setUp(self): self.mock_module = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) self.mock_module.start() self.mock_sleep = patch('time.sleep') self.mock_sleep.start() self.addCleanup(self.mock_module.stop) self.addCleanup(self.mock_sleep.stop)