Skip to content

Commit

Permalink
fix(server_info): handle splunk not present imports (#378)
Browse files Browse the repository at this point in the history
Handle imports when Splunk isn't present.
  • Loading branch information
sgoral-splunk authored Jul 5, 2024
2 parents e204a3d + 687b82f commit 46bc899
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
12 changes: 11 additions & 1 deletion solnlib/server_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@
import json
from typing import Any, Dict, Optional

from splunk.rest import getWebCertFile, getWebKeyFile
try:
from splunk.rest import getWebCertFile, getWebKeyFile
except (ModuleNotFoundError, ImportError):

def getWebCertFile():
return None

def getWebKeyFile():
return None


from splunklib import binding
from solnlib import splunk_rest_client as rest_client
from solnlib import utils
Expand Down
6 changes: 0 additions & 6 deletions tests/unit/conftest.py

This file was deleted.

29 changes: 29 additions & 0 deletions tests/unit/test_server_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,32 @@ def test_server_info_object_with_key_file(
assert kwargs.get("cert_file") is None
assert kwargs.get("key_file") is None
assert kwargs.get("verify") is None

@patch("solnlib.server_info.os.environ", autospec=True, return_value="$SPLUNK_HOME")
@patch(
"solnlib.server_info.get_splunkd_access_info",
autospec=True,
return_value=("https", "127.0.0.1", "8089"),
)
@patch("solnlib.server_info.rest_client", autospec=True)
def test_server_info_object_with_no_splunk_import(
self,
mock_rest_client,
mock_splunkd,
mock_os_env,
):
mock_rest_client.SplunkRestClient = MagicMock()

# we want to raise 'ModuleNotFoundError' when importing the required functions
with patch.dict("sys.modules", {"splunk": ModuleNotFoundError}):
server_info.ServerInfo(common.SESSION_KEY)

for call_arg in mock_rest_client.SplunkRestClient.call_args_list:
_, kwargs = call_arg
assert (
kwargs.get("cert_file") is None
) # comes from the empty function in except
assert (
kwargs.get("key_file") is None
) # comes from the empty function in except
assert kwargs.get("verify") is None

0 comments on commit 46bc899

Please sign in to comment.