Skip to content

Commit 68d2532

Browse files
committed
Python 3.x compatibility for test_search_command
1 parent f273708 commit 68d2532

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

tests/searchcommands/test_search_command.py

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
from splunklib.searchcommands.search_command import SearchCommand
2424
from splunklib.client import Service
2525

26-
from splunklib.six.moves import StringIO
26+
from splunklib.six import StringIO, BytesIO
2727
from splunklib.six.moves import zip as izip
2828
from json.encoder import encode_basestring as encode_string
2929
from unittest import main, TestCase
3030

3131
import csv
32+
import codecs
3233
import os
3334
import re
3435

@@ -123,17 +124,17 @@ def test_process_scpv1(self):
123124

124125
argv = ['test.py', 'not__GETINFO__or__EXECUTE__', 'option=value', 'fieldname']
125126
command = TestCommand()
126-
result = StringIO()
127+
result = BytesIO()
127128

128129
self.assertRaises(SystemExit, command.process, argv, ofile=result)
129-
self.assertRegexpMatches(result.getvalue(), expected)
130+
self.assertRegexpMatches(result.getvalue().decode('UTF-8'), expected)
130131

131132
# TestCommand.process should return configuration settings on Getinfo probe
132133

133134
argv = ['test.py', '__GETINFO__', 'required_option_1=value', 'required_option_2=value']
134135
command = TestCommand()
135136
ifile = StringIO('\n')
136-
result = StringIO()
137+
result = BytesIO()
137138

138139
self.assertEqual(str(command.configuration), '')
139140

@@ -157,12 +158,12 @@ def test_process_scpv1(self):
157158
# noinspection PyTypeChecker
158159
command.process(argv, ifile, ofile=result)
159160
except BaseException as error:
160-
self.fail('{0}: {1}: {2}\n'.format(type(error).__name__, error, result.getvalue()))
161+
self.fail('{0}: {1}: {2}\n'.format(type(error).__name__, error, result.getvalue().decode('UTF-8')))
161162

162-
self.assertEqual('\r\n\r\n\r\n', result.getvalue()) # No message header and no configuration settings
163+
self.assertEqual('\r\n\r\n\r\n', result.getvalue().decode('UTF-8')) # No message header and no configuration settings
163164

164165
ifile = StringIO('\n')
165-
result = StringIO()
166+
result = BytesIO()
166167

167168
# We might also put this sort of code into our SearchCommand.prepare override ...
168169

@@ -217,10 +218,10 @@ def test_process_scpv1(self):
217218
# noinspection PyTypeChecker
218219
command.process(argv, ifile, ofile=result)
219220
except BaseException as error:
220-
self.fail('{0}: {1}: {2}\n'.format(type(error).__name__, error, result.getvalue()))
221+
self.fail('{0}: {1}: {2}\n'.format(type(error).__name__, error, result.getvalue().decode('UTF-8')))
221222

222223
result.seek(0)
223-
reader = csv.reader(result)
224+
reader = csv.reader(codecs.iterdecode(result, 'UTF-8'))
224225
self.assertEqual([], next(reader))
225226
observed = dict(izip(next(reader), next(reader)))
226227
self.assertRaises(StopIteration, lambda: next(reader))
@@ -249,52 +250,52 @@ def test_process_scpv1(self):
249250

250251
command = TestCommand()
251252
ifile = StringIO('\n')
252-
result = StringIO()
253+
result = BytesIO()
253254

254255
self.assertRaises(SystemExit, command.process, argv, ifile, ofile=result)
255256
self.assertTrue(
256257
'error_message=Unrecognized test command option: undefined_option="value"\r\n\r\n',
257-
result.getvalue())
258+
result.getvalue().decode('UTF-8'))
258259

259260
# TestCommand.process should produce an error record when required options are missing
260261

261262
argv = ['test.py', action, 'required_option_2=value', 'fieldname_1']
262263
command = TestCommand()
263264
ifile = StringIO('\n')
264-
result = StringIO()
265+
result = BytesIO()
265266

