1919
2020import splunklib .client as client
2121
22+ def highest_port (service , base_port , * kinds ):
23+ """Find the first port >= base_port not in use by any input in kinds."""
24+ highest_port = base_port
25+ for input in service .inputs .list (* kinds ):
26+ port = int (input .name .split (':' )[- 1 ])
27+ highest_port = max (port , highest_port )
28+ return highest_port
29+
30+ class TestTcpInputNameHandling (testlib .SDKTestCase ):
31+ def setUp (self ):
32+ super (TestTcpInputNameHandling , self ).setUp ()
33+ self .base_port = highest_port (self .service , 10000 , 'tcp' , 'splunktcp' ) + 1
34+
35+ def tearDown (self ):
36+ for input in self .service .inputs .list ('tcp' , 'splunktcp' ):
37+ port = int (input .name .split (':' )[- 1 ])
38+ if port >= self .base_port :
39+ input .delete ()
40+ super (TestTcpInputNameHandling , self ).tearDown ()
41+
42+ def test_create_tcp_port (self ):
43+ for kind in ['tcp' , 'splunktcp' ]:
44+ input = self .service .inputs .create (kind , str (self .base_port ))
45+ self .check_entity (input )
46+ input .delete ()
47+
48+ def test_cannot_create_with_restrictToHost_in_name (self ):
49+ self .assertRaises (
50+ client .HTTPError ,
51+ lambda : self .service .inputs .create ('tcp' , 'boris:10000' )
52+ )
53+
54+ def test_remove_host_restriction (self ):
55+ if self .service .splunk_version < (5 ,):
56+ # We can't set restrictToHost before 5.0 due to a bug in splunkd.
57+ return
58+ input = self .service .inputs .create ('tcp' , str (self .base_port ), restrictToHost = 'boris' )
59+ input .update (restrictToHost = '' )
60+ input .refresh ()
61+ self .check_entity (input )
62+ input .delete ()
63+
64+ def test_create_tcp_ports_with_restrictToHost (self ):
65+ for kind in ['tcp' , 'splunktcp' ]:
66+ # Make sure we can create two restricted inputs on the same port
67+ boris = self .service .inputs .create (kind , str (self .base_port ), restrictToHost = 'boris' )
68+ natasha = self .service .inputs .create (kind , str (self .base_port ), restrictToHost = 'natasha' )
69+ # And that they both function
70+ boris .refresh ()
71+ natasha .refresh ()
72+ self .check_entity (boris )
73+ self .check_entity (natasha )
74+ boris .delete ()
75+ natasha .delete ()
76+
77+ def test_restricted_to_unrestricted_collision (self ):
78+ for kind in ['tcp' , 'splunktcp' ]:
79+ restricted = self .service .inputs .create (kind , str (self .base_port ), restrictToHost = 'boris' )
80+ self .assertRaises (
81+ client .HTTPError ,
82+ lambda : self .service .inputs .create (kind , str (self .base_port ))
83+ )
84+ restricted .delete ()
85+
86+ def test_unrestricted_to_restricted_collision (self ):
87+ for kind in ['tcp' , 'splunktcp' ]:
88+ unrestricted = self .service .inputs .create (kind , str (self .base_port ))
89+ self .assertRaises (
90+ client .HTTPError ,
91+ lambda : self .service .inputs .create (kind , str (self .base_port ), restrictToHos = 'boris' )
92+ )
93+ unrestricted .delete ()
94+
95+ def test_update_restrictToHost (self ):
96+ for kind in ['tcp' , 'splunktcp' ]:
97+ port = self .base_port
98+ while True : # Find the next unbound port
99+ try :
100+ boris = self .service .inputs .create (kind , str (port ), restrictToHost = 'boris' )
101+ except client .HTTPError as he :
102+ if he .status == 400 :
103+ port += 1
104+ else :
105+ break
106+
107+ # No matter what version we're actually running against,
108+ # we can check that on Splunk < 5.0, we get an exception
109+ # from trying to update restrictToHost.
110+ with self .fake_splunk_version ((4 ,3 )):
111+ self .assertRaises (
112+ client .IllegalOperationException ,
113+ lambda : boris .update (restrictToHost = 'hilda' )
114+ )
115+
116+ # And now back to real tests...
117+ if self .service .splunk_version >= (5 ,):
118+ boris .update (restrictToHost = 'hilda' )
119+ boris .refresh ()
120+ self .assertEqual ('hilda:' + str (self .base_port ), boris .name )
121+ boris .refresh ()
122+ self .check_entity (boris )
123+ boris .delete ()
124+
125+ def test_update_nonrestrictToHost (self ):
126+ for kind in ['tcp' , 'splunktcp' ]:
127+ port = self .base_port
128+ while True : # Find the next unbound port
129+ try :
130+ input = self .service .inputs .create (kind , str (port ), restrictToHost = 'boris' )
131+ except client .HTTPError as he :
132+ if he .status == 400 :
133+ port += 1
134+ else :
135+ break
136+ try :
137+ input .update (host = 'meep' )
138+ input .refresh ()
139+ self .assertTrue (input .name .startswith ('boris' ))
140+ except :
141+ input .delete ()
142+ raise
143+
22144class TestRead (testlib .SDKTestCase ):
23145 def test_read (self ):
24146 inputs = self .service .inputs
@@ -59,7 +181,7 @@ def test_inputs_list_on_one_kind_with_search(self):
59181 self .assertEqual (expected , found )
60182
61183 def test_oneshot (self ):
62- self .installAppFromCollection ('file_to_upload' )
184+ self .install_app_from_collection ('file_to_upload' )
63185
64186 index_name = testlib .tmpname ()
65187 index = self .service .indexes .create (index_name )
@@ -80,26 +202,23 @@ def test_oneshot_on_nonexistant_file(self):
80202 self .assertRaises (client .OperationFailedException ,
81203 self .service .inputs .oneshot , name )
82204
83-
84205class TestInput (testlib .SDKTestCase ):
85206 def setUp (self ):
86207 super (TestInput , self ).setUp ()
87208 inputs = self .service .inputs
88- test_inputs = [{'kind' : 'tcp' , 'name' : '9999' , 'host' : 'sdk-test' },
89- {'kind' : 'udp' , 'name' : '9999' , 'host' : 'sdk-test' }]
209+ unrestricted_port = str (highest_port (self .service , 10000 , 'tcp' , 'splunktcp' , 'udp' )+ 1 )
210+ restricted_port = str (highest_port (self .service , int (unrestricted_port )+ 1 , 'tcp' , 'splunktcp' )+ 1 )
211+ test_inputs = [{'kind' : 'tcp' , 'name' : unrestricted_port , 'host' : 'sdk-test' },
212+ {'kind' : 'udp' , 'name' : unrestricted_port , 'host' : 'sdk-test' },
213+ {'kind' : 'tcp' , 'name' : 'boris:' + restricted_port , 'host' : 'sdk-test' }]
90214 self ._test_entities = {}
91215
92- base_port = 10000
93- while True :
94- if str (base_port ) in inputs :
95- base_port += 1
96- else :
97- break
98-
99216 self ._test_entities ['tcp' ] = \
100- inputs .create ('tcp' , str ( base_port ) , host = 'sdk-test' )
217+ inputs .create ('tcp' , unrestricted_port , host = 'sdk-test' )
101218 self ._test_entities ['udp' ] = \
102- inputs .create ('udp' , str (base_port ), host = 'sdk-test' )
219+ inputs .create ('udp' , unrestricted_port , host = 'sdk-test' )
220+ self ._test_entities ['restrictedTcp' ] = \
221+ inputs .create ('tcp' , restricted_port , restrictToHost = 'boris' )
103222
104223 def tearDown (self ):
105224 super (TestInput , self ).tearDown ()
@@ -124,7 +243,7 @@ def test_lists_modular_inputs(self):
124243 else :
125244 # Install modular inputs to list, and restart
126245 # so they'll show up.
127- self .installAppFromCollection ("modular-inputs" )
246+ self .install_app_from_collection ("modular-inputs" )
128247 self .uncheckedRestartSplunk ()
129248
130249 inputs = self .service .inputs
@@ -159,11 +278,10 @@ def test_update(self):
159278 inputs = self .service .inputs
160279 for entity in self ._test_entities .itervalues ():
161280 kind , name = entity .kind , entity .name
162- kwargs = {'host' : 'foo' , 'sourcetype' : 'bar' }
281+ kwargs = {'host' : 'foo' }
163282 entity .update (** kwargs )
164283 entity .refresh ()
165284 self .assertEqual (entity .host , kwargs ['host' ])
166- self .assertEqual (entity .sourcetype , kwargs ['sourcetype' ])
167285
168286 def test_delete (self ):
169287 inputs = self .service .inputs
@@ -177,14 +295,17 @@ def test_delete(self):
177295 inputs .delete (name )
178296 self .assertFalse (name in inputs )
179297 else :
180- self .assertRaises (client .AmbiguousReferenceException ,
181- inputs .delete , name )
298+ if not name .startswith ('boris' ):
299+ self .assertRaises (client .AmbiguousReferenceException ,
300+ inputs .delete , name )
182301 self .service .inputs .delete (kind , name )
183302 self .assertFalse ((kind ,name ) in inputs )
184303 self .assertRaises (client .EntityDeletedException ,
185304 input_entity .refresh )
186305 remaining -= 1
187-
306+
307+
308+
188309
189310if __name__ == "__main__" :
190311 import unittest
0 commit comments