|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2011-2012 Splunk, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +from time import sleep |
| 18 | + |
| 19 | +import splunklib.client as client |
| 20 | +import splunklib.results as results |
| 21 | + |
| 22 | +import testlib |
| 23 | + |
| 24 | +class TestCase(testlib.TestCase): |
| 25 | + # UNDONE: Shouldn't the following assert something on exit? |
| 26 | + def check_properties(self, job, properties, secs = 10): |
| 27 | + while secs > 0 and len(properties) > 0: |
| 28 | + content = job.refresh().content |
| 29 | + |
| 30 | + # Try and check every property we specified. If we fail, |
| 31 | + # we'll try again later. If we succeed, delete it so we |
| 32 | + # don't check it again. |
| 33 | + for k, v in properties.items(): |
| 34 | + try: |
| 35 | + self.assertEqual(content[k], v) |
| 36 | + |
| 37 | + # Since we succeeded, delete it |
| 38 | + del properties[k] |
| 39 | + except: |
| 40 | + pass |
| 41 | + |
| 42 | + secs -= 1 |
| 43 | + sleep(1) |
| 44 | + |
| 45 | + def check_job(self, job): |
| 46 | + job.name |
| 47 | + job.path |
| 48 | + job.metadata |
| 49 | + job.content |
| 50 | + |
| 51 | + keys = [ |
| 52 | + 'cursorTime', 'delegate', 'diskUsage', 'dispatchState', |
| 53 | + 'doneProgress', 'dropCount', 'earliestTime', 'eventAvailableCount', |
| 54 | + 'eventCount', 'eventFieldCount', 'eventIsStreaming', |
| 55 | + 'eventIsTruncated', 'eventSearch', 'eventSorting', 'isDone', |
| 56 | + 'isFailed', 'isFinalized', 'isPaused', 'isPreviewEnabled', |
| 57 | + 'isRealTimeSearch', 'isRemoteTimeline', 'isSaved', 'isSavedSearch', |
| 58 | + 'isZombie', 'keywords', 'label', 'latestTime', 'messages', |
| 59 | + 'numPreviews', 'priority', 'remoteSearch', 'reportSearch', |
| 60 | + 'resultCount', 'resultIsStreaming', 'resultPreviewCount', |
| 61 | + 'runDuration', 'scanCount', 'searchProviders', 'sid', |
| 62 | + 'statusBuckets', 'ttl' |
| 63 | + ] |
| 64 | + for key in keys: self.assertTrue(key in job.content) |
| 65 | + |
| 66 | + def test_read(self): |
| 67 | + service = client.connect(**self.opts.kwargs) |
| 68 | + |
| 69 | + for job in service.jobs: |
| 70 | + self.check_job(job) |
| 71 | + job.refresh() |
| 72 | + self.check_job(job) |
| 73 | + |
| 74 | + def test_crud(self): |
| 75 | + service = client.connect(**self.opts.kwargs) |
| 76 | + |
| 77 | + jobs = service.jobs |
| 78 | + |
| 79 | + if not service.indexes.contains("sdk-tests"): |
| 80 | + service.indexes.create("sdk-tests") |
| 81 | + service.indexes['sdk-tests'].clean() |
| 82 | + |
| 83 | + # Make sure we can create a job |
| 84 | + job = jobs.create("search index=sdk-tests") |
| 85 | + self.assertTrue(jobs.contains(job.sid)) |
| 86 | + |
| 87 | + # Make sure we can cancel the job |
| 88 | + job.cancel() |
| 89 | + self.assertFalse(jobs.contains(job.sid)) |
| 90 | + |
| 91 | + # Search for non-existant data |
| 92 | + job = jobs.create("search index=sdk-tests TERM_DOES_NOT_EXIST") |
| 93 | + testlib.wait(job, lambda job: job['isDone'] == '1') |
| 94 | + self.assertEqual(job['isDone'], '1') |
| 95 | + self.assertEqual(job['eventCount'], '0') |
| 96 | + job.finalize() |
| 97 | + |
| 98 | + # Create a new job |
| 99 | + job = jobs.create("search * | head 1 | stats count") |
| 100 | + self.assertTrue(jobs.contains(job.sid)) |
| 101 | + |
| 102 | + # Set various properties on it |
| 103 | + job.disable_preview() |
| 104 | + job.pause() |
| 105 | + job.set_ttl(1000) |
| 106 | + job.set_priority(5) |
| 107 | + job.touch() |
| 108 | + job.refresh() |
| 109 | + |
| 110 | + # Assert that the properties got set properly |
| 111 | + self.check_properties(job, { |
| 112 | + 'isPreviewEnabled': '0', |
| 113 | + 'isPaused': '1', |
| 114 | + 'ttl': '1000', |
| 115 | + 'priority': '5' |
| 116 | + }) |
| 117 | + |
| 118 | + # Set more properties |
| 119 | + job.enable_preview() |
| 120 | + job.unpause() |
| 121 | + job.finalize() |
| 122 | + job.refresh() |
| 123 | + |
| 124 | + # Assert that they got set properly |
| 125 | + self.check_properties(job, { |
| 126 | + 'isPreviewEnabled': '1', |
| 127 | + 'isPaused': '0', |
| 128 | + 'isFinalized': '1' |
| 129 | + }) |
| 130 | + |
| 131 | + job.cancel() |
| 132 | + self.assertFalse(jobs.contains(job.sid)) |
| 133 | + |
| 134 | + def test_results(self): |
| 135 | + service = client.connect(**self.opts.kwargs) |
| 136 | + |
| 137 | + jobs = service.jobs |
| 138 | + |
| 139 | + # Run a new job to get the results, but we also make |
| 140 | + # sure that there is at least one event in the index already |
| 141 | + index = service.indexes['sdk-tests'] |
| 142 | + old_event_count = int(index['totalEventCount']) |
| 143 | + if old_event_count == 0: |
| 144 | + index.submit("test event") |
| 145 | + testlib.wait(index, lambda index: index['totalEventCount'] == '1') |
| 146 | + |
| 147 | + job = jobs.create("search index=sdk-tests | head 1 | stats count") |
| 148 | + testlib.wait(job, lambda job: job['isDone'] == '1') |
| 149 | + self.assertEqual(job['isDone'], '1') |
| 150 | + |
| 151 | + # Fetch the results |
| 152 | + reader = results.ResultsReader(job.results()) |
| 153 | + |
| 154 | + # The first one should always be RESULTS |
| 155 | + kind, result = reader.next() |
| 156 | + self.assertEqual(results.RESULTS, kind) |
| 157 | + self.assertEqual(int(result["preview"]), 0) |
| 158 | + |
| 159 | + # The second is always the actual result |
| 160 | + kind, result = reader.next() |
| 161 | + self.assertEqual(results.RESULT, kind) |
| 162 | + self.assertEqual(int(result["count"]), 1) |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + testlib.main() |
0 commit comments