Skip to content

Commit 510bbfe

Browse files
committed
More refactoring and improvements to units.
1 parent d71f155 commit 510bbfe

File tree

8 files changed

+344
-231
lines changed

8 files changed

+344
-231
lines changed

tests/runtests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@
2727
"test_data.py",
2828
"test_binding.py",
2929
"test_collection.py",
30-
"test_client.py",
3130
"test_app.py",
3231
"test_conf.py",
3332
"test_event_type.py",
3433
"test_fired_alert.py",
3534
"test_index.py",
3635
"test_input.py",
36+
"test_job.py",
37+
"test_logger.py",
3738
"test_message.py",
39+
"test_role.py",
3840
"test_user.py",
3941
"test_saved_search.py",
42+
"test_service.py",
4043
"test_examples.py",
4144
]
4245

tests/test_app.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@
1919
import testlib
2020

2121
class TestCase(testlib.TestCase):
22+
def check_app(self, app):
23+
app.name
24+
app.path
25+
app.content
26+
app.metadata
27+
2228
def test_read(self):
2329
service = client.connect(**self.opts.kwargs)
2430

2531
for app in service.apps:
26-
app.name
27-
app.path
28-
app.content
29-
app.metadata
30-
31-
for app in service.apps:
32+
self.check_app(app)
3233
app.refresh()
34+
self.check_app(app)
3335

3436
def test_crud(self):
3537
service = client.connect(**self.opts.kwargs)

tests/test_conf.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
import testlib
2020

2121
class TestCase(testlib.TestCase):
22+
def test_read(self):
23+
service = client.connect(**self.opts.kwargs)
24+
25+
confs = service.confs
26+
27+
# Make sure the collection contains some of the expected entries
28+
self.assertTrue('eventtypes' in confs)
29+
self.assertTrue('indexes' in confs)
30+
self.assertTrue('inputs' in confs)
31+
self.assertTrue('props' in confs)
32+
self.assertTrue('transforms' in confs)
33+
self.assertTrue('savedsearches' in confs)
34+
35+
for conf in confs:
36+
conf.name
37+
conf.path
38+
for stanza in conf:
39+
stanza.name
40+
stanza.path
41+
stanza.content
42+
stanza.metadata
2243

2344
def test_crud(self):
2445
service = client.connect(**self.opts.kwargs)
@@ -44,27 +65,5 @@ def test_crud(self):
4465
props.delete('sdk-tests')
4566
self.assertFalse(props.contains('sdk-tests'))
4667

47-
def test_read(self):
48-
service = client.connect(**self.opts.kwargs)
49-
50-
confs = service.confs
51-
52-
# Make sure the collection contains some of the expected entries
53-
self.assertTrue('eventtypes' in confs)
54-
self.assertTrue('indexes' in confs)
55-
self.assertTrue('inputs' in confs)
56-
self.assertTrue('props' in confs)
57-
self.assertTrue('transforms' in confs)
58-
self.assertTrue('savedsearches' in confs)
59-
60-
for conf in confs:
61-
conf.name
62-
conf.path
63-
for stanza in conf:
64-
stanza.name
65-
stanza.path
66-
stanza.content
67-
stanza.metadata
68-
6968
if __name__ == "__main__":
7069
testlib.main()

tests/test_fired_alert.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ def test_crud(self):
9898

9999
# Wait for the saved search to register the triggered alert
100100
self.assertTrue(alert_count(search) <= count)
101-
testlib.wait(search, lambda search: alert_count(search) == count)
101+
testlib.wait(
102+
search,
103+
lambda search: alert_count(search) == count,
104+
timeout=120)
102105
self.assertEqual(alert_count(search), count)
103106

104107
# And now .. after all that trouble, verify that we see the alerts!

