Skip to content

Commit d291cd3

Browse files
committed
test: change procArgs for splunk cli_common
1 parent 519ad53 commit d291cd3

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

tests/integration/test_conf_manager.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,50 @@
3232
}
3333

3434

35+
# conftest.py or test file
36+
37+
import tempfile
38+
import shutil
39+
import re
40+
import importlib.util
41+
import sys
42+
import os
43+
import pytest
44+
45+
46+
@pytest.fixture
47+
def patch_splunk_cli_common_module():
48+
import splunk.clilib.cli_common as comm
49+
50+
original_path = comm.__file__
51+
52+
with open(original_path, "r") as f:
53+
source = f.read()
54+
55+
pattern = r'procArgs\s*=\s*\[\s*os\.path\.join\(\s*os\.environ\[\s*["\']SPLUNK_HOME["\']\s*\],\s*["\']bin["\'],\s*["\']splunkd["\']\s*\)\s*,\s*["\']local-rest-uri["\']\s*,\s*["\']-p["\']\s*,\s*mgmtPort\s*\]'
56+
replacement = 'procArgs = [os.path.join(os.environ["SPLUNK_HOME"], "bin", "splunk"), "cmd", "splunkd", "local-rest-uri", "-p", mgmtPort]'
57+
58+
new_content = re.sub(pattern, replacement, source)
59+
60+
temp_dir = tempfile.mkdtemp()
61+
patched_module_path = os.path.join(temp_dir, "module.py")
62+
63+
with open(patched_module_path, "w") as f:
64+
f.write(new_content)
65+
66+
spec = importlib.util.spec_from_file_location("splunk.clilib.cli_common", patched_module_path)
67+
patched_module = importlib.util.module_from_spec(spec)
68+
sys.modules["splunk.clilib.cli_common"] = patched_module
69+
spec.loader.exec_module(patched_module)
70+
71+
yield
72+
73+
shutil.rmtree(temp_dir)
74+
importlib.invalidate_caches()
75+
sys.modules.pop("splunk.clilib.cli_common", None)
76+
77+
78+
3579
def _build_conf_manager(session_key: str) -> conf_manager.ConfManager:
3680
return conf_manager.ConfManager(
3781
session_key,
@@ -145,7 +189,8 @@ def test_conf_manager_update_conf_with_encrypted_keys():
145189
assert conf_file.get("stanza")["key2"] == "value2"
146190

147191

148-
def test_get_log_level(monkeypatch):
192+
def test_get_log_level(patch_splunk_cli_common_module, monkeypatch):
193+
149194
conftest.mock_splunk(monkeypatch)
150195

151196
session_key = context.get_session_key()

0 commit comments

Comments
 (0)