Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server_info): handle splunk not present imports #378

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading