Skip to content

Commit 46bc899

Browse files
fix(server_info): handle splunk not present imports (#378)
Handle imports when Splunk isn't present.
2 parents e204a3d + 687b82f commit 46bc899

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

solnlib/server_info.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@
2020
import json
2121
from typing import Any, Dict, Optional
2222

23-
from splunk.rest import getWebCertFile, getWebKeyFile
23+
try:
24+
from splunk.rest import getWebCertFile, getWebKeyFile
25+
except (ModuleNotFoundError, ImportError):
26+
27+
def getWebCertFile():
28+
return None
29+
30+
def getWebKeyFile():
31+
return None
32+
33+
2434
from splunklib import binding
2535
from solnlib import splunk_rest_client as rest_client
2636
from solnlib import utils

tests/unit/conftest.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/unit/test_server_info.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,32 @@ def test_server_info_object_with_key_file(
161161
assert kwargs.get("cert_file") is None
162162
assert kwargs.get("key_file") is None
163163
assert kwargs.get("verify") is None
164+
165+
@patch("solnlib.server_info.os.environ", autospec=True, return_value="$SPLUNK_HOME")
166+
@patch(
167+
"solnlib.server_info.get_splunkd_access_info",
168+
autospec=True,
169+
return_value=("https", "127.0.0.1", "8089"),
170+
)
171+
@patch("solnlib.server_info.rest_client", autospec=True)
172+
def test_server_info_object_with_no_splunk_import(
173+
self,
174+
mock_rest_client,
175+
mock_splunkd,
176+
mock_os_env,
177+
):
178+
mock_rest_client.SplunkRestClient = MagicMock()
179+
180+
# we want to raise 'ModuleNotFoundError' when importing the required functions
181+
with patch.dict("sys.modules", {"splunk": ModuleNotFoundError}):
182+
server_info.ServerInfo(common.SESSION_KEY)
183+
184+
for call_arg in mock_rest_client.SplunkRestClient.call_args_list:
185+
_, kwargs = call_arg
186+
assert (
187+
kwargs.get("cert_file") is None
188+
) # comes from the empty function in except
189+
assert (
190+
kwargs.get("key_file") is None
191+
) # comes from the empty function in except
192+
assert kwargs.get("verify") is None

0 commit comments

Comments
 (0)