mirror of
https://opendev.org/openstack/ansible-collections-openstack.git
synced 2026-07-25 01:04:28 +00:00
Fix unit test helpers for ansible-core 2.19+
ansible-core 2.19 introduced _ANSIBLE_PROFILE alongside _ANSIBLE_ARGS. set_module_args() in utils.py and test_baremetal_port_group.py only set _ANSIBLE_ARGS, causing "No serialization profile was specified" errors. Set _ANSIBLE_PROFILE to 'legacy' to match the pre-2.19 behavior. Change-Id: I1073f347ce18f061b5fa099442a6b6aa10c42507 Signed-off-by: Bertrand Lanson <bertrand.lanson@infomaniak.com>
This commit is contained in:
@@ -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):
|
||||||
|
|||||||
@@ -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):
|
||||||
|
|||||||
Reference in New Issue
Block a user