2020 import unittest
2121
2222from subprocess import Popen
23- import json
23+ import base64
2424import os
2525import shutil
2626import testlib
2727
28+ from splunklib .searchcommands import \
29+ StreamingCommand , Configuration , Option , validators
30+
31+ @Configuration ()
32+ class StubbedCommand (StreamingCommand ):
33+ fieldname = Option (
34+ doc = '''
35+ **Syntax:** **fieldname=***<fieldname>*
36+ **Description:** Name of the field that will hold the match count''' ,
37+ require = True , validate = validators .Fieldname ())
38+
39+ pattern = Option (
40+ doc = '''
41+ **Syntax:** **pattern=***<regular-expression>*
42+ **Description:** Regular expression pattern to match''' ,
43+ require = True , validate = validators .RegularExpression ())
44+
45+ def stream (self , records ):
46+ pass
47+
2848
2949class TestSearchCommandsApp (testlib .SDKTestCase ):
3050
@@ -37,15 +57,49 @@ def setUp(self):
3757 os .mkdir (path )
3858 return
3959
60+ def test_command_parser (self ):
61+ from splunklib .searchcommands .search_command_internals import \
62+ SearchCommandParser
63+
64+ parser = SearchCommandParser ()
65+ command = StubbedCommand ()
66+
67+ parser .parse (
68+ [
69+ 'fieldname=word_count' ,
70+ 'pattern="\\ \\ w+"' ,
71+ 'text_field_1' ,
72+ 'text_field_2'
73+ ],
74+ command )
75+
76+ command_line = str (command )
77+ self .assertEqual ('stubbed fieldname="word_count" pattern="\\ \\ w+" text_field_1 text_field_2' , command_line )
78+ return
79+
80+ def test_option_show_configuration (self ):
81+ self ._run (
82+ 'simulate' , [
83+ 'csv=%s' % TestSearchCommandsApp ._data_file ("input/population.csv" ),
84+ 'duration=00:00:10' ,
85+ 'interval=00:00:01' ,
86+ 'rate=200' ,
87+ 'seed=%s' % TestSearchCommandsApp ._seed ,
88+ 'show_configuration=true' ],
89+ __GETINFO__ = (
90+ 'input/population.csv' ,
91+ 'output/samples.csv' ,
92+ 'log/test_show_configuration.log' ))
93+ return
94+
4095 def test_generating_command_in_isolation (self ):
41- encoder = json .JSONEncoder (ensure_ascii = False )
4296 self ._run (
4397 'simulate' , [
4498 'csv=%s' % TestSearchCommandsApp ._data_file ("input/population.csv" ),
4599 'duration=00:00:10' ,
46100 'interval=00:00:01' ,
47101 'rate=200' ,
48- 'seed=%s' % encoder . encode ( TestSearchCommandsApp ._seed ) ],
102+ 'seed=%s' % TestSearchCommandsApp ._seed ],
49103 __GETINFO__ = (
50104 'input/population.csv' ,
51105 'output/samples.csv' ,
@@ -108,6 +162,8 @@ def test_streaming_command_on_server(self):
108162
109163 def _run (self , command , args , ** kwargs ):
110164 for operation in ['__GETINFO__' , '__EXECUTE__' ]:
165+ if operation not in kwargs :
166+ continue
111167 files = kwargs [operation ]
112168 process = TestSearchCommandsApp ._start_process (
113169 ['python' , command + '.py' , operation ] + args ,
@@ -133,9 +189,11 @@ def _start_process(cls, args, stdin, stdout, stderr):
133189
134190 package_directory = os .path .dirname (__file__ )
135191 data_directory = os .path .join (package_directory , 'searchcommands_data' )
136- app_bin = os .path .join (os .path .dirname (package_directory ), "examples/searchcommands_app/bin" )
192+ app_bin = os .path .join (
193+ os .path .dirname (package_directory ), "examples/searchcommands_app/bin" )
137194
138- _seed = '\xcd {\xf8 \xc4 \x1c 8=\x88 \n c\xe2 \xc4 \xee \xdb \xca l'
195+ _seed = base64 .encodestring (
196+ '\xcd {\xf8 \xc4 \x1c 8=\x88 \n c\xe2 \xc4 \xee \xdb \xca l' )
139197
140198if __name__ == "__main__" :
141199 unittest .main ()
0 commit comments