Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add loggers in Service, add test for service creation #623

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

Merged
merged 1 commit into from
Jul 8, 2025
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
7 changes: 6 additions & 1 deletion splunklib/searchcommands/search_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,24 +340,29 @@ def service(self):
of :code:`None` is returned.

"""

if self._service is not None:
return self._service

metadata = self._metadata

if metadata is None:
self.logger.warning("Missing metadata for service creation.")
return None

try:
searchinfo = self._metadata.searchinfo
except AttributeError:
self.logger.warning("Missing searchinfo in metadata for service creation.")
return None

splunkd_uri = searchinfo.splunkd_uri

if splunkd_uri is None:
if splunkd_uri is None or splunkd_uri == "" or splunkd_uri == " ":
self.logger.warning(f"Incorrect value for Splunkd URI: {splunkd_uri!r} in metadata")
return None


uri = urlsplit(splunkd_uri, allow_fragments=False)

self._service = Service(
Expand Down
49 changes: 49 additions & 0 deletions tests/searchcommands/test_search_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
import codecs
import os
import re
import logging

from io import TextIOWrapper
from unittest.mock import MagicMock, patch

import pytest

import splunklib
from splunklib.searchcommands import Configuration, StreamingCommand
from splunklib.searchcommands.decorators import ConfigurationSetting, Option
from splunklib.searchcommands.internals import ObjectView
from splunklib.searchcommands.search_command import SearchCommand
from splunklib.client import Service
from splunklib.utils import ensure_binary
Expand Down Expand Up @@ -265,6 +268,52 @@ def test_process_scpv2(self):

_package_directory = os.path.dirname(os.path.abspath(__file__))

class TestSearchCommandService(TestCase):
def setUp(self):
TestCase.setUp(self)
self.command = SearchCommand()
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.WARNING)
self.command.logger.addHandler(console_handler)

def test_service_exists(self):
self.command._service = Service()
self.assertIsNotNone(self.command.service)

def test_service_not_exists(self):
self.assertIsNone(self.command.service)

def test_missing_metadata(self):
with self.assertLogs(self.command.logger, level='WARNING') as log:
service = self.command.service
self.assertIsNone(service)
self.assertTrue(any("Missing metadata for service creation." in message for message in log.output))

def test_missing_searchinfo(self):
with self.assertLogs(self.command.logger, level='WARNING') as log:
self.command._metadata = ObjectView({})
self.assertIsNone(self.command.service)
self.assertTrue(any("Missing searchinfo in metadata for service creation." in message for message in log.output))


def test_missing_splunkd_uri(self):
with self.assertLogs(self.command.logger, level='WARNING') as log:
metadata = ObjectView({"searchinfo": ObjectView({"splunkd_uri": ""})})
self.command._metadata = metadata
self.assertIsNone(self.command.service)
self.assertTrue(any("Incorrect value for Splunkd URI: '' in metadata" in message for message in log.output))



def test_service_returns_valid_service_object(self):
metadata = ObjectView({"searchinfo":ObjectView({"splunkd_uri":"https://127.0.0.1:8089",
"session_key":"mock_session_key",
"app":"search",
})})
self.command._metadata = metadata
self.assertIsInstance(self.command.service, Service)



if __name__ == "__main__":
main()