diff --git a/docker-compose.yml b/docker-compose.yml
index 29490f871..5c3957a07 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -5,6 +5,7 @@ services:
platform: linux/amd64
environment:
- SPLUNK_START_ARGS=--accept-license
+ - SPLUNK_GENERAL_TERMS=--accept-sgt-current-at-splunk-com
- SPLUNK_HEC_TOKEN=11111111-1111-1111-1111-1111111111113
- SPLUNK_PASSWORD=changed!
- SPLUNK_APPS_URL=https://github.com/splunk/sdk-app-collection/releases/download/v1.1.0/sdkappcollection.tgz
@@ -18,10 +19,10 @@ services:
timeout: 5s
retries: 20
volumes:
- - "./tests/searchcommands/test_apps/eventing_app:/opt/splunk/etc/apps/eventing_app"
- - "./tests/searchcommands/test_apps/generating_app:/opt/splunk/etc/apps/generating_app"
- - "./tests/searchcommands/test_apps/reporting_app:/opt/splunk/etc/apps/reporting_app"
- - "./tests/searchcommands/test_apps/streaming_app:/opt/splunk/etc/apps/streaming_app"
+ - "./tests/system/test_apps/eventing_app:/opt/splunk/etc/apps/eventing_app"
+ - "./tests/system/test_apps/generating_app:/opt/splunk/etc/apps/generating_app"
+ - "./tests/system/test_apps/reporting_app:/opt/splunk/etc/apps/reporting_app"
+ - "./tests/system/test_apps/streaming_app:/opt/splunk/etc/apps/streaming_app"
- "./splunklib:/opt/splunk/etc/apps/eventing_app/lib/splunklib"
- "./splunklib:/opt/splunk/etc/apps/generating_app/lib/splunklib"
- "./splunklib:/opt/splunk/etc/apps/reporting_app/lib/splunklib"
diff --git a/tests/data/results.xml b/tests/integration/data/results.xml
similarity index 100%
rename from tests/data/results.xml
rename to tests/integration/data/results.xml
diff --git a/tests/data/streaming_results.xml b/tests/integration/data/streaming_results.xml
similarity index 100%
rename from tests/data/streaming_results.xml
rename to tests/integration/data/streaming_results.xml
diff --git a/tests/test_app.py b/tests/integration/test_app.py
similarity index 97%
rename from tests/test_app.py
rename to tests/integration/test_app.py
index 35be38146..d818e0de9 100755
--- a/tests/test_app.py
+++ b/tests/integration/test_app.py
@@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
+# integration test - tests SDK usage as communication with splunk over http api to create and manage apps
+
import logging
from tests import testlib
from splunklib import client
diff --git a/tests/test_binding.py b/tests/integration/test_binding.py
similarity index 100%
rename from tests/test_binding.py
rename to tests/integration/test_binding.py
diff --git a/tests/test_collection.py b/tests/integration/test_collection.py
similarity index 100%
rename from tests/test_collection.py
rename to tests/integration/test_collection.py
diff --git a/tests/test_conf.py b/tests/integration/test_conf.py
similarity index 100%
rename from tests/test_conf.py
rename to tests/integration/test_conf.py
diff --git a/tests/test_event_type.py b/tests/integration/test_event_type.py
similarity index 100%
rename from tests/test_event_type.py
rename to tests/integration/test_event_type.py
diff --git a/tests/test_fired_alert.py b/tests/integration/test_fired_alert.py
similarity index 100%
rename from tests/test_fired_alert.py
rename to tests/integration/test_fired_alert.py
diff --git a/tests/test_index.py b/tests/integration/test_index.py
similarity index 100%
rename from tests/test_index.py
rename to tests/integration/test_index.py
diff --git a/tests/test_input.py b/tests/integration/test_input.py
similarity index 100%
rename from tests/test_input.py
rename to tests/integration/test_input.py
diff --git a/tests/test_job.py b/tests/integration/test_job.py
similarity index 98%
rename from tests/test_job.py
rename to tests/integration/test_job.py
index a276e212b..d587b0ea7 100755
--- a/tests/test_job.py
+++ b/tests/integration/test_job.py
@@ -15,6 +15,7 @@
# under the License.
from io import BytesIO
+from pathlib import Path
from time import sleep
import io
@@ -399,7 +400,9 @@ def test_results_reader(self):
# Run jobs.export("search index=_internal | stats count",
# earliest_time="rt", latest_time="rt") and you get a
# streaming sequence of XML fragments containing results.
- with io.open('data/results.xml', mode='br') as input:
+ test_dir = Path(__file__).parent
+ data_file = test_dir / 'data' / 'results.xml'
+ with io.open(str(data_file), mode='br') as input:
reader = results.ResultsReader(input)
self.assertFalse(reader.is_preview)
N_results = 0
@@ -419,7 +422,9 @@ def test_results_reader_with_streaming_results(self):
# Run jobs.export("search index=_internal | stats count",
# earliest_time="rt", latest_time="rt") and you get a
# streaming sequence of XML fragments containing results.
- with io.open('data/streaming_results.xml', 'br') as input:
+ test_dir = Path(__file__).parent
+ data_file = test_dir / 'data' / 'streaming_results.xml'
+ with io.open(str(data_file), 'br') as input:
reader = results.ResultsReader(input)
N_results = 0
N_messages = 0
diff --git a/tests/test_kvstore_batch.py b/tests/integration/test_kvstore_batch.py
similarity index 100%
rename from tests/test_kvstore_batch.py
rename to tests/integration/test_kvstore_batch.py
diff --git a/tests/test_kvstore_conf.py b/tests/integration/test_kvstore_conf.py
similarity index 100%
rename from tests/test_kvstore_conf.py
rename to tests/integration/test_kvstore_conf.py
diff --git a/tests/test_kvstore_data.py b/tests/integration/test_kvstore_data.py
similarity index 100%
rename from tests/test_kvstore_data.py
rename to tests/integration/test_kvstore_data.py
diff --git a/tests/test_logger.py b/tests/integration/test_logger.py
similarity index 75%
rename from tests/test_logger.py
rename to tests/integration/test_logger.py
index 46623e363..f67d743a2 100755
--- a/tests/test_logger.py
+++ b/tests/integration/test_logger.py
@@ -16,7 +16,6 @@
from tests import testlib
-from splunklib import client
LEVELS = ["INFO", "WARN", "ERROR", "DEBUG", "CRIT"]
@@ -24,25 +23,25 @@
class LoggerTestCase(testlib.SDKTestCase):
def check_logger(self, logger):
self.check_entity(logger)
- self.assertTrue(logger['level'] in LEVELS)
+ self.assertTrue(logger["level"] in LEVELS)
def test_read(self):
for logger in self.service.loggers.list(count=10):
self.check_logger(logger)
def test_crud(self):
- self.assertTrue('AuditLogger' in self.service.loggers)
- logger = self.service.loggers['AuditLogger']
+ self.assertTrue("AuditLogger" in self.service.loggers)
+ logger = self.service.loggers["AuditLogger"]
- saved = logger['level']
+ saved = logger["level"]
for level in LEVELS:
logger.update(level=level)
logger.refresh()
- self.assertEqual(self.service.loggers['AuditLogger']['level'], level)
+ self.assertEqual(self.service.loggers["AuditLogger"]["level"], level)
logger.update(level=saved)
logger.refresh()
- self.assertEqual(self.service.loggers['AuditLogger']['level'], saved)
+ self.assertEqual(self.service.loggers["AuditLogger"]["level"], saved)
if __name__ == "__main__":
diff --git a/tests/test_macro.py b/tests/integration/test_macro.py
similarity index 100%
rename from tests/test_macro.py
rename to tests/integration/test_macro.py
diff --git a/tests/test_message.py b/tests/integration/test_message.py
similarity index 100%
rename from tests/test_message.py
rename to tests/integration/test_message.py
diff --git a/tests/test_modular_input.py b/tests/integration/test_modular_input.py
similarity index 100%
rename from tests/test_modular_input.py
rename to tests/integration/test_modular_input.py
diff --git a/tests/test_modular_input_kinds.py b/tests/integration/test_modular_input_kinds.py
similarity index 100%
rename from tests/test_modular_input_kinds.py
rename to tests/integration/test_modular_input_kinds.py
diff --git a/tests/test_results.py b/tests/integration/test_results.py
similarity index 100%
rename from tests/test_results.py
rename to tests/integration/test_results.py
diff --git a/tests/test_role.py b/tests/integration/test_role.py
similarity index 100%
rename from tests/test_role.py
rename to tests/integration/test_role.py
diff --git a/tests/test_saved_search.py b/tests/integration/test_saved_search.py
similarity index 100%
rename from tests/test_saved_search.py
rename to tests/integration/test_saved_search.py
diff --git a/tests/test_service.py b/tests/integration/test_service.py
similarity index 100%
rename from tests/test_service.py
rename to tests/integration/test_service.py
diff --git a/tests/test_storage_passwords.py b/tests/integration/test_storage_passwords.py
similarity index 99%
rename from tests/test_storage_passwords.py
rename to tests/integration/test_storage_passwords.py
index bda832dd9..f0ec74c3a 100644
--- a/tests/test_storage_passwords.py
+++ b/tests/integration/test_storage_passwords.py
@@ -21,6 +21,7 @@
class Tests(testlib.SDKTestCase):
def setUp(self):
+ # TODO: why not use super.setUp() ?
self.service = client.connect(**self.opts.kwargs)
self.storage_passwords = self.service.storage_passwords
diff --git a/tests/test_user.py b/tests/integration/test_user.py
similarity index 100%
rename from tests/test_user.py
rename to tests/integration/test_user.py
diff --git a/tests/test_utils.py b/tests/integration/test_utils.py
similarity index 90%
rename from tests/test_utils.py
rename to tests/integration/test_utils.py
index 40ed53197..258208065 100644
--- a/tests/test_utils.py
+++ b/tests/integration/test_utils.py
@@ -1,3 +1,4 @@
+from pathlib import Path
import unittest
import os
from tests import testlib
@@ -92,8 +93,10 @@ def checkFilePermissions(dir_path):
else:
checkFilePermissions(path)
- dir_path = os.path.join('..', 'splunklib')
- checkFilePermissions(dir_path)
+ test_file_path = Path(__file__)
+ # From tests/integration/test_job.py, go up 2 levels to project root, then to splunklib
+ splunklib_path = test_file_path.parent.parent.parent / 'splunklib'
+ checkFilePermissions(str(splunklib_path))
if __name__ == "__main__":
diff --git a/tests/searchcommands/test_apps/eventing_app/bin/eventingcsc.py b/tests/system/test_apps/eventing_app/bin/eventingcsc.py
similarity index 100%
rename from tests/searchcommands/test_apps/eventing_app/bin/eventingcsc.py
rename to tests/system/test_apps/eventing_app/bin/eventingcsc.py
diff --git a/tests/searchcommands/test_apps/eventing_app/default/app.conf b/tests/system/test_apps/eventing_app/default/app.conf
similarity index 100%
rename from tests/searchcommands/test_apps/eventing_app/default/app.conf
rename to tests/system/test_apps/eventing_app/default/app.conf
diff --git a/tests/searchcommands/test_apps/eventing_app/default/commands.conf b/tests/system/test_apps/eventing_app/default/commands.conf
similarity index 100%
rename from tests/searchcommands/test_apps/eventing_app/default/commands.conf
rename to tests/system/test_apps/eventing_app/default/commands.conf
diff --git a/tests/searchcommands/test_apps/eventing_app/metadata/default.meta b/tests/system/test_apps/eventing_app/metadata/default.meta
similarity index 100%
rename from tests/searchcommands/test_apps/eventing_app/metadata/default.meta
rename to tests/system/test_apps/eventing_app/metadata/default.meta
diff --git a/tests/searchcommands/test_apps/generating_app/bin/generatingcsc.py b/tests/system/test_apps/generating_app/bin/generatingcsc.py
similarity index 100%
rename from tests/searchcommands/test_apps/generating_app/bin/generatingcsc.py
rename to tests/system/test_apps/generating_app/bin/generatingcsc.py
diff --git a/tests/searchcommands/test_apps/generating_app/default/app.conf b/tests/system/test_apps/generating_app/default/app.conf
similarity index 100%
rename from tests/searchcommands/test_apps/generating_app/default/app.conf
rename to tests/system/test_apps/generating_app/default/app.conf
diff --git a/tests/searchcommands/test_apps/generating_app/default/commands.conf b/tests/system/test_apps/generating_app/default/commands.conf
similarity index 100%
rename from tests/searchcommands/test_apps/generating_app/default/commands.conf
rename to tests/system/test_apps/generating_app/default/commands.conf
diff --git a/tests/searchcommands/test_apps/generating_app/metadata/default.meta b/tests/system/test_apps/generating_app/metadata/default.meta
similarity index 100%
rename from tests/searchcommands/test_apps/generating_app/metadata/default.meta
rename to tests/system/test_apps/generating_app/metadata/default.meta
diff --git a/tests/searchcommands/test_apps/reporting_app/bin/reportingcsc.py b/tests/system/test_apps/reporting_app/bin/reportingcsc.py
similarity index 100%
rename from tests/searchcommands/test_apps/reporting_app/bin/reportingcsc.py
rename to tests/system/test_apps/reporting_app/bin/reportingcsc.py
diff --git a/tests/searchcommands/test_apps/reporting_app/default/app.conf b/tests/system/test_apps/reporting_app/default/app.conf
similarity index 100%
rename from tests/searchcommands/test_apps/reporting_app/default/app.conf
rename to tests/system/test_apps/reporting_app/default/app.conf
diff --git a/tests/searchcommands/test_apps/reporting_app/default/commands.conf b/tests/system/test_apps/reporting_app/default/commands.conf
similarity index 100%
rename from tests/searchcommands/test_apps/reporting_app/default/commands.conf
rename to tests/system/test_apps/reporting_app/default/commands.conf
diff --git a/tests/searchcommands/test_apps/reporting_app/metadata/default.meta b/tests/system/test_apps/reporting_app/metadata/default.meta
similarity index 100%
rename from tests/searchcommands/test_apps/reporting_app/metadata/default.meta
rename to tests/system/test_apps/reporting_app/metadata/default.meta
diff --git a/tests/searchcommands/test_apps/streaming_app/bin/streamingcsc.py b/tests/system/test_apps/streaming_app/bin/streamingcsc.py
similarity index 100%
rename from tests/searchcommands/test_apps/streaming_app/bin/streamingcsc.py
rename to tests/system/test_apps/streaming_app/bin/streamingcsc.py
diff --git a/tests/searchcommands/test_apps/streaming_app/default/app.conf b/tests/system/test_apps/streaming_app/default/app.conf
similarity index 100%
rename from tests/searchcommands/test_apps/streaming_app/default/app.conf
rename to tests/system/test_apps/streaming_app/default/app.conf
diff --git a/tests/searchcommands/test_apps/streaming_app/default/commands.conf b/tests/system/test_apps/streaming_app/default/commands.conf
similarity index 100%
rename from tests/searchcommands/test_apps/streaming_app/default/commands.conf
rename to tests/system/test_apps/streaming_app/default/commands.conf
diff --git a/tests/searchcommands/test_apps/streaming_app/metadata/default.meta b/tests/system/test_apps/streaming_app/metadata/default.meta
similarity index 100%
rename from tests/searchcommands/test_apps/streaming_app/metadata/default.meta
rename to tests/system/test_apps/streaming_app/metadata/default.meta
diff --git a/tests/searchcommands/test_csc_apps.py b/tests/system/test_csc_apps.py
similarity index 100%
rename from tests/searchcommands/test_csc_apps.py
rename to tests/system/test_csc_apps.py
diff --git a/tests/test_all.py b/tests/test_all.py
deleted file mode 100755
index 55b4d77f5..000000000
--- a/tests/test_all.py
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/usr/bin/env python
-#
-# Copyright © 2011-2023 Splunk, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License"): you may
-# not use this file except in compliance with the License. You may obtain
-# a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-# License for the specific language governing permissions and limitations
-# under the License.
-
-"""Runs all the Splunk SDK for Python unit tests."""
-
-import os
-import unittest
-
-os.chdir(os.path.dirname(os.path.abspath(__file__)))
-suite = unittest.defaultTestLoader.discover('.')
-
-if __name__ == '__main__':
- unittest.TextTestRunner().run(suite)
diff --git a/tests/data/custom_search/hashtags.baseline b/tests/unit/data/custom_search/hashtags.baseline
similarity index 100%
rename from tests/data/custom_search/hashtags.baseline
rename to tests/unit/data/custom_search/hashtags.baseline
diff --git a/tests/data/custom_search/hashtags.in b/tests/unit/data/custom_search/hashtags.in
similarity index 100%
rename from tests/data/custom_search/hashtags.in
rename to tests/unit/data/custom_search/hashtags.in
diff --git a/tests/data/custom_search/multibyte_input.gz b/tests/unit/data/custom_search/multibyte_input.gz
similarity index 100%
rename from tests/data/custom_search/multibyte_input.gz
rename to tests/unit/data/custom_search/multibyte_input.gz
diff --git a/tests/data/custom_search/tophashtags.baseline b/tests/unit/data/custom_search/tophashtags.baseline
similarity index 100%
rename from tests/data/custom_search/tophashtags.baseline
rename to tests/unit/data/custom_search/tophashtags.baseline
diff --git a/tests/data/custom_search/tophashtags.in b/tests/unit/data/custom_search/tophashtags.in
similarity index 100%
rename from tests/data/custom_search/tophashtags.in
rename to tests/unit/data/custom_search/tophashtags.in
diff --git a/tests/data/custom_search/usercount.baseline b/tests/unit/data/custom_search/usercount.baseline
similarity index 100%
rename from tests/data/custom_search/usercount.baseline
rename to tests/unit/data/custom_search/usercount.baseline
diff --git a/tests/data/custom_search/usercount.in b/tests/unit/data/custom_search/usercount.in
similarity index 100%
rename from tests/data/custom_search/usercount.in
rename to tests/unit/data/custom_search/usercount.in
diff --git a/tests/data/custom_search/v1_search_input.gz b/tests/unit/data/custom_search/v1_search_input.gz
similarity index 100%
rename from tests/data/custom_search/v1_search_input.gz
rename to tests/unit/data/custom_search/v1_search_input.gz
diff --git a/tests/data/services.server.info.xml b/tests/unit/data/services.server.info.xml
similarity index 100%
rename from tests/data/services.server.info.xml
rename to tests/unit/data/services.server.info.xml
diff --git a/tests/data/services.xml b/tests/unit/data/services.xml
similarity index 100%
rename from tests/data/services.xml
rename to tests/unit/data/services.xml
diff --git a/tests/modularinput/data/argument_with_defaults.xml b/tests/unit/modularinput/data/argument_with_defaults.xml
similarity index 100%
rename from tests/modularinput/data/argument_with_defaults.xml
rename to tests/unit/modularinput/data/argument_with_defaults.xml
diff --git a/tests/modularinput/data/argument_without_defaults.xml b/tests/unit/modularinput/data/argument_without_defaults.xml
similarity index 100%
rename from tests/modularinput/data/argument_without_defaults.xml
rename to tests/unit/modularinput/data/argument_without_defaults.xml
diff --git a/tests/modularinput/data/conf_with_0_inputs.xml b/tests/unit/modularinput/data/conf_with_0_inputs.xml
similarity index 100%
rename from tests/modularinput/data/conf_with_0_inputs.xml
rename to tests/unit/modularinput/data/conf_with_0_inputs.xml
diff --git a/tests/modularinput/data/conf_with_2_inputs.xml b/tests/unit/modularinput/data/conf_with_2_inputs.xml
similarity index 100%
rename from tests/modularinput/data/conf_with_2_inputs.xml
rename to tests/unit/modularinput/data/conf_with_2_inputs.xml
diff --git a/tests/modularinput/data/conf_with_invalid_inputs.xml b/tests/unit/modularinput/data/conf_with_invalid_inputs.xml
similarity index 100%
rename from tests/modularinput/data/conf_with_invalid_inputs.xml
rename to tests/unit/modularinput/data/conf_with_invalid_inputs.xml
diff --git a/tests/modularinput/data/event_maximal.xml b/tests/unit/modularinput/data/event_maximal.xml
similarity index 100%
rename from tests/modularinput/data/event_maximal.xml
rename to tests/unit/modularinput/data/event_maximal.xml
diff --git a/tests/modularinput/data/event_minimal.xml b/tests/unit/modularinput/data/event_minimal.xml
similarity index 100%
rename from tests/modularinput/data/event_minimal.xml
rename to tests/unit/modularinput/data/event_minimal.xml
diff --git a/tests/modularinput/data/scheme_with_defaults.xml b/tests/unit/modularinput/data/scheme_with_defaults.xml
similarity index 100%
rename from tests/modularinput/data/scheme_with_defaults.xml
rename to tests/unit/modularinput/data/scheme_with_defaults.xml
diff --git a/tests/modularinput/data/scheme_without_defaults.xml b/tests/unit/modularinput/data/scheme_without_defaults.xml
similarity index 100%
rename from tests/modularinput/data/scheme_without_defaults.xml
rename to tests/unit/modularinput/data/scheme_without_defaults.xml
diff --git a/tests/modularinput/data/scheme_without_defaults_and_argument_title.xml b/tests/unit/modularinput/data/scheme_without_defaults_and_argument_title.xml
similarity index 100%
rename from tests/modularinput/data/scheme_without_defaults_and_argument_title.xml
rename to tests/unit/modularinput/data/scheme_without_defaults_and_argument_title.xml
diff --git a/tests/modularinput/data/stream_with_one_event.xml b/tests/unit/modularinput/data/stream_with_one_event.xml
similarity index 100%
rename from tests/modularinput/data/stream_with_one_event.xml
rename to tests/unit/modularinput/data/stream_with_one_event.xml
diff --git a/tests/modularinput/data/stream_with_two_events.xml b/tests/unit/modularinput/data/stream_with_two_events.xml
similarity index 100%
rename from tests/modularinput/data/stream_with_two_events.xml
rename to tests/unit/modularinput/data/stream_with_two_events.xml
diff --git a/tests/modularinput/data/validation.xml b/tests/unit/modularinput/data/validation.xml
similarity index 100%
rename from tests/modularinput/data/validation.xml
rename to tests/unit/modularinput/data/validation.xml
diff --git a/tests/modularinput/data/validation_error.xml b/tests/unit/modularinput/data/validation_error.xml
similarity index 100%
rename from tests/modularinput/data/validation_error.xml
rename to tests/unit/modularinput/data/validation_error.xml
diff --git a/tests/modularinput/modularinput_testlib.py b/tests/unit/modularinput/modularinput_testlib.py
similarity index 100%
rename from tests/modularinput/modularinput_testlib.py
rename to tests/unit/modularinput/modularinput_testlib.py
diff --git a/tests/modularinput/test_event.py b/tests/unit/modularinput/test_event.py
similarity index 98%
rename from tests/modularinput/test_event.py
rename to tests/unit/modularinput/test_event.py
index 35e9c09cd..9ff928da8 100644
--- a/tests/modularinput/test_event.py
+++ b/tests/unit/modularinput/test_event.py
@@ -21,7 +21,7 @@
import pytest
-from tests.modularinput.modularinput_testlib import xml_compare, data_open
+from tests.unit.modularinput.modularinput_testlib import xml_compare, data_open
from splunklib.modularinput.event import Event, ET
from splunklib.modularinput.event_writer import EventWriter
diff --git a/tests/modularinput/test_input_definition.py b/tests/unit/modularinput/test_input_definition.py
similarity index 97%
rename from tests/modularinput/test_input_definition.py
rename to tests/unit/modularinput/test_input_definition.py
index 93601b352..171d792ee 100644
--- a/tests/modularinput/test_input_definition.py
+++ b/tests/unit/modularinput/test_input_definition.py
@@ -14,7 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
-from tests.modularinput.modularinput_testlib import unittest, data_open
+from tests.unit.modularinput.modularinput_testlib import unittest, data_open
from splunklib.modularinput.input_definition import InputDefinition
diff --git a/tests/modularinput/test_scheme.py b/tests/unit/modularinput/test_scheme.py
similarity index 98%
rename from tests/modularinput/test_scheme.py
rename to tests/unit/modularinput/test_scheme.py
index 7e7ddcc9c..01793531d 100644
--- a/tests/modularinput/test_scheme.py
+++ b/tests/unit/modularinput/test_scheme.py
@@ -14,7 +14,7 @@
# under the License.
import xml.etree.ElementTree as ET
-from tests.modularinput.modularinput_testlib import unittest, xml_compare, data_open
+from tests.unit.modularinput.modularinput_testlib import unittest, xml_compare, data_open
from splunklib.modularinput.scheme import Scheme
from splunklib.modularinput.argument import Argument
diff --git a/tests/modularinput/test_script.py b/tests/unit/modularinput/test_script.py
similarity index 99%
rename from tests/modularinput/test_script.py
rename to tests/unit/modularinput/test_script.py
index 49c259725..07d6e2872 100644
--- a/tests/modularinput/test_script.py
+++ b/tests/unit/modularinput/test_script.py
@@ -7,7 +7,7 @@
from splunklib.modularinput import Script, EventWriter, Scheme, Argument, Event
from splunklib.modularinput.utils import xml_compare
-from tests.modularinput.modularinput_testlib import data_open
+from tests.unit.modularinput.modularinput_testlib import data_open
TEST_SCRIPT_PATH = "__IGNORED_SCRIPT_PATH__"
diff --git a/tests/modularinput/test_validation_definition.py b/tests/unit/modularinput/test_validation_definition.py
similarity index 95%
rename from tests/modularinput/test_validation_definition.py
rename to tests/unit/modularinput/test_validation_definition.py
index 1b71a2206..0eaa077fd 100644
--- a/tests/modularinput/test_validation_definition.py
+++ b/tests/unit/modularinput/test_validation_definition.py
@@ -15,7 +15,7 @@
# under the License.
-from tests.modularinput.modularinput_testlib import unittest, data_open
+from tests.unit.modularinput.modularinput_testlib import unittest, data_open
from splunklib.modularinput.validation_definition import ValidationDefinition
diff --git a/tests/searchcommands/__init__.py b/tests/unit/searchcommands/__init__.py
similarity index 100%
rename from tests/searchcommands/__init__.py
rename to tests/unit/searchcommands/__init__.py
diff --git a/tests/unit/searchcommands/apps/app_with_logging_configuration/app.log b/tests/unit/searchcommands/apps/app_with_logging_configuration/app.log
new file mode 100644
index 000000000..a6b2cdba4
--- /dev/null
+++ b/tests/unit/searchcommands/apps/app_with_logging_configuration/app.log
@@ -0,0 +1,9 @@
+2025-07-29 11:22:46,641, Level=WARNING, Pid=74394, File=search_command.py, Line=350, Missing metadata for service creation.
+2025-07-30 09:39:03,387, Level=WARNING, Pid=17670, File=search_command.py, Line=350, Missing metadata for service creation.
+2025-07-31 15:58:07,275, Level=WARNING, Pid=76649, File=search_command.py, Line=350, Missing metadata for service creation.
+2025-08-07 11:32:52,268, Level=WARNING, Pid=99541, File=search_command.py, Line=350, Missing metadata for service creation.
+2025-08-07 11:40:09,926, Level=WARNING, Pid=4064, File=search_command.py, Line=350, Missing metadata for service creation.
+2025-08-07 11:40:37,027, Level=WARNING, Pid=4441, File=search_command.py, Line=350, Missing metadata for service creation.
+2025-08-07 11:47:17,168, Level=WARNING, Pid=8853, File=search_command.py, Line=350, Missing metadata for service creation.
+2025-08-07 11:47:22,006, Level=WARNING, Pid=8872, File=search_command.py, Line=350, Missing metadata for service creation.
+2025-08-07 11:47:26,296, Level=WARNING, Pid=8905, File=search_command.py, Line=350, Missing metadata for service creation.
diff --git a/tests/searchcommands/apps/app_with_logging_configuration/bin/empty-directory b/tests/unit/searchcommands/apps/app_with_logging_configuration/bin/empty-directory
similarity index 100%
rename from tests/searchcommands/apps/app_with_logging_configuration/bin/empty-directory
rename to tests/unit/searchcommands/apps/app_with_logging_configuration/bin/empty-directory
diff --git a/tests/searchcommands/apps/app_with_logging_configuration/default/alternative-logging.conf b/tests/unit/searchcommands/apps/app_with_logging_configuration/default/alternative-logging.conf
similarity index 100%
rename from tests/searchcommands/apps/app_with_logging_configuration/default/alternative-logging.conf
rename to tests/unit/searchcommands/apps/app_with_logging_configuration/default/alternative-logging.conf
diff --git a/tests/searchcommands/apps/app_with_logging_configuration/default/logging.conf b/tests/unit/searchcommands/apps/app_with_logging_configuration/default/logging.conf
similarity index 100%
rename from tests/searchcommands/apps/app_with_logging_configuration/default/logging.conf
rename to tests/unit/searchcommands/apps/app_with_logging_configuration/default/logging.conf
diff --git a/tests/searchcommands/apps/app_with_logging_configuration/logging.conf b/tests/unit/searchcommands/apps/app_with_logging_configuration/logging.conf
similarity index 100%
rename from tests/searchcommands/apps/app_with_logging_configuration/logging.conf
rename to tests/unit/searchcommands/apps/app_with_logging_configuration/logging.conf
diff --git a/tests/searchcommands/apps/app_without_logging_configuration/bin/empty-directory b/tests/unit/searchcommands/apps/app_without_logging_configuration/bin/empty-directory
similarity index 100%
rename from tests/searchcommands/apps/app_without_logging_configuration/bin/empty-directory
rename to tests/unit/searchcommands/apps/app_without_logging_configuration/bin/empty-directory
diff --git a/tests/searchcommands/apps/app_without_logging_configuration/default/empty-directory b/tests/unit/searchcommands/apps/app_without_logging_configuration/default/empty-directory
similarity index 100%
rename from tests/searchcommands/apps/app_without_logging_configuration/default/empty-directory
rename to tests/unit/searchcommands/apps/app_without_logging_configuration/default/empty-directory
diff --git a/tests/searchcommands/chunked_data_stream.py b/tests/unit/searchcommands/chunked_data_stream.py
similarity index 100%
rename from tests/searchcommands/chunked_data_stream.py
rename to tests/unit/searchcommands/chunked_data_stream.py
diff --git a/tests/searchcommands/test_builtin_options.py b/tests/unit/searchcommands/test_builtin_options.py
similarity index 99%
rename from tests/searchcommands/test_builtin_options.py
rename to tests/unit/searchcommands/test_builtin_options.py
index 174baed07..a987683ec 100644
--- a/tests/searchcommands/test_builtin_options.py
+++ b/tests/unit/searchcommands/test_builtin_options.py
@@ -29,7 +29,7 @@
from splunklib.searchcommands.decorators import Configuration
from splunklib.searchcommands.search_command import SearchCommand
-from tests.searchcommands import rebase_environment, package_directory
+from tests.unit.searchcommands import rebase_environment, package_directory
# portable log level names
# https://stackoverflow.com/a/49724281
diff --git a/tests/searchcommands/test_configuration_settings.py b/tests/unit/searchcommands/test_configuration_settings.py
similarity index 100%
rename from tests/searchcommands/test_configuration_settings.py
rename to tests/unit/searchcommands/test_configuration_settings.py
diff --git a/tests/searchcommands/test_decorators.py b/tests/unit/searchcommands/test_decorators.py
similarity index 99%
rename from tests/searchcommands/test_decorators.py
rename to tests/unit/searchcommands/test_decorators.py
index 3cc571dd9..24e9ae364 100755
--- a/tests/searchcommands/test_decorators.py
+++ b/tests/unit/searchcommands/test_decorators.py
@@ -27,7 +27,7 @@
from splunklib.searchcommands.internals import json_encode_string
from splunklib.searchcommands.search_command import SearchCommand
-from tests.searchcommands import rebase_environment
+from tests.unit.searchcommands import rebase_environment
@Configuration()
diff --git a/tests/searchcommands/test_generator_command.py b/tests/unit/searchcommands/test_generator_command.py
similarity index 100%
rename from tests/searchcommands/test_generator_command.py
rename to tests/unit/searchcommands/test_generator_command.py
diff --git a/tests/searchcommands/test_internals_v1.py b/tests/unit/searchcommands/test_internals_v1.py
similarity index 100%
rename from tests/searchcommands/test_internals_v1.py
rename to tests/unit/searchcommands/test_internals_v1.py
diff --git a/tests/searchcommands/test_internals_v2.py b/tests/unit/searchcommands/test_internals_v2.py
similarity index 100%
rename from tests/searchcommands/test_internals_v2.py
rename to tests/unit/searchcommands/test_internals_v2.py
diff --git a/tests/searchcommands/test_multibyte_processing.py b/tests/unit/searchcommands/test_multibyte_processing.py
similarity index 100%
rename from tests/searchcommands/test_multibyte_processing.py
rename to tests/unit/searchcommands/test_multibyte_processing.py
diff --git a/tests/searchcommands/test_reporting_command.py b/tests/unit/searchcommands/test_reporting_command.py
similarity index 100%
rename from tests/searchcommands/test_reporting_command.py
rename to tests/unit/searchcommands/test_reporting_command.py
diff --git a/tests/searchcommands/test_search_command.py b/tests/unit/searchcommands/test_search_command.py
similarity index 60%
rename from tests/searchcommands/test_search_command.py
rename to tests/unit/searchcommands/test_search_command.py
index 849a8888b..9d60de0b8 100755
--- a/tests/searchcommands/test_search_command.py
+++ b/tests/unit/searchcommands/test_search_command.py
@@ -18,18 +18,13 @@
from json.encoder import encode_basestring as encode_string
from unittest import main, TestCase
-import csv
-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
@@ -37,12 +32,14 @@
from splunklib.client import Service
from splunklib.utils import ensure_binary
-from io import StringIO, BytesIO
+from io import BytesIO
def build_command_input(getinfo_metadata, execute_metadata, execute_body):
- input = (f'chunked 1.0,{len(ensure_binary(getinfo_metadata))},0\n{getinfo_metadata}' +
- f'chunked 1.0,{len(ensure_binary(execute_metadata))},{len(ensure_binary(execute_body))}\n{execute_metadata}{execute_body}')
+ input = (
+ f"chunked 1.0,{len(ensure_binary(getinfo_metadata))},0\n{getinfo_metadata}"
+ + f"chunked 1.0,{len(ensure_binary(execute_metadata))},{len(ensure_binary(execute_body))}\n{execute_metadata}{execute_body}"
+ )
ifile = BytesIO(ensure_binary(input))
@@ -58,7 +55,7 @@ class TestCommand(SearchCommand):
def echo(self, records):
for record in records:
- if record.get('action') == 'raise_exception':
+ if record.get("action") == "raise_exception":
raise Exception(self)
yield record
@@ -101,11 +98,15 @@ class TestStreamingCommand(StreamingCommand):
def stream(self, records):
serial_number = 0
for record in records:
- action = record['action']
- if action == 'raise_error':
- raise RuntimeError('Testing')
- value = self.search_results_info if action == 'get_search_results_info' else None
- yield {'_serial': serial_number, 'data': value}
+ action = record["action"]
+ if action == "raise_error":
+ raise RuntimeError("Testing")
+ value = (
+ self.search_results_info
+ if action == "get_search_results_info"
+ else None
+ )
+ yield {"_serial": serial_number, "data": value}
serial_number += 1
@@ -121,7 +122,7 @@ def test_process_scpv2(self):
# 1. Recognize all standard options:
metadata = (
- '{{'
+ "{{"
'"action": "getinfo", "preview": false, "searchinfo": {{'
'"latest_time": "0",'
'"splunk_version": "20150522",'
@@ -134,7 +135,7 @@ def test_process_scpv2(self):
'"show_configuration={show_configuration}",'
'"required_option_1=value_1",'
'"required_option_2=value_2"'
- '],'
+ "],"
'"search": "A%7C%20inputlookup%20tweets%20%7C%20countmatches%20fieldname%3Dword_count%20pattern%3D%22%5Cw%2B%22%20text%20record%3Dt%20%7C%20export%20add_timestamp%3Df%20add_offset%3Dt%20format%3Dcsv%20segmentation%3Draw",'
'"earliest_time": "0",'
'"session_key": "0JbG1fJEvXrL6iYZw9y7tmvd6nHjTKj7ggaE7a4Jv5R0UIbeYJ65kThn^3hiNeoqzMT_LOtLpVR3Y8TIJyr5bkHUElMijYZ8l14wU0L4n^Oa5QxepsZNUIIQCBm^",'
@@ -149,16 +150,19 @@ def test_process_scpv2(self):
'"show_configuration={show_configuration}",'
'"required_option_1=value_1",'
'"required_option_2=value_2"'
- '],'
+ "],"
'"maxresultrows": 10,'
'"command": "countmatches"'
- '}}'
- '}}')
+ "}}"
+ "}}"
+ )
basedir = self._package_directory
- logging_configuration = os.path.join(basedir, 'apps', 'app_with_logging_configuration', 'logging.conf')
- logging_level = 'ERROR'
+ logging_configuration = os.path.join(
+ basedir, "apps", "app_with_logging_configuration", "logging.conf"
+ )
+ logging_level = "ERROR"
record = False
show_configuration = True
@@ -166,19 +170,20 @@ def test_process_scpv2(self):
dispatch_dir=encode_string(""),
logging_configuration=encode_string(logging_configuration)[1:-1],
logging_level=logging_level,
- record=('true' if record is True else 'false'),
- show_configuration=('true' if show_configuration is True else 'false'))
+ record=("true" if record is True else "false"),
+ show_configuration=("true" if show_configuration is True else "false"),
+ )
execute_metadata = '{"action":"execute","finished":true}'
- execute_body = 'test\r\ndata\r\n测试\r\n'
+ execute_body = "test\r\ndata\r\n测试\r\n"
ifile = build_command_input(getinfo_metadata, execute_metadata, execute_body)
command = TestCommand()
result = BytesIO()
- argv = ['some-external-search-command.py']
+ argv = ["some-external-search-command.py"]
- self.assertEqual(command.logging_level, 'WARNING')
+ self.assertEqual(command.logging_level, "WARNING")
self.assertIs(command.record, None)
self.assertIs(command.show_configuration, None)
@@ -186,27 +191,27 @@ def test_process_scpv2(self):
# noinspection PyTypeChecker
command.process(argv, ifile, ofile=result)
except SystemExit as error:
- self.fail('Unexpected exception: {}: {}'.format(type(error).__name__, error))
+ self.fail(
+ "Unexpected exception: {}: {}".format(type(error).__name__, error)
+ )
self.assertEqual(command.logging_configuration, logging_configuration)
- self.assertEqual(command.logging_level, 'ERROR')
+ self.assertEqual(command.logging_level, "ERROR")
self.assertEqual(command.record, record)
self.assertEqual(command.show_configuration, show_configuration)
- self.assertEqual(command.required_option_1, 'value_1')
- self.assertEqual(command.required_option_2, 'value_2')
+ self.assertEqual(command.required_option_1, "value_1")
+ self.assertEqual(command.required_option_2, "value_2")
expected = (
- 'chunked 1.0,68,0\n'
+ "chunked 1.0,68,0\n"
'{"inspector":{"messages":[["INFO","test command configuration: "]]}}'
- 'chunked 1.0,17,32\n'
+ "chunked 1.0,17,32\n"
'{"finished":true}test,__mv_test\r\n'
- 'data,\r\n'
- '测试,\r\n'
+ "data,\r\n"
+ "测试,\r\n"
)
- self.assertEqual(
- expected,
- result.getvalue().decode('utf-8'))
+ self.assertEqual(expected, result.getvalue().decode("utf-8"))
self.assertEqual(command.protocol_version, 2)
@@ -222,43 +227,68 @@ def test_process_scpv2(self):
command_metadata = command.metadata
input_header = command.input_header
- self.assertIsNone(input_header['allowStream'])
- self.assertEqual(input_header['infoPath'], os.path.join(command_metadata.searchinfo.dispatch_dir, 'info.csv'))
- self.assertIsNone(input_header['keywords'])
- self.assertEqual(input_header['preview'], command_metadata.preview)
- self.assertIs(input_header['realtime'], False)
- self.assertEqual(input_header['search'], command_metadata.searchinfo.search)
- self.assertEqual(input_header['sid'], command_metadata.searchinfo.sid)
- self.assertEqual(input_header['splunkVersion'], command_metadata.searchinfo.splunk_version)
- self.assertIsNone(input_header['truncated'])
-
- self.assertEqual(command_metadata.preview, input_header['preview'])
- self.assertEqual(command_metadata.searchinfo.app, 'searchcommands_app')
- self.assertEqual(command_metadata.searchinfo.args,
- ['logging_configuration=' + logging_configuration, 'logging_level=ERROR', 'record=false',
- 'show_configuration=true', 'required_option_1=value_1', 'required_option_2=value_2'])
- self.assertEqual(command_metadata.searchinfo.dispatch_dir, os.path.dirname(input_header['infoPath']))
+ self.assertIsNone(input_header["allowStream"])
+ self.assertEqual(
+ input_header["infoPath"],
+ os.path.join(command_metadata.searchinfo.dispatch_dir, "info.csv"),
+ )
+ self.assertIsNone(input_header["keywords"])
+ self.assertEqual(input_header["preview"], command_metadata.preview)
+ self.assertIs(input_header["realtime"], False)
+ self.assertEqual(input_header["search"], command_metadata.searchinfo.search)
+ self.assertEqual(input_header["sid"], command_metadata.searchinfo.sid)
+ self.assertEqual(
+ input_header["splunkVersion"], command_metadata.searchinfo.splunk_version
+ )
+ self.assertIsNone(input_header["truncated"])
+
+ self.assertEqual(command_metadata.preview, input_header["preview"])
+ self.assertEqual(command_metadata.searchinfo.app, "searchcommands_app")
+ self.assertEqual(
+ command_metadata.searchinfo.args,
+ [
+ "logging_configuration=" + logging_configuration,
+ "logging_level=ERROR",
+ "record=false",
+ "show_configuration=true",
+ "required_option_1=value_1",
+ "required_option_2=value_2",
+ ],
+ )
+ self.assertEqual(
+ command_metadata.searchinfo.dispatch_dir,
+ os.path.dirname(input_header["infoPath"]),
+ )
self.assertEqual(command_metadata.searchinfo.earliest_time, 0.0)
self.assertEqual(command_metadata.searchinfo.latest_time, 0.0)
- self.assertEqual(command_metadata.searchinfo.owner, 'admin')
- self.assertEqual(command_metadata.searchinfo.raw_args, command_metadata.searchinfo.args)
- self.assertEqual(command_metadata.searchinfo.search,
- 'A| inputlookup tweets | countmatches fieldname=word_count pattern="\\w+" text record=t | export add_timestamp=f add_offset=t format=csv segmentation=raw')
- self.assertEqual(command_metadata.searchinfo.session_key,
- '0JbG1fJEvXrL6iYZw9y7tmvd6nHjTKj7ggaE7a4Jv5R0UIbeYJ65kThn^3hiNeoqzMT_LOtLpVR3Y8TIJyr5bkHUElMijYZ8l14wU0L4n^Oa5QxepsZNUIIQCBm^')
- self.assertEqual(command_metadata.searchinfo.sid, '1433261372.158')
- self.assertEqual(command_metadata.searchinfo.splunk_version, '20150522')
- self.assertEqual(command_metadata.searchinfo.splunkd_uri, 'https://127.0.0.1:8089')
- self.assertEqual(command_metadata.searchinfo.username, 'admin')
+ self.assertEqual(command_metadata.searchinfo.owner, "admin")
+ self.assertEqual(
+ command_metadata.searchinfo.raw_args, command_metadata.searchinfo.args
+ )
+ self.assertEqual(
+ command_metadata.searchinfo.search,
+ 'A| inputlookup tweets | countmatches fieldname=word_count pattern="\\w+" text record=t | export add_timestamp=f add_offset=t format=csv segmentation=raw',
+ )
+ self.assertEqual(
+ command_metadata.searchinfo.session_key,
+ "0JbG1fJEvXrL6iYZw9y7tmvd6nHjTKj7ggaE7a4Jv5R0UIbeYJ65kThn^3hiNeoqzMT_LOtLpVR3Y8TIJyr5bkHUElMijYZ8l14wU0L4n^Oa5QxepsZNUIIQCBm^",
+ )
+ self.assertEqual(command_metadata.searchinfo.sid, "1433261372.158")
+ self.assertEqual(command_metadata.searchinfo.splunk_version, "20150522")
+ self.assertEqual(
+ command_metadata.searchinfo.splunkd_uri, "https://127.0.0.1:8089"
+ )
+ self.assertEqual(command_metadata.searchinfo.username, "admin")
self.assertEqual(command_metadata.searchinfo.maxresultrows, 10)
- self.assertEqual(command_metadata.searchinfo.command, 'countmatches')
-
+ self.assertEqual(command_metadata.searchinfo.command, "countmatches")
self.maxDiff = None
self.assertIsInstance(command.service, Service)
- self.assertEqual(command.service.authority, command_metadata.searchinfo.splunkd_uri)
+ self.assertEqual(
+ command.service.authority, command_metadata.searchinfo.splunkd_uri
+ )
self.assertEqual(command.service.token, command_metadata.searchinfo.session_key)
self.assertEqual(command.service.namespace.app, command.metadata.searchinfo.app)
self.assertIsNone(command.service.namespace.owner)
@@ -268,6 +298,7 @@ def test_process_scpv2(self):
_package_directory = os.path.dirname(os.path.abspath(__file__))
+
class TestSearchCommandService(TestCase):
def setUp(self):
TestCase.setUp(self)
@@ -284,36 +315,54 @@ 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:
+ 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))
+ 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:
+ 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))
-
+ 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:
+ 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))
-
-
+ 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",
- })})
+ 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()
diff --git a/tests/searchcommands/test_streaming_command.py b/tests/unit/searchcommands/test_streaming_command.py
similarity index 100%
rename from tests/searchcommands/test_streaming_command.py
rename to tests/unit/searchcommands/test_streaming_command.py
diff --git a/tests/searchcommands/test_validators.py b/tests/unit/searchcommands/test_validators.py
similarity index 100%
rename from tests/searchcommands/test_validators.py
rename to tests/unit/searchcommands/test_validators.py
diff --git a/tests/test_data.py b/tests/unit/test_data.py
similarity index 52%
rename from tests/test_data.py
rename to tests/unit/test_data.py
index b2bd01588..7fb24f967 100755
--- a/tests/test_data.py
+++ b/tests/unit/test_data.py
@@ -18,135 +18,160 @@
from os import path
import xml.etree.ElementTree as et
-from tests import testlib
+import unittest
from splunklib import data
-class DataTestCase(testlib.SDKTestCase):
+class DataTestCase(unittest.TestCase):
def test_elems(self):
result = data.load("")
self.assertTrue(result is None)
result = data.load("")
- self.assertEqual(result, {'a': None})
+ self.assertEqual(result, {"a": None})
result = data.load("1")
- self.assertEqual(result, {'a': "1"})
+ self.assertEqual(result, {"a": "1"})
result = data.load("")
- self.assertEqual(result, {'a': {'b': None}})
+ self.assertEqual(result, {"a": {"b": None}})
result = data.load("1")
- self.assertEqual(result, {'a': {'b': '1'}})
+ self.assertEqual(result, {"a": {"b": "1"}})
result = data.load("")
- self.assertEqual(result, {'a': {'b': [None, None]}})
+ self.assertEqual(result, {"a": {"b": [None, None]}})
result = data.load("12")
- self.assertEqual(result, {'a': {'b': ['1', '2']}})
+ self.assertEqual(result, {"a": {"b": ["1", "2"]}})
result = data.load("")
- self.assertEqual(result, {'a': {'b': None, 'c': None}})
+ self.assertEqual(result, {"a": {"b": None, "c": None}})
result = data.load("12")
- self.assertEqual(result, {'a': {'b': '1', 'c': '2'}})
+ self.assertEqual(result, {"a": {"b": "1", "c": "2"}})
result = data.load("1")
- self.assertEqual(result, {'a': {'b': {'c': '1'}}})
+ self.assertEqual(result, {"a": {"b": {"c": "1"}}})
result = data.load("12")
- self.assertEqual(result, {'a': {'b': [{'c': '1'}, '2']}})
+ self.assertEqual(result, {"a": {"b": [{"c": "1"}, "2"]}})
- result = data.load('alphabeta')
- self.assertEqual(result, {'e': {'a1': ['alpha', 'beta']}})
+ result = data.load("alphabeta")
+ self.assertEqual(result, {"e": {"a1": ["alpha", "beta"]}})
result = data.load("v2")
- self.assertEqual(result, {'e': {'a1': ['v2', 'v1']}})
+ self.assertEqual(result, {"e": {"a1": ["v2", "v1"]}})
def test_attrs(self):
result = data.load("")
- self.assertEqual(result, {'e': {'a1': 'v1'}})
+ self.assertEqual(result, {"e": {"a1": "v1"}})
result = data.load("")
- self.assertEqual(result, {'e': {'a1': 'v1', 'a2': 'v2'}})
+ self.assertEqual(result, {"e": {"a1": "v1", "a2": "v2"}})
result = data.load("v2")
- self.assertEqual(result, {'e': {'$text': 'v2', 'a1': 'v1'}})
+ self.assertEqual(result, {"e": {"$text": "v2", "a1": "v1"}})
result = data.load("2")
- self.assertEqual(result, {'e': {'a1': 'v1', 'b': '2'}})
+ self.assertEqual(result, {"e": {"a1": "v1", "b": "2"}})
result = data.load("v2bv2")
- self.assertEqual(result, {'e': {'a1': 'v1', 'b': 'bv2'}})
+ self.assertEqual(result, {"e": {"a1": "v1", "b": "bv2"}})
result = data.load("v2")
- self.assertEqual(result, {'e': {'a1': ['v2', 'v1']}})
+ self.assertEqual(result, {"e": {"a1": ["v2", "v1"]}})
result = data.load("v2")
- self.assertEqual(result,
- {'e1': {'a1': 'v1', 'e2': {'$text': 'v2', 'a1': 'v1'}}})
+ self.assertEqual(
+ result, {"e1": {"a1": "v1", "e2": {"$text": "v2", "a1": "v1"}}}
+ )
def test_real(self):
"""Test some real Splunk response examples."""
testpath = path.dirname(path.abspath(__file__))
- fh = open(path.join(testpath, "data/services.xml"), 'r')
+ fh = open(path.join(testpath, "data/services.xml"), "r")
result = data.load(fh.read())
- self.assertTrue('feed' in result)
- self.assertTrue('author' in result.feed)
- self.assertTrue('entry' in result.feed)
+ self.assertTrue("feed" in result)
+ self.assertTrue("author" in result.feed)
+ self.assertTrue("entry" in result.feed)
titles = [item.title for item in result.feed.entry]
self.assertEqual(
titles,
- ['alerts', 'apps', 'authentication', 'authorization', 'data',
- 'deployment', 'licenser', 'messages', 'configs', 'saved',
- 'scheduled', 'search', 'server', 'streams', 'broker', 'clustering',
- 'masterlm'])
-
- fh = open(path.join(testpath, "data/services.server.info.xml"), 'r')
+ [
+ "alerts",
+ "apps",
+ "authentication",
+ "authorization",
+ "data",
+ "deployment",
+ "licenser",
+ "messages",
+ "configs",
+ "saved",
+ "scheduled",
+ "search",
+ "server",
+ "streams",
+ "broker",
+ "clustering",
+ "masterlm",
+ ],
+ )
+
+ fh = open(path.join(testpath, "data/services.server.info.xml"), "r")
result = data.load(fh.read())
- self.assertTrue('feed' in result)
- self.assertTrue('author' in result.feed)
- self.assertTrue('entry' in result.feed)
- self.assertEqual(result.feed.title, 'server-info')
- self.assertEqual(result.feed.author.name, 'Splunk')
- self.assertEqual(result.feed.entry.content.cpu_arch, 'i386')
- self.assertEqual(result.feed.entry.content.os_name, 'Darwin')
- self.assertEqual(result.feed.entry.content.os_version, '10.8.0')
+ self.assertTrue("feed" in result)
+ self.assertTrue("author" in result.feed)
+ self.assertTrue("entry" in result.feed)
+ self.assertEqual(result.feed.title, "server-info")
+ self.assertEqual(result.feed.author.name, "Splunk")
+ self.assertEqual(result.feed.entry.content.cpu_arch, "i386")
+ self.assertEqual(result.feed.entry.content.os_name, "Darwin")
+ self.assertEqual(result.feed.entry.content.os_version, "10.8.0")
def test_invalid(self):
if sys.version_info[1] >= 7:
self.assertRaises(et.ParseError, data.load, "")
else:
from xml.etree.ElementTree import ParseError
+
self.assertRaises(ParseError, data.load, "")
self.assertRaises(KeyError, data.load, "a")
def test_dict(self):
- result = data.load("""
+ result = data.load(
+ """
- """)
+ """
+ )
self.assertEqual(result, {})
- result = data.load("""
+ result = data.load(
+ """
v1
v2
- """)
- self.assertEqual(result, {'n1': "v1", 'n2': "v2"})
+ """
+ )
+ self.assertEqual(result, {"n1": "v1", "n2": "v2"})
- result = data.load("""
+ result = data.load(
+ """
v1
v2
- """)
- self.assertEqual(result, {'content': {'n1': "v1", 'n2': "v2"}})
+ """
+ )
+ self.assertEqual(result, {"content": {"n1": "v1", "n2": "v2"}})
- result = data.load("""
+ result = data.load(
+ """
@@ -160,11 +185,14 @@ def test_dict(self):
- """)
- self.assertEqual(result,
- {'content': {'n1': {'n1n1': "n1v1"}, 'n2': {'n2n1': "n2v1"}}})
+ """
+ )
+ self.assertEqual(
+ result, {"content": {"n1": {"n1n1": "n1v1"}, "n2": {"n2n1": "n2v1"}}}
+ )
- result = data.load("""
+ result = data.load(
+ """
@@ -173,29 +201,34 @@ def test_dict(self):
- """)
- self.assertEqual(result,
- {'content': {'n1': ['1', '2', '3', '4']}})
+ """
+ )
+ self.assertEqual(result, {"content": {"n1": ["1", "2", "3", "4"]}})
def test_list(self):
result = data.load("""
""")
self.assertEqual(result, [])
- result = data.load("""
+ result = data.load(
+ """
- 1
- 2
- 3
- 4
-
""")
- self.assertEqual(result, ['1', '2', '3', '4'])
+ """
+ )
+ self.assertEqual(result, ["1", "2", "3", "4"])
- result = data.load("""
+ result = data.load(
+ """
- 1
- 2
- 3
- 4
- """)
- self.assertEqual(result, {'content': ['1', '2', '3', '4']})
+ """
+ )
+ self.assertEqual(result, {"content": ["1", "2", "3", "4"]})
- result = data.load("""
+ result = data.load(
+ """
-
@@ -205,10 +238,12 @@ def test_list(self):
- 3
- 4
- """)
- self.assertEqual(result, {'content': [['1', '2'], ['3', '4']]})
+ """
+ )
+ self.assertEqual(result, {"content": [["1", "2"], ["3", "4"]]})
- result = data.load("""
+ result = data.load(
+ """
- v1
@@ -216,34 +251,36 @@ def test_list(self):
- v3
- v4
- """)
- self.assertEqual(result,
- {'content': [{'n1': "v1"}, {'n2': "v2"}, {'n3': "v3"}, {'n4': "v4"}]})
+ """
+ )
+ self.assertEqual(
+ result,
+ {"content": [{"n1": "v1"}, {"n2": "v2"}, {"n3": "v3"}, {"n4": "v4"}]},
+ )
- result = data.load("""
+ result = data.load(
+ """
101089
i386
0
- """)
- self.assertEqual(result,
- {'build': '101089', 'cpu_arch': 'i386', 'isFree': '0'})
+ """
+ )
+ self.assertEqual(result, {"build": "101089", "cpu_arch": "i386", "isFree": "0"})
def test_record(self):
d = data.record()
- d.update({'foo': 5,
- 'bar.baz': 6,
- 'bar.qux': 7,
- 'bar.zrp.meep': 8,
- 'bar.zrp.peem': 9})
- self.assertEqual(d['foo'], 5)
- self.assertEqual(d['bar.baz'], 6)
- self.assertEqual(d['bar'], {'baz': 6, 'qux': 7, 'zrp': {'meep': 8, 'peem': 9}})
+ d.update(
+ {"foo": 5, "bar.baz": 6, "bar.qux": 7, "bar.zrp.meep": 8, "bar.zrp.peem": 9}
+ )
+ self.assertEqual(d["foo"], 5)
+ self.assertEqual(d["bar.baz"], 6)
+ self.assertEqual(d["bar"], {"baz": 6, "qux": 7, "zrp": {"meep": 8, "peem": 9}})
self.assertEqual(d.foo, 5)
self.assertEqual(d.bar.baz, 6)
- self.assertEqual(d.bar, {'baz': 6, 'qux': 7, 'zrp': {'meep': 8, 'peem': 9}})
- self.assertRaises(KeyError, d.__getitem__, 'boris')
+ self.assertEqual(d.bar, {"baz": 6, "qux": 7, "zrp": {"meep": 8, "peem": 9}})
+ self.assertRaises(KeyError, d.__getitem__, "boris")
if __name__ == "__main__":