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