Skip to content

Commit 7373fc4

Browse files
author
David Noble
committed
Two fixes that account for differences between Python 2.6 and Python 2.7
+ Logging level names are unchecked by Python 2.6 Also improved our own checks and enhanced unit tests coverage. See change to splunklib.searchcommands.search_command.py and tests.searchcommands.test_decorators + Expected results for search results info access vary based on Python version number See change to tests.searchcommands.test_search_command Signed-off-by: David Noble <[email protected]>
1 parent edc1315 commit 7373fc4

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

splunklib/searchcommands/search_command.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from ordereddict import OrderedDict # python 2.6
2525

2626
from inspect import getmembers
27-
from logging import getLevelName
27+
from logging import _levelNames, getLevelName
2828
from os import path
2929
from sys import argv, stdin, stdout
3030
from urlparse import urlsplit
@@ -106,7 +106,17 @@ def logging_level(self):
106106
def logging_level(self, value):
107107
if value is None:
108108
value = self._default_logging_level
109-
self.logger.setLevel(value if type(value) is not str else value.upper())
109+
if type(value) is str:
110+
try:
111+
level = _levelNames[value.upper()]
112+
except KeyError:
113+
raise ValueError('Unrecognized logging level: %s' % value)
114+
else:
115+
try:
116+
level = int(value)
117+
except ValueError:
118+
raise ValueError('Unrecognized logging level: %s' % value)
119+
self.logger.setLevel(level)
110120
return
111121

112122
show_configuration = Option(doc='''

tests/searchcommands/test_decorators.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ def test_builtin_options(self):
6666
command.logging_level = variant
6767
self.assertEquals(command.logging_level, warning if level_name == notset else level_name)
6868

69+
# logging_level accepts any numeric value
70+
71+
for level in 999, 999.999:
72+
command.logging_level = level
73+
self.assertEqual(command.logging_level, 'Level 999')
74+
75+
# logging_level raises a value error for unknown logging level names
76+
77+
current_value = command.logging_level
78+
79+
try:
80+
command.logging_level = 'foo'
81+
except ValueError:
82+
pass
83+
except BaseException as e:
84+
self.fail('Expected ValueError, but %s was raised' % type(e))
85+
else:
86+
self.fail('Expected ValueError, but logging_level=%s' % command.logging_level)
87+
88+
self.assertEqual(command.logging_level, current_value)
89+
6990
app_root = os.path.join(TestDecorators._package_directory, 'data', 'app')
7091
command = SearchCommand() # guarantee: no logging.conf
7192
directory = os.getcwd()

0 commit comments

Comments
 (0)