Skip to content

Commit 6429ae4

Browse files
committed
refractoring
1 parent e3a09e7 commit 6429ae4

File tree

4 files changed

+82
-63
lines changed

4 files changed

+82
-63
lines changed

tests/searchcommands/chunked_data_stream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, version, meta, data):
1616
dialect=dialect)
1717

1818

19-
class ChunkedDataStreamIter(collections.Iterator):
19+
class ChunkedDataStreamIter(collections.abc.Iterator):
2020
def __init__(self, chunk_stream):
2121
self.chunk_stream = chunk_stream
2222

@@ -30,7 +30,7 @@ def __next__(self):
3030
raise StopIteration
3131

3232

33-
class ChunkedDataStream(collections.Iterable):
33+
class ChunkedDataStream(collections.abc.Iterable):
3434
def __iter__(self):
3535
return ChunkedDataStreamIter(self)
3636

tests/searchcommands/test_decorators.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def fix_up(cls, command_class):
310310

311311
def test_new_configuration_setting(self):
312312

313-
class Test():
313+
class Test:
314314
generating = ConfigurationSetting()
315315

316316
@ConfigurationSetting(name='required_fields')
@@ -470,6 +470,7 @@ def test_option(self):
470470

471471
self.assertEqual(observed, expected)
472472

473+
TestSearchCommand.__test__ = False
473474

474475
if __name__ == "__main__":
475476
main()

tests/searchcommands/test_internals_v2.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def random_list(population, *args):
9797

9898

9999
def random_unicode():
100-
return ''.join([six.chr(x) for x in random.sample(list(range(MAX_NARROW_UNICODE)), random.randint(0, max_length))])
100+
return ''.join([str(x) for x in random.sample(list(range(MAX_NARROW_UNICODE)), random.randint(0, max_length))])
101101

102102
# endregion
103103

@@ -324,7 +324,7 @@ def _load_chunks(self, ifile):
324324
_recordings_path = os.path.join(_package_path, 'recordings', 'scpv2', 'Splunk-6.3')
325325

326326

327-
class TestRecorder():
327+
class TestRecorder:
328328

329329
def __init__(self, test_case):
330330

@@ -410,12 +410,11 @@ def _record(*args, **kwargs):
410410
return _record
411411

412412

413-
class Test():
413+
class Test:
414414

415415
def __init__(self, fieldnames, data_generators):
416416

417417
TestCase.__init__(self)
418-
419418
self._data_generators = list(chain((lambda: self._serial_number, time), data_generators))
420419
self._fieldnames = list(chain(('_serial', '_time'), fieldnames))
421420
self._recorder = TestRecorder(self)
@@ -467,5 +466,9 @@ def _run(self):
467466
# test.record()
468467
# test.playback()
469468

469+
Test.__test__ = False
470+
TestRecorder.__test__ = False
471+
472+
470473
if __name__ == "__main__":
471474
main()

tests/searchcommands/test_search_command.py

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@
3636

3737
import pytest
3838

39+
3940
def build_command_input(getinfo_metadata, execute_metadata, execute_body):
4041
input = ('chunked 1.0,{},0\n{}'.format(len(six.ensure_binary(getinfo_metadata)), getinfo_metadata) +
41-
'chunked 1.0,{},{}\n{}{}'.format(len(six.ensure_binary(execute_metadata)), len(six.ensure_binary(execute_body)), execute_metadata, execute_body))
42+
'chunked 1.0,{},{}\n{}{}'.format(len(six.ensure_binary(execute_metadata)),
43+
len(six.ensure_binary(execute_body)), execute_metadata, execute_body))
4244

4345
ifile = BytesIO(six.ensure_binary(input))
4446

4547
ifile = TextIOWrapper(ifile)
4648

4749
return ifile
4850

51+
4952
@Configuration()
5053
class TestCommand(SearchCommand):
51-
5254
required_option_1 = Option(require=True)
5355
required_option_2 = Option(require=True)
5456

@@ -104,6 +106,7 @@ def stream(self, records):
104106
yield {'_serial': serial_number, 'data': value}
105107
serial_number += 1
106108

109+
107110
@pytest.mark.smoke
108111
class TestSearchCommand(TestCase):
109112
def setUp(self):
@@ -145,7 +148,8 @@ def test_process_scpv1(self):
145148

