<s> """<STR_LIT>""" <EOL> import functools <EOL> import os <EOL> import shutil <EOL> import uuid <EOL> import unittest <EOL> import mox <EOL> import nose . plugins . skip <EOL> import nova . image . fake <EOL> import shutil <EOL> import stubout <EOL> from eventlet import greenthread <EOL> from nova import fakerabbit <EOL> from nova import flags <EOL> from nova import log <EOL> from nova import rpc <EOL> from nova import utils <EOL> from nova import service <EOL> from nova . virt import fake <EOL> FLAGS = flags . FLAGS <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_bool ( '<STR_LIT>' , True , <EOL> '<STR_LIT>' ) <EOL> LOG = log . getLogger ( '<STR_LIT>' ) <EOL> class skip_test ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg ) : <EOL> self . message = msg <EOL> def __call__ ( self , func ) : <EOL> @ functools . wraps ( func ) <EOL> def _skipper ( * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> raise nose . SkipTest ( self . message ) <EOL> return _skipper <EOL> class skip_if ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , condition , msg ) : <EOL> self . condition = condition <EOL> self . message = msg <EOL> def __call__ ( self , func ) : <EOL> @ functools . wraps ( func ) <EOL> def _skipper ( * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if self . condition : <EOL> raise nose . SkipTest ( self . message ) <EOL> func ( * args , ** kw ) <EOL> return _skipper <EOL> class skip_unless ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , condition , msg ) : <EOL> self . condition = condition <EOL> self . message = msg <EOL> def __call__ ( self , func ) : <EOL> @ functools . wraps ( func ) <EOL> def _skipper ( * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if not self . condition : <EOL> raise nose . SkipTest ( self . message ) <EOL> func ( * args , ** kw ) <EOL> return _skipper <EOL> def skip_if_fake ( func ) : <EOL> """<STR_LIT>""" <EOL> def _skipper ( * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if FLAGS . fake_tests : <EOL> raise unittest . SkipTest ( '<STR_LIT>' ) <EOL> else : <EOL> return func ( * args , ** kw ) <EOL> return _skipper <EOL> class TestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( TestCase , self ) . setUp ( ) <EOL> self . start = utils . utcnow ( ) <EOL> shutil . copyfile ( os . path . join ( FLAGS . state_path , FLAGS . sqlite_clean_db ) , <EOL> os . path . join ( FLAGS . state_path , FLAGS . sqlite_db ) ) <EOL> self . mox = mox . Mox ( ) <EOL> self . stubs = stubout . StubOutForTesting ( ) <EOL> self . flag_overrides = { } <EOL> self . injected = [ ] <EOL> self . _services = [ ] <EOL> self . _original_flags = FLAGS . FlagValuesDict ( ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . mox . UnsetStubs ( ) <EOL> self . stubs . UnsetAll ( ) <EOL> self . stubs . SmartUnsetAll ( ) <EOL> self . mox . VerifyAll ( ) <EOL> super ( TestCase , self ) . tearDown ( ) <EOL> finally : <EOL> if FLAGS . fake_rabbit : <EOL> fakerabbit . reset_all ( ) <EOL> if FLAGS . connection_type == '<STR_LIT>' : <EOL> if hasattr ( fake . FakeConnection , '<STR_LIT>' ) : <EOL> del fake . FakeConnection . _instance <EOL> if FLAGS . image_service == '<STR_LIT>' : <EOL> nova . image . fake . FakeImageService_reset ( ) <EOL> self . reset_flags ( ) <EOL> for x in self . injected : <EOL> try : <EOL> x . stop ( ) <EOL> except AssertionError : <EOL> pass <EOL> for x in self . _services : <EOL> try : <EOL> x . kill ( ) <EOL> except Exception : <EOL> pass <EOL> def flags ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> for k , v in kw . iteritems ( ) : <EOL> if k not in self . flag_overrides : <EOL> self . flag_overrides [ k ] = getattr ( FLAGS , k ) <EOL> setattr ( FLAGS , k , v ) <EOL> def reset_flags ( self ) : <EOL> """<STR_LIT>""" <EOL> FLAGS . Reset ( ) <EOL> for k , v in self . _original_flags . iteritems ( ) : <EOL> setattr ( FLAGS , k , v ) <EOL> def start_service ( self , name , host = None , ** kwargs ) : <EOL> host = host and host or uuid . uuid4 ( ) . hex <EOL> kwargs . setdefault ( '<STR_LIT:host>' , host ) <EOL> kwargs . setdefault ( '<STR_LIT>' , '<STR_LIT>' % name ) <EOL> svc = service . Service . create ( ** kwargs ) <EOL> svc . start ( ) <EOL> self . _services . append ( svc ) <EOL> return svc <EOL> def assertDictMatch ( self , d1 , d2 , approx_equal = False , tolerance = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> def raise_assertion ( msg ) : <EOL> d1str = str ( d1 ) <EOL> d2str = str ( d2 ) <EOL> base_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' % locals ( ) ) <EOL> raise AssertionError ( base_msg ) <EOL> d1keys = set ( d1 . keys ( ) ) <EOL> d2keys = set ( d2 . keys ( ) ) <EOL> if d1keys != d2keys : <EOL> d1only = d1keys - d2keys <EOL> d2only = d2keys - d1keys <EOL> raise_assertion ( '<STR_LIT>' <EOL> '<STR_LIT>' % locals ( ) ) <EOL> for key in d1keys : <EOL> d1value = d1 [ key ] <EOL> d2value = d2 [ key ] <EOL> try : <EOL> error = abs ( float ( d1value ) - float ( d2value ) ) <EOL> within_tolerance = error <= tolerance <EOL> except ( ValueError , TypeError ) : <EOL> within_tolerance = False <EOL> if hasattr ( d1value , '<STR_LIT>' ) and hasattr ( d2value , '<STR_LIT>' ) : <EOL> self . assertDictMatch ( d1value , d2value ) <EOL> elif '<STR_LIT>' in ( d1value , d2value ) : <EOL> continue <EOL> elif approx_equal and within_tolerance : <EOL> continue <EOL> elif d1value != d2value : <EOL> raise_assertion ( "<STR_LIT>" <EOL> "<STR_LIT>" % locals ( ) ) <EOL> def assertDictListMatch ( self , L1 , L2 , approx_equal = False , tolerance = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> def raise_assertion ( msg ) : <EOL> L1str = str ( L1 ) <EOL> L2str = str ( L2 ) <EOL> base_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' % locals ( ) ) <EOL> raise AssertionError ( base_msg ) <EOL> L1count = len ( L1 ) <EOL> L2count = len ( L2 ) <EOL> if L1count != L2count : <EOL> raise_assertion ( '<STR_LIT>' <EOL> '<STR_LIT>' % locals ( ) ) <EOL> for d1 , d2 in zip ( L1 , L2 ) : <EOL> self . assertDictMatch ( d1 , d2 , approx_equal = approx_equal , <EOL> tolerance = tolerance ) <EOL> def assertSubDictMatch ( self , sub_dict , super_dict ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( set ( sub_dict . keys ( ) ) . issubset ( set ( super_dict . keys ( ) ) ) ) <EOL> for k , sub_value in sub_dict . items ( ) : <EOL> super_value = super_dict [ k ] <EOL> if isinstance ( sub_value , dict ) : <EOL> self . assertSubDictMatch ( sub_value , super_value ) <EOL> elif '<STR_LIT>' in ( sub_value , super_value ) : <EOL> continue <EOL> else : <EOL> self . assertEqual ( sub_value , super_value ) <EOL> def assertIn ( self , a , b , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> f = super ( TestCase , self ) . assertIn <EOL> except AttributeError : <EOL> self . assertTrue ( a in b , * args , ** kwargs ) <EOL> else : <EOL> f ( a , b , * args , ** kwargs ) <EOL> def assertNotIn ( self , a , b , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> f = super ( TestCase , self ) . assertNotIn <EOL> except AttributeError : <EOL> self . assertFalse ( a in b , * args , ** kwargs ) <EOL> else : <EOL> f ( a , b , * args , ** kwargs ) </s>
