Skip to content
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
2 changes: 1 addition & 1 deletion splunklib/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def read(self, n=None):


@deprecation.deprecated(
details="Use the JSONResultsReader function instead in conjuction with the 'output_mode' query param set to 'json'"
details="Use the JSONResultsReader function instead in conjunction with the 'output_mode' query param set to 'json'"
)
class ResultsReader:
"""This class returns dictionaries and Splunk messages from an XML results
Expand Down
69 changes: 38 additions & 31 deletions tests/integration/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from splunklib.binding import _log_duration, HTTPError

import pytest
import warnings


class TestUtilities(testlib.SDKTestCase):
Expand Down Expand Up @@ -446,22 +447,25 @@ def test_results_reader(self):
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
N_messages = 0
for r in reader:
from collections import OrderedDict

self.assertTrue(
isinstance(r, OrderedDict) or isinstance(r, results.Message)
)
if isinstance(r, OrderedDict):
N_results += 1
elif isinstance(r, results.Message):
N_messages += 1
self.assertEqual(N_results, 4999)
self.assertEqual(N_messages, 2)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)

reader = results.ResultsReader(input)
self.assertFalse(reader.is_preview)
N_results = 0
N_messages = 0
for r in reader:
from collections import OrderedDict

self.assertTrue(
isinstance(r, OrderedDict) or isinstance(r, results.Message)
)
if isinstance(r, OrderedDict):
N_results += 1
elif isinstance(r, results.Message):
N_messages += 1
self.assertEqual(N_results, 4999)
self.assertEqual(N_messages, 2)

def test_results_reader_with_streaming_results(self):
# Run jobs.export("search index=_internal | stats count",
Expand All @@ -470,21 +474,24 @@ def test_results_reader_with_streaming_results(self):
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
for r in reader:
from collections import OrderedDict

self.assertTrue(
isinstance(r, OrderedDict) or isinstance(r, results.Message)
)
if isinstance(r, OrderedDict):
N_results += 1
elif isinstance(r, results.Message):
N_messages += 1
self.assertEqual(N_results, 3)
self.assertEqual(N_messages, 3)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)

reader = results.ResultsReader(input)
N_results = 0
N_messages = 0
for r in reader:
from collections import OrderedDict

self.assertTrue(
isinstance(r, OrderedDict) or isinstance(r, results.Message)
)
if isinstance(r, OrderedDict):
N_results += 1
elif isinstance(r, results.Message):
N_messages += 1
self.assertEqual(N_results, 3)
self.assertEqual(N_messages, 3)

def test_xmldtd_filter(self):
s = results._XMLDTDFilter(
Expand Down
9 changes: 6 additions & 3 deletions tests/integration/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from time import sleep
from tests import testlib
from splunklib import results
import warnings


class ResultsTestCase(testlib.SDKTestCase):
Expand Down Expand Up @@ -169,9 +170,11 @@ def test_read_raw_field_with_segmentation(self):
self.assert_parsed_results_equals(xml_text, expected_results)

def assert_parsed_results_equals(self, xml_text, expected_results):
results_reader = results.ResultsReader(BytesIO(xml_text.encode("utf-8")))
actual_results = list(results_reader)
self.assertEqual(expected_results, actual_results)
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
results_reader = results.ResultsReader(BytesIO(xml_text.encode("utf-8")))
actual_results = list(results_reader)
self.assertEqual(expected_results, actual_results)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/searchcommands/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


@Configuration()
class TestSearchCommand(SearchCommand):
class SearchCommandForTests(SearchCommand):
boolean = Option(
doc="""
**Syntax:** **boolean=***<value>*
Expand Down Expand Up @@ -399,7 +399,7 @@ def test_option(self):
'show_configuration="f"',
]

command = TestSearchCommand()
command = SearchCommandForTests()
options = command.options

options.reset()
Expand Down
16 changes: 12 additions & 4 deletions tests/unit/searchcommands/test_internals_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import random
import sys
import warnings

import pytest
from sys import float_info
Expand Down Expand Up @@ -185,10 +186,14 @@ def test_record_writer_with_random_data(self, save_recording=False):
writer.write_metric(name, metric)

self.assertEqual(writer._chunk_count, 0)
self.assertEqual(writer._record_count, 31)

with warnings.catch_warnings():
warnings.simplefilter("ignore", PendingDeprecationWarning)
self.assertEqual(writer._total_record_count, 0)
self.assertEqual(writer._record_count, 31)

self.assertEqual(writer.pending_record_count, 31)
self.assertGreater(writer._buffer.tell(), 0)
self.assertEqual(writer._total_record_count, 0)
self.assertEqual(writer.committed_record_count, 0)
fieldnames.sort()
writer._fieldnames.sort()
Expand All @@ -204,12 +209,15 @@ def test_record_writer_with_random_data(self, save_recording=False):

writer.flush(finished=True)

with warnings.catch_warnings():
warnings.simplefilter("ignore", PendingDeprecationWarning)
self.assertEqual(writer._record_count, 0)
self.assertEqual(writer._total_record_count, 31)

self.assertEqual(writer._chunk_count, 1)
self.assertEqual(writer._record_count, 0)
self.assertEqual(writer.pending_record_count, 0)
self.assertEqual(writer._buffer.tell(), 0)
self.assertEqual(writer._buffer.getvalue(), "")
self.assertEqual(writer._total_record_count, 31)
self.assertEqual(writer.committed_record_count, 31)

self.assertRaises(AssertionError, writer.flush, finished=True, partial=True)
Expand Down
14 changes: 9 additions & 5 deletions tests/unit/searchcommands/test_search_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import os
import logging
import warnings

from io import TextIOWrapper

Expand Down Expand Up @@ -49,7 +50,7 @@ def build_command_input(getinfo_metadata, execute_metadata, execute_body):


@Configuration()
class TestCommand(SearchCommand):
class CommandForTests(SearchCommand):
required_option_1 = Option(require=True)
required_option_2 = Option(require=True)

Expand Down Expand Up @@ -160,7 +161,7 @@ def test_process_scpv2(self):

ifile = build_command_input(getinfo_metadata, execute_metadata, execute_body)

command = TestCommand()
command = CommandForTests()
result = BytesIO()
argv = ["some-external-search-command.py"]

Expand All @@ -184,8 +185,8 @@ def test_process_scpv2(self):
self.assertEqual(command.required_option_2, "value_2")

expected = (
"chunked 1.0,68,0\n"
'{"inspector":{"messages":[["INFO","test command configuration: "]]}}'
"chunked 1.0,79,0\n"
'{"inspector":{"messages":[["INFO","commandfortests command configuration: "]]}}'
"chunked 1.0,17,32\n"
'{"finished":true}test,__mv_test\r\n'
"data,\r\n"
Expand All @@ -206,7 +207,10 @@ def test_process_scpv2(self):
self.assertEqual([], command.fieldnames)

command_metadata = command.metadata
input_header = command.input_header

with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
input_header = command.input_header

self.assertIsNone(input_header["allowStream"])
self.assertEqual(
Expand Down