|
| 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.search_command_internals import SearchCommandParser |
| 23 | +from splunklib.searchcommands import Configuration, StreamingCommand |
| 24 | + |
| 25 | +import logging |
| 26 | + |
| 27 | +@Configuration() |
| 28 | +class SearchCommand(StreamingCommand): |
| 29 | + |
| 30 | + def stream(self, records): |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +class TestSearchCommandsDecorators(unittest.TestCase): |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + super(TestSearchCommandsDecorators, self).setUp() |
| 38 | + return |
| 39 | + |
| 40 | + def test_builtin_options(self): |
| 41 | + |
| 42 | + # logging_level accepts all logging levels and returns their canonical |
| 43 | + # string values |
| 44 | + |
| 45 | + warning = logging.getLevelName(logging.WARNING) |
| 46 | + notset = logging.getLevelName(logging.NOTSET) |
| 47 | + command = SearchCommand() |
| 48 | + |
| 49 | + self.assertEquals(command.logging_level, warning) |
| 50 | + |
| 51 | + for level in logging._levelNames: |
| 52 | + if type(level) is int: |
| 53 | + command.logging_level = level |
| 54 | + level_name = logging.getLevelName(level) |
| 55 | + self.assertEquals(command.logging_level, warning if level_name == notset else level_name) |
| 56 | + else: |
| 57 | + level_name = logging.getLevelName(logging.getLevelName(level)) |
| 58 | + for variant in level, level.lower(), level.capitalize(): |
| 59 | + command.logging_level = variant |
| 60 | + self.assertEquals(command.logging_level, warning if level_name == notset else level_name) |
| 61 | + |
| 62 | + # show_configuration accepts Splunk boolean values |
| 63 | + |
| 64 | + boolean_values = { |
| 65 | + '0': False, '1': True, |
| 66 | + 'f': False, 't': True, |
| 67 | + 'n': False, 'y': True, |
| 68 | + 'no': False, 'yes': True, |
| 69 | + 'false': False, 'true': True} |
| 70 | + |
| 71 | + for value in boolean_values: |
| 72 | + for variant in [value, value.capitalize(), value.upper()]: |
| 73 | + command.show_configuration = variant |
| 74 | + self.assertEquals(command.show_configuration, boolean_values[value]) |
| 75 | + |
| 76 | + for value in 'any-other-string', 13: |
| 77 | + try: |
| 78 | + command.show_configuration = value |
| 79 | + except ValueError: |
| 80 | + pass |
| 81 | + except Exception as e: |
| 82 | + self.fail('Expected ValueError, but a %s was raised' % type(e)) |
| 83 | + else: |
| 84 | + self.fail('Expected ValueError, but show_configuration=%s' % command.show_configuration) |
| 85 | + |
| 86 | + # SearchCommandParser recognizes each built-in option |
| 87 | + |
| 88 | + for argv in ['logging_level=DEBUG'], ['show_configuration=true']: |
| 89 | + parser = SearchCommandParser() |
| 90 | + parser.parse(argv, command) |
| 91 | + |
| 92 | + return |
0 commit comments