tests/test_job.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2011-2012 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
from time import sleep
18+
19+
import splunklib.client as client
20+
import splunklib.results as results
21+
22+
import testlib
23+
24+
class TestCase(testlib.TestCase):
25+
# UNDONE: Shouldn't the following assert something on exit?
26+
def check_properties(self, job, properties, secs = 10):
27+
while secs > 0 and len(properties) > 0:
28+
content = job.refresh().content
29+
30+
# Try and check every property we specified. If we fail,
31+
# we'll try again later. If we succeed, delete it so we
32+
# don't check it again.
33+
for k, v in properties.items():
34+
try:
35+
self.assertEqual(content[k], v)
36+
37+
# Since we succeeded, delete it
38+
del properties[k]
39+
except:
40+
pass
41+
42+
secs -= 1
43+
sleep(1)
44+
45+
def check_job(self, job):
46+
job.name
47+
job.path
48+
job.metadata
49+
job.content
50+
51+
keys = [
52+
'cursorTime', 'delegate', 'diskUsage', 'dispatchState',
53+
'doneProgress', 'dropCount', 'earliestTime', 'eventAvailableCount',
54+
'eventCount', 'eventFieldCount', 'eventIsStreaming',
55+
'eventIsTruncated', 'eventSearch', 'eventSorting', 'isDone',
56+
'isFailed', 'isFinalized', 'isPaused', 'isPreviewEnabled',
57+
'isRealTimeSearch', 'isRemoteTimeline', 'isSaved', 'isSavedSearch',
58+
'isZombie', 'keywords', 'label', 'latestTime', 'messages',
59+
'numPreviews', 'priority', 'remoteSearch', 'reportSearch',
60+
'resultCount', 'resultIsStreaming', 'resultPreviewCount',
61+
'runDuration', 'scanCount', 'searchProviders', 'sid',
62+
'statusBuckets', 'ttl'
63+
]
64+
for key in keys: self.assertTrue(key in job.content)
65+
66+
def test_read(self):
67+
service = client.connect(**self.opts.kwargs)
68+
69+
for job in service.jobs:
70+
self.check_job(job)
71+
job.refresh()
72+
self.check_job(job)
73+
74+
def test_crud(self):
75+
service = client.connect(**self.opts.kwargs)
76+
77+
jobs = service.jobs
78+
79+
if not service.indexes.contains("sdk-tests"):
80+
service.indexes.create("sdk-tests")
81+
service.indexes['sdk-tests'].clean()
82+
83+
# Make sure we can create a job
84+
job = jobs.create("search index=sdk-tests")
85+
self.assertTrue(jobs.contains(job.sid))
86+
87+
# Make sure we can cancel the job
88+
job.cancel()
89+
self.assertFalse(jobs.contains(job.sid))
90+
91+
# Search for non-existant data
92+
job = jobs.create("search index=sdk-tests TERM_DOES_NOT_EXIST")
93+
testlib.wait(job, lambda job: job['isDone'] == '1')
94+
self.assertEqual(job['isDone'], '1')
95+
self.assertEqual(job['eventCount'], '0')
96+
job.finalize()
97+
98+
# Create a new job
99+
job = jobs.create("search * | head 1 | stats count")
100+
self.assertTrue(jobs.contains(job.sid))
101+
102+
# Set various properties on it
103+
job.disable_preview()
104+
job.pause()
105+
job.set_ttl(1000)
106+
job.set_priority(5)
107+
job.touch()
108+
job.refresh()
109+
110+
# Assert that the properties got set properly
111+
self.check_properties(job, {
112+
'isPreviewEnabled': '0',
113+
'isPaused': '1',
114+
'ttl': '1000',
115+
'priority': '5'
116+
})
117+
118+
# Set more properties
119+
job.enable_preview()
120+
job.unpause()
121+
job.finalize()
122+
job.refresh()
123+
124+
# Assert that they got set properly
125+
self.check_properties(job, {
126+
'isPreviewEnabled': '1',
127+
'isPaused': '0',
128+
'isFinalized': '1'
129+
})
130+
131+
job.cancel()
132+
self.assertFalse(jobs.contains(job.sid))
133+
134+
def test_results(self):
135+
service = client.connect(**self.opts.kwargs)
136+
137+
jobs = service.jobs
138+
139+
# Run a new job to get the results, but we also make
140+
# sure that there is at least one event in the index already
141+
index = service.indexes['sdk-tests']
142+
old_event_count = int(index['totalEventCount'])
143+
if old_event_count == 0:
144+
index.submit("test event")
145+
testlib.wait(index, lambda index: index['totalEventCount'] == '1')
146+
147+
job = jobs.create("search index=sdk-tests | head 1 | stats count")
148+
testlib.wait(job, lambda job: job['isDone'] == '1')
149+
self.assertEqual(job['isDone'], '1')
150+
151+
# Fetch the results
152+
reader = results.ResultsReader(job.results())
153+
154+
# The first one should always be RESULTS
155+
kind, result = reader.next()
156+
self.assertEqual(results.RESULTS, kind)
157+
self.assertEqual(int(result["preview"]), 0)
158+
159+
# The second is always the actual result
160+
kind, result = reader.next()
161+
self.assertEqual(results.RESULT, kind)
162+
self.assertEqual(int(result["count"]), 1)
163+
164+
if __name__ == "__main__":
165+
testlib.main()

