Skip to content

Commit 91d2b2f

Browse files
author
David Noble
committed
Fixed '\r\n' problem correctly by setting the mode of sys.stdout.fileno to binary on 'win32'
Reenabled generating command test on/off server, but disabled content comparisons until we make time to create a more sensible generation command for testing.
1 parent 3a957cc commit 91d2b2f

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

splunklib/searchcommands/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ class sets this value unconditionally. You cannot override it.
150150
from .reporting_command import ReportingCommand
151151
from .streaming_command import StreamingCommand
152152

153+
if sys.platform == 'win32':
154+
# Work around the fact that on Windows '\n' is mapped to '\r\n'
155+
# The typical solution is to simply open files in binary mode, but stdout
156+
# is already open, thus this hack
157+
import msvcrt
158+
import os
159+
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
160+
153161

154162
def dispatch(command_class, argv=sys.argv, input_file=sys.stdin, output_file=
155163
sys.stdout, module_name=None):

splunklib/searchcommands/search_command_internals.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# under the License.
1414

1515
import collections
16-
import os
1716
import re
1817
import urllib2 as urllib
1918

@@ -170,9 +169,7 @@ def write(self, output_file):
170169
# for level, message in self:
171170
# output_file.write('%s=%s\r\n' % (level, message))
172171
self
173-
output_file.write(MessagesHeader.line_ending)
174-
175-
line_ending = '\n' if os.name == 'nt' else '\r\n'
172+
output_file.write('\r\n')
176173

177174

178175
class SearchCommandParser(object):

tests/test_searchcommands_app.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ def disable_test_option_show_configuration(self):
285285
os.path.join('log', 'test_option_show_configuration.log')))
286286
return
287287

288+
# TODO, use a generating command that doesn't do random sampling because
289+
# a seed is no guarantee that the same sample is produced on every platform
290+
# and all versions of python
291+
288292
def test_generating_command_configuration(self):
289293
self._assertCorrectConfiguration(
290294
StubbedGeneratingCommand(), 'test_generating_command_configuration')
@@ -306,14 +310,14 @@ def test_generating_command_in_isolation(self):
306310
os.path.join('output', 'test_generating_command_in_isolation.execute.csv'),
307311
os.path.join('log', 'test_generating_command_in_isolation.log')))
308312
self._assertCorrectOutputFile('test_generating_command_in_isolation.getinfo.csv')
309-
self._assertCorrectOutputFile('test_generating_command_in_isolation.execute.csv')
313+
# self._assertCorrectOutputFile('test_generating_command_in_isolation.execute.csv')
310314
return
311315

312-
def disabled_test_generating_command_on_server(self):
313-
# TODO, use a generating command that doesn't do random sampling
314-
self._assertCorrectOneshotResults(
316+
def test_generating_command_on_server(self):
317+
expected, actual = self._getOneshotResults(
315318
'| simulate csv=population.csv rate=200 interval=00:00:01 duration=00:00:02 seed=%s' % TestSearchCommandsApp._seed,
316319
'test_generating_command_on_server')
320+
# self.assertMultilLineEqual(expected, actual)
317321
return
318322

319323
def test_reporting_command_configuration(self):
@@ -351,9 +355,10 @@ def test_reporting_command_in_isolation(self):
351355
return
352356

353357
def test_reporting_command_on_server(self):
354-
self._assertCorrectOneshotResults(
358+
expected, actual = self._getOneshotResults(
355359
'| inputcsv tweets_with_word_counts.csv | sum total=total word_count',
356360
'test_reporting_command_on_server')
361+
self.assertMultiLineEqual(expected, actual)
357362
return
358363

359364
def test_streaming_command_configuration(self):
@@ -379,9 +384,10 @@ def test_streaming_command_in_isolation(self):
379384
return
380385

381386
def test_streaming_command_on_server(self):
382-
self._assertCorrectOneshotResults(
387+
expected, actual = self._getOneshotResults(
383388
'| inputcsv tweets.csv | countmatches fieldname=word_count pattern="\\\\w+" text',
384389
'test_streaming_command_on_server')
390+
self.assertMultiLineEqual(expected, actual)
385391
return
386392

387393
def _assertCorrectConfiguration(self, command, test_name):
@@ -398,7 +404,16 @@ def _assertCorrectConfiguration(self, command, test_name):
398404
expected = ''.join(input_file.readlines())
399405
self.assertMultiLineEqual(expected, actual)
400406

401-
def _assertCorrectOneshotResults(self, query, test_name):
407+
def _assertCorrectOutputFile(self, name):
408+
expected = os.path.join('_expected_results', name)
409+
actual = os.path.join('output', name)
410+
with TestSearchCommandsApp._open_data_file(expected, 'r') as expected:
411+
with TestSearchCommandsApp._open_data_file(actual, 'r') as actual:
412+
for actual_line, expected_line in zip(actual, expected):
413+
self.assertTrue(actual_line == expected_line)
414+
return
415+
416+
def _getOneshotResults(self, query, test_name):
402417
response = self.service.jobs.oneshot(query, app="searchcommands_app")
403418
reader = ResultsReader(response)
404419
actual = []
@@ -409,18 +424,9 @@ def _assertCorrectOneshotResults(self, query, test_name):
409424
actual += ['Message: %s' % result]
410425
actual = actual + ['is_preview = %s' % reader.is_preview]
411426
actual = '\n'.join(actual)
412-
with TestSearchCommandsApp._open_data_file('_expected_results/%s.txt' % test_name, 'r') as expected:
413-
self.assertMultiLineEqual(''.join(expected.readlines()), ''.join(actual))
414-
return
415-
416-
def _assertCorrectOutputFile(self, name):
417-
expected = os.path.join('_expected_results', name)
418-
actual = os.path.join('output', name)
419-
with TestSearchCommandsApp._open_data_file(expected, 'r') as expected:
420-
with TestSearchCommandsApp._open_data_file(actual, 'r') as actual:
421-
for actual_line, expected_line in zip(actual, expected):
422-
self.assertTrue(actual_line == expected_line)
423-
return
427+
with TestSearchCommandsApp._open_data_file('_expected_results/%s.txt' % test_name, 'r') as expected_file:
428+
expected = ''.join(expected_file.readlines())
429+
return actual, expected
424430

425431
def _run(self, command, args, **kwargs):
426432
for operation in ['__GETINFO__', '__EXECUTE__']:

0 commit comments

Comments
 (0)