Skip to content

Commit a422b20

Browse files
author
Frederick Ross
committed
Rework test_input.py
Fix a couple of things in related tests as well. Big speedup, but still several seconds to run.
1 parent 1cde214 commit a422b20

3 files changed

Lines changed: 87 additions & 108 deletions

File tree

‎tests/test_collection.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ def test_list(self):
7171
msg='on %s (expected: %s, found: %s)' % \
7272
(coll_name, expected, found))
7373

74+
def test_list_with_count(self):
75+
N = 5
76+
for coll_name in collections:
77+
coll = getattr(self.service, coll_name)
78+
expected = [ent.name for ent in coll.list()][:N]
79+
N = len(expected) # in case there are <N elements
80+
found = [ent.name for ent in coll.list(count=N)]
81+
self.assertEqual(expected, found,
82+
msg='on %s (expected %s, found %s' % \
83+
(coll_name, expected, found))
84+
7485
def test_list_with_search(self):
7586
for coll_name in collections:
7687
coll = getattr(self.service, coll_name)
@@ -81,7 +92,6 @@ def test_list_with_search(self):
8192
self.assertEqual(expected, found,
8293
msg='on %s (expected: %s, found: %s)' % \
8394
(coll_name, expected, found))
84-
return expected, found
8595

8696
def test_list_with_sort_dir(self):
8797
for coll_name in collections:

‎tests/test_index.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_submit(self):
6767
self.assertEqual(self.index['sync'], '0')
6868
self.assertEqual(self.index['disabled'], '0')
6969
self.index.submit("Hello again!", sourcetype="Boris", host="meep")
70-
# testlib.wait(self.index, lambda idx: int(idx['totalEventCount']) > 0, timeout=2)
70+
# testlib.restart() <-- waiting for a workaround
7171
self.index.refresh()
7272
self.assertEqual(int(self.index['totalEventCount']), eventCount+1)
7373

‎tests/test_input.py‎

Lines changed: 75 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -18,120 +18,89 @@
1818

1919
import splunklib.client as client
2020

21-
class TestCase(testlib.TestCase):
22-
def check_input(self, entity):
23-
self.check_entity(entity)
24-
25-
self.assertTrue(entity.kind is not None)
26-
27-
# Note: The disabled flag appears to be the only common content
28-
# attribute, as there are apparently cases where even index does
29-
# not appear.
30-
entity.content.disabled
31-
32-
if entity.kind == "ad":
33-
entity.content.monitorSubtree
34-
return
35-
36-
if entity.kind == "monitor":
37-
return
38-
39-
if entity.kind == "registry":
40-
entity.content.baseline
41-
entity.content.proc
42-
entity.content.hive
43-
entity.content.index
44-
return
45-
46-
if entity.kind == "script":
47-
entity.content._rcvbuf
48-
entity.content.index
49-
entity.content.interval
50-
return
51-
52-
if entity.kind == "tcp":
53-
entity.content._rcvbuf
54-
entity.content.group
55-
return
56-
57-
if entity.kind == "splunktcp":
58-
entity.content._rcvbuf
59-
entity.content.group
60-
entity.content.host
61-
entity.content.index
62-
return
63-
64-
if entity.kind == "udp":
65-
entity.content._rcvbuf
66-
entity.content.group
67-
entity.content.host
68-
entity.content.index
69-
return
70-
71-
if entity.kind == "win-event-log-collections":
72-
entity.content.lookup_host
73-
entity.content.name
74-
return
75-
76-
if entity.kind == "win-perfmon":
77-
entity.content.interval
78-
entity.content.object
79-
return
80-
81-
if entity.kind == "win-wmi-collections":
82-
entity.content.classes
83-
entity.content.interval
84-
entity.content.lookup_host
85-
entity.content.wql
86-
return
87-
88-
self.fail("Unknown input kind: '%s'" % entity.kind)
21+
test_inputs = [{'kind': 'tcp', 'name': '9999', 'host': 'sdk-test'},
22+
{'kind': 'udp', 'name': '9999', 'host': 'sdk-test'}]
8923

24+
class TestRead(testlib.TestCase):
9025
def test_read(self):
91-
service = client.connect(**self.opts.kwargs)
92-
93-
inputs = service.inputs
94-
95-
for item in inputs:
96-
self.check_input(item)
26+
inputs = self.service.inputs
27+
# count doesn't work on inputs; known problem tested for in
28+
# test_collection.py. This test will speed up dramatically
29+
# when that's fixed.
30+
for item in inputs.list(count=5):
31+
self.check_entity(item)
9732
item.refresh()
98-
self.check_input(item)
33+
self.check_entity(item)
9934

