Skip to content

Commit 99bba23

Browse files
author
Shakeel Mohamed
committed
add unit tests for generating commands; fix logging.py path
1 parent fbbdf4e commit 99bba23

2 files changed

Lines changed: 103 additions & 4 deletions

File tree

‎splunklib/searchcommands/logging.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def configure(name, path=None):
7171
.. _ConfigParser format: http://goo.gl/K6edZ8
7272
7373
"""
74-
app_directory = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0])))
74+
# TODO: verify that reverting to this, instead of using sys.argv[0] works
75+
app_directory = os.path.dirname(os.getcwd())
7576

7677
if path is None:
7778
probing_path = [

‎tests/searchcommands/test_examples.py‎

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,25 @@
1919
except ImportError:
2020
import unittest
2121

22+
import imp
2223
from json import JSONEncoder
2324
from subprocess import Popen
2425
import os
2526
import shutil
2627
from tests import testlib
2728

29+
try:
30+
from cStringIO import StringIO
31+
except ImportError:
32+
from StringIO import StringIO
33+
34+
35+
2836
from splunklib.results import \
2937
Message, ResultsReader
3038

3139
from splunklib.searchcommands import \
32-
GeneratingCommand, ReportingCommand, StreamingCommand, Configuration, Option, validators
33-
40+
GeneratingCommand, ReportingCommand, StreamingCommand, Configuration, Option, validators, dispatch
3441

3542
@Configuration()
3643
class StubbedGeneratingCommand(GeneratingCommand):
@@ -198,8 +205,13 @@ def stream(self, records):
198205
pass
199206

200207

208+
def get_searchcommand_example(filename):
209+
return os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "examples", "searchcommands_app", "bin", filename)
210+
211+
201212
class TestSearchCommandsApp(testlib.SDKTestCase):
202213
def setUp(self):
214+
203215
super(TestSearchCommandsApp, self).setUp()
204216
for directory in 'log', 'output':
205217
path = TestSearchCommandsApp._data_file(directory)
@@ -284,6 +296,92 @@ def test_generating_command_in_isolation(self):
284296
# self._assertCorrectOutputFile('test_generating_command_in_isolation.execute.csv')
285297
return
286298

299+
def test_generating_command_as_unit(self):
300+
simulate_path = get_searchcommand_example("simulate.py")
301+
self.assertTrue(os.path.isfile(simulate_path))
302+
303+
# Copy population.csv to the $SPLUNK_HOME/var/run/splunk/ directory
304+
population_file = os.path.join(os.path.dirname(simulate_path), "population.csv")
305+
shutil.copy(population_file, validators.File._var_run_splunk)
306+
307+
# load the SimulateCommand class from simulate.py
308+
simulate = imp.load_source('searchcommands_app', simulate_path)
309+
310+
instream = StringIO()
311+
outstream = StringIO()
312+
cli_args = [
313+
"simulate.py",
314+
"__EXECUTE__",
315+
"duration=00:00:10",
316+
"csv=population.csv",
317+
"rate=1",
318+
"interval=00:00:01"]
319+
# Run the process
320+
dispatch(simulate.SimulateCommand, cli_args, instream, outstream, "__main__")
321+
322+
rows = outstream.getvalue().split("\r\n")[1:]
323+
324+
found_fields = rows[0].split(",")
325+
expected_fields = [
326+
'_time',
327+
'_serial',
328+
'text',
329+
'__mv__time',
330+
'__mv__serial',
331+
'__mv_text',
332+
]
333+
self.assertEqual(len(expected_fields), len(found_fields))
334+
self.assertEqual(expected_fields, found_fields)
335+
336+
# did we get at least one event?
337+
self.assertTrue(2 < len(rows))
338+
339+
return
340+
341+
def test_helloworld_generating_command_as_unit(self):
342+
# TODO: revise this test to use dispatch instead
343+
helloworld_path = get_searchcommand_example("generatehello.py")
344+
345+
self.assertTrue(os.path.isfile(helloworld_path))
346+
347+
helloworld = imp.load_source('searchcommands_app', helloworld_path)
348+
instance = helloworld.GenerateHelloCommand()
349+
350+
instream = StringIO()
351+
outstream = StringIO()
352+
cli_args = [
353+
"generatehello.py",
354+
"__EXECUTE__",
355+
"count=5",
356+
]
357+
# Run the process
358+
dispatch(helloworld.GenerateHelloCommand, cli_args, instream, outstream, "__main__")
359+
360+
rows = outstream.getvalue().split("\r\n")[1:]
361+
362+
found_fields = rows[0].split(",")
363+
expected_fields = [
364+
'_time',
365+
'event_no',
366+
'_raw',
367+
'__mv__time',
368+
'__mv_event_no',
369+
'__mv__raw',
370+
]
371+
372+
self.assertEqual(len(expected_fields), len(found_fields))
373+
self.assertEqual(expected_fields, found_fields)
374+
375+
events = rows[1:-1]
376+
self.assertEqual(5, len(events))
377+
378+
for i in range(0, len(events)):
379+
event = events[i].split(",")
380+
self.assertEqual(i + 1, int(event[1]))
381+
self.assertEqual(i + 1, int(event[2][-1]))
382+
383+
return
384+
287385
def test_generating_command_on_server(self):
288386
expected, actual = self._getOneshotResults(
289387
'| simulate csv=population.csv rate=200 interval=00:00:01 duration=00:00:02 seed=%s' % TestSearchCommandsApp._seed,
@@ -355,7 +453,7 @@ def test_streaming_command_in_isolation(self):
355453
return
356454

357455
def test_streaming_command_on_server(self):
358-
#TODO: add integration test to updload tweets.csv relative to $SPLUNK_HOME/var/run
456+
#TODO: add integration test to upload tweets.csv relative to $SPLUNK_HOME/var/run
359457
#http://docs.splunk.com/Documentation/Splunk/6.1.3/SearchReference/Inputcsv
360458

361459
expected, actual = self._getOneshotResults(

0 commit comments

Comments
 (0)