Skip to content

Commit 0ab0918

Browse files
author
David Noble
committed
Fixes to tests.searchcommands tests that were problematic on either Linux or Windows
We're good on OS X and CentOS 7 running against Splunk 6.3. More test fixes on the way for Windows.
1 parent 7688b2c commit 0ab0918

File tree

6 files changed

+63
-51
lines changed

6 files changed

+63
-51
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
* Added support for Search Command Protocol V2, for Splunk 6.3+.
66

7+
* Added `splunklib` logger so that command loggers can be configured independently of the `splunklib.searchcommands` module.
8+
9+
See `examples/searchcommands_app/package/default/logger.conf` for guidance on logging configuration.
10+
711
* Added `splunklib.searchcommands.validators.Match` class for verifying that an option value matches a regular expression pattern.
812

913
### Bug fixes

tests/searchcommands/test_decorators.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ def fix_up(cls, command_class):
221221
(True, False),
222222
(None, 'anything other than a bool')),
223223
('maxinputs',
224-
(0, 50000, sys.maxsize),
225-
(None, -1, sys.maxsize + 1, 'anything other than an int')),
224+
(0, 50000, sys.maxint),
225+
(None, -1, sys.maxint + 1, 'anything other than an int')),
226226
('overrides_timeorder',
227227
(True, False),
228228
(None, 'anything other than a bool')),
@@ -391,7 +391,7 @@ def test_option(self):
391391
"('code', u'foo == \"bar\"'),"
392392
"('duration', u'24:59:59'),"
393393
"('fieldname', u'some.field_name'),"
394-
"('file', u'" + __file__ + "'),"
394+
"('file', u" + unicode(repr(__file__)) + "),"
395395
"('integer', u'100'),"
396396
"('logging_configuration', " + repr(environment.logging_configuration) + "),"
397397
"('logging_level', u'WARNING'),"
@@ -404,7 +404,7 @@ def test_option(self):
404404
"('required_code', u'foo == \"bar\"'),"
405405
"('required_duration', u'24:59:59'),"
406406
"('required_fieldname', u'some.field_name'),"
407-
"('required_file', u'" + __file__ + "'),"
407+
"('required_file', u" + unicode(repr(__file__)) + "),"
408408
"('required_integer', u'100'),"
409409
"('required_map', 'foo'),"
410410
"('required_match', u'123-45-6789'),"
@@ -414,7 +414,7 @@ def test_option(self):
414414
"('set', u'bar'),"
415415
"('show_configuration', u'f')])")
416416

417-
observed = repr(command.options)
417+
observed = unicode(repr(command.options))
418418
self.assertEqual(observed, expected)
419419

