Skip to content

Commit 5384f4a

Browse files
author
Shakeel Mohamed
committed
use an app_root parameter for SearchCommand and Logging.configure to handle tests
1 parent cca213e commit 5384f4a

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

examples/searchcommands_app/bin/simulate.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ class SimulateCommand(GeneratingCommand):
8686

8787
def generate(self):
8888
""" Yields one random record at a time for the duration of `duration` """
89-
# TODO: fix this, __str__ function is getting an error on self.options
9089
self.logger.debug('SimulateCommand: %s', self) # log command line
9190
if not self.records:
9291
if self.seed is not None:

splunklib/searchcommands/logging.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import os
2020
import sys
2121

22+
def get_app_directory(probing_path):
23+
return os.path.dirname(os.path.abspath(os.path.dirname(probing_path)))
2224

23-
def configure(name, path=None):
25+
def configure(name, probing_path=None, app_root=None):
2426
""" Configure logging and return a logger and the location of its logging
2527
configuration file.
2628
@@ -57,64 +59,67 @@ def configure(name, path=None):
5759
return.
5860
5961
You may short circuit the search for a logging configuration file by
60-
providing an alternative file location in `path`. Logging configuration
62+
providing an alternative file location in `probing_path`. Logging configuration
6163
files must be in `ConfigParser format`_.
6264
6365
#Arguments:
6466
6567
:param name: Logger name
6668
:type name: str
67-
:param path: Location of an alternative logging configuration file or `None`
68-
:type path: str or NoneType
69+
:param probing_path: Location of an alternative logging configuration file or `None`
70+
:type probing_path: str or NoneType
6971
:returns: A logger and the location of its logging configuration file
72+
:param app_root: The root of the application directory, used primarily by tests.
73+
:type app_root: str or NoneType
7074
7175
.. _ConfigParser format: http://goo.gl/K6edZ8
7276
7377
"""
74-
# TODO: verify that reverting to this, instead of using sys.argv[0] works
75-
app_directory = os.path.dirname(os.getcwd())
78+
if app_root:
79+
print "app_root is " + app_root
80+
app_directory = get_app_directory(sys.argv[0]) if app_root is None else app_root
7681

77-
if path is None:
78-
probing_path = [
82+
if probing_path is None:
83+
probing_paths = [
7984
'local/%s.logging.conf' % name,
8085
'default/%s.logging.conf' % name,
8186
'local/logging.conf',
8287
'default/logging.conf']
83-
for relative_path in probing_path:
88+
for relative_path in probing_paths:
8489
configuration_file = os.path.join(app_directory, relative_path)
8590
if os.path.exists(configuration_file):
86-
path = configuration_file
91+
probing_path = configuration_file
8792
break
88-
elif not os.path.isabs(path):
93+
elif not os.path.isabs(probing_path):
8994
found = False
9095
for conf in 'local', 'default':
91-
configuration_file = os.path.join(app_directory, conf, path)
96+
configuration_file = os.path.join(app_directory, conf, probing_path)
9297
if os.path.exists(configuration_file):
93-
path = configuration_file
98+
probing_path = configuration_file
9499
found = True
95100
break
96101
if not found:
97102
raise ValueError(
98103
'Logging configuration file "%s" not found in local or default '
99-
'directory' % path)
100-
elif not os.path.exists(path):
104+
'directory' % probing_path)
105+
elif not os.path.exists(probing_path):
101106
raise ValueError('Logging configuration file "%s" not found')
102107

103-
if path is not None:
108+
if probing_path is not None:
104109
working_directory = os.getcwd()
105110
os.chdir(app_directory)
106111
try:
107112
splunk_home = os.path.normpath(os.path.join(working_directory, os.environ['SPLUNK_HOME']))
108113
except KeyError:
109114
splunk_home = working_directory # reasonable in debug scenarios
110115
try:
111-
path = os.path.abspath(path)
112-
fileConfig(path, {'SPLUNK_HOME': splunk_home})
116+
probing_path = os.path.abspath(probing_path)
117+
fileConfig(probing_path, {'SPLUNK_HOME': splunk_home})
113118
finally:
114119
os.chdir(working_directory)
115120

116121
if len(root.handlers) == 0:
117122
root.addHandler(StreamHandler())
118123

119124
logger = getLogger(name)
120-
return logger, path
125+
return logger, probing_path

splunklib/searchcommands/search_command.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ class SearchCommand(object):
4343
4444
"""
4545

46-
def __init__(self):
46+
def __init__(self, app_root=None):
47+
"""
48+
:param app_root: The root of the application directory, used primarily by tests.
49+
:type app_root: str or NoneType
50+
"""
4751

4852
# Variables that may be used, but not altered by derived classes
4953

50-
self.logger, self._logging_configuration = logging.configure(type(self).__name__)
54+
self.logger, self._logging_configuration = logging.configure(type(self).__name__, app_root=app_root)
5155
self.input_header = InputHeader()
5256
self.messages = MessagesHeader()
5357

@@ -61,6 +65,7 @@ def __init__(self):
6165

6266
# Variables backing option/property values
6367

68+
self._app_root = app_root
6469
self._default_logging_level = self.logger.level
6570
self._configuration = None
6671
self._fieldnames = None
@@ -95,7 +100,7 @@ def logging_configuration(self):
95100
@logging_configuration.setter
96101
def logging_configuration(self, value):
97102
self.logger, self._logging_configuration = logging.configure(
98-
type(self).__name__, value)
103+
type(self).__name__, value, app_root=self._app_root)
99104
return
100105

101106
@Option

tests/searchcommands/test_decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ def test_builtin_options(self):
117117

118118
# A search command loads {local,default}/logging.conf when it is
119119
# available
120-
121-
command = SearchCommand() # guarantee: default/logging.conf
120+
command = SearchCommand(
121+
app_root=app_root) # guarantee: default/logging.conf
122122
self.assertEqual(command.logging_configuration,
123123
default_logging_configuration)
124124

0 commit comments

Comments
 (0)