<s> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> from cStringIO import StringIO <EOL> from unittest import main , TestCase <EOL> import os <EOL> import sys <EOL> import logging <EOL> from splunklib . searchcommands import environment <EOL> from splunklib . searchcommands . decorators import Configuration <EOL> from splunklib . searchcommands . search_command import SearchCommand <EOL> from tests . searchcommands import package_directory , rebase_environment <EOL> @ Configuration ( ) <EOL> class StubbedSearchCommand ( SearchCommand ) : <EOL> class ConfigurationSettings ( SearchCommand . ConfigurationSettings ) : <EOL> @ classmethod <EOL> def fix_up ( cls , command_class ) : <EOL> pass <EOL> class TestBuiltinOptions ( TestCase ) : <EOL> def setUp ( self ) : <EOL> TestCase . setUp ( self ) <EOL> def test_logging_configuration ( self ) : <EOL> rebase_environment ( '<STR_LIT>' ) <EOL> self . assertIsInstance ( environment . splunklib_logger , logging . Logger ) <EOL> self . assertIsNone ( environment . logging_configuration ) <EOL> self . assertEqual ( len ( logging . root . handlers ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( logging . Logger . manager . loggerDict ) , <NUM_LIT:1> ) <EOL> self . assertIsInstance ( logging . root . handlers [ <NUM_LIT:0> ] , logging . StreamHandler ) <EOL> self . assertIs ( environment . splunklib_logger , logging . getLogger ( '<STR_LIT>' ) ) <EOL> self . assertIsNone ( environment . logging_configuration ) <EOL> command = StubbedSearchCommand ( ) <EOL> self . assertIs ( command . logger , logging . getLogger ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( len ( command . logger . handlers ) , <NUM_LIT:0> ) <EOL> self . assertIsNone ( command . logging_configuration ) <EOL> self . assertIs ( command . logger . root , logging . root ) <EOL> self . assertEqual ( len ( logging . root . handlers ) , <NUM_LIT:1> ) <EOL> root_handler = logging . root . handlers [ <NUM_LIT:0> ] <EOL> self . assertIsInstance ( root_handler , logging . StreamHandler ) <EOL> self . assertEqual ( root_handler . stream , sys . stderr ) <EOL> self . assertEqual ( command . logging_level , logging . getLevelName ( logging . WARNING ) ) <EOL> root_handler . stream = StringIO ( ) <EOL> message = '<STR_LIT>' <EOL> command . logger . warning ( message ) <EOL> self . assertEqual ( root_handler . stream . getvalue ( ) , message + '<STR_LIT:\n>' ) <EOL> rebase_environment ( '<STR_LIT>' ) <EOL> command = StubbedSearchCommand ( ) <EOL> self . assertEqual ( command . logging_configuration , os . path . join ( environment . app_root , '<STR_LIT:default>' , '<STR_LIT>' ) ) <EOL> self . assertIs ( command . logger , logging . getLogger ( '<STR_LIT>' ) ) <EOL> command . logging_configuration = '<STR_LIT>' <EOL> self . assertEqual ( <EOL> command . logging_configuration , os . path . join ( environment . app_root , '<STR_LIT:default>' , '<STR_LIT>' ) ) <EOL> self . assertIs ( command . logger , logging . getLogger ( '<STR_LIT>' ) ) <EOL> app_root_logging_configuration = os . path . join ( environment . app_root , '<STR_LIT>' ) <EOL> command . logging_configuration = app_root_logging_configuration <EOL> self . assertEqual ( command . logging_configuration , app_root_logging_configuration ) <EOL> self . assertIs ( command . logger , logging . getLogger ( '<STR_LIT>' ) ) <EOL> try : <EOL> command . logging_configuration = '<STR_LIT:foo>' <EOL> except ValueError : <EOL> pass <EOL> except BaseException as e : <EOL> self . fail ( '<STR_LIT>' . format ( type ( e ) ) ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' . format ( command . logging_configuration ) ) <EOL> try : <EOL> command . logging_configuration = os . path . join ( package_directory , '<STR_LIT>' ) <EOL> except ValueError : <EOL> pass <EOL> except BaseException as e : <EOL> self . fail ( '<STR_LIT>' . format ( type ( e ) ) ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' . format ( command . logging_configuration ) ) <EOL> def test_logging_level ( self ) : <EOL> rebase_environment ( '<STR_LIT>' ) <EOL> command = StubbedSearchCommand ( ) <EOL> warning = logging . getLevelName ( logging . WARNING ) <EOL> notset = logging . getLevelName ( logging . NOTSET ) <EOL> logging . root . setLevel ( logging . WARNING ) <EOL> self . assertEqual ( command . logging_level , warning ) <EOL> self . assertEquals ( warning , command . logging_level ) <EOL> for level in logging . _levelNames : <EOL> if type ( level ) is int : <EOL> command . logging_level = level <EOL> level_name = logging . getLevelName ( level ) <EOL> self . assertEquals ( command . logging_level , warning if level_name == notset else level_name ) <EOL> else : <EOL> level_name = logging . getLevelName ( logging . getLevelName ( level ) ) <EOL> for variant in level , level . lower ( ) , level . capitalize ( ) : <EOL> command . logging_level = variant <EOL> self . assertEquals ( command . logging_level , warning if level_name == notset else level_name ) <EOL> for level in <NUM_LIT> , <NUM_LIT> : <EOL> command . logging_level = level <EOL> self . assertEqual ( command . logging_level , '<STR_LIT>' ) <EOL> current_value = command . logging_level <EOL> try : <EOL> command . logging_level = '<STR_LIT:foo>' <EOL> except ValueError : <EOL> pass <EOL> except BaseException as e : <EOL> self . fail ( '<STR_LIT>' . format ( type ( e ) ) ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' . format ( command . logging_level ) ) <EOL> self . assertEqual ( command . logging_level , current_value ) <EOL> def test_record ( self ) : <EOL> self . _test_boolean_option ( StubbedSearchCommand . record ) <EOL> def test_show_configuration ( self ) : <EOL> self . _test_boolean_option ( StubbedSearchCommand . show_configuration ) <EOL> def _test_boolean_option ( self , option ) : <EOL> rebase_environment ( '<STR_LIT>' ) <EOL> command = StubbedSearchCommand ( ) <EOL> boolean_values = { <EOL> '<STR_LIT:0>' : False , '<STR_LIT:1>' : True , <EOL> '<STR_LIT:f>' : False , '<STR_LIT:t>' : True , <EOL> '<STR_LIT:n>' : False , '<STR_LIT:y>' : True , <EOL> '<STR_LIT>' : False , '<STR_LIT:yes>' : True , <EOL> '<STR_LIT:false>' : False , '<STR_LIT:true>' : True } <EOL> for value in boolean_values : <EOL> for variant in value , value . capitalize ( ) , value . upper ( ) : <EOL> for s in variant , bytes ( variant ) : <EOL> option . fset ( command , s ) <EOL> self . assertEquals ( option . fget ( command ) , boolean_values [ value ] ) <EOL> option . fset ( command , None ) <EOL> self . assertEquals ( option . fget ( command ) , None ) <EOL> for value in <NUM_LIT> , b'<STR_LIT>' , '<STR_LIT:string>' , object ( ) : <EOL> try : <EOL> option . fset ( command , value ) <EOL> except ValueError : <EOL> pass <EOL> except BaseException as error : <EOL> self . fail ( '<STR_LIT>' . format ( <EOL> option . name , repr ( value ) , type ( error ) ) ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' . format ( <EOL> option . name , repr ( option . fget ( command ) ) ) ) <EOL> return <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