146149
self.assertEqual(str(command.configuration), '')
147150

148-
expected = ("[('clear_required_fields', None, [1]), ('distributed', None, [2]), ('generates_timeorder', None, [1]), "
151+
expected = (
152+
"[('clear_required_fields', None, [1]), ('distributed', None, [2]), ('generates_timeorder', None, [1]), "
149153
"('generating', None, [1, 2]), ('maxinputs', None, [2]), ('overrides_timeorder', None, [1]), "
150154
"('required_fields', None, [1, 2]), ('requires_preop', None, [1]), ('retainsevents', None, [1]), "
151155
"('run_in_preview', None, [2]), ('streaming', None, [1]), ('streaming_preop', None, [1, 2]), "
@@ -160,7 +164,8 @@ def test_process_scpv1(self):
160164
except BaseException as error:
161165
self.fail('{0}: {1}: {2}\n'.format(type(error).__name__, error, result.getvalue().decode('UTF-8')))
162166

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

165170
ifile = StringIO('\n')
166171
result = BytesIO()
@@ -188,12 +193,14 @@ def test_process_scpv1(self):
188193
configuration.run_in_preview = True
189194
configuration.type = 'streaming'
190195

191-
expected = ('clear_required_fields="True", generates_timeorder="True", generating="True", overrides_timeorder="True", '
192-
'required_fields="[\'foo\', \'bar\']", requires_preop="True", retainsevents="True", streaming="True", '
193-
'streaming_preop="some streaming command"')
196+
expected = (
197+
'clear_required_fields="True", generates_timeorder="True", generating="True", overrides_timeorder="True", '
198+
'required_fields="[\'foo\', \'bar\']", requires_preop="True", retainsevents="True", streaming="True", '
199+
'streaming_preop="some streaming command"')
194200
self.assertEqual(str(command.configuration), expected)
195201

196-
expected = ("[('clear_required_fields', True, [1]), ('distributed', True, [2]), ('generates_timeorder', True, [1]), "
202+
expected = (
203+
"[('clear_required_fields', True, [1]), ('distributed', True, [2]), ('generates_timeorder', True, [1]), "
197204
"('generating', True, [1, 2]), ('maxinputs', 50000, [2]), ('overrides_timeorder', True, [1]), "
198205
"('required_fields', ['foo', 'bar'], [1, 2]), ('requires_preop', True, [1]), "
199206
"('retainsevents', True, [1]), ('run_in_preview', True, [2]), ('streaming', True, [1]), "
@@ -215,21 +222,20 @@ def test_process_scpv1(self):
215222
self.assertRaises(StopIteration, lambda: next(reader))
216223

217224
expected = {
218-
'clear_required_fields': '1', '__mv_clear_required_fields': '',
219-
'generating': '1', '__mv_generating': '',
220-
'generates_timeorder': '1', '__mv_generates_timeorder': '',
221-
'overrides_timeorder': '1', '__mv_overrides_timeorder': '',
222-
'requires_preop': '1', '__mv_requires_preop': '',
223-
'required_fields': 'foo,bar', '__mv_required_fields': '',
224-
'retainsevents': '1', '__mv_retainsevents': '',
225-
'streaming': '1', '__mv_streaming': '',
225+
'clear_required_fields': '1', '__mv_clear_required_fields': '',
226+
'generating': '1', '__mv_generating': '',
227+
'generates_timeorder': '1', '__mv_generates_timeorder': '',
228+
'overrides_timeorder': '1', '__mv_overrides_timeorder': '',
229+
'requires_preop': '1', '__mv_requires_preop': '',
230+
'required_fields': 'foo,bar', '__mv_required_fields': '',
231+
'retainsevents': '1', '__mv_retainsevents': '',
232+
'streaming': '1', '__mv_streaming': '',
226233
'streaming_preop': 'some streaming command', '__mv_streaming_preop': '',
227234
}
228235

229236
self.assertDictEqual(expected, observed) # No message header and no configuration settings
230237

231238
for action in '__GETINFO__', '__EXECUTE__':
232-
233239
# TestCommand.process should produce an error record on parser errors
234240

235241
argv = [
@@ -366,42 +372,43 @@ def test_process_scpv2(self):
366372

367373
metadata = (
368374
'{{'
369-
'"action": "getinfo", "preview": false, "searchinfo": {{'
370-
'"latest_time": "0",'
371-
'"splunk_version": "20150522",'
372-
'"username": "admin",'
373-
'"app": "searchcommands_app",'
374-
'"args": ['
375-
'"logging_configuration={logging_configuration}",'
376-
'"logging_level={logging_level}",'
377-
'"record={record}",'
378-
'"show_configuration={show_configuration}",'
379-
'"required_option_1=value_1",'
380-
'"required_option_2=value_2"'
381-
'],'
382-
'"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",'
383-
'"earliest_time": "0",'
384-
'"session_key": "0JbG1fJEvXrL6iYZw9y7tmvd6nHjTKj7ggaE7a4Jv5R0UIbeYJ65kThn^3hiNeoqzMT_LOtLpVR3Y8TIJyr5bkHUElMijYZ8l14wU0L4n^Oa5QxepsZNUIIQCBm^",'
385-
'"owner": "admin",'
386-
'"sid": "1433261372.158",'
387-
'"splunkd_uri": "https://127.0.0.1:8089",'
388-
'"dispatch_dir": {dispatch_dir},'
389-
'"raw_args": ['
390-
'"logging_configuration={logging_configuration}",'
391-
'"logging_level={logging_level}",'
392-
'"record={record}",'
393-
'"show_configuration={show_configuration}",'
394-
'"required_option_1=value_1",'
395-
'"required_option_2=value_2"'
396-
'],'
397-
'"maxresultrows": 10,'
398-
'"command": "countmatches"'
399-
'}}'
375+
'"action": "getinfo", "preview": false, "searchinfo": {{'
376+
'"latest_time": "0",'
377+
'"splunk_version": "20150522",'
378+
'"username": "admin",'
379+
'"app": "searchcommands_app",'
380+
'"args": ['
381+
'"logging_configuration={logging_configuration}",'
382+
'"logging_level={logging_level}",'
383+
'"record={record}",'
384+
'"show_configuration={show_configuration}",'
385+
'"required_option_1=value_1",'
386+
'"required_option_2=value_2"'
387+
'],'
388+
'"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",'
389+
'"earliest_time": "0",'
390+
'"session_key": "0JbG1fJEvXrL6iYZw9y7tmvd6nHjTKj7ggaE7a4Jv5R0UIbeYJ65kThn^3hiNeoqzMT_LOtLpVR3Y8TIJyr5bkHUElMijYZ8l14wU0L4n^Oa5QxepsZNUIIQCBm^",'
391+
'"owner": "admin",'
392+
'"sid": "1433261372.158",'
393+
'"splunkd_uri": "https://127.0.0.1:8089",'
394+
'"dispatch_dir": {dispatch_dir},'
395+
'"raw_args": ['
396+
'"logging_configuration={logging_configuration}",'
397+
'"logging_level={logging_level}",'
398+
'"record={record}",'
399+
'"show_configuration={show_configuration}",'
400+
'"required_option_1=value_1",'
401+
'"required_option_2=value_2"'
402+
'],'
403+
'"maxresultrows": 10,'
404+
'"command": "countmatches"'
405+
'}}'
400406
'}}')
401407

402408
basedir = self._package_directory
403409

404-
default_logging_configuration = os.path.join(basedir, 'apps', 'app_with_logging_configuration', 'default', 'logging.conf')
410+
default_logging_configuration = os.path.join(basedir, 'apps', 'app_with_logging_configuration', 'default',
411+
'logging.conf')
405412
dispatch_dir = os.path.join(basedir, 'recordings', 'scpv2', 'Splunk-6.3', 'countmatches.dispatch_dir')
406413
logging_configuration = os.path.join(basedir, 'apps', 'app_with_logging_configuration', 'logging.conf')
407414
logging_level = 'ERROR'
@@ -480,14 +487,18 @@ def test_process_scpv2(self):
480487

481488
self.assertEqual(command_metadata.preview, input_header['preview'])
482489
self.assertEqual(command_metadata.searchinfo.app, 'searchcommands_app')
483-
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'])
490+
self.assertEqual(command_metadata.searchinfo.args,
491+
['logging_configuration=' + logging_configuration, 'logging_level=ERROR', 'record=false',
492+
'show_configuration=true', 'required_option_1=value_1', 'required_option_2=value_2'])
484493
self.assertEqual(command_metadata.searchinfo.dispatch_dir, os.path.dirname(input_header['infoPath']))
485494
self.assertEqual(command_metadata.searchinfo.earliest_time, 0.0)
486495
self.assertEqual(command_metadata.searchinfo.latest_time, 0.0)
487496
self.assertEqual(command_metadata.searchinfo.owner, 'admin')
488497
self.assertEqual(command_metadata.searchinfo.raw_args, command_metadata.searchinfo.args)
489-
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')
490-
self.assertEqual(command_metadata.searchinfo.session_key, '0JbG1fJEvXrL6iYZw9y7tmvd6nHjTKj7ggaE7a4Jv5R0UIbeYJ65kThn^3hiNeoqzMT_LOtLpVR3Y8TIJyr5bkHUElMijYZ8l14wU0L4n^Oa5QxepsZNUIIQCBm^')
498+
self.assertEqual(command_metadata.searchinfo.search,
499+
'A| inputlookup tweets | countmatches fieldname=word_count pattern="\\w+" text record=t | export add_timestamp=f add_offset=t format=csv segmentation=raw')
500+
self.assertEqual(command_metadata.searchinfo.session_key,
501+
'0JbG1fJEvXrL6iYZw9y7tmvd6nHjTKj7ggaE7a4Jv5R0UIbeYJ65kThn^3hiNeoqzMT_LOtLpVR3Y8TIJyr5bkHUElMijYZ8l14wU0L4n^Oa5QxepsZNUIIQCBm^')
491502
self.assertEqual(command_metadata.searchinfo.sid, '1433261372.158')
492503
self.assertEqual(command_metadata.searchinfo.splunk_version, '20150522')
493504
self.assertEqual(command_metadata.searchinfo.splunkd_uri, 'https://127.0.0.1:8089')
@@ -668,7 +679,8 @@ def test_process_scpv2(self):
668679
except BaseException as error:
669680
self.fail('{0}: {1}: {2}\n'.format(type(error).__name__, error, result.getvalue().decode('utf-8')))
670681
else:
671-
self.fail('Expected SystemExit, not a return from TestCommand.process: {}\n'.format(result.getvalue().decode('utf-8')))
682+
self.fail('Expected SystemExit, not a return from TestCommand.process: {}\n'.format(
683+
result.getvalue().decode('utf-8')))
672684

673685
self.assertEqual(command.logging_configuration, logging_configuration)
674686
self.assertEqual(command.logging_level, logging_level)
@@ -680,9 +692,9 @@ def test_process_scpv2(self):
680692
finished = r'\"finished\":true'
681693

682694
inspector = \
683-
r'\"inspector\":\{\"messages\":\[\[\"ERROR\",\"Exception at \\\".+\\\", line \d+ : test ' \
684-
r'logging_configuration=\\\".+\\\" logging_level=\\\"WARNING\\\" record=\\\"f\\\" ' \
685-
r'required_option_1=\\\"value_1\\\" required_option_2=\\\"value_2\\\" show_configuration=\\\"f\\\"\"\]\]\}'
695+
r'\"inspector\":\{\"messages\":\[\[\"ERROR\",\"Exception at \\\".+\\\", line \d+ : test ' \
696+
r'logging_configuration=\\\".+\\\" logging_level=\\\"WARNING\\\" record=\\\"f\\\" ' \
697+
r'required_option_1=\\\"value_1\\\" required_option_2=\\\"value_2\\\" show_configuration=\\\"f\\\"\"\]\]\}'
686698

687699
six.assertRegex(
688700
self,
@@ -753,5 +765,8 @@ def test_process_scpv2(self):
753765
_package_directory = os.path.dirname(os.path.abspath(__file__))
754766

755767

768+
TestCommand.__test__ = False
769+
TestStreamingCommand.__test__ = False
770+
756771
if __name__ == "__main__":
757772
main()

0 commit comments

Comments
 (0)