Skip to content

Commit d71f155

Browse files
committed
More refactoring of units.
1 parent d0551d6 commit d71f155

File tree

8 files changed

+394
-193
lines changed

8 files changed

+394
-193
lines changed

tests/runtests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
"test_collection.py",
3030
"test_client.py",
3131
"test_app.py",
32+
"test_conf.py",
3233
"test_event_type.py",
3334
"test_fired_alert.py",
35+
"test_index.py",
36+
"test_input.py",
37+
"test_message.py",
3438
"test_user.py",
3539
"test_saved_search.py",
3640
"test_examples.py",

tests/test_app.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818

1919
import testlib
2020

21-
def delete_app(service, name):
22-
if (service.apps.contains(name)):
23-
service.apps.delete(name)
24-
testlib.restart(service)
25-
2621
class TestCase(testlib.TestCase):
2722
def test_read(self):
2823
service = client.connect(**self.opts.kwargs)
@@ -41,7 +36,7 @@ def test_crud(self):
4136

4237
appname = "sdk-test-app"
4338

44-
delete_app(service, appname)
39+
testlib.delete_app(service, appname)
4540
self.assertFalse(appname in service.apps)
4641

4742
kwargs = {
@@ -72,7 +67,7 @@ def test_crud(self):
7267
self.assertEqual(app['manageable'], "0")
7368
self.assertEqual(app['visible'], "0")
7469

75-
delete_app(service, appname)
70+
testlib.delete_app(service, appname)
7671
self.assertFalse(appname in service.apps)
7772

7873
if __name__ == "__main__":

tests/test_client.py

Lines changed: 4 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17-
from os import path
1817
from time import sleep
1918

2019
import splunklib.client as client
@@ -23,11 +22,6 @@
2322

2423
import testlib
2524

26-
def delete_app(service, name):
27-
if (service.apps.contains(name)):
28-
service.apps.delete(name)
29-
testlib.restart(service)
30-
3125
# Verify that we can instantiate and connect to a service, test basic
3226
# interaction with the service and make sure we can connect and interact
3327
# with a variety of namespace configurations.
@@ -62,7 +56,7 @@ def test(self):
6256

6357
appname = "sdk-test-app"
6458
username = "sdk-test-user"
65-
delete_app(service, appname)
59+
testlib.delete_app(service, appname)
6660
if username in service.users: service.users.delete(username)
6761
self.assertFalse(service.apps.contains(appname))
6862
self.assertFalse(service.users.contains(username))
@@ -98,7 +92,7 @@ def test(self):
9892
service_ns.apps()
9993

10094
# Cleanup
101-
delete_app(service, appname)
95+
testlib.delete_app(service, appname)
10296
service.users.delete(username)
10397

10498
self.assertFalse(service.apps.contains(appname))
@@ -127,34 +121,6 @@ def test_capabilities(self):
127121
capabilities = service.capabilities
128122
for item in expected: self.assertTrue(item in capabilities)
129123

130-
def test_confs(self):
131-
service = client.connect(**self.opts.kwargs)
132-
133-
for conf in service.confs:
134-
for stanza in conf: stanza.refresh()
135-
break # no need to refresh every conf file for the test
136-
137-
self.assertTrue(service.confs.contains('props'))
138-
props = service.confs['props']
139-
140-
if 'sdk-tests' in props: props.delete('sdk-tests')
141-
self.assertFalse('sdk-tests' in props)
142-
143-
stanza = props.create('sdk-tests')
144-
self.assertTrue(props.contains('sdk-tests'))
145-
self.assertEqual(stanza.name,'sdk-tests')
146-
self.assertTrue('maxDist' in stanza.content)
147-
value = int(stanza['maxDist'])
148-
stanza.update(maxDist=value+1)
149-
stanza.refresh()
150-
self.assertEqual(stanza['maxDist'], str(value + 1))
151-
stanza.update(maxDist=value)
152-
stanza.refresh()
153-
self.assertEqual(stanza['maxDist'], str(value))
154-
155-
props.delete('sdk-tests')
156-
self.assertFalse(props.contains('sdk-tests'))
157-
158124
def test_info(self):
159125
service = client.connect(**self.opts.kwargs)
160126

@@ -165,113 +131,6 @@ def test_info(self):
165131
"os_build", "os_name", "os_version", "serverName", "version" ]
166132
for key in keys: self.assertTrue(key in info.keys())
167133

