diff --git a/solnlib/server_info.py b/solnlib/server_info.py index 01643067..c61b5e67 100644 --- a/solnlib/server_info.py +++ b/solnlib/server_info.py @@ -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 diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py deleted file mode 100644 index 1dbefb3d..00000000 --- a/tests/unit/conftest.py +++ /dev/null @@ -1,6 +0,0 @@ -import sys -from unittest.mock import MagicMock - -# mock modules of 'splunk' library added 'splunk_rest_client' -sys.modules["splunk"] = MagicMock() -sys.modules["splunk.rest"] = MagicMock() diff --git a/tests/unit/test_server_info.py b/tests/unit/test_server_info.py index f7f6e5a6..41fd0f98 100644 --- a/tests/unit/test_server_info.py +++ b/tests/unit/test_server_info.py @@ -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