Skip to content

Commit 1377b0b

Browse files
author
David Noble
committed
Resolves a number of test failures verified locally
TODO: Verify on EL6-4 OSX-8, OSX-9, and WS08-R2 test rigs 1. DVPL-3260: Make Job.isReady use the dispatchState field on HTTP status 200 to determine if it is indeed ready 2. test_disable_enable (test_index.IndexTest) 3. test_setpriority (test_job.TestJobWithDelayedDone) 4. test_enable_preview (test_job.TestJobWithDelayedDone) 5. test_index (test_examples.ExamplesTestCase) 6. test_list_with_sort_mode_auto (test_collection.CollectionTestCase+) 7. test_search (test_examples.ExamplesTestCase) 8. test_suppress (test_saved_search.TestSavedSearch) 9. test_update (test_app.TestApp)
1 parent edabaa4 commit 1377b0b

File tree

9 files changed

+50
-29
lines changed

9 files changed

+50
-29
lines changed

examples/search.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def main(argv):
7979

8080
job = service.jobs.create(search, **kwargs_create)
8181
while True:
82-
job.refresh()
82+
while not job.is_ready():
83+
pass
8384
stats = {'isDone': job['isDone'],
8485
'doneProgress': job['doneProgress'],
8586
'scanCount': job['scanCount'],

splunklib/client.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,6 @@ def restart_required(self):
527527
titles = [messages['entry']['title']]
528528
else:
529529
titles = [x['title'] for x in messages['entry']]
530-
for title in titles:
531-
print title
532530
result = 'restart_required' in titles
533531
return result
534532

@@ -981,7 +979,7 @@ def content(self):
981979
def disable(self):
982980
"""Disables the entity at this endpoint."""
983981
self.post("disable")
984-
if self.service._splunk_version[0] < 6:
982+
if self.service.restart_required:
985983
self.restartSplunk()
986984
return self
987985

@@ -2993,7 +2991,7 @@ def suppress(self, expiration):
29932991
29942992
:return: The :class:`SavedSearch`.
29952993
"""
2996-
self.post("suppress", suppressed="1", expiration=expiration)
2994+
self.post("suppress", expiration=expiration)
29972995
return self
29982996

29992997
@property
@@ -3015,7 +3013,7 @@ def unsuppress(self):
30153013
30163014
:return: The :class:`SavedSearch`.
30173015
"""
3018-
self.post("suppress", suppressed="0", expiration="0")
3016+
self.post("suppress", expiration="0")
30193017
return self
30203018

30213019

splunklib/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def fromkv(k, v):
225225
def __getitem__(self, key):
226226
if key in self:
227227
return dict.__getitem__(self, key)
228-
key = key + self.sep
228+
key += self.sep
229229
result = record()
230230
for k,v in self.iteritems():
231231
if not k.startswith(key):

tests/test_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ def test_update(self):
7070
'author': "Me",
7171
'description': "Test app description",
7272
'label': "SDK Test",
73-
'manageable': False,
73+
'version': "1.1",
7474
'visible': True,
7575
}
7676
self.app.update(**kwargs)
7777
self.app.refresh()
7878
self.assertEqual(self.app['author'], "Me")
7979
self.assertEqual(self.app['label'], "SDK Test")
80-
self.assertEqual(self.app['manageable'], "0")
80+
self.assertEqual(self.app['version'], "1.1")
8181
self.assertEqual(self.app['visible'], "1")
8282

8383
def test_delete(self):

tests/test_collection.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,28 @@ def test_list_with_sort_dir(self):
138138
(coll_name, expected, found))
139139

140140
def test_list_with_sort_mode_auto(self):
141+
# The jobs collection requires special handling. The sort_dir kwarg is
142+
# needed because the default sorting direction for jobs is "desc", not
143+
# "asc". The sort_key kwarg is required because there is no default
144+
# sort_key for jobs in Splunk 6.
141145
for coll_name in collections:
142146
coll = getattr(self.service, coll_name)
143-
# sort_dir is needed because the default sorting direction
144-
# for jobs is "desc", not "asc", so we have to set it explicitly or our tests break.
145-
expected = [ent.name for ent in coll.list(sort_mode="auto", sort_dir="asc")]
147+
if coll_name == 'jobs':
148+
expected = [ent.name for ent in coll.list(
149+
sort_mode="auto", sort_dir="asc", sort_key="sid")]
150+
else:
151+
expected = [ent.name for ent in coll.list(sort_mode="auto")]
152+
146153
if len(expected) == 0:
147154
logging.debug("No entities in collection %s; skipping test.", coll_name)
148-
found = [ent.name for ent in coll.list(sort_dir="asc")]
149-
self.assertEqual(expected, found,
150-
msg='on %s (expected: %s, found: %s)' % \
151-
(coll_name, expected, found))
155+
156+
if coll_name == 'jobs':
157+
found = [ent.name for ent in coll.list(
158+
sort_dir="asc", sort_key="sid")]
159+
else:
160+
found = [ent.name for ent in coll.list()]
161+
162+
self.assertEqual(expected, found, msg='on %s (expected: %s, found: %s)' % (coll_name, expected, found))
152163

