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:
Bertrand Lanson
2026-05-19 20:38:34 +02:00
parent 836d7410b2
commit 7644aeaf32
2 changed files with 7 additions and 52 deletions

View File

@@ -1,12 +1,13 @@
import importlib.util
import json
import unittest
from pathlib import Path
from unittest import mock
from unittest.mock import patch
from ansible.module_utils import basic
from ansible.module_utils._text import to_bytes
from ansible_collections.openstack.cloud.tests.unit.modules.utils import (
AnsibleExitJson,
AnsibleFailJson,
ModuleTestCase,
set_module_args,
)
def _load_module_under_test():
@@ -22,53 +23,6 @@ def _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]):
def to_dict(self, computed=False):