15
15
# under the License.
16
16
17
17
from os import path
18
- import sys
19
18
from time import sleep
20
- import unittest
21
19
22
20
import splunklib .client as client
23
21
from splunklib .binding import HTTPError
24
22
import splunklib .results as results
25
- from utils import parse
26
23
27
- opts = None # Command line options
24
+ import testlib
28
25
29
26
def create_app (service , name ):
30
27
service .apps .create (name )
31
- restart (service )
28
+ testlib . restart (service )
32
29
33
30
def create_user (service , name , password = "changeme" , roles = "power" ):
34
31
service .users .create (name , password = password , roles = roles )
35
32
36
33
def delete_app (service , name ):
37
34
if (service .apps .contains (name )):
38
35
service .apps .delete (name )
39
- restart (service )
36
+ testlib . restart (service )
40
37
41
38
def delete_user (service , name ):
42
39
if (service .users .contains (name )):
43
40
service .users .delete (name )
44
41
45
- def restart (service ):
46
- """Restart the given service and wait for it to wake back up."""
47
- service .restart ()
48
- sleep (5 ) # Wait for service to notice restart
49
- wait_for_restart (service )
50
-
51
- # When an event is submitted to an index it takes a while before the event
52
- # is registered by the index's totalEventCount.
53
- def wait_event_count (index , count , secs ):
54
- """Wait up to the given number of secs for the given index's
55
- totalEventCount to reach the given value."""
56
- done = False
57
- while not done and secs > 0 :
58
- sleep (1 )
59
- index .refresh ()
60
- done = index ['totalEventCount' ] == count
61
- secs -= 1 # Approximate
62
-
63
- def wait_for_restart (service ):
64
- retry = 30
65
- while retry > 0 :
66
- retry -= 1
67
- try :
68
- service .login () # Awake yet?
69
- return
70
- except :
71
- sleep (2 )
72
-
73
42
# Verify that we can instantiate and connect to a service, test basic
74
43
# interaction with the service and make sure we can connect and interact
75
44
# with a variety of namespace configurations.
76
- class ServiceTestCase (unittest .TestCase ):
45
+ class ServiceTestCase (testlib .TestCase ):
77
46
def test (self ):
78
- kwargs = opts .kwargs .copy ()
47
+ kwargs = self . opts .kwargs .copy ()
79
48
80
49
# Verify connect with no namespace
81
50
service = client .connect (** kwargs )
@@ -146,30 +115,9 @@ def test(self):
146
115
self .assertFalse (service .apps .contains (app ))
147
116
self .assertFalse (service .users .contains (user ))
148
117
149
- class ClientTestCase (unittest .TestCase ):
150
- def setUp (self ):
151
- self .service = client .connect (** opts .kwargs )
152
-
153
- def assertHttp (self , allowed_error_codes , fn , * args , ** kwargs ):
154
- # This is a special case of "assertRaises", where we want to check
155
- # that HTTP calls return the right status.
156
- try :
157
- returnVal = fn (* args , ** kwargs )
158
- return returnVal
159
- except HTTPError as e :
160
- error_msg = "Unexpected error code: %d" % e .status
161
- if (isinstance (allowed_error_codes , list )):
162
- self .assertTrue (e .status in allowed_error_Codes , error_msg )
163
- else :
164
- self .assertTrue (e .status == allowed_error_codes , error_msg )
165
- except Exception as e :
166
- self .fail ("HTTPError not raised, caught %s instead" , str (type (e )))
167
-
168
- def tearDown (self ):
169
- pass
170
-
118
+ class ClientTestCase (testlib .TestCase ):
171
119
def test_apps (self ):
172
- service = self .service
120
+ service = client . connect ( ** self .opts . kwargs )
173
121
174
122
for app in service .apps : app .refresh ()
175
123
@@ -189,6 +137,8 @@ def test_apps(self):
189
137
self .assertFalse (service .apps .contains ('sdk-tests' ))
190
138
191
139
def test_capabilities (self ):
140
+ service = client .connect (** self .opts .kwargs )
141
+
192
142
expected = [
193
143
"admin_all_objects" , "change_authentication" ,
194
144
"change_own_password" , "delete_by_keyword" ,
@@ -204,11 +154,12 @@ def test_capabilities(self):
204
154
"rest_apps_view" , "rest_properties_get" , "rest_properties_set" ,
205
155
"restart_splunkd" , "rtsearch" , "schedule_search" , "search" ,
206
156
"use_file_operator" ]
207
- capabilities = self .service .capabilities
157
+
158
+ capabilities = service .capabilities
208
159
for item in expected : self .assertTrue (item in capabilities )
209
160
210
161
def test_confs (self ):
211
- service = self .service
162
+ service = client . connect ( ** self .opts . kwargs )
212
163
213
164
for conf in service .confs :
214
165
for stanza in conf : stanza .refresh ()
@@ -237,15 +188,17 @@ def test_confs(self):
237
188
self .assertFalse (props .contains ('sdk-tests' ))
238
189
239
190
def test_info (self ):
240
- info = self .service .info
191
+ service = client .connect (** self .opts .kwargs )
192
+
193
+ info = service .info
241
194
keys = [
242
195
"build" , "cpu_arch" , "guid" , "isFree" , "isTrial" , "licenseKeys" ,
243
196
"licenseSignature" , "licenseState" , "master_guid" , "mode" ,
244
197
"os_build" , "os_name" , "os_version" , "serverName" , "version" ]
245
198
for key in keys : self .assertTrue (key in info .keys ())
246
199
247
200
def test_indexes (self ):
248
- service = self .service
201
+ service = client . connect ( ** self .opts . kwargs )
249
202
250
203
for index in service .indexes : index .refresh ()
251
204
@@ -290,35 +243,38 @@ def test_indexes(self):
290
243
cn = index .attach ()
291
244
cn .write ("Hello World!" )
292
245
cn .close ()
293
- wait_event_count (index , '1' , 30 )
246
+ testlib . wait (index , lambda index : index [ 'totalEventCount' ] == '1' )
294
247
self .assertEqual (index ['totalEventCount' ], '1' )
295
248
296
249
index .submit ("Hello again!!" )
297
- wait_event_count (index , '2' , 30 )
250
+ testlib . wait (index , lambda index : index [ 'totalEventCount' ] == '2' )
298
251
self .assertEqual (index ['totalEventCount' ], '2' )
299
252
300
253
# The following test must run on machine where splunkd runs,
301
254
# otherwise a failure is expected
302
255
testpath = path .dirname (path .abspath (__file__ ))
303
256
index .upload (path .join (testpath , "testfile.txt" ))
304
- wait_event_count (index , '3' , 30 )
257
+ testlib . wait (index , lambda index : index [ 'totalEventCount' ] == '3' )
305
258
self .assertEqual (index ['totalEventCount' ], '3' )
306
259
307
260
index .clean ()
308
261
index .refresh ()
309
262
self .assertEqual (index ['totalEventCount' ], '0' )
310
263
311
264
def test_indexes_metadata (self ):
312
- metadata = self .service .indexes .itemmeta ()
265
+ service = client .connect (** self .opts .kwargs )
266
+
267
+ metadata = service .indexes .itemmeta ()
313
268
self .assertTrue (metadata .has_key ('eai:acl' ))
314
269
self .assertTrue (metadata .has_key ('eai:attributes' ))
315
- for index in self . service .indexes :
270
+ for index in service .indexes :
316
271
metadata = index .metadata
317
272
self .assertTrue (metadata .has_key ('eai:acl' ))
318
273
self .assertTrue (metadata .has_key ('eai:attributes' ))
319
274
320
275
def test_inputs (self ):
321
- inputs = self .service .inputs
276
+ service = client .connect (** self .opts .kwargs )
277
+ inputs = service .inputs
322
278
323
279
for input_ in inputs : input_ .refresh ()
324
280
@@ -348,50 +304,35 @@ def test_inputs(self):
348
304
inputs .delete ('9999' )
349
305
self .assertFalse (inputs .contains ('9999' ))
350
306
351
- def runjob (self , query , secs ):
352
- """Create a job to run the given search and wait up to (approximately)
353
- the given number of seconds for it to complete."""
354
- job = self .service .jobs .create (query )
355
- return self .wait_for_completion (job , secs = secs )
356
-
357
- def wait_for_completion (self , job , secs = 30 ):
358
- done = False
359
- while not done and secs > 0 :
360
- sleep (1 )
361
- job .refresh ()
362
- done = bool (int (job ['isDone' ]))
363
- secs -= 1 # Approximate
364
- return job
365
-
307
+ # UNDONE: Shouldnt the following assert something on exit?
366
308
def check_properties (self , job , properties , secs = 10 ):
367
309
while secs > 0 and len (properties ) > 0 :
368
- read_props = job .refresh ().content
369
- asserted = []
310
+ content = job .refresh ().content
370
311
371
312
# Try and check every property we specified. If we fail,
372
313
# we'll try again later. If we succeed, delete it so we
373
314
# don't check it again.
374
- for prop_name in properties .keys ():
315
+ for k , v in properties .items ():
375
316
try :
376
- expected_value = properties [prop_name ]
377
- self .assertEqual (read_props [prop_name ], expected_value )
317
+ self .assertEqual (content [k ], v )
378
318
379
319
# Since we succeeded, delete it
380
- del properties [prop_name ]
320
+ del properties [k ]
381
321
except :
382
322
pass
383
323
384
324
secs -= 1
385
325
sleep (1 )
386
326
387
327
def test_jobs (self ):
388
- jobs = self .service . jobs
328
+ service = client . connect ( ** self .opts . kwargs )
389
329
330
+ jobs = service .jobs
390
331
for job in jobs : job .refresh ()
391
332
392
- if not self . service .indexes .contains ("sdk-tests" ):
393
- self . service .indexes .create ("sdk-tests" )
394
- self . service .indexes ['sdk-tests' ].clean ()
333
+ if not service .indexes .contains ("sdk-tests" ):
334
+ service .indexes .create ("sdk-tests" )
335
+ service .indexes ['sdk-tests' ].clean ()
395
336
396
337
# Make sure we can create a job
397
338
job = jobs .create ("search index=sdk-tests" )
@@ -419,9 +360,10 @@ def test_jobs(self):
419
360
self .assertFalse (jobs .contains (job .sid ))
420
361
421
362
# Search for non-existant data
422
- job = self .runjob ("search index=sdk-tests TERM_DOES_NOT_EXIST" , 10 )
423
- self .assertTrue (bool (int (job ['isDone' ])))
424
- self .assertEqual (int (job ['eventCount' ]), 0 )
363
+ job = jobs .create ("search index=sdk-tests TERM_DOES_NOT_EXIST" )
364
+ testlib .wait (job , lambda job : job ['isDone' ] == '1' )
365
+ self .assertEqual (job ['isDone' ], '1' )
366
+ self .assertEqual (job ['eventCount' ], '0' )
425
367
job .finalize ()
426
368
427
369
# Create a new job
@@ -459,13 +401,15 @@ def test_jobs(self):
459
401
460
402
# Run a new job to get the results, but we also make
461
403
# sure that there is at least one event in the index already
462
- index = self . service .indexes ['sdk-tests' ]
404
+ index = service .indexes ['sdk-tests' ]
463
405
old_event_count = int (index ['totalEventCount' ])
464
406
if old_event_count == 0 :
465
407
index .submit ("test event" )
466
- wait_event_count (index , 1 , 10 )
408
+ testlib . wait (index , lambda index : index [ 'totalEventCount' ] == '1' )
467
409
468
- job = self .runjob ("search index=sdk-tests | head 1 | stats count" , 10 )
410
+ job = jobs .create ("search index=sdk-tests | head 1 | stats count" )
411
+ testlib .wait (job , lambda job : job ['isDone' ] == '1' )
412
+ self .assertEqual (job ['isDone' ], '1' )
469
413
470
414
# Fetch the results
471
415
reader = results .ResultsReader (job .results ())
@@ -481,7 +425,7 @@ def test_jobs(self):
481
425
self .assertEqual (int (result ["count" ]), 1 )
482
426
483
427
def test_loggers (self ):
484
- service = self .service
428
+ service = client . connect ( ** self .opts . kwargs )
485
429
486
430
levels = ["INFO" , "WARN" , "ERROR" , "DEBUG" , "CRIT" ]
487
431
for logger in service .loggers :
@@ -500,16 +444,26 @@ def test_loggers(self):
500
444
self .assertEqual (service .loggers ['AuditLogger' ]['level' ], saved )
501
445
502
446
def test_parse (self ):
503
- response = self .service .parse ("search *" )
447
+ service = client .connect (** self .opts .kwargs )
448
+
449
+ response = service .parse ("search *" )
504
450
self .assertEqual (response .status , 200 )
505
451
506
- response = self . service .parse ("search index=twitter status_count=* | stats count(status_source) as count by status_source | sort -count | head 20" )
452
+ response = service .parse ("search index=twitter status_count=* | stats count(status_source) as count by status_source | sort -count | head 20" )
507
453
self .assertEqual (response .status , 200 )
508
454
509
- self .assertHttp (400 , self .service .parse , "xyzzy" )
455
+ try :
456
+ service .parse ("xyzzy" )
457
+ self .fail ()
458
+ except HTTPError , e :
459
+ self .assertEqual (e .status , 400 )
460
+ except :
461
+ self .fail ()
510
462
511
463
def test_messages (self ):
512
- messages = self .service .messages
464
+ service = client .connect (** self .opts .kwargs )
465
+
466
+ messages = service .messages
513
467
514
468
if messages .contains ('sdk-test-message1' ):
515
469
messages .delete ('sdk-test-message1' )
@@ -545,16 +499,19 @@ def test_messages(self):
545
499
with self .assertRaises (ValueError ):
546
500
messages .create (None , value = "What?" )
547
501
messages .create (42 , value = "Who, me?" )
548
- messages .create ([1 ,2 , 3 ], value = "Who, me?" )
502
+ messages .create ([1 , 2 , 3 ], value = "Who, me?" )
549
503
550
504
def test_restart (self ):
551
- restart (self .service )
552
- self .service .login () # Make sure we are awake
505
+ service = client .connect (** self .opts .kwargs )
506
+ testlib .restart (service )
507
+ service .login () # Make sure we are awake
553
508
554
509
def test_roles (self ):
555
- roles = self .service .roles
510
+ service = client .connect (** self .opts .kwargs )
511
+
512
+ roles = service .roles
556
513
557
- capabilities = self . service .capabilities
514
+ capabilities = service .capabilities
558
515
for role in roles :
559
516
for capability in role .content .capabilities :
560
517
self .assertTrue (capability in capabilities )
@@ -571,7 +528,8 @@ def test_roles(self):
571
528
self .assertFalse (roles .contains ("sdk-tester" ))
572
529
573
530
def test_settings (self ):
574
- settings = self .service .settings
531
+ service = client .connect (** self .opts .kwargs )
532
+ settings = service .settings
575
533
576
534
# Verify that settings contains the keys we expect
577
535
keys = [
@@ -596,8 +554,9 @@ def test_settings(self):
596
554
self .assertEqual (updated , original )
597
555
598
556
def test_users (self ):
599
- users = self .service .users
600
- roles = self .service .roles
557
+ service = client .connect (** self .opts .kwargs )
558
+ users = service .users
559
+ roles = service .roles
601
560
602
561
# Verify that we can read the users collection
603
562
for user in users :
@@ -642,13 +601,5 @@ def test_users(self):
642
601
self .assertFalse (users .contains ("SDK-User" ))
643
602
self .assertFalse (users .contains ("sdk-user" ))
644
603
645
- # Runs the given named test, useful for debugging.
646
- def runone (test ):
647
- suite = unittest .TestSuite ()
648
- suite .addTest (test )
649
- unittest .TextTestRunner ().run (suite )
650
-
651
604
if __name__ == "__main__" :
652
- opts = parse (sys .argv [1 :], {}, ".splunkrc" )
653
- #runone(ClientTestCase("test_users"))
654
- unittest .main (argv = sys .argv [:1 ])
605
+ testlib .main ()
0 commit comments