168-
def test_indexes(self):
169-
service = client.connect(**self.opts.kwargs)
170-
171-
for index in service.indexes: index.refresh()
172-
173-
if not service.indexes.contains("sdk-tests"):
174-
service.indexes.create("sdk-tests")
175-
self.assertTrue(service.indexes.contains("sdk-tests"))
176-
177-
# Scan indexes and make sure the entities look familiar
178-
attrs = [
179-
'thawedPath', 'quarantineFutureSecs',
180-
'isInternal', 'maxHotBuckets', 'disabled', 'homePath',
181-
'compressRawdata', 'maxWarmDBCount', 'frozenTimePeriodInSecs',
182-
'memPoolMB', 'maxHotSpanSecs', 'minTime', 'blockSignatureDatabase',
183-
'serviceMetaPeriod', 'coldToFrozenDir', 'quarantinePastSecs',
184-
'maxConcurrentOptimizes', 'maxMetaEntries', 'minRawFileSyncSecs',
185-
'maxMemMB', 'maxTime', 'partialServiceMetaPeriod', 'maxHotIdleSecs',
186-
'coldToFrozenScript', 'thawedPath_expanded', 'coldPath_expanded',
187-
'defaultDatabase', 'throttleCheckPeriod', 'totalEventCount',
188-
'enableRealtimeSearch', 'indexThreads', 'maxDataSize',
189-
'currentDBSizeMB', 'homePath_expanded', 'blockSignSize',
190-
'syncMeta', 'assureUTF8', 'rotatePeriodInSecs', 'sync',
191-
'suppressBannerList', 'rawChunkSizeBytes', 'coldPath',
192-
'maxTotalDataSizeMB'
193-
]
194-
for index in service.indexes:
195-
for attr in attrs: self.assertTrue(attr in index.content)
196-
197-
index = service.indexes['sdk-tests']
198-
199-
index.disable()
200-
index.refresh()
201-
self.assertEqual(index['disabled'], '1')
202-
203-
index.enable()
204-
index.refresh()
205-
self.assertEqual(index['disabled'], '0')
206-
207-
index.clean()
208-
index.refresh()
209-
self.assertEqual(index['totalEventCount'], '0')
210-
211-
cn = index.attach()
212-
cn.write("Hello World!")
213-
cn.close()
214-
testlib.wait(index, lambda index: index['totalEventCount'] == '1')
215-
self.assertEqual(index['totalEventCount'], '1')
216-
217-
index.submit("Hello again!!")
218-
testlib.wait(index, lambda index: index['totalEventCount'] == '2')
219-
self.assertEqual(index['totalEventCount'], '2')
220-
221-
# The following test must run on machine where splunkd runs,
222-
# otherwise a failure is expected
223-
testpath = path.dirname(path.abspath(__file__))
224-
index.upload(path.join(testpath, "testfile.txt"))
225-
testlib.wait(index, lambda index: index['totalEventCount'] == '3')
226-
self.assertEqual(index['totalEventCount'], '3')
227-
228-
index.clean()
229-
index.refresh()
230-
self.assertEqual(index['totalEventCount'], '0')
231-
232-
def test_indexes_metadata(self):
233-
service = client.connect(**self.opts.kwargs)
234-
235-
metadata = service.indexes.itemmeta()
236-
self.assertTrue(metadata.has_key('eai:acl'))
237-
self.assertTrue(metadata.has_key('eai:attributes'))
238-
for index in service.indexes:
239-
metadata = index.metadata
240-
self.assertTrue(metadata.has_key('eai:acl'))
241-
self.assertTrue(metadata.has_key('eai:attributes'))
242-
243-
def test_inputs(self):
244-
service = client.connect(**self.opts.kwargs)
245-
inputs = service.inputs
246-
247-
for input_ in inputs: input_.refresh()
248-
249-
# Scan inputs and look for some common attributes
250-
# Note: The disabled flag appears to be the only common attribute, as
251-
# there are apparently cases where even index does not appear.
252-
attrs = ['disabled']
253-
for input_ in inputs:
254-
for attr in attrs:
255-
self.assertTrue(attr in input_.content)
256-
257-
for kind in inputs.kinds:
258-
for input_ in inputs.list(kind):
259-
self.assertEqual(input_.kind, kind)
260-
261-
if inputs.contains('9999'): inputs.delete('9999')
262-
self.assertFalse(inputs.contains('9999'))
263-
inputs.create("tcp", "9999", host="sdk-test")
264-
self.assertTrue(inputs.contains('9999'))
265-
input_ = inputs['9999']
266-
self.assertEqual(input_.kind, "tcp")
267-
self.assertEqual(input_['host'], "sdk-test")
268-
input_.update(host="foo", sourcetype="bar")
269-
input_.refresh()
270-
self.assertEqual(input_['host'], "foo")
271-
self.assertEqual(input_['sourcetype'], "bar")
272-
inputs.delete('9999')
273-
self.assertFalse(inputs.contains('9999'))
274-
275134
# UNDONE: Shouldnt the following assert something on exit?
276135
def check_properties(self, job, properties, secs = 10):
277136
while secs > 0 and len(properties) > 0:
@@ -307,7 +166,7 @@ def test_jobs(self):
307166
self.assertTrue(jobs.contains(job.sid))
308167

