Skip to content

Commit 7519acc

Browse files
author
Fred Ross
committed
Merge pull request #51 from splunk/fross/test-suite-on-ace
All tests now pace with 4.3.3 and Ace.
2 parents 9ed4065 + fe4e198 commit 7519acc

22 files changed

+103
-69
lines changed

splunklib/client.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def messages(self):
401401
@property
402402
def modular_input_kinds(self):
403403
"""Returns a collection of the modular input kinds on this Splunk instance."""
404-
if self.splunk_version[0] >= 5:
404+
if self.splunk_version >= (5,):
405405
return ReadOnlyCollection(self, PATH_MODULAR_INPUTS, item=ModularInputKind)
406406
else:
407407
raise IllegalOperationException("Modular inputs are not supported before Splunk version 5.")
@@ -1481,7 +1481,7 @@ def default(self):
14811481
return index['defaultDatabase']
14821482

14831483
def delete(self, name):
1484-
if self.service.splunk_version[0] >= 5:
1484+
if self.service.splunk_version >= (5,):
14851485
Collection.delete(self, name)
14861486
else:
14871487
raise IllegalOperationException("Deleting indexes via the REST API is "
@@ -1548,28 +1548,42 @@ def attached_socket(self, *args, **kwargs):
15481548

15491549
def clean(self, timeout=60):
15501550
"""Deletes the contents of the index.
1551-
1551+
1552+
`clean` blocks until the index is empty, since it needs to restore
1553+
values at the end.
1554+
15521555
:param `timeout`: The time-out period for the operation, in seconds (the
15531556
default is 60).
15541557
"""
15551558
self.refresh()
15561559
tds = self['maxTotalDataSizeMB']
15571560
ftp = self['frozenTimePeriodInSecs']
1558-
self.update(maxTotalDataSizeMB=1, frozenTimePeriodInSecs=1)
1559-
self.roll_hot_buckets()
1560-
1561-
# Wait until the event count goes to zero
1562-
count = 0
1563-
while self.content.totalEventCount != '0' and count < timeout:
1564-
sleep(1)
1565-
count += 1
1566-
self.refresh()
1561+
was_disabled_initially = self.disabled
1562+
try:
1563+
if (not was_disabled_initially and \
1564+
self.service.splunk_version < (5,)):
1565+
# Need to disable the index first on Splunk 4.x,
1566+
# but it doesn't work to disable it on 5.0.
1567+
self.disable()
1568+
self.update(maxTotalDataSizeMB=1, frozenTimePeriodInSecs=1)
1569+
self.roll_hot_buckets()
15671570

1568-
# Restore original values
1569-
self.update(maxTotalDataSizeMB=tds,
1570-
frozenTimePeriodInSecs=ftp)
1571-
if self.content.totalEventCount != '0':
1572-
raise OperationError, "Operation timed out."
1571+
start = datetime.now()
1572+
diff = timedelta(seconds=timeout)
1573+
# Wait until event count goes to 0.
1574+
while self.content.totalEventCount != '0' and datetime.now() < start+diff:
1575+
sleep(1)
1576+
self.refresh()
1577+
finally:
1578+
# Restore original values
1579+
self.update(maxTotalDataSizeMB=tds, frozenTimePeriodInSecs=ftp)
1580+
if self.content.totalEventCount != '0':
1581+
raise OperationError, "Cleaning index %s took longer than %s seconds; timing out." % \
1582+
(self.name, timeout)
1583+
if (not was_disabled_initially and \
1584+
self.service.splunk_version < (5,)):
1585+
# Re-enable the index if it was originally enabled and we messed with it.
1586+
self.enable()
15731587
return self
15741588

15751589
def disable(self):

tests/test_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import splunklib.client as client
2121
import splunklib.data as data
2222

23-
class TestApp(testlib.TestCase):
23+
class TestApp(testlib.SDKTestCase):
2424
app = None
2525
app_name = None
2626
def setUp(self):

tests/test_binding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def isatom(body):
400400
root.find(XNAME_ID) is not None and \
401401
root.find(XNAME_TITLE) is not None
402402

403-
class TestPluggableHTTP(testlib.TestCase):
403+
class TestPluggableHTTP(testlib.SDKTestCase):
404404
# Verify pluggable HTTP reqeust handlers.
405405
def test_handlers(self):
406406
paths = ["/services", "authentication/users",

tests/test_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
expected_fields_keys = set(['required', 'optional', 'wildcard'])
3838

3939

40-
class TestCase(testlib.TestCase):
40+
class CollectionTestCase(testlib.SDKTestCase):
4141
def setUp(self):
42-
super(TestCase, self).setUp()
42+
super(CollectionTestCase, self).setUp()
4343
if self.service.splunk_version[0] >= 5 and 'modular_input_kinds' not in collections:
4444
collections.append('modular_input_kinds') # Not supported before Splunk 5.0
4545
else:

tests/test_conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import splunklib.client as client
2121

22-
class TestRead(testlib.TestCase):
22+
class TestRead(testlib.SDKTestCase):
2323
def test_read(self):
2424
service = client.connect(**self.opts.kwargs)
2525

@@ -36,7 +36,7 @@ def test_read(self):
3636
for stanza in confs['indexes'].list(count=5):
3737
self.check_entity(stanza)
3838

39-
class TestConfs(testlib.TestCase):
39+
class TestConfs(testlib.SDKTestCase):
4040
def setUp(self):
4141
super(TestConfs, self).setUp()
4242
self.app_name = testlib.tmpname()

tests/test_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import splunklib.data as data
2323

24-
class TestCase(testlib.TestCase):
24+
class DataTestCase(testlib.SDKTestCase):
2525
def test_elems(self):
2626
result = data.load("")
2727
self.assertTrue(result is None)

tests/test_event_type.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
import splunklib.client as client
2121

22-
class TestRead(testlib.TestCase):
22+
class TestRead(testlib.SDKTestCase):
2323
def test_read(self):
2424
for event_type in self.service.event_types.list(count=1):
2525
self.check_entity(event_type)
2626

27-
class TestCreate(testlib.TestCase):
27+
class TestCreate(testlib.SDKTestCase):
2828
def test_create(self):
2929
self.event_type_name = testlib.tmpname()
3030
event_types = self.service.event_types
@@ -47,7 +47,7 @@ def tearDown(self):
4747
except KeyError:
4848
pass
4949

50-
class TestEventType(testlib.TestCase):
50+
class TestEventType(testlib.SDKTestCase):
5151
def setUp(self):
5252
super(TestEventType, self).setUp()
5353
self.event_type_name = testlib.tmpname()

tests/test_examples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ def start(script, stdin=None, stdout=PIPE, stderr=None):
5151
return Popen(script, stdin=stdin, stdout=stdout, stderr=stderr, cwd='../examples')
5252

5353
# Rudimentary sanity check for each of the examples
54-
class TestCase(testlib.TestCase):
54+
class ExamplesTestCase(testlib.SDKTestCase):
5555
def check_commands(self, *args):
5656
for arg in args:
5757
self.assertEquals(run(arg), 0)
5858

5959
def setUp(self):
60-
testlib.TestCase.setUp(self)
60+
super(ExamplesTestCase, self).setUp()
6161

6262
# Ignore result, it might already exist
6363
run("index.py create sdk-tests")

tests/test_fired_alert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
import splunklib.client as client
2121

22-
class TestCase(testlib.TestCase):
22+
class FiredAlertTestCase(testlib.SDKTestCase):
2323
def setUp(self):
24-
super(TestCase, self).setUp()
24+
super(FiredAlertTestCase, self).setUp()
2525
self.index_name = testlib.tmpname()
2626
self.assertFalse(self.index_name in self.service.indexes)
2727
self.index = self.service.indexes.create(self.index_name)
@@ -44,7 +44,7 @@ def setUp(self):
4444
query, **kwargs)
4545

4646
def tearDown(self):
47-
super(TestCase, self).tearDown()
47+
super(FiredAlertTestCase, self).tearDown()
4848
for saved_search in self.service.saved_searches:
4949
if saved_search.name.startswith('delete-me'):
5050
self.service.saved_searches.delete(saved_search.name)

tests/test_index.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import logging
2424

25-
class IndexTest(testlib.TestCase):
25+
class IndexTest(testlib.SDKTestCase):
2626
def setUp(self):
2727
super(IndexTest, self).setUp()
2828
self.index_name = testlib.tmpname()
@@ -34,7 +34,7 @@ def tearDown(self):
3434
# 5.0. In 4.x, we just have to leave them lying around until
3535
# someone cares to go clean them up. Unique naming prevents
3636
# clashes, though.
37-
if self.service.splunk_version[0] >= 5:
37+
if self.service.splunk_version >= (5,):
3838
self.service.indexes.delete(self.index_name)
3939
else:
4040
logging.warning("test_index.py:TestDeleteIndex: Skipped: cannot "
@@ -57,6 +57,17 @@ def test_disable_enable(self):
5757
self.index.refresh()
5858
self.assertEqual(self.index['disabled'], '0')
5959

60+
def test_submit_and_clean(self):
61+
self.index.refresh()
62+
63+
originalCount = int(self.index['totalEventCount'])
64+
self.index.submit("Hello again!", sourcetype="Boris", host="meep")
65+
testlib.retry(self.index, 'totalEventCount', str(originalCount+1), step=1)
66+
self.assertEqual(self.index['totalEventCount'], str(originalCount+1))
67+
68+
self.index.clean(timeout=500)
69+
self.assertEqual(self.index['totalEventCount'], '0')
70+
6071
class IndexWithRestartTest(IndexTest):
6172
def setUp(self):
6273
super(IndexWithRestartTest, self).setUp()
@@ -67,7 +78,6 @@ def test_prefresh(self):
6778
index = self.service.indexes[self.index_name]
6879
self.assertEqual(self.index['disabled'], '0') # Index is prefreshed
6980

70-
7181
def test_submit(self):
7282
eventCount = int(self.index['totalEventCount'])
7383
self.assertEqual(self.index['sync'], '0')
@@ -76,19 +86,6 @@ def test_submit(self):
7686
testlib.retry(self.index, 'totalEventCount', str(eventCount+1), step=1)
7787
self.assertEqual(self.index['totalEventCount'], str(eventCount+1))
7888

79-
def test_submit_and_clean(self):
80-
# This fails on Ace beta because the index cannot be cleaned or deleted when disabled.
81-
self.index.refresh()
82-
originalCount = int(self.index['totalEventCount'])
83-
self.index.submit("Hello again!", sourcetype="Boris", host="meep")
84-
testlib.retry(self.index, 'totalEventCount', str(originalCount+1), step=1)
85-
self.assertEqual(self.index['totalEventCount'], str(originalCount+1))
86-
self.index.disable()
87-
self.index.clean()
88-
testlib.retry(self.index, 'totalEventCount', '0', step=1, times=60)
89-
self.index.refresh()
90-
self.assertEqual(self.index['totalEventCount'], '0')
91-
9289
def test_submit_via_attach(self):
9390
eventCount = int(self.index['totalEventCount'])
9491
cn = self.index.attach()

0 commit comments

Comments
 (0)