153164
def test_list_with_sort_mode_alpha_case(self):
154165
for coll_name in collections:

tests/test_examples.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import splunklib.client as client
2525

26+
2627
def check_multiline(testcase, first, second, message=None):
2728
"""Assert that two multi-line strings are equal."""
2829
testcase.assertTrue(isinstance(first, basestring),
@@ -35,12 +36,14 @@ def check_multiline(testcase, first, second, message=None):
3536
if first != second:
3637
testcase.fail("Multiline strings are not equal: %s" % message)
3738

39+
3840
# Run the given python script and return its exit code.
3941
def run(script, stdin=None, stdout=PIPE, stderr=None):
4042
process = start(script, stdin, stdout, stderr)
4143
process.communicate()
4244
return process.wait()
4345

46+
4447
# Start the given python script and return the corresponding process object.
4548
# The script can be specified as either a string or arg vector. In either case
4649
# it will be prefixed to invoke python explicitly.
@@ -50,11 +53,13 @@ def start(script, stdin=None, stdout=PIPE, stderr=None):
5053
script = ["python"] + script
5154
return Popen(script, stdin=stdin, stdout=stdout, stderr=stderr, cwd='../examples')
5255

56+
5357
# Rudimentary sanity check for each of the examples
5458
class ExamplesTestCase(testlib.SDKTestCase):
5559
def check_commands(self, *args):
56-
for arg in args:
57-
self.assertEquals(run(arg), 0)
60+
for arg in args:
61+
result = run(arg)
62+
self.assertEquals(result, 0)
5863

5964
def setUp(self):
6065
super(ExamplesTestCase, self).setUp()
@@ -155,7 +160,7 @@ def test_index(self):
155160
"index.py disable sdk-tests",
156161
"index.py enable sdk-tests",
157162
"index.py clean sdk-tests")
158-
self.restartSplunk()
163+
self.service.restart(120)
159164

160165
def test_info(self):
161166
self.check_commands(

tests/test_job.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,29 +194,33 @@ def test_enable_preview(self):
194194
print "Test requires sdk-app-collection. Skipping."
195195
return
196196
self.install_app_from_collection("sleep_command")
197-
self.query = "search index=_internal | sleep 100"
197+
sleep_duration = 100
198+
self.query = "search index=_internal | sleep %d" % sleep_duration
198199
self.job = self.service.jobs.create(
199200
query=self.query,
200201
earliest_time="-1m",
201202
priority=5,
202203
latest_time="now")
203-
self.assertEqual(self.job['isPreviewEnabled'], '0')
204+
while not self.job.is_ready():
205+
pass
206+
self.assertEqual(self.job.content['isPreviewEnabled'], '0')
204207
self.job.enable_preview()
205208

206-
def is_preview():
207-
self.job.refresh()
208-
if self.job.is_done():
209+
def is_preview_enabled():
210+
is_done = self.job.is_done()
211+
if is_done:
209212
self.fail('Job finished before preview enabled.')
210-
return self.job['isPreviewEnabled'] == '1'
213+
return self.job.content['isPreviewEnabled'] == '1'
211214

212-
self.assertEventuallyTrue(is_preview)
215+
self.assertEventuallyTrue(is_preview_enabled)
216+
return
213217

214218
def test_setpriority(self):
215219
if not self.app_collection_installed():
216220
print "Test requires sdk-app-collection. Skipping."
217221
return
218-
sleep_duration = 10
219222
self.install_app_from_collection("sleep_command")
223+
sleep_duration = 100
220224
self.query = "search index=_internal | sleep %s" % sleep_duration
221225
self.job = self.service.jobs.create(
222226
query=self.query,
@@ -238,10 +242,10 @@ def test_setpriority(self):
238242
def f():
239243
if self.job.is_done():
240244
self.fail("Job already done before priority was set.")
241-
print self.job.content['dispatchState']
242245
return int(self.job.content['priority']) == new_priority
243246

244247
self.assertEventuallyTrue(f, timeout=sleep_duration + 5)
248+
return
245249

246250

247251
class TestJob(testlib.SDKTestCase):

tests/test_saved_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def test_suppress(self):
190190
self.assertGreaterEqual(suppressed_time, 0)
191191
new_suppressed_time = suppressed_time+100
192192
self.saved_search.suppress(new_suppressed_time)
193-
self.assertLessEqual(self.saved_search['suppressed'],
193+
self.assertLessEqual(self.saved_search['suppressed'],
194194
new_suppressed_time)
195195
self.assertGreater(self.saved_search['suppressed'],
196196
suppressed_time)

tests/testlib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ def install_app_from_collection(self, name):
172172
except client.HTTPError as he:
173173
if he.status == 400:
174174
raise IOError("App %s not found in app collection" % name)
175+
if self.service.restart_required:
176+
self.service.restart(120)
175177
self.installedApps.append(name)
176178

177179
def app_collection_installed(self):

0 commit comments

Comments
 (0)