266267
self.assertRaises(SystemExit, command.process, argv, ifile, ofile=result)
267268

268269
self.assertTrue(
269270
'error_message=A value for test command option required_option_1 is required\r\n\r\n',
270-
result.getvalue())
271+
result.getvalue().decode('UTF-8'))
271272

272273
argv = ['test.py', action, 'fieldname_1']
273274
command = TestCommand()
274275
ifile = StringIO('\n')
275-
result = StringIO()
276+
result = BytesIO()
276277

277278
self.assertRaises(SystemExit, command.process, argv, ifile, ofile=result)
278279

279280
self.assertTrue(
280281
'error_message=Values for these test command options are required: required_option_1, required_option_2'
281282
'\r\n\r\n',
282-
result.getvalue())
283+
result.getvalue().decode('UTF-8'))
283284

284285
# TestStreamingCommand.process should exit on processing exceptions
285286

286287
ifile = StringIO('\naction\r\nraise_error\r\n')
287288
argv = ['test.py', '__EXECUTE__']
288289
command = TestStreamingCommand()
289-
result = StringIO()
290+
result = BytesIO()
290291

291292
try:
292293
# noinspection PyTypeChecker
293294
command.process(argv, ifile, ofile=result)
294295
except SystemExit as error:
295296
self.assertNotEqual(error.code, 0)
296297
self.assertRegexpMatches(
297-
result.getvalue(),
298+
result.getvalue().decode('UTF-8'),
298299
r'^error_message=RuntimeError at ".+", line \d+ : Testing\r\n\r\n$')
299300
except BaseException as error:
300301
self.fail('Expected SystemExit, but caught {}: {}'.format(type(error).__name__, error))
@@ -309,7 +310,7 @@ def test_process_scpv1(self):
309310
ifile = StringIO('infoPath:' + info_path + '\n\naction\r\nget_search_results_info\r\n')
310311
argv = ['test.py', '__EXECUTE__']
311312
command = TestStreamingCommand()
312-
result = StringIO()
313+
result = BytesIO()
313314

314315
try:
315316
# noinspection PyTypeChecker
@@ -318,14 +319,14 @@ def test_process_scpv1(self):
318319
self.fail('Expected no exception, but caught {}: {}'.format(type(error).__name__, error))
319320
else:
320321
self.assertRegexpMatches(
321-
result.getvalue(),
322+
result.getvalue().decode('UTF-8'),
322323
r'^\r\n'
323324
r'('
324325
r'data,__mv_data,_serial,__mv__serial\r\n'
325-
r'"\{.*u\'is_summary_index\': 0, .+\}",,0,'
326+
r'\"\{.*u\'is_summary_index\': 0, .+\}\",,0,'
326327
r'|'
327328
r'_serial,__mv__serial,data,__mv_data\r\n'
328-
r'0,,"\{.*u\'is_summary_index\': 0, .+\}",'
329+
r'0,,\"\{.*\'is_summary_index\': 0, .+\}\",'
329330
r')'
330331
r'\r\n$'
331332
)
@@ -434,7 +435,7 @@ def test_process_scpv2(self):
434435
'chunked 1.0,{},{}\n{}{}'.format(len(execute_metadata), len(execute_body), execute_metadata, execute_body))
435436

436437
command = TestCommand()
437-
result = StringIO()
438+
result = BytesIO()
438439
argv = ['some-external-search-command.py']
439440

440441
self.assertEqual(command.logging_level, 'WARNING')
@@ -460,7 +461,7 @@ def test_process_scpv2(self):
460461
'chunked 1.0,17,23\n'
461462
'{"finished":true}test,__mv_test\r\n'
462463
'data,\r\n',
463-
result.getvalue())
464+
result.getvalue().decode('utf-8'))
464465

465466
self.assertEqual(command.protocol_version, 2)
466467

