|
17 | 17 | import testlib |
18 | 18 | import logging |
19 | 19 |
|
| 20 | +import unittest |
| 21 | + |
20 | 22 | import splunklib.client as client |
21 | 23 | import splunklib.results as results |
22 | 24 |
|
@@ -103,7 +105,7 @@ def test_read_jobs(self): |
103 | 105 | job.refresh() |
104 | 106 | self.check_job(job) |
105 | 107 |
|
106 | | -def retry(job, field, expected, times=3): |
| 108 | +def retry(job, field, expected, times=10): |
107 | 109 | # Sometimes there is a slight delay in the value getting |
108 | 110 | # set in splunkd. If it fails, just try again. |
109 | 111 | tries = times |
@@ -208,122 +210,77 @@ def test_touch(self): |
208 | 210 | # This is a problem to test. You have to wait for it to change |
209 | 211 | # before touch gets anywhere, and the granularity of ttl is seconds. |
210 | 212 | old_ttl = int(self.job['ttl']) |
211 | | - import time; time.sleep(2) |
| 213 | + # import time; time.sleep(3) |
212 | 214 | self.job.touch() |
213 | 215 | self.job.refresh() |
214 | 216 | new_ttl = int(self.job['ttl']) |
215 | 217 | if new_ttl == old_ttl: |
216 | 218 | self.fail("Didn't wait long enough for TTL to change and make touch meaningful.") |
217 | 219 | self.assertGreaterEqual(int(self.job['ttl']), old_ttl) |
218 | 220 |
|
219 | | - # def test_update(self): |
220 | | - # job = self.job |
221 | | - |
222 | | - # new_priority = int(job['priority']) % 10 + 1 |
223 | | - # new_ttl = int(job['ttl']) + 1 |
224 | | - |
225 | | - # job.disable_preview() |
226 | | - # job.pause() |
227 | | - # job.set_ttl(new_ttl) |
228 | | - # job.set_priority(new_priority) |
229 | | - # job.touch() |
230 | | - |
231 | | - # job.refresh() |
232 | | - |
233 | | - # self.assertEqual(job['isPaused'], '1') |
234 | | - # self.assertEqual(job['isPreviewEnabled'], '0') |
235 | | - # self.assertEqual(int(job['ttl']), new_ttl) |
236 | | - # self.assertEqual(int(job['priority']), new_priority) |
237 | | - |
238 | | - # job.enable_preview() |
239 | | - # job.unpause() |
240 | | - # job.refresh() |
241 | | - |
242 | | - # #self.assertEqual(job['isPreviewEnabled'], '1') |
243 | | - # self.assertEqual(job['isPaused'], '0') |
244 | | - # self.assertEqual(job['isFinalized'], '0') |
245 | | - |
246 | | - # job.disable_preview() |
247 | | - # job.pause() |
248 | | - # job.set_ttl(1001) |
249 | | - # job.set_priority(6) |
250 | | - # job.touch() |
251 | | - # job.finalize() |
252 | | - |
253 | | - # job.refresh() |
254 | | - |
255 | | - # self.assertEqual(job['isPaused'], '1') |
256 | | - # self.assertEqual(job['isPreviewEnabled'], '0') |
257 | | - # self.assertEqual(job['ttl'], '1001') |
258 | | - # self.assertEqual(job['priority'], '6') |
259 | | - # self.assertEqual(job['isFinalized'], '1') |
260 | | - |
261 | | -# def test_results(self): |
262 | | -# service = client.connect(**self.opts.kwargs) |
263 | | - |
264 | | -# jobs = service.jobs |
265 | | - |
266 | | -# # Run a new job to get the results, but we also make |
267 | | -# # sure that there is at least one event in the index already |
268 | | -# index = service.indexes['_internal'] |
269 | | -# self.assertTrue(index['totalEventCount'] > 0) |
270 | | - |
271 | | -# job = jobs.create("search index=_internal | head 1 | stats count") |
272 | | -# job.refresh() |
273 | | -# self.assertEqual(job['isDone'], '0') |
274 | | -# # When a job was first created in Splunk 4.x, results would |
275 | | -# # return 204 before results were available. Itay requested a |
276 | | -# # change for Ace, and now it just returns 200 with an empty |
277 | | -# # <results/> element. Thus this test is obsolete. I leave it |
278 | | -# # here as a caution to future generations: |
279 | | -# # self.assertRaises(ValueError, job.results) |
280 | | -# while not job.isDone(): |
281 | | -# sleep(1) |
282 | | -# reader = results.ResultsReader(job.results(timeout=60)) |
283 | | -# job.refresh() |
284 | | -# self.assertEqual(job['isDone'], '1') |
285 | | - |
286 | | -# self.assertEqual(reader.is_preview, False) |
287 | | - |
288 | | -# result = reader.next() |
289 | | -# self.assertTrue(isinstance(result, dict)) |
290 | | -# self.assertLessEqual(int(result["count"]), 1) |
291 | | - |
292 | | -# # Repeat the same thing, but without the .is_preview reference. |
293 | | -# job = jobs.create("search index=_internal | head 1 | stats count") |
294 | | -# while not job.isDone(): |
295 | | -# sleep(1) |
296 | | -# reader = results.ResultsReader(job.results(timeout=60)) |
297 | | -# job.refresh() |
298 | | -# self.assertEqual(job['isDone'], '1') |
299 | | -# result = reader.next() |
300 | | -# self.assertTrue(isinstance(result, dict)) |
301 | | -# self.assertLessEqual(int(result["count"]), 1) |
302 | | - |
303 | | -# def test_results_reader(self): |
304 | | -# # Run jobs.export("search index=_internal | stats count", |
305 | | -# # earliest_time="rt", latest_time="rt") and you get a |
306 | | -# # streaming sequence of XML fragments containing results. |
307 | | -# with open('streaming_results.xml') as input: |
308 | | -# reader = results.ResultsReader(input) |
309 | | -# print reader.next() |
310 | | -# self.assertTrue(isinstance(reader.next(), dict)) |
311 | | - |
312 | | -# def test_xmldtd_filter(self): |
313 | | -# from StringIO import StringIO |
314 | | -# s = results.XMLDTDFilter(StringIO("<?xml asdf awe awdf=""><boris>Other stuf</boris><?xml dafawe \n asdfaw > ab")) |
315 | | -# self.assertEqual(s.read(3), "<bo") |
316 | | -# self.assertEqual(s.read(), "ris>Other stuf</boris> ab") |
317 | | - |
318 | | - |
319 | | -# def test_concatenated_stream(self): |
320 | | -# from StringIO import StringIO |
321 | | -# s = results.ConcatenatedStream(StringIO("This is a test "), |
322 | | -# StringIO("of the emergency broadcast system.")) |
323 | | -# self.assertEqual(s.read(3), "Thi") |
324 | | -# self.assertEqual(s.read(20), 's is a test of the e') |
325 | | -# self.assertEqual(s.read(), 'mergency broadcast system.') |
326 | | - |
| 221 | + def test_enable_preview(self): |
| 222 | + if self.job['isPreviewEnabled'] == '1': |
| 223 | + self.job.disable_preview() |
| 224 | + self.job.refresh() |
| 225 | + self.assertEqual(job['isPreviewEnabled'], '0') |
| 226 | + self.job.enable_preview() |
| 227 | + retry(self.job, 'isPreviewEnabled', '1', times=5) |
| 228 | + self.assertEqual(self.job['isPreviewEnabled'], '1') |
| 229 | + |
| 230 | + def test_disable_preview(self): |
| 231 | + if self.job['isPreviewEnabled'] == '0': |
| 232 | + self.job.enable_preview() |
| 233 | + self.job.refresh() |
| 234 | + self.assertEqual(self.job['isPreviewEnabled'], '1') |
| 235 | + self.job.disable_preview() |
| 236 | + retry(self.job, 'isPreviewEnabled', '0') |
| 237 | + self.assertEqual(self.job['isPreviewEnabled'], '0') |
| 238 | + |
| 239 | +class TestResultsReader(unittest.TestCase): |
| 240 | + def test_results_reader(self): |
| 241 | + # Run jobs.export("search index=_internal | stats count", |
| 242 | + # earliest_time="rt", latest_time="rt") and you get a |
| 243 | + # streaming sequence of XML fragments containing results. |
| 244 | + with open('results.xml') as input: |
| 245 | + reader = results.ResultsReader(input) |
| 246 | + self.assertFalse(reader.is_preview) |
| 247 | + N = 0 |
| 248 | + for r in reader: |
| 249 | + logging.debug("Type of result was %s", r.__class__.__name__) |
| 250 | + import collections |
| 251 | + self.assertTrue(isinstance(r, collections.OrderedDict) |
| 252 | + or isinstance(r, results.Message)) |
| 253 | + N += 1 |
| 254 | + self.assertEqual(N, 4999) |
| 255 | + |
| 256 | + def test_results_reader_with_streaming_results(self): |
| 257 | + # Run jobs.export("search index=_internal | stats count", |
| 258 | + # earliest_time="rt", latest_time="rt") and you get a |
| 259 | + # streaming sequence of XML fragments containing results. |
| 260 | + with open('streaming_results.xml') as input: |
| 261 | + reader = results.ResultsReader(input) |
| 262 | + N = 0 |
| 263 | + for r in reader: |
| 264 | + logging.debug("Type of result was %s", r.__class__.__name__) |
| 265 | + import collections |
| 266 | + self.assertTrue(isinstance(r, collections.OrderedDict) |
| 267 | + or isinstance(r, results.Message)) |
| 268 | + N += 1 |
| 269 | + self.assertEqual(N, 4999) |
| 270 | + |
| 271 | + |
| 272 | + def test_xmldtd_filter(self): |
| 273 | + from StringIO import StringIO |
| 274 | + s = results.XMLDTDFilter(StringIO("<?xml asdf awe awdf=""><boris>Other stuf</boris><?xml dafawe \n asdfaw > ab")) |
| 275 | + self.assertEqual(s.read(), "<boris>Other stuf</boris> ab") |
| 276 | + |
| 277 | + def test_concatenated_stream(self): |
| 278 | + from StringIO import StringIO |
| 279 | + s = results.ConcatenatedStream(StringIO("This is a test "), |
| 280 | + StringIO("of the emergency broadcast system.")) |
| 281 | + self.assertEqual(s.read(3), "Thi") |
| 282 | + self.assertEqual(s.read(20), 's is a test of the e') |
| 283 | + self.assertEqual(s.read(), 'mergency broadcast system.') |
327 | 284 |
|
328 | 285 | if __name__ == "__main__": |
329 | 286 | testlib.main() |
0 commit comments