Skip to content

Commit 713b3ef

Browse files
committed
test: fix integration tests, add deprecation for create_from_token
1 parent d291cd3 commit 713b3ef

File tree

3 files changed

+41
-48
lines changed

3 files changed

+41
-48
lines changed

solnlib/modular_input/event_writer.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import threading
2424
import time
2525
import traceback
26+
import warnings
2627
from abc import ABCMeta, abstractmethod
2728
from random import randint
2829
from typing import List, Union
@@ -39,6 +40,16 @@
3940
__all__ = ["ClassicEventWriter", "HECEventWriter"]
4041

4142

43+
deprecation_msg = (
44+
"Function 'create_from_token' is deprecated and incompatible with 'global_settings_schema=True'. "
45+
"Use 'create_from_token_with_session_key' instead."
46+
)
47+
48+
49+
class FunctionDeprecated(Exception):
50+
pass
51+
52+
4253
class EventWriter(metaclass=ABCMeta):
4354
"""Base class of event writer."""
4455

@@ -272,12 +283,16 @@ def create_from_token(
272283
Created HECEventWriter.
273284
"""
274285

286+
warnings.warn(deprecation_msg, DeprecationWarning, stacklevel=2)
287+
288+
if global_settings_schema:
289+
raise FunctionDeprecated(deprecation_msg)
290+
275291
return HECEventWriter(
276292
None,
277293
None,
278294
None,
279295
None,
280-
None,
281296
hec_uri=hec_uri,
282297
hec_token=hec_token,
283298
global_settings_schema=global_settings_schema,

tests/integration/test_conf_manager.py

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,50 +32,6 @@
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-
7935
def _build_conf_manager(session_key: str) -> conf_manager.ConfManager:
8036
return conf_manager.ConfManager(
8137
session_key,
@@ -189,8 +145,9 @@ def test_conf_manager_update_conf_with_encrypted_keys():
189145
assert conf_file.get("stanza")["key2"] == "value2"
190146

191147

192-
def test_get_log_level(patch_splunk_cli_common_module, monkeypatch):
193-
148+
@mock.patch("splunk.clilib.cli_common.getMgmtUri")
149+
def test_get_log_level(mock_mgm_uri, monkeypatch):
150+
mock_mgm_uri.return_value = "https://127.0.0.1:8089"
194151
conftest.mock_splunk(monkeypatch)
195152

196153
session_key = context.get_session_key()

tests/unit/test_modular_input_event_writer.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from unittest.mock import patch
2424

2525
from solnlib.modular_input import ClassicEventWriter, HECEventWriter
26+
from solnlib.modular_input.event_writer import FunctionDeprecated, deprecation_msg
2627

2728

2829
def test_classic_event_writer(monkeypatch):
@@ -299,7 +300,6 @@ def mock_get_hec_config(
299300
(create_hec_event_writer__create_from_input, False, "https"),
300301
(create_hec_event_writer__create_from_token_with_session_key, True, "https"),
301302
(create_hec_event_writer__create_from_token_with_session_key, False, "https"),
302-
(create_hec_event_writer__create_from_token, True, "https"),
303303
(create_hec_event_writer__create_from_token, False, "https"),
304304
],
305305
)
@@ -318,3 +318,24 @@ def mock_get_hec_config(
318318

319319
ev = create_hec_event_writer(hec)
320320
assert ev._rest_client.scheme == expected_scheme
321+
322+
323+
@pytest.mark.parametrize("hec", [False, True])
324+
def test_hec_event_writer_create_from_token_deprecation(hec, monkeypatch, caplog):
325+
common.mock_splunkhome(monkeypatch)
326+
327+
def mock_get_hec_config(
328+
self, hec_input_name, session_key, scheme, host, port, **context
329+
):
330+
return "8088", "87de04d1-0823-11e6-9c94-a45e60e"
331+
332+
monkeypatch.setattr(HECEventWriter, "_get_hec_config", mock_get_hec_config)
333+
334+
if hec:
335+
with pytest.raises(FunctionDeprecated) as e:
336+
create_hec_event_writer__create_from_token(hec=True)
337+
assert e.value.args[0] == deprecation_msg
338+
else:
339+
with pytest.warns(DeprecationWarning, match=deprecation_msg):
340+
ev = create_hec_event_writer__create_from_token(hec=False)
341+
assert ev._rest_client.scheme == "https"

0 commit comments

Comments
 (0)