Skip to content

Commit 30bbe79

Browse files
author
Frederick Ross
committed
Extended test suite a little more for restrictToHost, got it all passing again.
1 parent 3f83362 commit 30bbe79

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

splunklib/client.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,17 +1670,19 @@ def __init__(self, service, path, kind=None, **kwargs):
16701670
Entity.__init__(self, service, path, **kwargs)
16711671
if kind is None:
16721672
path_segments = path.split('/')
1673-
i = path_segments.index('inputs')
1674-
self.kind = '/'.join(path_segments[i+1:-1])
1675-
assert self.kind is not None
1673+
i = path_segments.index('inputs')+1
1674+
if path_segments[i] == 'tcp':
1675+
self.kind = path_segments[i] + '/' + path_segments[i+1]
1676+
else:
1677+
self.kind = path_segments[i]
16761678
else:
16771679
self.kind = kind
16781680

1679-
# Maintain compatibility with older kind labels
1680-
if self.kind == 'tcp/raw':
1681-
self.kind = 'tcp'
1682-
if self.kind == 'tcp/cooked':
1683-
self.kind = 'splunktcp'
1681+
# Handle old input kind names.
1682+
if self.kind == 'tcp':
1683+
self.kind = 'tcp/raw'
1684+
if self.kind == 'splunktcp':
1685+
self.kind = 'tcp/cooked'
16841686

16851687
def update(self, **kwargs):
16861688
if self.kind not in ['tcp', 'splunktcp', 'tcp/raw', 'tcp/cooked']:
@@ -1698,7 +1700,7 @@ def update(self, **kwargs):
16981700
base_path, name = self.path.rsplit('/', 2)[:-1]
16991701
else:
17001702
base_path, name = self.path.rsplit('/', 1)
1701-
if 'restrictToHost' in new_kwargs:
1703+
if 'restrictToHost' in new_kwargs and new_kwargs['restrictToHost'] != '':
17021704
self.path = base_path + '/' + new_kwargs['restrictToHost'] + ':' + port
17031705
else:
17041706
self.path = base_path + '/' + port
@@ -1839,7 +1841,8 @@ def _get_kind_list(self, subpath=[]):
18391841
if entry.title == 'all' or this_subpath == ['tcp','ssl']:
18401842
continue
18411843
elif 'create' in [x.rel for x in entry.link]:
1842-
kinds.append('/'.join(subpath + [entry.title]))
1844+
path = '/'.join(subpath + [entry.title])
1845+
kinds.append(path)
18431846
else:
18441847
subkinds = self._get_kind_list(subpath + [entry.title])
18451848
kinds.extend(subkinds)

tests/test_input.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def test_cannot_create_with_restrictToHost_in_name(self):
5151
lambda: self.service.inputs.create('tcp', 'boris:10000')
5252
)
5353

54+
def test_remove_host_restriction(self):
55+
input = self.service.inputs.create('tcp', str(self.base_port), restrictToHost='boris')
56+
input.update(restrictToHost='')
57+
input.refresh()
58+
self.check_entity(input)
59+
input.delete()
60+
5461
def test_create_tcp_ports_with_restrictToHost(self):
5562
for kind in ['tcp', 'splunktcp']:
5663
# Make sure we can create two restricted inputs on the same port
@@ -101,9 +108,13 @@ def test_update_restrictToHost(self):
101108
def test_update_nonrestrictToHost(self):
102109
for kind in ['tcp', 'splunktcp']:
103110
input = self.service.inputs.create(kind, str(self.base_port), restrictToHost='boris')
104-
input.update(host='meep')
105-
input.refresh()
106-
self.assertTrue(input.name.startswith('boris'))
111+
try:
112+
input.update(host='meep')
113+
input.refresh()
114+
self.assertTrue(input.name.startswith('boris'))
115+
except:
116+
input.delete()
117+
raise
107118

108119
class TestRead(testlib.SDKTestCase):
109120
def test_read(self):
@@ -170,20 +181,19 @@ class TestInput(testlib.SDKTestCase):
170181
def setUp(self):
171182
super(TestInput, self).setUp()
172183
inputs = self.service.inputs
173-
tcp_port = str(highest_port(self.service, 10000, 'tcp', 'splunktcp')+1)
174-
udp_port = str(highest_port(self.service, 10000, 'udp')+1)
175-
restricted_tcp_port = str(highest_port(self.service, int(tcp_port)+1, 'tcp', 'splunktcp')+1)
176-
test_inputs = [{'kind': 'tcp', 'name': tcp_port, 'host': 'sdk-test'},
177-
{'kind': 'udp', 'name': udp_port, 'host': 'sdk-test'},
178-
{'kind': 'tcp', 'name': 'boris:' + restricted_tcp_port, 'host': 'sdk-test'}]
184+
unrestricted_port = str(highest_port(self.service, 10000, 'tcp', 'splunktcp', 'udp')+1)
185+
restricted_port = str(highest_port(self.service, int(unrestricted_port)+1, 'tcp', 'splunktcp')+1)
186+
test_inputs = [{'kind': 'tcp', 'name': unrestricted_port, 'host': 'sdk-test'},
187+
{'kind': 'udp', 'name': unrestricted_port, 'host': 'sdk-test'},
188+
{'kind': 'tcp', 'name': 'boris:' + restricted_port, 'host': 'sdk-test'}]
179189
self._test_entities = {}
180190

181191
self._test_entities['tcp'] = \
182-
inputs.create('tcp', str(tcp_port), host='sdk-test')
192+
inputs.create('tcp', unrestricted_port, host='sdk-test')
183193
self._test_entities['udp'] = \
184-
inputs.create('udp', str(udp_port), host='sdk-test')
194+
inputs.create('udp', unrestricted_port, host='sdk-test')
185195
self._test_entities['restrictedTcp'] = \
186-
inputs.create('tcp', restricted_tcp_port, restrictToHost='boris')
196+
inputs.create('tcp', restricted_port, restrictToHost='boris')
187197

188198
def tearDown(self):
189199
super(TestInput, self).tearDown()
@@ -260,8 +270,9 @@ def test_delete(self):
260270
inputs.delete(name)
261271
self.assertFalse(name in inputs)
262272
else:
263-
self.assertRaises(client.AmbiguousReferenceException,
264-
inputs.delete, name)
273+
if not name.startswith('boris'):
274+
self.assertRaises(client.AmbiguousReferenceException,
275+
inputs.delete, name)
265276
self.service.inputs.delete(kind, name)
266277
self.assertFalse((kind,name) in inputs)
267278
self.assertRaises(client.EntityDeletedException,

0 commit comments

Comments
 (0)