Skip to content

Commit 2fa9cb0

Browse files
authored
Fix data tag related tests (#697)
1 parent f7cfe06 commit 2fa9cb0

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

changelogs/fragments/data_tag.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
trivial:
3+
- "Fixes unit tests execution by updating set_module_args to automatically patch module arguments using the new patch_module_args for ansible 2.19"

tests/unit/modules/utils.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,36 @@
1111
from ansible.module_utils._text import to_bytes
1212

1313

14+
cur_context = None
15+
16+
1417
def set_module_args(args):
18+
global cur_context
19+
20+
# Add common defaults
1521
if "_ansible_remote_tmp" not in args:
1622
args["_ansible_remote_tmp"] = "/tmp"
1723
if "_ansible_keep_remote_files" not in args:
1824
args["_ansible_keep_remote_files"] = False
1925

20-
args = json.dumps({"ANSIBLE_MODULE_ARGS": args})
21-
basic._ANSIBLE_ARGS = to_bytes(args)
26+
# Clean up any previous context manager if it exists
27+
if cur_context is not None:
28+
try:
29+
cur_context.__exit__(None, None, None)
30+
except Exception:
31+
pass
32+
cur_context = None
33+
34+
# Try to use the newer patch_module_args
35+
try:
36+
from ansible.module_utils.testing import patch_module_args
37+
38+
cur_context = patch_module_args(args)
39+
cur_context.__enter__()
40+
except ImportError:
41+
# Fall back to original behavior for older Ansible versions
42+
serialized_args = json.dumps({"ANSIBLE_MODULE_ARGS": args})
43+
basic._ANSIBLE_ARGS = to_bytes(serialized_args)
2244

2345

2446
class AnsibleExitJson(Exception):

0 commit comments

Comments
 (0)