|
32 | 32 | } |
33 | 33 |
|
34 | 34 |
|
| 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 | + |
35 | 79 | def _build_conf_manager(session_key: str) -> conf_manager.ConfManager: |
36 | 80 | return conf_manager.ConfManager( |
37 | 81 | session_key, |
@@ -145,7 +189,8 @@ def test_conf_manager_update_conf_with_encrypted_keys(): |
145 | 189 | assert conf_file.get("stanza")["key2"] == "value2" |
146 | 190 |
|
147 | 191 |
|
148 | | -def test_get_log_level(monkeypatch): |
| 192 | +def test_get_log_level(patch_splunk_cli_common_module, monkeypatch): |
| 193 | + |
149 | 194 | conftest.mock_splunk(monkeypatch) |
150 | 195 |
|
151 | 196 | session_key = context.get_session_key() |
|
0 commit comments