<s> import inspect <EOL> import six <EOL> from webob . util import status_reasons <EOL> from nova import context <EOL> from nova import exception <EOL> from nova import test <EOL> class FakeNotifier ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . provided_context = None <EOL> self . provided_event = None <EOL> self . provided_payload = None <EOL> def error ( self , context , event , payload ) : <EOL> self . provided_context = context <EOL> self . provided_event = event <EOL> self . provided_payload = payload <EOL> def good_function ( self , context ) : <EOL> return <NUM_LIT> <EOL> def bad_function_exception ( self , context , extra , blah = "<STR_LIT:a>" , boo = "<STR_LIT:b>" , zoo = None ) : <EOL> raise test . TestingException ( ) <EOL> class WrapExceptionTestCase ( test . NoDBTestCase ) : <EOL> def test_wrap_exception_good_return ( self ) : <EOL> wrapped = exception . wrap_exception ( '<STR_LIT:foo>' ) <EOL> self . assertEqual ( <NUM_LIT> , wrapped ( good_function ) ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> def test_wrap_exception_with_notifier ( self ) : <EOL> notifier = FakeNotifier ( ) <EOL> wrapped = exception . wrap_exception ( notifier ) <EOL> ctxt = context . get_admin_context ( ) <EOL> self . assertRaises ( test . TestingException , <EOL> wrapped ( bad_function_exception ) , <NUM_LIT:1> , ctxt , <NUM_LIT:3> , zoo = <NUM_LIT:3> ) <EOL> self . assertEqual ( "<STR_LIT>" , notifier . provided_event ) <EOL> self . assertEqual ( notifier . provided_context , ctxt ) <EOL> self . assertEqual ( <NUM_LIT:3> , notifier . provided_payload [ '<STR_LIT:args>' ] [ '<STR_LIT>' ] ) <EOL> for key in [ '<STR_LIT>' , '<STR_LIT:args>' ] : <EOL> self . assertIn ( key , notifier . provided_payload . keys ( ) ) <EOL> class NovaExceptionTestCase ( test . NoDBTestCase ) : <EOL> def test_default_error_msg ( self ) : <EOL> class FakeNovaException ( exception . NovaException ) : <EOL> msg_fmt = "<STR_LIT>" <EOL> exc = FakeNovaException ( ) <EOL> self . assertEqual ( '<STR_LIT>' , six . text_type ( exc ) ) <EOL> def test_error_msg ( self ) : <EOL> self . assertEqual ( '<STR_LIT:test>' , <EOL> six . text_type ( exception . NovaException ( '<STR_LIT:test>' ) ) ) <EOL> def test_default_error_msg_with_kwargs ( self ) : <EOL> class FakeNovaException ( exception . NovaException ) : <EOL> msg_fmt = "<STR_LIT>" <EOL> exc = FakeNovaException ( code = <NUM_LIT> ) <EOL> self . assertEqual ( '<STR_LIT>' , six . text_type ( exc ) ) <EOL> self . assertEqual ( '<STR_LIT>' , exc . message ) <EOL> def test_error_msg_exception_with_kwargs ( self ) : <EOL> class FakeNovaException ( exception . NovaException ) : <EOL> msg_fmt = "<STR_LIT>" <EOL> exc = FakeNovaException ( code = <NUM_LIT> , misspelled_code = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , six . text_type ( exc ) ) <EOL> self . assertEqual ( '<STR_LIT>' , exc . message ) <EOL> def test_default_error_code ( self ) : <EOL> class FakeNovaException ( exception . NovaException ) : <EOL> code = <NUM_LIT> <EOL> exc = FakeNovaException ( ) <EOL> self . assertEqual ( <NUM_LIT> , exc . kwargs [ '<STR_LIT:code>' ] ) <EOL> def test_error_code_from_kwarg ( self ) : <EOL> class FakeNovaException ( exception . NovaException ) : <EOL> code = <NUM_LIT> <EOL> exc = FakeNovaException ( code = <NUM_LIT> ) <EOL> self . assertEqual ( exc . kwargs [ '<STR_LIT:code>' ] , <NUM_LIT> ) <EOL> def test_cleanse_dict ( self ) : <EOL> kwargs = { '<STR_LIT:foo>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:4> } <EOL> self . assertEqual ( { '<STR_LIT:foo>' : <NUM_LIT:1> } , exception . _cleanse_dict ( kwargs ) ) <EOL> kwargs = { } <EOL> self . assertEqual ( { } , exception . _cleanse_dict ( kwargs ) ) <EOL> def test_format_message_local ( self ) : <EOL> class FakeNovaException ( exception . NovaException ) : <EOL> msg_fmt = "<STR_LIT>" <EOL> exc = FakeNovaException ( ) <EOL> self . assertEqual ( six . text_type ( exc ) , exc . format_message ( ) ) <EOL> def test_format_message_remote ( self ) : <EOL> class FakeNovaException_Remote ( exception . NovaException ) : <EOL> msg_fmt = "<STR_LIT>" <EOL> if six . PY2 : <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" <EOL> else : <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> exc = FakeNovaException_Remote ( ) <EOL> self . assertEqual ( u"<STR_LIT>" , six . text_type ( exc ) ) <EOL> self . assertEqual ( "<STR_LIT>" , exc . format_message ( ) ) <EOL> def test_format_message_remote_error ( self ) : <EOL> class FakeNovaException_Remote ( exception . NovaException ) : <EOL> msg_fmt = "<STR_LIT>" <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" <EOL> self . flags ( fatal_exception_format_errors = False ) <EOL> exc = FakeNovaException_Remote ( lame_arg = '<STR_LIT>' ) <EOL> self . assertEqual ( "<STR_LIT>" , exc . format_message ( ) ) <EOL> class ConvertedExceptionTestCase ( test . NoDBTestCase ) : <EOL> def test_instantiate ( self ) : <EOL> exc = exception . ConvertedException ( <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( exc . code , <NUM_LIT> ) <EOL> self . assertEqual ( exc . title , '<STR_LIT>' ) <EOL> self . assertEqual ( exc . explanation , '<STR_LIT>' ) <EOL> def test_instantiate_without_title_known_code ( self ) : <EOL> exc = exception . ConvertedException ( <NUM_LIT> ) <EOL> self . assertEqual ( exc . title , status_reasons [ <NUM_LIT> ] ) <EOL> def test_instantiate_without_title_unknown_code ( self ) : <EOL> exc = exception . ConvertedException ( <NUM_LIT> ) <EOL> self . assertEqual ( exc . title , '<STR_LIT>' ) <EOL> def test_instantiate_bad_code ( self ) : <EOL> self . assertRaises ( KeyError , exception . ConvertedException , <NUM_LIT:10> ) <EOL> class ExceptionTestCase ( test . NoDBTestCase ) : <EOL> @ staticmethod <EOL> def _raise_exc ( exc ) : <EOL> raise exc ( <NUM_LIT> ) <EOL> def test_exceptions_raise ( self ) : <EOL> self . flags ( fatal_exception_format_errors = False ) <EOL> for name in dir ( exception ) : <EOL> exc = getattr ( exception , name ) <EOL> if isinstance ( exc , type ) : <EOL> self . assertRaises ( exc , self . _raise_exc , exc ) <EOL> class ExceptionValidMessageTestCase ( test . NoDBTestCase ) : <EOL> def test_messages ( self ) : <EOL> failures = [ ] <EOL> for name , obj in inspect . getmembers ( exception ) : <EOL> if name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> continue <EOL> if not inspect . isclass ( obj ) : <EOL> continue <EOL> if not issubclass ( obj , exception . NovaException ) : <EOL> continue <EOL> e = obj <EOL> if e . msg_fmt == "<STR_LIT>" : <EOL> failures . append ( '<STR_LIT>' % name ) <EOL> if failures : <EOL> self . fail ( '<STR_LIT:\n>' . join ( failures ) ) </s>
