|
18 | 18 |
|
19 | 19 | import splunklib.client as client |
20 | 20 |
|
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'}] |
89 | 23 |
|
| 24 | +class TestRead(testlib.TestCase): |
90 | 25 | 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) |
97 | 32 | item.refresh() |
98 | | - self.check_input(item) |
| 33 | + self.check_entity(item) |
99 | 34 |
|
| 35 | + def test_read_kind(self): |
| 36 | + inputs = self.service.inputs |
100 | 37 | 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): |
102 | 42 | self.assertEqual(item.kind, kind) |
103 | 43 |
|
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) |
134 | 73 |
|
| 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) |
135 | 104 |
|
136 | 105 | if __name__ == "__main__": |
137 | 106 | testlib.main() |
0 commit comments