35+
def test_read_kind(self):
36+
inputs = self.service.inputs
10037
for kind in inputs.kinds:
101-
for item in inputs.list(kind):
38+
# count doesn't work on inputs; known problem tested for in
39+
# test_collection.py. This test will speed up dramatically
40+
# when that's fixed.
41+
for item in inputs.list(kind, count=3):
10242
self.assertEqual(item.kind, kind)
10343

104-
def test_crud(self):
105-
service = client.connect(**self.opts.kwargs)
106-
107-
inputs = service.inputs
108-
109-
for input in inputs:
110-
if input.name == '9999':
111-
inputs.delete(input.kind, input.name)
112-
self.assertFalse('9999' in inputs)
113-
inputs.create("tcp", "9999", host="sdk-test")
114-
self.assertTrue('9999' in inputs)
115-
input_ = inputs['9999']
116-
self.assertEqual(input_.kind, "tcp")
117-
self.assertEqual(input_['host'], "sdk-test")
118-
input_.update(host="foo", sourcetype="bar")
119-
input_.refresh()
120-
self.assertEqual(input_['host'], "foo")
121-
self.assertEqual(input_['sourcetype'], "bar")
122-
123-
inputs.create("udp", "9999", host="sdk-test")
124-
self.assertRaises(ValueError, inputs.__getitem__, '9999')
125-
self.assertTrue(isinstance(inputs['udp', '9999'], client.Input))
126-
self.assertTrue(isinstance(inputs['tcp', '9999'], client.Input))
127-
128-
for input in inputs:
129-
if input.name == '9999':
130-
inputs.delete(input.kind, input.name)
131-
self.assertFalse('9999' in inputs)
132-
self.assertFalse(inputs.contains('9999'));
133-
44+
class TestInput(testlib.TestCase):
45+
def setUp(self):
46+
testlib.TestCase.setUp(self)
47+
inputs = self.service.inputs
48+
self.test_entities = {}
49+
for test_input in test_inputs:
50+
self.test_entities[test_input['kind']] = \
51+
inputs.create(**test_input)
52+
53+
def tearDown(self):
54+
testlib.TestCase.tearDown(self)
55+
for test_input in test_inputs:
56+
try:
57+
self.service.inputs.delete(
58+
kind=test_input['kind'],
59+
name=test_input['name'])
60+
except KeyError:
61+
pass
62+
63+
def test_create(self):
64+
inputs = self.service.inputs
65+
for test_input in test_inputs:
66+
kind, name, host = test_input['kind'], test_input['name'], test_input['host']
67+
entity = self.test_entities[kind]
68+
entity = inputs[kind, name]
69+
self.check_entity(entity)
70+
self.assertEqual(entity.name, name)
71+
self.assertEqual(entity.kind, kind)
72+
self.assertEqual(entity.host, host)
13473

74+
def test_read(self):
75+
inputs = self.service.inputs
76+
for test_input in test_inputs:
77+
kind, name = test_input['kind'], test_input['name']
78+
this_entity = self.test_entities[kind]
79+
read_entity = inputs[kind, name]
80+
self.assertEqual(this_entity.kind, read_entity.kind)
81+
self.assertEqual(this_entity.name, read_entity.name)
82+
self.assertEqual(this_entity.host, read_entity.host)
83+
84+
def test_update(self):
85+
inputs = self.service.inputs
86+
for test_input in test_inputs:
87+
kind, name = test_input['kind'], test_input['name']
88+
entity = self.test_entities[kind, name]
89+
kwargs = {'host': 'foo', 'sourcetype': 'bar'}
90+
entity.update(**kwargs)
91+
entity.refresh()
92+
self.assertEqual(entity.host, kwargs['host'])
93+
self.assertEqual(entity.sourcetype, kwargs['sourcetype'])
94+
95+
def test_delete(self):
96+
inputs = self.service.inputs
97+
for test_input in test_inputs:
98+
kind, name = test_input['kind'], test_input['name']
99+
self.assertTrue(name in inputs)
100+
self.assertTrue((kind,name) in inputs)
101+
self.service.inputs.delete(kind['name'])
102+
self.assertFalse(name in inputs)
103+
self.assertFalse((kind,name) in inputs)
135104

136105
if __name__ == "__main__":
137106
testlib.main()

0 commit comments

Comments
 (0)