Skip to content

Commit 99a8918

Browse files
author
David Noble
committed
Fixed tests for Python 2.6
* tests.searchcommands are skipped when running under Python 2.6 Rationale: - Search commands require Splunk's Python which has been at 2.7 since Splunk 4.3 - Search commands should be tested outside of splunk using "splunk cmd python <command-line> * Test fixes eliminate (1) DeprecationWarning messages regarding use of BaseException.message and (2) a break introduced these past few days. * TODO: Track down six cookie authentication failures, each with the same failure: AttributeError: 'TestCookieAuthentication' object has no attribute assertIsNotNone' * VERIFIED: No Linux or OS X breaks on Python 2.7
1 parent 5ab4992 commit 99a8918

File tree

8 files changed

+47
-35
lines changed

8 files changed

+47
-35
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ examples/searchcommands_app/searchcommand_app.log
2222
Test Results*.html
2323
tests/searchcommands/data/app/app.log
2424
splunk_sdk.egg-info/
25-
dist/
25+
dist/
26+
examples/searchcommands_app/package/default/commands.conf
27+
examples/searchcommands_app/package/bin/packages

splunklib/modularinput/script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def run_script(self, args, event_writer, input_stream):
9393
return 0
9494
except Exception as e:
9595
root = ET.Element("error")
96-
ET.SubElement(root, "message").text = e.message
96+
ET.SubElement(root, "message").text = str(e)
9797
event_writer.write_xml_document(root)
9898

9999
return 1

tests/searchcommands/__init__.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,29 @@
1717

1818
from __future__ import absolute_import, division, print_function, unicode_literals
1919

20-
from os import path
21-
import logging
20+
from sys import version_info as python_version
2221

23-
from splunklib.searchcommands import environment
24-
from splunklib import searchcommands
22+
if not (python_version[0] == 2 and python_version[1] == 7):
23+
def load_tests(loader, tests, pattern):
24+
return
25+
else:
26+
from os import path
27+
import logging
2528

26-
package_directory = path.dirname(path.abspath(__file__))
27-
project_root = path.dirname(path.dirname(package_directory))
29+
from splunklib.searchcommands import environment
30+
from splunklib import searchcommands
2831

32+
package_directory = path.dirname(path.abspath(__file__))
33+
project_root = path.dirname(path.dirname(package_directory))
2934

30-
def rebase_environment(name):
3135

32-
environment.app_root = path.join(package_directory, 'apps', name)
33-
logging.Logger.manager.loggerDict.clear()
34-
del logging.root.handlers[:]
36+
def rebase_environment(name):
3537

36-
environment.splunklib_logger, environment.logging_configuration = environment.configure_logging('splunklib')
37-
searchcommands.logging_configuration = environment.logging_configuration
38-
searchcommands.splunklib_logger = environment.splunklib_logger
39-
searchcommands.app_root = environment.app_root
38+
environment.app_root = path.join(package_directory, 'apps', name)
39+
logging.Logger.manager.loggerDict.clear()
40+
del logging.root.handlers[:]
41+
42+
environment.splunklib_logger, environment.logging_configuration = environment.configure_logging('splunklib')
43+
searchcommands.logging_configuration = environment.logging_configuration
44+
searchcommands.splunklib_logger = environment.splunklib_logger
45+
searchcommands.app_root = environment.app_root

tests/test_all.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
import os
2020
try:
21-
import unittest2 as unittest
21+
import unittest2 as unittest # We must be sure to get unittest2--not unittest--on Python 2.6
2222
except ImportError:
2323
import unittest
24-
import testlib
2524

25+
os.chdir(os.path.dirname(os.path.abspath(__file__)))
2626
suite = unittest.defaultTestLoader.discover('.')
2727

2828
if __name__ == '__main__':

tests/test_binding.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ def test_login_fails_with_bad_cookie(self):
565565
new_context.get("apps/local")
566566
self.fail()
567567
except AuthenticationError as ae:
568-
self.assertEqual(ae.message, "Request failed: Session is not logged in.")
568+
self.assertEqual(str(ae), "Request failed: Session is not logged in.")
569569

570570
def test_login_with_multiple_cookies(self):
571571
bad_cookie = 'bad=cookie'
@@ -575,7 +575,7 @@ def test_login_with_multiple_cookies(self):
575575
new_context.get("apps/local")
576576
self.fail()
577577
except AuthenticationError as ae:
578-
self.assertEqual(ae.message, "Request failed: Session is not logged in.")
578+
self.assertEqual(str(ae), "Request failed: Session is not logged in.")
579579
# Bring in a valid cookie now
580580
for key, value in self.context.get_cookies().items():
581581
new_context.get_cookies()[key] = value
@@ -598,7 +598,8 @@ def test_login_fails_without_cookie_or_token(self):
598598
binding.connect(**opts)
599599
self.fail()
600600
except AuthenticationError as ae:
601-
self.assertEqual(ae.message, "Login failed.")
601+
self.assertEqual(str(ae), "Login failed.")
602+
602603

603604
class TestNamespace(unittest.TestCase):
604605
def test_namespace(self):

tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ExamplesTestCase(testlib.SDKTestCase):
5959
def check_commands(self, *args):
6060
for arg in args:
6161
result = run(arg)
62-
self.assertEquals(result, 0, '"{}" run failed with result code {}'.format(arg, result))
62+
self.assertEquals(result, 0, '"{0}" run failed with result code {1}'.format(arg, result))
6363
self.service.login() # Because a Splunk restart invalidates our session
6464

6565
def setUp(self):

tests/test_job.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727

2828
from splunklib.binding import _log_duration, HTTPError
2929

30-
from xml.etree.ElementTree import ParseError
30+
# TODO: Determine if we should be importing ExpatError if ParseError is not avaialble (e.g., on Python 2.6)
31+
# There's code below that now catches SyntaxError instead of ParseError. Should we be catching ExpathError instead?
32+
33+
# from xml.etree.ElementTree import ParseError
3134

3235

3336
class TestUtilities(testlib.SDKTestCase):
@@ -65,7 +68,7 @@ def test_export(self):
6568
isinstance(ds[0], results.Message))
6669
nonmessages = [d for d in ds if isinstance(d, dict)]
6770
self.assertTrue(len(nonmessages) <= 3)
68-
71+
6972
def test_export_docstring_sample(self):
7073
import splunklib.client as client
7174
import splunklib.results as results
@@ -79,7 +82,7 @@ def test_export_docstring_sample(self):
7982
# Normal events are returned as dicts
8083
pass #print result
8184
assert rr.is_preview == False
82-
85+
8386
def test_results_docstring_sample(self):
8487
import splunklib.results as results
8588
service = self.service # cheat
@@ -95,7 +98,7 @@ def test_results_docstring_sample(self):
9598
# Normal events are returned as dicts
9699
pass #print result
97100
assert rr.is_preview == False
98-
101+
99102
def test_preview_docstring_sample(self):
100103
import splunklib.client as client
101104
import splunklib.results as results
@@ -113,7 +116,7 @@ def test_preview_docstring_sample(self):
113116
pass #print "Preview of a running search job."
114117
else:
115118
pass #print "Job is finished. Results are final."
116-
119+
117120
def test_oneshot_docstring_sample(self):
118121
import splunklib.client as client
119122
import splunklib.results as results
@@ -274,8 +277,8 @@ def setUp(self):
274277
super(TestJob, self).setUp()
275278
self.query = "search index=_internal | head 3"
276279
self.job = self.service.jobs.create(
277-
query=self.query,
278-
earliest_time="-1m",
280+
query=self.query,
281+
earliest_time="-1m",
279282
latest_time="now")
280283

281284
def tearDown(self):
@@ -324,7 +327,7 @@ def test_finalize(self):
324327
def test_setttl(self):
325328
old_ttl = int(self.job['ttl'])
326329
new_ttl = old_ttl + 1000
327-
330+
328331
from datetime import datetime
329332
start_time = datetime.now()
330333
self.job.set_ttl(new_ttl)
@@ -362,7 +365,7 @@ def test_search_invalid_query_as_json(self):
362365
}
363366
try:
364367
self.service.jobs.create('invalid query', **args)
365-
except ParseError as pe:
368+
except SyntaxError as pe:
366369
self.fail("Something went wrong with parsing the REST API response. %s" % pe.message)
367370
except HTTPError as he:
368371
self.assertEqual(he.status, 400)
@@ -385,7 +388,7 @@ def test_results_reader(self):
385388
from collections import OrderedDict
386389
except:
387390
from splunklib.ordereddict import OrderedDict
388-
self.assertTrue(isinstance(r, OrderedDict)
391+
self.assertTrue(isinstance(r, OrderedDict)
389392
or isinstance(r, results.Message))
390393
if isinstance(r, OrderedDict):
391394
N_results += 1
@@ -407,7 +410,7 @@ def test_results_reader_with_streaming_results(self):
407410
from collections import OrderedDict
408411
except:
409412
from splunklib.ordereddict import OrderedDict
410-
self.assertTrue(isinstance(r, OrderedDict)
413+
self.assertTrue(isinstance(r, OrderedDict)
411414
or isinstance(r, results.Message))
412415
if isinstance(r, OrderedDict):
413416
N_results += 1

tests/test_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def test_login_fails_with_bad_cookie(self):
214214
service2.apps.get()
215215
self.fail()
216216
except AuthenticationError as ae:
217-
self.assertEqual(ae.message, "Request failed: Session is not logged in.")
217+
self.assertEqual(str(ae), "Request failed: Session is not logged in.")
218218

219219
def test_autologin_with_cookie(self):
220220
self.service.login()
@@ -237,7 +237,7 @@ def test_login_fails_with_no_cookie(self):
237237
service2.login()
238238
self.fail()
239239
except AuthenticationError as ae:
240-
self.assertEqual(ae.message, "Login failed.")
240+
self.assertEqual(str(ae), "Login failed.")
241241

242242
def test_login_with_multiple_cookie_headers(self):
243243
cookies = {

0 commit comments

Comments
 (0)