1818 import unittest2 as unittest
1919except ImportError :
2020 import unittest
21+
22+ from searchcommands_test .utilities import data_directory , open_data_file
23+ from subprocess import PIPE , Popen
24+
25+ import os
2126import testlib
2227
2328
24- class SearchCommandsTestCase (testlib .SDKTestCase ):
29+ class TestSearchCommands (testlib .SDKTestCase ):
30+
2531 def setUp (self ):
26- super (SearchCommandsTestCase , self ).setUp ()
27- self .uncheckedRestartSplunk ()
28-
29- def test_lists_search_commands (self ):
30- version_major = self .service .splunk_version [0 ]
31- if version_major < 5 :
32- print (
33- 'The splunklib.searchcommands module does not support Splunk '
34- '%d. Skipping.'
35- % version_major )
36- return
37- elif not self .app_collection_installed ():
38- print ('Test requires sdk-app-collection. Skipping.' )
39- return
40- else :
41- # Install modular inputs to list, and restart so they'll show up
42- self .install_app_from_collection ("modular-inputs" )
43- self .uncheckedRestartSplunk ()
44- inputs = self .service .inputs
45- if ('abcd' ,'test2' ) not in inputs :
46- inputs .create ('abcd' , 'test2' , field1 = 'boris' )
32+ super (TestSearchCommands , self ).setUp ()
33+ # TODO: delete all output files
34+
35+ def test_generating_command (self ):
36+ self ._run (
37+ 'simulate' , [
38+ 'csv=%s/sample.csv ' % data_directory ,
39+ 'interval=00:00:01' ,
40+ 'rate=200' ,
41+ 'runtime=00:00:10' ],
42+ __GETINFO__ = (
43+ 'input/sample.csv' ,
44+ 'output/sample.csv' ,
45+ 'error/test_generating_command.log' ),
46+ __EXECUTE__ = (
47+ 'input/sample.csv' ,
48+ 'output/sample.csv' ,
49+ 'error/test_generating_command.log' )
50+ )
51+ return
52+
53+ def test_reporting_command (self ):
54+ self ._run (
55+ 'sum' , [
56+ '__map__' , 'total=total' , 'count' ],
57+ __GETINFO__ = (
58+ 'input/counts.csv' ,
59+ 'output/subtotals.csv' ,
60+ 'error/test_reporting_command.log' ),
61+ __EXECUTE__ = (
62+ 'input/counts.csv' ,
63+ 'output/subtotals.csv' ,
64+ 'error/test_reporting_command.log' )
65+ )
66+ self ._run (
67+ 'sum' , [
68+ 'total=total' , 'count' ],
69+ __GETINFO__ = (
70+ 'input/subtotals.csv' ,
71+ 'output/totals.csv' ,
72+ 'error/test_reporting_command.log' ),
73+ __EXECUTE__ = (
74+ 'input/subtotals.csv' ,
75+ 'output/totals.csv' ,
76+ 'error/test_reporting_command.log' )
77+ )
78+ return
79+
80+ def test_streaming_command (self , m ):
81+ self ._run (
82+ 'countmatches' , [
83+ 'fieldname=word_count' ,
84+ 'pattern=\\ w+' ,
85+ 'text' ],
86+ __GETINFO__ = (
87+ 'input/tweets.csv' ,
88+ 'output/tweet_and_word_counts.csv' ,
89+ 'error/test_streaming_command.log' ),
90+ __EXECUTE__ = (
91+ 'input/tweets.csv' ,
92+ 'output/tweet_and_word_counts.csv' ,
93+ 'error/test_generating_command.log' )
94+ )
95+ return
4796
48- input = inputs ['abcd' , 'test2' ]
49- self .assertEqual (input .field1 , 'boris' )
50- for m in self .service .modular_input_kinds :
51- self .check_modular_input_kind (m )
97+ def _run (self , command , args , ** kwargs ):
98+ for operation in ['__GETINFO__' , '__EXECUTE__' ]:
99+ files = kwargs [operation ]
100+ process = TestSearchCommands ._start_process (
101+ ['python' , command , operation ] + args ,
102+ open_data_file (files [0 ], 'r' ),
103+ open_data_file (files [1 ], 'w' ),
104+ open_data_file (files [2 ], 'a' ))
105+ process .communicate ()
106+ status = process .wait ()
107+ self .assertEqual (status , 0 , "%s status: %d" % (operation , status ))
108+ return
52109
53- def check_modular_input_kind (self , m ):
54- print m .name
55- if m .name == 'test1' :
56- self .assertEqual ('Test "Input" - 1' , m ['title' ])
57- self .assertEqual ("xml" , m ['streaming_mode' ])
58- elif m .name == 'test2' :
59- self .assertEqual ('test2' , m ['title' ])
60- self .assertEqual ('simple' , m ['streaming_mode' ])
110+ @classmethod
111+ def _start_process (cls , args , stdin , stdout , stderr ):
112+ return Popen (args , stdin , stdout , stderr , cwd = cls .app_bin )
61113
114+ app_bin = os .path .join (os .path .dirname (os .path .dirname (__file__ )), "examples/searchcommands_app/bin" )
62115
63116if __name__ == "__main__" :
64- unittest .main ()
117+ unittest .main ()
0 commit comments