<s> import mock <EOL> import webob <EOL> from nova . api . openstack . compute import deferred_delete as dd_v21 <EOL> from nova . api . openstack . compute . legacy_v2 . contrib import deferred_delete <EOL> from nova . compute import api as compute_api <EOL> from nova import context <EOL> from nova import exception <EOL> from nova import test <EOL> from nova . tests . unit . api . openstack import fakes <EOL> class FakeRequest ( object ) : <EOL> def __init__ ( self , context ) : <EOL> self . environ = { '<STR_LIT>' : context } <EOL> class DeferredDeleteExtensionTestV21 ( test . NoDBTestCase ) : <EOL> ext_ver = dd_v21 . DeferredDeleteController <EOL> def setUp ( self ) : <EOL> super ( DeferredDeleteExtensionTestV21 , self ) . setUp ( ) <EOL> self . fake_input_dict = { } <EOL> self . fake_uuid = '<STR_LIT>' <EOL> self . fake_context = context . RequestContext ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . fake_req = FakeRequest ( self . fake_context ) <EOL> self . extension = self . ext_ver ( ) <EOL> def test_force_delete ( self ) : <EOL> self . mox . StubOutWithMock ( compute_api . API , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( compute_api . API , '<STR_LIT>' ) <EOL> fake_instance = '<STR_LIT>' <EOL> compute_api . API . get ( self . fake_context , self . fake_uuid , <EOL> expected_attrs = None , <EOL> want_objects = True ) . AndReturn ( fake_instance ) <EOL> compute_api . API . force_delete ( self . fake_context , fake_instance ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . extension . _force_delete ( self . fake_req , self . fake_uuid , <EOL> self . fake_input_dict ) <EOL> if isinstance ( self . extension , dd_v21 . DeferredDeleteController ) : <EOL> status_int = self . extension . _force_delete . wsgi_code <EOL> else : <EOL> status_int = res . status_int <EOL> self . assertEqual ( <NUM_LIT> , status_int ) <EOL> def test_force_delete_instance_not_found ( self ) : <EOL> self . mox . StubOutWithMock ( compute_api . API , '<STR_LIT>' ) <EOL> compute_api . API . get ( self . fake_context , self . fake_uuid , <EOL> expected_attrs = None , <EOL> want_objects = True ) . AndRaise ( <EOL> exception . InstanceNotFound ( instance_id = '<STR_LIT>' ) ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . extension . _force_delete , <EOL> self . fake_req , <EOL> self . fake_uuid , <EOL> self . fake_input_dict ) <EOL> @ mock . patch . object ( compute_api . API , '<STR_LIT>' ) <EOL> @ mock . patch . object ( compute_api . API , '<STR_LIT>' , <EOL> side_effect = exception . InstanceIsLocked ( <EOL> instance_uuid = '<STR_LIT>' ) ) <EOL> def test_force_delete_instance_locked ( self , mock_force_delete , mock_get ) : <EOL> req = fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> ex = self . assertRaises ( webob . exc . HTTPConflict , <EOL> self . extension . _force_delete , <EOL> req , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , ex . explanation ) <EOL> def test_restore ( self ) : <EOL> self . mox . StubOutWithMock ( compute_api . API , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( compute_api . API , '<STR_LIT>' ) <EOL> fake_instance = '<STR_LIT>' <EOL> compute_api . API . get ( self . fake_context , self . fake_uuid , <EOL> expected_attrs = None , <EOL> want_objects = True ) . AndReturn ( fake_instance ) <EOL> compute_api . API . restore ( self . fake_context , fake_instance ) <EOL> self . mox . ReplayAll ( ) <EOL> res = self . extension . _restore ( self . fake_req , self . fake_uuid , <EOL> self . fake_input_dict ) <EOL> if isinstance ( self . extension , dd_v21 . DeferredDeleteController ) : <EOL> status_int = self . extension . _restore . wsgi_code <EOL> else : <EOL> status_int = res . status_int <EOL> self . assertEqual ( <NUM_LIT> , status_int ) <EOL> def test_restore_instance_not_found ( self ) : <EOL> self . mox . StubOutWithMock ( compute_api . API , '<STR_LIT>' ) <EOL> compute_api . API . get ( self . fake_context , self . fake_uuid , <EOL> expected_attrs = None , want_objects = True ) . AndRaise ( <EOL> exception . InstanceNotFound ( instance_id = '<STR_LIT>' ) ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertRaises ( webob . exc . HTTPNotFound , self . extension . _restore , <EOL> self . fake_req , self . fake_uuid , <EOL> self . fake_input_dict ) <EOL> def test_restore_raises_conflict_on_invalid_state ( self ) : <EOL> self . mox . StubOutWithMock ( compute_api . API , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( compute_api . API , '<STR_LIT>' ) <EOL> fake_instance = '<STR_LIT>' <EOL> exc = exception . InstanceInvalidState ( attr = '<STR_LIT>' , <EOL> state = '<STR_LIT>' , method = '<STR_LIT>' , <EOL> instance_uuid = '<STR_LIT>' ) <EOL> compute_api . API . get ( self . fake_context , self . fake_uuid , <EOL> expected_attrs = None , <EOL> want_objects = True ) . AndReturn ( fake_instance ) <EOL> compute_api . API . restore ( self . fake_context , fake_instance ) . AndRaise ( <EOL> exc ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertRaises ( webob . exc . HTTPConflict , self . extension . _restore , <EOL> self . fake_req , self . fake_uuid , self . fake_input_dict ) <EOL> class DeferredDeleteExtensionTestV2 ( DeferredDeleteExtensionTestV21 ) : <EOL> ext_ver = deferred_delete . DeferredDeleteController <EOL> class DeferredDeletePolicyEnforcementV21 ( test . NoDBTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( DeferredDeletePolicyEnforcementV21 , self ) . setUp ( ) <EOL> self . controller = dd_v21 . DeferredDeleteController ( ) <EOL> self . req = fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> def test_restore_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . _restore , self . req , fakes . FAKE_UUID , <EOL> body = { '<STR_LIT>' : { } } ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) <EOL> def test_force_delete_policy_failed ( self ) : <EOL> rule_name = "<STR_LIT>" <EOL> self . policy . set_rules ( { rule_name : "<STR_LIT>" } ) <EOL> exc = self . assertRaises ( <EOL> exception . PolicyNotAuthorized , <EOL> self . controller . _force_delete , self . req , fakes . FAKE_UUID , <EOL> body = { '<STR_LIT>' : { } } ) <EOL> self . assertEqual ( <EOL> "<STR_LIT>" % rule_name , <EOL> exc . format_message ( ) ) </s>
