|
19 | 19 | except ImportError: |
20 | 20 | import unittest |
21 | 21 |
|
| 22 | +import imp |
22 | 23 | from json import JSONEncoder |
23 | 24 | from subprocess import Popen |
24 | 25 | import os |
25 | 26 | import shutil |
26 | 27 | from tests import testlib |
27 | 28 |
|
| 29 | +try: |
| 30 | + from cStringIO import StringIO |
| 31 | +except ImportError: |
| 32 | + from StringIO import StringIO |
| 33 | + |
| 34 | + |
| 35 | + |
28 | 36 | from splunklib.results import \ |
29 | 37 | Message, ResultsReader |
30 | 38 |
|
31 | 39 | from splunklib.searchcommands import \ |
32 | | - GeneratingCommand, ReportingCommand, StreamingCommand, Configuration, Option, validators |
33 | | - |
| 40 | + GeneratingCommand, ReportingCommand, StreamingCommand, Configuration, Option, validators, dispatch |
34 | 41 |
|
35 | 42 | @Configuration() |
36 | 43 | class StubbedGeneratingCommand(GeneratingCommand): |
@@ -198,8 +205,13 @@ def stream(self, records): |
198 | 205 | pass |
199 | 206 |
|
200 | 207 |
|
| 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 | + |
201 | 212 | class TestSearchCommandsApp(testlib.SDKTestCase): |
202 | 213 | def setUp(self): |
| 214 | + |
203 | 215 | super(TestSearchCommandsApp, self).setUp() |
204 | 216 | for directory in 'log', 'output': |
205 | 217 | path = TestSearchCommandsApp._data_file(directory) |
@@ -284,6 +296,92 @@ def test_generating_command_in_isolation(self): |
284 | 296 | # self._assertCorrectOutputFile('test_generating_command_in_isolation.execute.csv') |
285 | 297 | return |
286 | 298 |
|
| 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 | + |
287 | 385 | def test_generating_command_on_server(self): |
288 | 386 | expected, actual = self._getOneshotResults( |
289 | 387 | '| 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): |
355 | 453 | return |
356 | 454 |
|
357 | 455 | 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 |
359 | 457 | #http://docs.splunk.com/Documentation/Splunk/6.1.3/SearchReference/Inputcsv |
360 | 458 |
|
361 | 459 | expected, actual = self._getOneshotResults( |
|
0 commit comments