|
19 | 19 |
|
20 | 20 | import splunklib.client as client |
21 | 21 |
|
| 22 | +def highest_port(service, base_port, *kinds): |
| 23 | + """Find the first port >= base_port not in use by any input in kinds.""" |
| 24 | + highest_port = base_port |
| 25 | + for input in service.inputs.list(*kinds): |
| 26 | + port = int(input.name.split(':')[-1]) |
| 27 | + highest_port = max(port, highest_port) |
| 28 | + return highest_port |
| 29 | + |
| 30 | +class TestTcpInputNameHandling(testlib.SDKTestCase): |
| 31 | + def setUp(self): |
| 32 | + super(TestTcpInputNameHandling, self).setUp() |
| 33 | + self.base_port = highest_port(self.service, 10000, 'tcp', 'splunktcp') + 1 |
| 34 | + |
| 35 | + def tearDown(self): |
| 36 | + for input in self.service.inputs.list('tcp', 'splunktcp'): |
| 37 | + port = int(input.name.split(':')[-1]) |
| 38 | + if port >= self.base_port: |
| 39 | + input.delete() |
| 40 | + super(TestTcpInputNameHandling, self).tearDown() |
| 41 | + |
| 42 | + def test_create_tcp_port(self): |
| 43 | + for kind in ['tcp', 'splunktcp']: |
| 44 | + input = self.service.inputs.create(kind, str(self.base_port)) |
| 45 | + self.check_entity(input) |
| 46 | + input.delete() |
| 47 | + |
| 48 | + def test_cannot_create_with_restrictToHost_in_name(self): |
| 49 | + self.assertRaises( |
| 50 | + client.HTTPError, |
| 51 | + lambda: self.service.inputs.create('tcp', 'boris:10000') |
| 52 | + ) |
| 53 | + |
| 54 | + def test_create_tcp_ports_with_restrictToHost(self): |
| 55 | + for kind in ['tcp', 'splunktcp']: |
| 56 | + # Make sure we can create two restricted inputs on the same port |
| 57 | + boris = self.service.inputs.create(kind, str(self.base_port), restrictToHost='boris') |
| 58 | + natasha = self.service.inputs.create(kind, str(self.base_port), restrictToHost='natasha') |
| 59 | + # And that they both function |
| 60 | + boris.refresh() |
| 61 | + natasha.refresh() |
| 62 | + self.check_entity(boris) |
| 63 | + self.check_entity(natasha) |
| 64 | + boris.delete() |
| 65 | + natasha.delete() |
| 66 | + |
| 67 | + def test_restricted_to_unrestricted_collision(self): |
| 68 | + for kind in ['tcp', 'splunktcp']: |
| 69 | + restricted = self.service.inputs.create(kind, str(self.base_port), restrictToHost='boris') |
| 70 | + self.assertRaises( |
| 71 | + client.HTTPError, |
| 72 | + lambda: self.service.inputs.create(kind, str(self.base_port)) |
| 73 | + ) |
| 74 | + restricted.delete() |
| 75 | + |
| 76 | + def test_unrestricted_to_restricted_collision(self): |
| 77 | + for kind in ['tcp', 'splunktcp']: |
| 78 | + unrestricted = self.service.inputs.create(kind, str(self.base_port)) |
| 79 | + self.assertRaises( |
| 80 | + client.HTTPError, |
| 81 | + lambda: self.service.inputs.create(kind, str(self.base_port), restrictToHos='boris') |
| 82 | + ) |
| 83 | + unrestricted.delete() |
| 84 | + |
| 85 | + def test_update_restrictToHost(self): |
| 86 | + for kind in ['tcp', 'splunktcp']: |
| 87 | + boris = self.service.inputs.create(kind, str(self.base_port), restrictToHost='boris') |
| 88 | + with self.fake_splunk_version((4,3)): |
| 89 | + self.assertRaises( |
| 90 | + client.IllegalOperationException, |
| 91 | + lambda: boris.update(restrictToHost='hilda') |
| 92 | + ) |
| 93 | + if self.service.splunk_version >= (5,): |
| 94 | + boris.update(restrictToHost='hilda') |
| 95 | + boris.refresh() |
| 96 | + self.assertEqual('hilda:' + str(self.base_port), boris.name) |
| 97 | + boris.refresh() |
| 98 | + self.check_entity(boris) |
| 99 | + boris.delete() |
| 100 | + |
22 | 101 | class TestRead(testlib.SDKTestCase): |
23 | 102 | def test_read(self): |
24 | 103 | inputs = self.service.inputs |
@@ -80,26 +159,20 @@ def test_oneshot_on_nonexistant_file(self): |
80 | 159 | self.assertRaises(client.OperationFailedException, |
81 | 160 | self.service.inputs.oneshot, name) |
82 | 161 |
|
83 | | - |
84 | 162 | class TestInput(testlib.SDKTestCase): |
85 | 163 | def setUp(self): |
86 | 164 | super(TestInput, self).setUp() |
87 | 165 | inputs = self.service.inputs |
88 | | - test_inputs = [{'kind': 'tcp', 'name': '9999', 'host': 'sdk-test'}, |
89 | | - {'kind': 'udp', 'name': '9999', 'host': 'sdk-test'}] |
| 166 | + tcp_port = str(highest_port(self.service, 10000, 'tcp', 'splunktcp')+1) |
| 167 | + udp_port = str(highest_port(self.service, 10000, 'udp')+1) |
| 168 | + test_inputs = [{'kind': 'tcp', 'name': tcp_port, 'host': 'sdk-test'}, |
| 169 | + {'kind': 'udp', 'name': udp_port, 'host': 'sdk-test'}] |
90 | 170 | self._test_entities = {} |
91 | 171 |
|
92 | | - base_port = 10000 |
93 | | - while True: |
94 | | - if str(base_port) in inputs: |
95 | | - base_port += 1 |
96 | | - else: |
97 | | - break |
98 | | - |
99 | 172 | self._test_entities['tcp'] = \ |
100 | | - inputs.create('tcp', str(base_port), host='sdk-test') |
| 173 | + inputs.create('tcp', str(tcp_port), host='sdk-test') |
101 | 174 | self._test_entities['udp'] = \ |
102 | | - inputs.create('udp', str(base_port), host='sdk-test') |
| 175 | + inputs.create('udp', str(udp_port), host='sdk-test') |
103 | 176 |
|
104 | 177 | def tearDown(self): |
105 | 178 | super(TestInput, self).tearDown() |
@@ -184,7 +257,9 @@ def test_delete(self): |
184 | 257 | self.assertRaises(client.EntityDeletedException, |
185 | 258 | input_entity.refresh) |
186 | 259 | remaining -= 1 |
187 | | - |
| 260 | + |
| 261 | + |
| 262 | + |
188 | 263 |
|
189 | 264 | if __name__ == "__main__": |
190 | 265 | import unittest |
|
0 commit comments