Merge "Fix unit test helpers for ansible-core 2.19+"

This commit is contained in:
Zuul
2026-05-20 07:23:41 +00:00
committed by Gerrit Code Review
2 changed files with 7 additions and 52 deletions

View File

@@ -1,12 +1,13 @@
import importlib.util import importlib.util
import json
import unittest
from pathlib import Path from pathlib import Path
from unittest import mock from unittest import mock
from unittest.mock import patch
from ansible.module_utils import basic from ansible_collections.openstack.cloud.tests.unit.modules.utils import (
from ansible.module_utils._text import to_bytes AnsibleExitJson,
AnsibleFailJson,
ModuleTestCase,
set_module_args,
)
def _load_module_under_test(): def _load_module_under_test():
@@ -22,53 +23,6 @@ def _load_module_under_test():
baremetal_port_group = _load_module_under_test() baremetal_port_group = _load_module_under_test()
def set_module_args(args):
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
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)
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(unittest.TestCase):
mock_module = None
mock_sleep = None
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()
set_module_args({})
self.addCleanup(self.mock_module.stop)
self.addCleanup(self.mock_sleep.stop)
class FakePortGroup(dict[str, object]): class FakePortGroup(dict[str, object]):
def to_dict(self, computed=False): def to_dict(self, computed=False):

View File

@@ -14,6 +14,7 @@ def set_module_args(args):
args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args) basic._ANSIBLE_ARGS = to_bytes(args)
basic._ANSIBLE_PROFILE = 'legacy'
class AnsibleExitJson(Exception): class AnsibleExitJson(Exception):