420420
expected = (

tests/searchcommands/test_internals_v1.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,9 @@ def test_command_line_parser_unquote(self):
206206

207207
# Command line with an assortment of string values
208208

209-
print('Observed:', options[-4], '=>', parser.unquote(options[-4]))
210-
print('Expected:', expected[-4])
211209
self.assertEqual(expected[-4], parser.unquote(options[-4]))
212210

213211
for i in range(0, len(options)):
214-
print(i, 'Observed:', options[i], '=>', parser.unquote(options[i]))
215-
print(i, 'Expected:', expected[i])
216212
self.assertEqual(expected[i], parser.unquote(options[i]))
217213

218214
self.assertRaises(SyntaxError, parser.unquote, '"')

tests/searchcommands/test_internals_v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from functools import wraps
2525
from glob import iglob
2626
from itertools import chain, ifilter, imap, izip
27-
from sys import float_info, maxsize, maxunicode
27+
from sys import float_info, maxint as maxsize, maxunicode
2828
from tempfile import mktemp
2929
from time import time
3030
from types import MethodType

tests/searchcommands/test_searchcommands_app.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@
3939

4040

4141
def pypy():
42-
process = Popen(['pypy', '--version'], stderr=PIPE, stdout=PIPE)
43-
output, errors = process.communicate()
44-
return process.returncode == 0
42+
try:
43+
process = Popen(['pypy', '--version'], stderr=PIPE, stdout=PIPE)
44+
except OSError:
45+
return False
46+
else:
47+
process.communicate()
48+
return process.returncode == 0
4549

4650

4751
class Recording(object):
@@ -123,23 +127,28 @@ def __iter__(self):
123127

124128
class TestSearchCommandsApp(TestCase):
125129

130+
app_root = os.path.join(project_root, 'examples', 'searchcommands_app', 'build', 'searchcommands_app')
131+
126132
def setUp(self):
133+
if not os.path.isdir(TestSearchCommandsApp.app_root):
134+
build_command = os.path.join(project_root, 'examples', 'searchcommands_app', 'setup.py build')
135+
self.skipTest("You must build the searchcommands_app by running " + build_command)
127136
TestCase.setUp(self)
128137

129138
def test_countmatches_as_unit(self):
130139

131140
expected, output, errors, exit_status = self._run_command('countmatches', action='getinfo', protocol=1)
132-
self.assertEqual(0, exit_status)
141+
self.assertEqual(0, exit_status, msg=unicode(errors))
133142
# self.assertEqual('', errors)
134143
self.assertEqual(expected, output)
135144

136145
expected, output, errors, exit_status = self._run_command('countmatches', action='execute', protocol=1)
137-
self.assertEqual(0, exit_status)
146+
self.assertEqual(0, exit_status, msg=unicode(errors))
138147
# self.assertEqual('', errors)
139148
self.assertEqual(expected, output)
140149

141150
expected, output, errors, exit_status = self._run_command('countmatches')
142-
self.assertEqual(0, exit_status)
151+
self.assertEqual(0, exit_status, msg=unicode(errors))
143152
# self.assertEqual('', errors)
144153
self.assertEqual(expected, output)
145154

@@ -148,17 +157,17 @@ def test_countmatches_as_unit(self):
148157
def test_generatehello_as_unit(self):
149158

150159
expected, output, errors, exit_status = self._run_command('generatehello', action='getinfo', protocol=1)
151-
self.assertEqual(0, exit_status)
160+
self.assertEqual(0, exit_status, msg=unicode(errors))
152161
# self.assertEqual('', errors)
153162
# self.assertEqual(expected, output)
154163

155164
expected, output, errors, exit_status = self._run_command('generatehello', action='execute', protocol=1)
156-
self.assertEqual(0, exit_status)
165+
self.assertEqual(0, exit_status, msg=unicode(errors))
157166
# self.assertEqual('', errors)
158167
# self.assertEqual(expected, output)
159168

160169
expected, output, errors, exit_status = self._run_command('generatehello')
161-
self.assertEqual(0, exit_status)
170+
self.assertEqual(0, exit_status, msg=unicode(errors))
162171
# self.assertEqual('', errors)
163172
# self.assertEqual(expected, output)
164173

@@ -171,17 +180,17 @@ def test_generatehello_as_unit(self):
171180
def test_pypygeneratetext_as_unit(self):
172181

173182
expected, output, errors, exit_status = self._run_command('pypygeneratetext', action='getinfo', protocol=1)
174-
self.assertEqual(0, exit_status)
183+
self.assertEqual(0, exit_status, msg=unicode(errors))
175184
# self.assertEqual('', errors)
176185
self.assertEqual(expected, output)
177186

178187
expected, output, errors, exit_status = self._run_command('pypygeneratetext', action='execute', protocol=1)
179-
self.assertEqual(0, exit_status)
188+
self.assertEqual(0, exit_status, msg=unicode(errors))
180189
# self.assertEqual('', errors)
181190
# self.assertEqual(expected, output)
182191

183192
expected, output, errors, exit_status = self._run_command('pypygeneratetext')
184-
self.assertEqual(0, exit_status)
193+
self.assertEqual(0, exit_status, msg=unicode(errors))
185194
# self.assertEqual('', errors)
186195
# self.assertEqual(expected, output)
187196

@@ -190,32 +199,32 @@ def test_pypygeneratetext_as_unit(self):
190199
def test_sum_as_unit(self):
191200

192201
expected, output, errors, exit_status = self._run_command('sum', action='getinfo', phase='reduce', protocol=1)
193-
self.assertEqual(0, exit_status)
202+
self.assertEqual(0, exit_status, msg=unicode(errors))
194203
# self.assertEqual('', errors)
195204
self.assertInfoEqual(output, expected)
196205

197206
expected, output, errors, exit_status = self._run_command('sum', action='getinfo', phase='map', protocol=1)
198-
self.assertEqual(0, exit_status)
207+
self.assertEqual(0, exit_status, msg=unicode(errors))
199208
# self.assertEqual('', errors)
200209
self.assertInfoEqual(expected, output)
201210

202211
expected, output, errors, exit_status = self._run_command('sum', action='execute', phase='map', protocol=1)
203-
self.assertEqual(0, exit_status)
212+
self.assertEqual(0, exit_status, msg=unicode(errors))
204213
# self.assertEqual('', errors)
205214
self.assertEqual(expected, output)
206215

207216
expected, output, errors, exit_status = self._run_command('sum', action='execute', phase='reduce', protocol=1)
208-
self.assertEqual(0, exit_status)
217+
self.assertEqual(0, exit_status, msg=unicode(errors))
209218
# self.assertEqual('', errors)
210219
self.assertEqual(expected, output)
211220

212221
expected, output, errors, exit_status = self._run_command('sum', phase='map')
213-
self.assertEqual(0, exit_status)
222+
self.assertEqual(0, exit_status, msg=unicode(errors))
214223
# self.assertEqual('', errors)
215224
self.assertEqual(expected, output)
216225

217226
expected, output, errors, exit_status = self._run_command('sum', phase='reduce')
218-
self.assertEqual(0, exit_status)
227+
self.assertEqual(0, exit_status, msg=unicode(errors))
219228
# self.assertEqual('', errors)
220229
self.assertEqual(expected, output)
221230

@@ -239,7 +248,8 @@ def assertInfoEqual(self, output, expected):
239248
self.assertDictEqual(expected, output)
240249

241250
def _get_search_command_path(self, name):
242-
path = os.path.join(project_root, 'examples', 'searchcommands_app', 'package', 'bin', name + '.py')
251+
path = os.path.join(
252+
project_root, 'examples', 'searchcommands_app', 'build', 'searchcommands_app', 'bin', name + '.py')
243253
self.assertTrue(path)
244254
return path
245255

tests/searchcommands/test_validators.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import os
2525
import re
2626
import sys
27+
import tempfile
2728

2829
# P2 [ ] TODO: Verify that all format methods produce 'None' when value is None
2930

@@ -102,22 +103,23 @@ def test_file(self):
102103
# Create a file on $SPLUNK_HOME/var/run/splunk
103104

104105
file_name = 'TestValidators.test_file'
105-
full_path = os.path.join(validators.File._var_run_splunk, file_name)
106+
tempdir = tempfile.gettempdir()
107+
full_path = os.path.join(tempdir, file_name)
106108

107109
try:
108-
validator = validators.File(mode='w', buffering=4096)
110+
validator = validators.File(mode='w', buffering=4096, directory=tempdir)
109111

110112
with validator(file_name) as f:
111113
f.write('some text')
112114

113-
validator = validators.File(mode='a')
115+
validator = validators.File(mode='a', directory=tempdir)
114116

115117
with validator(full_path) as f:
116118
f.write('\nmore text')
117119

118-
# Verify that you can read the file from $SPLUNK_HOME/var/run/splunk using an absolute or relative path
120+
# Verify that you can read the file from a file using an absolute or relative path
119121

120-
validator = validators.File()
122+
validator = validators.File(directory=tempdir)
121123

122124
for path in file_name, full_path:
123125
with validator(path) as f:
@@ -138,8 +140,8 @@ def test_file(self):
138140

139141
def test_integer(self):
140142

141-
maxsize = sys.maxsize
142-
minsize = -(sys.maxsize - 1)
143+
maxint = sys.maxint
144+
minint = -(sys.maxint - 1)
143145

144146
# The Integer validator should convert values in the range of a Python long which has unlimited precision
145147
# Anecdotal evidence: This portion of the test checks 5-10 K integer values and runs for less than 2-3 seconds
@@ -153,18 +155,18 @@ def test(integer):
153155
self.assertIsInstance(value, long)
154156
self.assertEqual(validator.format(integer), unicode(integer))
155157

156-
test(2L * minsize)
157-
test(minsize)
158+
test(2L * minint)
159+
test(minint)
158160
test(-1)
159161
test(0)
160162
test(1)
161-
test(2L * maxsize)
163+
test(2L * maxint)
162164

163165
count = 3
164166

165-
start = -randint(0, maxsize - 1)
166-
stop = sys.maxsize
167-
step = randint(1000000000000000, 2000000000000000)
167+
start = -randint(0, maxint - 1)
168+
stop = maxint
169+
step = randint(1000000, 2000000)
168170

169171
for i in xrange(start, stop, step):
170172
test(i)
@@ -174,21 +176,21 @@ def test(integer):
174176

175177
validator = validators.Integer(minimum=0)
176178
self.assertEqual(validator.__call__(0), 0)
177-
self.assertEqual(validator.__call__(2L * maxsize), 2L * maxsize)
179+
self.assertEqual(validator.__call__(2L * maxint), 2L * maxint)
178180
self.assertRaises(ValueError, validator.__call__, -1)
179181

180-
validator = validators.Integer(minimum=1, maximum=maxsize)
182+
validator = validators.Integer(minimum=1, maximum=maxint)
181183
self.assertEqual(validator.__call__(1), 1)
182-
self.assertEqual(validator.__call__(maxsize), maxsize)
184+
self.assertEqual(validator.__call__(maxint), maxint)
183185
self.assertRaises(ValueError, validator.__call__, 0)
184-
self.assertRaises(ValueError, validator.__call__, maxsize + 1)
186+
self.assertRaises(ValueError, validator.__call__, maxint + 1)
185187

186-
validator = validators.Integer(minimum=minsize, maximum=maxsize)
187-
self.assertEqual(validator.__call__(minsize), minsize)
188+
validator = validators.Integer(minimum=minint, maximum=maxint)
189+
self.assertEqual(validator.__call__(minint), minint)
188190
self.assertEqual(validator.__call__(0), 0)
189-
self.assertEqual(validator.__call__(maxsize), maxsize)
190-
self.assertRaises(ValueError, validator.__call__, minsize - 1L)
191-
self.assertRaises(ValueError, validator.__call__, maxsize + 1L)
191+
self.assertEqual(validator.__call__(maxint), maxint)
192+
self.assertRaises(ValueError, validator.__call__, minint - 1L)
193+
self.assertRaises(ValueError, validator.__call__, maxint + 1L)
192194

193195
return
194196

0 commit comments

Comments
 (0)