#NEWLINE# def _shelve_instance( self , shelved_offload_time , clean_shutdown = True ) : #NEWLINE# #INDENT# CONF . set_override ( 'shelved_offload_time' , shelved_offload_time ) #NEWLINE# host = 'fake-mini' #NEWLINE# instance = self . _create_fake_instance_obj ( params = { #NEWLINE# #INDENT# 'host' : host , #NEWLINE# #UNINDENT# } ) #NEWLINE# image_id = 'fake_image_id' #NEWLINE# host = 'fake-mini' #NEWLINE# self . useFixture ( utils_fixture . TimeFixture ( ) ) #NEWLINE# instance . task_state = task_states . SHELVING #NEWLINE# instance . save ( ) #NEWLINE# self . mox . StubOutWithMock ( self . compute , '_notify_about_instance_usage' ) #NEWLINE# self . mox . StubOutWithMock ( self . compute . driver , 'snapshot' ) #NEWLINE# self . mox . StubOutWithMock ( self . compute . driver , 'power_off' ) #NEWLINE# self . mox . StubOutWithMock ( self . compute , '_get_power_state' ) #NEWLINE# self . mox . StubOutWithMock ( self . compute . network_api , 'cleanup_instance_network_on_host' ) #NEWLINE# self . compute . _notify_about_instance_usage ( self . context , instance , 'shelve.start' ) #NEWLINE# if clean_shutdown : #NEWLINE# #INDENT# self . compute . driver . power_off ( instance , CONF . shutdown_timeout , self . compute . SHUTDOWN_RETRY_INTERVAL ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . compute . driver . power_off ( instance , 0 , 0 ) #NEWLINE# #UNINDENT# self . compute . _get_power_state ( self . context , instance ) . AndReturn ( 123 ) #NEWLINE# if ( CONF . shelved_offload_time == 0 ) : #NEWLINE# #INDENT# self . compute . network_api . cleanup_instance_network_on_host ( self . context , instance , instance . host ) #NEWLINE# #UNINDENT# self . compute . driver . snapshot ( self . context , instance , 'fake_image_id' , mox . IgnoreArg ( ) ) #NEWLINE# tracking = { #NEWLINE# #INDENT# 'last_state' : instance . vm_state , #NEWLINE# #UNINDENT# } #NEWLINE# def check_save( expected_task_state = None ) : #NEWLINE# #INDENT# self . assertEqual ( 123 , instance . power_state ) #NEWLINE# if ( tracking [ 'last_state' ] == vm_states . ACTIVE ) : #NEWLINE# #INDENT# if ( CONF . shelved_offload_time == 0 ) : #NEWLINE# #INDENT# self . assertEqual ( task_states . SHELVING_OFFLOADING , instance . task_state ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . assertIsNone ( instance . task_state ) #NEWLINE# #UNINDENT# self . assertEqual ( vm_states . SHELVED , instance . vm_state ) #NEWLINE# self . assertEqual ( [ task_states . SHELVING , task_states . SHELVING_IMAGE_UPLOADING ] , expected_task_state ) #NEWLINE# self . assertIn ( 'shelved_at' , instance . system_metadata ) #NEWLINE# self . assertEqual ( image_id , instance . system_metadata [ 'shelved_image_id' ] ) #NEWLINE# self . assertEqual ( host , instance . system_metadata [ 'shelved_host' ] ) #NEWLINE# tracking [ 'last_state' ] = instance . vm_state #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( tracking [ 'last_state' ] == vm_states . SHELVED ) and ( CONF . shelved_offload_time == 0 ) ) : #NEWLINE# #INDENT# self . assertIsNone ( instance . host ) #NEWLINE# self . assertIsNone ( instance . node ) #NEWLINE# self . assertIsNone ( instance . task_state ) #NEWLINE# self . assertEqual ( vm_states . SHELVED_OFFLOADED , instance . vm_state ) #NEWLINE# self . assertEqual ( [ task_states . SHELVING , task_states . SHELVING_OFFLOADING ] , expected_task_state ) #NEWLINE# tracking [ 'last_state' ] = instance . vm_state #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . fail ( 'Unexpected save!' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# self . compute . _notify_about_instance_usage ( self . context , instance , 'shelve.end' ) #NEWLINE# if ( CONF . shelved_offload_time == 0 ) : #NEWLINE# #INDENT# self . compute . _notify_about_instance_usage ( self . context , instance , 'shelve_offload.start' ) #NEWLINE# self . compute . driver . power_off ( instance , 0 , 0 ) #NEWLINE# self . compute . _get_power_state ( self . context , instance ) . AndReturn ( 123 ) #NEWLINE# self . compute . _notify_about_instance_usage ( self . context , instance , 'shelve_offload.end' ) #NEWLINE# #UNINDENT# self . mox . ReplayAll ( ) #NEWLINE# with mock . patch . object ( instance , 'save' ) as mock_save : #NEWLINE# #INDENT# mock_save . side_effect = check_save #NEWLINE# self . compute . shelve_instance ( self . context , instance , image_id = image_id , clean_shutdown = clean_shutdown )