309168
# Scan jobs and make sure the entities look familiar
310-
attrs = [
169+
keys = [
311170
'cursorTime', 'delegate', 'diskUsage', 'dispatchState',
312171
'doneProgress', 'dropCount', 'earliestTime', 'eventAvailableCount',
313172
'eventCount', 'eventFieldCount', 'eventIsStreaming',
@@ -321,7 +180,7 @@ def test_jobs(self):
321180
'statusBuckets', 'ttl'
322181
]
323182
for job in jobs:
324-
for attr in attrs: self.assertTrue(attr in job.content)
183+
for key in keys: self.assertTrue(key in job.content)
325184

326185
# Make sure we can cancel the job
327186
job.cancel()
@@ -428,47 +287,6 @@ def test_parse(self):
428287
except:
429288
self.fail()
430289

431-
def test_messages(self):
432-
service = client.connect(**self.opts.kwargs)
433-
434-
messages = service.messages
435-
436-
if messages.contains('sdk-test-message1'):
437-
messages.delete('sdk-test-message1')
438-
if messages.contains('sdk-test-message2'):
439-
messages.delete('sdk-test-message2')
440-
self.assertFalse(messages.contains('sdk-test-message1'))
441-
self.assertFalse(messages.contains('sdk-test-message2'))
442-
443-
messages.create('sdk-test-message1', value="Hello!")
444-
self.assertTrue(messages.contains('sdk-test-message1'))
445-
self.assertEqual(messages['sdk-test-message1'].value, "Hello!")
446-
447-
messages.create('sdk-test-message2', value="World!")
448-
self.assertTrue(messages.contains('sdk-test-message2'))
449-
self.assertEqual(messages['sdk-test-message2'].value, "World!")
450-
451-
messages.delete('sdk-test-message1')
452-
messages.delete('sdk-test-message2')
453-
self.assertFalse(messages.contains('sdk-test-message1'))
454-
self.assertFalse(messages.contains('sdk-test-message2'))
455-
456-
# Verify that message names with spaces work correctly
457-
if messages.contains('sdk test message'):
458-
messages.delete('sdk test message')
459-
self.assertFalse(messages.contains('sdk test message'))
460-
messages.create('sdk test message', value="xyzzy")
461-
self.assertTrue(messages.contains('sdk test message'))
462-
self.assertEqual(messages['sdk test message'].value, "xyzzy")
463-
messages.delete('sdk test message')
464-
self.assertFalse(messages.contains('sdk test message'))
465-
466-
# Verify that create raises a ValueError on invalid name args
467-
with self.assertRaises(ValueError):
468-
messages.create(None, value="What?")
469-
messages.create(42, value="Who, me?")
470-
messages.create([1, 2, 3], value="Who, me?")
471-
472290
def test_restart(self):
473291
service = client.connect(**self.opts.kwargs)
474292
testlib.restart(service)

tests/test_conf.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
23+
def test_crud(self):
24+
service = client.connect(**self.opts.kwargs)
25+
26+
self.assertTrue('props' in service.confs)
27+
props = service.confs['props']
28+
29+
if 'sdk-tests' in props: props.delete('sdk-tests')
30+
self.assertFalse('sdk-tests' in props)
31+
32+
stanza = props.create('sdk-tests')
33+
self.assertTrue(props.contains('sdk-tests'))
34+
self.assertEqual(stanza.name,'sdk-tests')
35+
self.assertTrue('maxDist' in stanza.content)
36+
value = int(stanza['maxDist'])
37+
stanza.update(maxDist=value+1)
38+
stanza.refresh()
39+
self.assertEqual(stanza['maxDist'], str(value + 1))
40+
stanza.update(maxDist=value)
41+
stanza.refresh()
42+
self.assertEqual(stanza['maxDist'], str(value))
43+
44+
props.delete('sdk-tests')
45+
self.assertFalse(props.contains('sdk-tests'))
46+
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+
69+
if __name__ == "__main__":
70+
testlib.main()

0 commit comments

Comments
 (0)