|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2011-2013 Splunk, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +try: |
| 18 | + import unittest2 as unittest |
| 19 | +except ImportError: |
| 20 | + import unittest |
| 21 | + |
| 22 | +from splunklib.searchcommands import Configuration, StreamingCommand |
| 23 | +from cStringIO import StringIO |
| 24 | + |
| 25 | + |
| 26 | +@Configuration() |
| 27 | +class SearchCommand(StreamingCommand): |
| 28 | + |
| 29 | + def stream(self, records): |
| 30 | + value = 0 |
| 31 | + for record in records: |
| 32 | + action = record['Action'] |
| 33 | + if action == 'raise_error': |
| 34 | + raise RuntimeError('Testing') |
| 35 | + yield {'Data': value} |
| 36 | + value += 1 |
| 37 | + return |
| 38 | + |
| 39 | +class TestSearchCommandsCommand(unittest.TestCase): |
| 40 | + |
| 41 | + def setUp(self): |
| 42 | + super(TestSearchCommandsCommand, self).setUp() |
| 43 | + return |
| 44 | + |
| 45 | + def test_process(self): |
| 46 | + |
| 47 | + # Command.process should complain if supports_getinfo == False |
| 48 | + # We support dynamic configuration, not static |
| 49 | + |
| 50 | + expected = \ |
| 51 | + 'error_message=Command search appears to be statically configured and static configuration is unsupported by splunklib.searchcommands. Please ensure that default/commands.conf contains this stanza: [search] | filename = foo.py | supports_getinfo = true | supports_rawargs = true | outputheader = true\r\n' \ |
| 52 | + '\r\n' |
| 53 | + |
| 54 | + command = SearchCommand() |
| 55 | + result = StringIO() |
| 56 | + command.process(['foo.py'], output_file=result) |
| 57 | + result.reset() |
| 58 | + observed = result.read() |
| 59 | + self.assertEqual(observed, expected) |
| 60 | + |
| 61 | + # Command.process should return configuration settings on Getinfo probe |
| 62 | + |
| 63 | + expected = \ |
| 64 | + '\r\n' \ |
| 65 | + 'changes_colorder,clear_required_fields,enableheader,generating,local,maxinputs,needs_empty_results,outputheader,overrides_timeorder,passauth,perf_warn_limit,required_fields,requires_srinfo,retainsevents,run_in_preview,stderr_dest,streaming,supports_multivalues,supports_rawargs,__mv_changes_colorder,__mv_clear_required_fields,__mv_enableheader,__mv_generating,__mv_local,__mv_maxinputs,__mv_needs_empty_results,__mv_outputheader,__mv_overrides_timeorder,__mv_passauth,__mv_perf_warn_limit,__mv_required_fields,__mv_requires_srinfo,__mv_retainsevents,__mv_run_in_preview,__mv_stderr_dest,__mv_streaming,__mv_supports_multivalues,__mv_supports_rawargs\r\n' \ |
| 66 | + '1,0,1,0,0,0,1,1,0,0,0,,0,1,1,log,1,1,1,,,,,,,,,,,,,,,,,,,\r\n' |
| 67 | + |
| 68 | + command = SearchCommand() |
| 69 | + result = StringIO() |
| 70 | + command.process(['foo.py', '__GETINFO__'], output_file=result) |
| 71 | + result.reset() |
| 72 | + observed = result.read() |
| 73 | + self.assertEqual(observed, expected) |
| 74 | + |
| 75 | + # Command.process should produce an error record on parser errors, if |
| 76 | + # invoked to get configuration settings |
| 77 | + |
| 78 | + expected = \ |
| 79 | + '\r\n' \ |
| 80 | + 'ERROR,__mv_ERROR\r\n' \ |
| 81 | + 'Unrecognized option: undefined_option = value,\r\n' |
| 82 | + |
| 83 | + command = SearchCommand() |
| 84 | + result = StringIO() |
| 85 | + command.process(['foo.py', '__GETINFO__', 'undefined_option=value'], output_file=result) |
| 86 | + result.reset() |
| 87 | + observed = result.read() |
| 88 | + self.assertEqual(observed, expected) |
| 89 | + |
| 90 | + # Command.process should produce an error message and exit on parser |
| 91 | + # errors, if invoked to execute |
| 92 | + |
| 93 | + expected = \ |
| 94 | + 'error_message=Unrecognized option: undefined_option = value\r\n' \ |
| 95 | + '\r\n' |
| 96 | + |
| 97 | + command = SearchCommand() |
| 98 | + result = StringIO() |
| 99 | + |
| 100 | + try: |
| 101 | + command.process(args=['foo.py', '__EXECUTE__', 'undefined_option=value'], input_file=StringIO('\r\n'), output_file=result) |
| 102 | + except SystemExit as e: |
| 103 | + result.reset() |
| 104 | + observed = result.read() |
| 105 | + self.assertEqual(e.code != 0) |
| 106 | + self.assertEqual(observed, expected) |
| 107 | + except BaseException as e: |
| 108 | + self.fail("Expected SystemExit, but caught %s" % type(e)) |
| 109 | + else: |
| 110 | + self.fail("Expected SystemExit, but no exception was raised") |
| 111 | + |
| 112 | + # Command.process should exit on processing exceptions |
| 113 | + |
| 114 | + command = SearchCommand() |
| 115 | + result = StringIO() |
| 116 | + |
| 117 | + try: |
| 118 | + command.process(args=['foo.py', '__EXECUTE__'], input_file=StringIO('\r\nAction\r\nraise_error'), output_file=result) |
| 119 | + except SystemExit as e: |
| 120 | + result.reset() |
| 121 | + observed = result.read() |
| 122 | + self.assertEqual(e.code != 0) |
| 123 | + self.assertEqual(observed, expected) |
| 124 | + except BaseException as e: |
| 125 | + self.fail("Expected SystemExit, but caught %s" % type(e)) |
| 126 | + else: |
| 127 | + self.fail("Expected SystemExit, but no exception was raised") |
| 128 | + |
| 129 | + return |
0 commit comments