Skip to content

Commit e092e70

Browse files
author
Frederick Ross
committed
Fix code review comments from David Foster.
Subclass ValueError into AmbiguousReference for cases when there are multiple entities in a collection with the same name. Comment on a few tests that are weird. Simplify __contains__ to refer to __getitem__, which now handles all the necessary cases.
1 parent fa8c121 commit e092e70

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

splunklib/client.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class IncomparableException(Exception):
113113
class JobNotReadyException(Exception):
114114
pass
115115

116+
class AmbiguousReference(ValueError):
117+
pass
118+
116119
def trailing(template, *targets):
117120
"""Substring of *template* following all *targets*.
118121
@@ -886,19 +889,12 @@ def __contains__(self, name):
886889
if autologin is enabled.
887890
"""
888891
try:
889-
response = self.get(name)
890-
entries = self._load_list(response)
891-
if len(entries) == 0:
892-
# We need this because in 4.2 and 4.3,
893-
# fired alerts return an empty feed instead of 404.
894-
return False
895-
else:
896-
return True
897-
except HTTPError as he:
898-
if he.status == 404:
899-
return False
900-
else:
901-
raise
892+
self[name]
893+
return True
894+
except KeyError:
895+
return False
896+
except AmbiguousReference:
897+
return True
902898

903899
def __getitem__(self, key):
904900
"""Fetch an item named *key* from this collection.
@@ -958,7 +954,7 @@ def __getitem__(self, key):
958954
response = self.get(key, owner=ns.owner, app=ns.app)
959955
entries = self._load_list(response)
960956
if len(entries) > 1:
961-
raise ValueError("Found multiple entities named '%s'; please specify a namespace." % key)
957+
raise AmbiguousReference("Found multiple entities named '%s'; please specify a namespace." % key)
962958
elif len(entries) == 0:
963959
raise KeyError(key)
964960
else:
@@ -1013,7 +1009,10 @@ def _entity_path(self, state):
10131009
"""Calculate the path to an entity to be returned.
10141010
10151011
*state* should be the dictionary returned by
1016-
:func:`_parse_atom_entry`.
1012+
:func:`_parse_atom_entry`. :func:`_entity_path` extracts the
1013+
link to this entity from *state*, and strips all the namespace
1014+
prefixes from it to leave only the relative path of the entity
1015+
itself, sans namespace.
10171016
10181017
:rtype: string
10191018
:returns: an absolute path
@@ -1522,6 +1521,8 @@ def __contains__(self, key):
15221521
return True
15231522
except KeyError:
15241523
return False
1524+
except AmbiguousReference:
1525+
return True
15251526

15261527

15271528
def create(self, kind, name, **kwargs):

tests/test_fired_alert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ def f(search):
9494
testlib.wait(search, f, timeout=120)
9595
self.assertEqual(len(search.history()), 1)
9696

97-
# When it first runs the alert count should be zero.
97+
# There should be no alerts if the search job hasn't been
98+
# created yet.
9899
search.refresh()
99-
print alert_count(search)
100100
self.assertTrue((alert_count(search) == 0) == (search_name not in fired_alerts))
101101

102102
# Submit events and verify that they each trigger the expected

tests/test_job.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def test_results(self):
182182

183183
result = reader.next()
184184
self.assertTrue(isinstance(result, dict))
185-
self.assertLessEqual(int(result["count"]), 1000)
185+
self.assertLessEqual(int(result["count"]), 1)
186186

187187
# Repeat the same thing, but without the .is_preview reference.
188188
job = jobs.create("search index=_internal | head 1 | stats count")
@@ -193,7 +193,7 @@ def test_results(self):
193193
self.assertEqual(job['isDone'], '1')
194194
result = reader.next()
195195
self.assertTrue(isinstance(result, dict))
196-
self.assertLessEqual(int(result["count"]), 100)
196+
self.assertLessEqual(int(result["count"]), 1)
197197

198198
def test_results_reader(self):
199199
# Run jobs.export("search index=_internal | stats count",

tests/test_saved_search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ def test_dispatch(self):
199199
try:
200200
job.preview().close()
201201
except ValueError:
202-
pass
202+
pass # Probably not in a state that we can actually get
203+
# events from yet.
203204
job.cancel()
204205

205206
# Dispatch with some additional options

0 commit comments

Comments
 (0)