Skip to content

Commit 1894dca

Browse files
author
Frederick Ross
committed
Resolved David's comments.
1 parent 7aa2b55 commit 1894dca

File tree

7 files changed

+63
-48
lines changed

7 files changed

+63
-48
lines changed

tests/test_conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_confs(self):
7979
key = testlib.tmpname()
8080
val = testlib.tmpname()
8181
stanza.update(**{key: val})
82-
self.assertEventuallyEqual(1, lambda: stanza.refresh() and len(stanza), pause_time=0.2)
82+
self.assertEventuallyTrue(lambda: stanza.refresh() and len(stanza) == 1, pause_time=0.2)
8383
self.assertEqual(len(stanza), 1)
8484
self.assertTrue(key in stanza)
8585

tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def test_analytics(self):
284284
tracker.track("test_event", distinct_id="123abc", abc="12345")
285285

286286
# Wait until the events get indexed
287-
self.assertEventuallyEqual('2', lambda: index.refresh()['totalEventCount'])
287+
self.assertEventuallyTrue(lambda: index.refresh()['totalEventCount'] == '2')
288288

289289
# Now, we create a retriever to retrieve the events
290290
retriever = analytics.output.AnalyticsRetriever(

tests/test_fired_alert.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,24 @@ def test_alerts_on_events(self):
6262
self.assertEqual(len(self.saved_search.fired_alerts), 0)
6363

6464
self.index.enable()
65-
self.assertEventuallyEqual('0', lambda: self.index.refresh() and self.index['disabled'], timeout=25)
65+
self.assertEventuallyTrue(lambda: self.index.refresh() and self.index['disabled'] == '0', timeout=25)
6666

6767
eventCount = int(self.index['totalEventCount'])
6868
self.assertEqual(self.index['sync'], '0')
6969
self.assertEqual(self.index['disabled'], '0')
7070
self.index.refresh()
7171
self.index.submit('This is a test ' + testlib.tmpname(),
7272
sourcetype='sdk_use', host='boris')
73-
self.assertEventuallyEqual(str(eventCount+1), lambda: self.index.refresh() and self.index['totalEventCount'], timeout=50)
74-
self.assertEventuallyEqual(
75-
1,
76-
lambda: self.saved_search.refresh() and self.saved_search.alert_count,
77-
timeout=200
78-
)
73+
def f():
74+
self.index.refresh()
75+
return int(self.index['totalEventCount']) == eventCount+1
76+
self.assertEventuallyTrue(f, timeout=50)
77+
78+
def g():
79+
self.saved_search.refresh()
80+
return self.saved_search.alert_count == 1
81+
self.assertEventuallyTrue(g, timeout=200)
82+
7983
alerts = self.saved_search.fired_alerts
8084
self.assertEqual(len(alerts), 1)
8185

tests/test_index.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def setUp(self):
2727
super(IndexTest, self).setUp()
2828
self.index_name = testlib.tmpname()
2929
self.index = self.service.indexes.create(self.index_name)
30-
self.assertEventuallyEqual('0', lambda: self.index.refresh()['disabled'])
30+
self.assertEventuallyTrue(lambda: self.index.refresh()['disabled'] == '0')
3131

3232
def tearDown(self):
3333
super(IndexTest, self).tearDown()
@@ -73,7 +73,7 @@ def test_submit_and_clean(self):
7373
self.index.refresh()
7474
originalCount = int(self.index['totalEventCount'])
7575
self.index.submit("Hello again!", sourcetype="Boris", host="meep")
76-
self.assertEventuallyEqual(originalCount+1, self.totalEventCount)
76+
self.assertEventuallyTrue(lambda: self.totalEventCount == originalCount+1)
7777

7878
# Cleaning an enabled index on 4.x takes forever, so we disable it.
7979
# However, cleaning it on 5 requires it to be enabled.
@@ -91,21 +91,21 @@ def test_submit(self):
9191
self.assertEqual(self.index['sync'], '0')
9292
self.assertEqual(self.index['disabled'], '0')
9393
self.index.submit("Hello again!", sourcetype="Boris", host="meep")
94-
self.assertEventuallyEqual(eventCount+1, self.totalEventCount)
94+
self.assertEventuallyTrue(lambda: self.totalEventCount == eventCount+1)
9595

9696
def test_submit_via_attach(self):
9797
eventCount = int(self.index['totalEventCount'])
9898
cn = self.index.attach()
9999
cn.send("Hello Boris!\r\n")
100100
cn.close()
101-
self.assertEventuallyEqual(eventCount+1, self.totalEventCount)
101+
self.assertEventuallyTrue(lambda: self.totalEventCount == eventCount+1)
102102

103103
def test_submit_via_attached_socket(self):
104104
eventCount = int(self.index['totalEventCount'])
105105
f = self.index.attached_socket
106106
with f() as sock:
107107
sock.send('Hello world!\r\n')
108-
self.assertEventuallyEqual(eventCount+1, self.totalEventCount)
108+
self.assertEventuallyTrue(lambda: self.totalEventCount == eventCount+1)
109109

110110
def test_upload(self):
111111
self.installAppFromCollection("file_to_upload")
@@ -114,7 +114,7 @@ def test_upload(self):
114114

115115
path = self.pathInApp("file_to_upload", ["log.txt"])
116116
self.index.upload(path)
117-
self.assertEventuallyEqual(eventCount+4, self.totalEventCount)
117+
self.assertEventuallyTrue(lambda: self.totalEventCount == eventCount+4)
118118

119119
if __name__ == "__main__":
120120
import unittest

tests/test_input.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ def test_oneshot(self):
6969

7070
path = self.pathInApp("file_to_upload", ["log.txt"])
7171
self.service.inputs.oneshot(path, index=index_name)
72-
self.assertEventuallyEqual(
73-
str(eventCount+4),
74-
lambda: index.refresh()['totalEventCount']
75-
)
72+
73+
def f():
74+
index.refresh()
75+
return int(index['totalEventCount'] == eventCount+4)
76+
self.assertEventuallyTrue(f)
7677

7778
def test_oneshot_on_nonexistant_file(self):
7879
name = testlib.tmpname()

tests/test_job.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,16 @@ def test_setpriority(self):
141141
# of a job unless Splunk is running as root. This is because Splunk jobs
142142
# are tied up with operating system processes and their priorities.
143143
self.assertEqual(5, int(self.job['priority']))
144+
144145
new_priority = 3
145146
self.job.set_priority(new_priority)
146-
def priority():
147+
148+
def f():
147149
if self.job.is_done():
148150
self.fail("Job already done before priority was set.")
149151
self.job.refresh()
150-
return int(self.job['priority'])
151-
self.assertEventuallyEqual(
152-
new_priority,
153-
priority,
154-
timeout=120
155-
)
152+
return int(self.job['priority']) == new_priority
153+
self.assertEventuallyTrue(f, timeout=120)
156154

157155
class TestJob(testlib.SDKTestCase):
158156
def setUp(self):
@@ -190,28 +188,22 @@ def test_pause(self):
190188
self.assertEqual(self.job['isPaused'], '0')
191189

192190
self.job.pause()
193-
self.assertEventuallyEqual(
194-
'1',
195-
lambda: self.job.refresh()['isPaused']
196-
)
191+
self.assertEventuallyTrue(lambda: self.job.refresh()['isPaused'] == '1')
197192

198193
def test_unpause(self):
199194
if self.job['isPaused'] == '0':
200195
self.job.pause()
201196
self.job.refresh()
202197
self.assertEqual(self.job['isPaused'], '1')
203198
self.job.unpause()
204-
self.assertEventuallyEqual(
205-
'0',
206-
lambda: self.job.refresh()['isPaused']
207-
)
199+
self.assertEventuallyTrue(lambda: self.job.refresh()['isPaused'] == '0')
208200

209201
def test_finalize(self):
210202
if self.job['isFinalized'] == '1':
211203
self.fail("Job is already finalized; can't test .finalize() method.")
212204
else:
213205
self.job.finalize()
214-
self.assertEventuallyEqual('1', lambda: self.job.refresh()['isFinalized'])
206+
self.assertEventuallyTrue(lambda: self.job.refresh()['isFinalized'] == '1')
215207

216208
def test_setttl(self):
217209
old_ttl = int(self.job['ttl'])

tests/testlib.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,6 @@ class SDKTestCase(unittest.TestCase):
7272
restart_already_required = False
7373
installedApps = []
7474

75-
def assertEventuallyEqual(self, expected, func, timeout=10, pause_time=0.5,
76-
timeout_message="Operation timed out."):
77-
self.assertEventuallyTrue(
78-
lambda: expected == func(),
79-
timeout=timeout, pause_time=pause_time,
80-
timeout_message=timeout_message
81-
)
82-
8375
def assertEventuallyTrue(self, predicate, timeout=10, pause_time=0.5,
8476
timeout_message="Operation timed out."):
8577
assert pause_time < timeout
@@ -134,6 +126,14 @@ def check_entity(self, entity):
134126
raise
135127

136128
def clearRestartMessage(self):
129+
"""Tell Splunk to forget that it needs to be restarted.
130+
131+
This is used mostly in cases such as deleting a temporary application.
132+
Splunk asks to be restarted when that happens, but unless the application
133+
contained modular input kinds or the like, it isn't necessary.
134+
"""
135+
if not self.service.restart_required:
136+
raise ValueError("Tried to clear restart message when there was none.")
137137
try:
138138
self.service.delete("messages/restart_required")
139139
except client.HTTPError as he:
@@ -158,20 +158,35 @@ def installAppFromCollection(self, name):
158158
def pathInApp(self, appName, pathComponents):
159159
"""Return a path to *pathComponents* in *appName*.
160160
161-
*pathComponents* shold be a list of strings giving the components.
161+
`pathInApp` is used to refer to files in applications installed with
162+
`installAppFromCollection`. For example, the app `file_to_upload` in
163+
the collection contains `log.txt`. To get the path to it, call::
164+
165+
pathInApp('file_to_upload', ['log.txt'])
166+
167+
The path to `setup.xml` in `has_setup_xml` would be fetched with::
168+
169+
pathInApp('has_setup_xml', ['default', 'setup.xml'])
170+
171+
`pathInApp` figures out the correct separator to use (based on whether
172+
splunkd is running on Windows or Unix) and joins the elements in
173+
*pathComponents* into a path relative to the application specified by
174+
*appName*.
175+
176+
*pathComponents* should be a list of strings giving the components.
162177
This function will try to figure out the correct separator (/ or \)
163178
for the platform that splunkd is running on and construct the path
164179
as needed.
165180
166181
:return: A string giving the path.
167182
"""
168183
splunkHome = self.service.settings['SPLUNK_HOME']
169-
if "/" in splunkHome and "\\" in splunkHome:
170-
raise ValueError("There are both forward and back slashes in $SPLUNK_HOME. What system are you on?!?")
184+
if "\\" in splunkHome:
185+
# This clause must come first, since Windows machines may
186+
# have mixed \ and / in their paths.
187+
separator = "\\"
171188
elif "/" in splunkHome:
172189
separator = "/"
173-
elif "\\" in splunkHome:
174-
separator = "\\"
175190
else:
176191
raise ValueError("No separators in $SPLUNK_HOME. Can't determine what file separator to use.")
177192
appPath = separator.join([splunkHome, "etc", "apps", appName] + pathComponents)
@@ -198,6 +213,9 @@ def setUpClass(cls):
198213
def setUp(self):
199214
unittest.TestCase.setUp(self)
200215
self.service = client.connect(**self.opts.kwargs)
216+
# If Splunk is in a state requiring restart, go ahead
217+
# and restart. That way we'll be sane for the rest of
218+
# the test.
201219
if self.service.restart_required:
202220
self.restartSplunk()
203221
logging.debug("Connected to splunkd version %s", '.'.join(str(x) for x in self.service.splunk_version))

0 commit comments

Comments
 (0)