@@ -626,7 +627,7 @@ def test_process_scpv2(self):
626627
'chunked 1.0,{},{}\n{}{}'.format(len(execute_metadata), len(execute_body), execute_metadata, execute_body))
627628

628629
command = TestCommand()
629-
result = StringIO()
630+
result = BytesIO()
630631
argv = ['test.py']
631632

632633
# noinspection PyTypeChecker
@@ -645,7 +646,7 @@ def test_process_scpv2(self):
645646
'["ERROR","Illegal value: show_configuration=Non-boolean value"]]}}\n'
646647
'chunked 1.0,17,0\n'
647648
'{"finished":true}',
648-
result.getvalue())
649+
result.getvalue().decode('utf-8'))
649650

650651
self.assertEqual(command.protocol_version, 2)
651652

@@ -672,17 +673,17 @@ def test_process_scpv2(self):
672673
'chunked 1.0,{},{}\n{}{}'.format(len(execute_metadata), len(execute_body), execute_metadata, execute_body))
673674

674675
command = TestCommand()
675-
result = StringIO()
676+
result = BytesIO()
676677
argv = ['test.py']
677678

678679
try:
679680
command.process(argv, ifile, ofile=result)
680681
except SystemExit as error:
681682
self.assertNotEqual(0, error.code)
682683
except BaseException as error:
683-
self.fail('{0}: {1}: {2}\n'.format(type(error).__name__, error, result.getvalue()))
684+
self.fail('{0}: {1}: {2}\n'.format(type(error).__name__, error, result.getvalue().decode('utf-8')))
684685
else:
685-
self.fail('Expected SystemExit, not a return from TestCommand.process: {}\n'.format(result.getvalue()))
686+
self.fail('Expected SystemExit, not a return from TestCommand.process: {}\n'.format(result.getvalue().decode('utf-8')))
686687

687688
self.assertEqual(command.logging_configuration, logging_configuration)
688689
self.assertEqual(command.logging_level, logging_level)
@@ -691,15 +692,21 @@ def test_process_scpv2(self):
691692
self.assertEqual(command.required_option_1, 'value_1')
692693
self.assertEqual(command.required_option_2, 'value_2')
693694

694-
finished = r'"finished":true'
695+
finished = r'\"finished\":true'
695696

696-
inspector = \
697-
r'"inspector":\{"messages":\[\["ERROR","StandardError at \\".+\\", line \d+ : test ' \
698-
r'logging_configuration=\\".+\\" logging_level=\\"WARNING\\" record=\\"f\\" ' \
699-
r'required_option_1=\\"value_1\\" required_option_2=\\"value_2\\" show_configuration=\\"f\\""\]\]\}'
697+
if six.PY2:
698+
inspector = \
699+
r'\"inspector\":\{\"messages\":\[\[\"ERROR\",\"StandardError at \\\".+\\\", line \d+ : test ' \
700+
r'logging_configuration=\\\".+\\\" logging_level=\\\"WARNING\\\" record=\\\"f\\\" ' \
701+
r'required_option_1=\\\"value_1\\\" required_option_2=\\\"value_2\\\" show_configuration=\\\"f\\\"\"\]\]\}'
702+
else:
703+
inspector = \
704+
r'\"inspector\":\{\"messages\":\[\[\"ERROR\",\"Exception at \\\".+\\\", line \d+ : test ' \
705+
r'logging_configuration=\\\".+\\\" logging_level=\\\"WARNING\\\" record=\\\"f\\\" ' \
706+
r'required_option_1=\\\"value_1\\\" required_option_2=\\\"value_2\\\" show_configuration=\\\"f\\\"\"\]\]\}'
700707

701708
self.assertRegexpMatches(
702-
result.getvalue(),
709+
result.getvalue().decode('utf-8'),
703710
r'^chunked 1.0,2,0\n'
704711
r'\{\}\n'
705712
r'chunked 1.0,\d+,0\n'

0 commit comments

Comments
 (0)