|
22 | 22 | import codecs
|
23 | 23 | import os
|
24 | 24 | import re
|
| 25 | +import logging |
25 | 26 |
|
26 | 27 | from io import TextIOWrapper
|
| 28 | +from unittest.mock import MagicMock, patch |
27 | 29 |
|
28 | 30 | import pytest
|
29 | 31 |
|
30 | 32 | import splunklib
|
31 | 33 | from splunklib.searchcommands import Configuration, StreamingCommand
|
32 | 34 | from splunklib.searchcommands.decorators import ConfigurationSetting, Option
|
| 35 | +from splunklib.searchcommands.internals import ObjectView |
33 | 36 | from splunklib.searchcommands.search_command import SearchCommand
|
34 | 37 | from splunklib.client import Service
|
35 | 38 | from splunklib.utils import ensure_binary
|
@@ -265,6 +268,52 @@ def test_process_scpv2(self):
|
265 | 268 |
|
266 | 269 | _package_directory = os.path.dirname(os.path.abspath(__file__))
|
267 | 270 |
|
| 271 | +class TestSearchCommandService(TestCase): |
| 272 | + def setUp(self): |
| 273 | + TestCase.setUp(self) |
| 274 | + self.command = SearchCommand() |
| 275 | + console_handler = logging.StreamHandler() |
| 276 | + console_handler.setLevel(logging.WARNING) |
| 277 | + self.command.logger.addHandler(console_handler) |
| 278 | + |
| 279 | + def test_service_exists(self): |
| 280 | + self.command._service = Service() |
| 281 | + self.assertIsNotNone(self.command.service) |
| 282 | + |
| 283 | + def test_service_not_exists(self): |
| 284 | + self.assertIsNone(self.command.service) |
| 285 | + |
| 286 | + def test_missing_metadata(self): |
| 287 | + with self.assertLogs(self.command.logger, level='WARNING') as log: |
| 288 | + service = self.command.service |
| 289 | + self.assertIsNone(service) |
| 290 | + self.assertTrue(any("Missing metadata for service creation." in message for message in log.output)) |
| 291 | + |
| 292 | + def test_missing_searchinfo(self): |
| 293 | + with self.assertLogs(self.command.logger, level='WARNING') as log: |
| 294 | + self.command._metadata = ObjectView({}) |
| 295 | + self.assertIsNone(self.command.service) |
| 296 | + self.assertTrue(any("Missing searchinfo in metadata for service creation." in message for message in log.output)) |
| 297 | + |
| 298 | + |
| 299 | + def test_missing_splunkd_uri(self): |
| 300 | + with self.assertLogs(self.command.logger, level='WARNING') as log: |
| 301 | + metadata = ObjectView({"searchinfo": ObjectView({"splunkd_uri": ""})}) |
| 302 | + self.command._metadata = metadata |
| 303 | + self.assertIsNone(self.command.service) |
| 304 | + self.assertTrue(any("Incorrect value for Splunkd URI: '' in metadata" in message for message in log.output)) |
| 305 | + |
| 306 | + |
| 307 | + |
| 308 | + def test_service_returns_valid_service_object(self): |
| 309 | + metadata = ObjectView({"searchinfo":ObjectView({"splunkd_uri":"https://127.0.0.1:8089", |
| 310 | + "session_key":"mock_session_key", |
| 311 | + "app":"search", |
| 312 | + })}) |
| 313 | + self.command._metadata = metadata |
| 314 | + self.assertIsInstance(self.command.service, Service) |
| 315 | + |
| 316 | + |
268 | 317 |
|
269 | 318 | if __name__ == "__main__":
|
270 | 319 | main()
|
0 commit comments