tests/test_logger.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2011-2012 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
import splunklib.client as client
18+
19+
import testlib
20+
21+
LEVELS = ["INFO", "WARN", "ERROR", "DEBUG", "CRIT"]
22+
23+
class TestCase(testlib.TestCase):
24+
def test_read(self):
25+
service = client.connect(**self.opts.kwargs)
26+
27+
for logger in service.loggers:
28+
logger.name
29+
logger.path
30+
logger.content
31+
self.assertTrue(logger['level'] in LEVELS)
32+
33+
def test_crud(self):
34+
service = client.connect(**self.opts.kwargs)
35+
36+
self.assertTrue(service.loggers.contains("AuditLogger"))
37+
logger = service.loggers['AuditLogger']
38+
39+
saved = logger['level']
40+
for level in LEVELS:
41+
logger.update(level=level)
42+
logger.refresh()
43+
self.assertEqual(service.loggers['AuditLogger']['level'], level)
44+
45+
logger.update(level=saved)
46+
logger.refresh()
47+
self.assertEqual(service.loggers['AuditLogger']['level'], saved)
48+
49+
if __name__ == "__main__":
50+
testlib.main()

tests/test_role.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2011-2012 Splunk, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
import splunklib.client as client
18+
19+
import testlib
20+
21+
class TestCase(testlib.TestCase):
22+
def check_role(self, role):
23+
role.name
24+
role.path
25+
role.metadata
26+
role.content
27+
28+
service = role.service
29+
capabilities = service.capabilities
30+
for capability in role.content.capabilities:
31+
self.assertTrue(capability in capabilities)
32+
33+
def test_read(self):
34+
service = client.connect(**self.opts.kwargs)
35+
36+
for role in service.roles:
37+
self.check_role(role)
38+
role.refresh()
39+
self.check_role(role)
40+
41+
def test_crud(self):
42+
service = client.connect(**self.opts.kwargs)
43+
44+
roles = service.roles
45+
46+
if roles.contains("sdk-tester"): roles.delete("sdk-tester")
47+
self.assertFalse(roles.contains("sdk-tester"))
48+
49+
role = roles.create("sdk-tester")
50+
self.assertTrue(roles.contains("sdk-tester"))
51+
52+
self.assertTrue(role.content.has_key('capabilities'))
53+
54+
roles.delete("sdk-tester")
55+
self.assertFalse(roles.contains("sdk-tester"))
56+
57+
if __name__ == "__main__":
58+
testlib.main()

0 commit comments

Comments
 (0)