<s> from oslo_utils import uuidutils <EOL> import webob <EOL> from nova . api . openstack . compute import admin_actions as admin_actions_v21 <EOL> from nova . api . openstack . compute . legacy_v2 . contrib import admin_actions as admin_actions_v2 <EOL> from nova . compute import vm_states <EOL> from nova import exception <EOL> from nova import objects <EOL> from nova import test <EOL> from nova . tests . unit . api . openstack import fakes <EOL> class ResetStateTestsV21 ( test . NoDBTestCase ) : <EOL> admin_act = admin_actions_v21 <EOL> bad_request = exception . ValidationError <EOL> def setUp ( self ) : <EOL> super ( ResetStateTestsV21 , self ) . setUp ( ) <EOL> self . uuid = uuidutils . generate_uuid ( ) <EOL> self . admin_api = self . admin_act . AdminActionsController ( ) <EOL> self . compute_api = self . admin_api . compute_api <EOL> self . request = self . _get_request ( ) <EOL> self . context = self . request . environ [ '<STR_LIT>' ] <EOL> def _get_request ( self ) : <EOL> return fakes . HTTPRequest . blank ( '<STR_LIT>' ) <EOL> def test_no_state ( self ) : <EOL> self . assertRaises ( self . bad_request , <EOL> self . admin_api . _reset_state , <EOL> self . request , self . uuid , <EOL> body = { "<STR_LIT>" : None } ) <EOL> def test_bad_state ( self ) : <EOL> self . assertRaises ( self . bad_request , <EOL> self . admin_api . _reset_state , <EOL> self . request , self . uuid , <EOL> body = { "<STR_LIT>" : { "<STR_LIT:state>" : "<STR_LIT>" } } ) <EOL> def test_no_instance ( self ) : <EOL> self . mox . StubOutWithMock ( self . compute_api , '<STR_LIT>' ) <EOL> exc = exception . InstanceNotFound ( instance_id = '<STR_LIT>' ) <EOL> self . compute_api . get ( self . context , self . uuid , expected_attrs = None , <EOL> want_objects = True ) . AndRaise ( exc ) <EOL> self . mox . ReplayAll ( ) <EOL> self . assertRaises ( webob . exc . HTTPNotFound , <EOL> self . admin_api . _reset_state , <EOL> self . request , self . uuid , <EOL> body = { "<STR_LIT>" : { "<STR_LIT:state>" : "<STR_LIT>" } } ) <EOL> def _setup_mock ( self , expected ) : <EOL> instance = objects . Instance ( ) <EOL> instance . uuid = self . uuid <EOL> instance . vm_state = '<STR_LIT>' <EOL> instance . task_state = '<STR_LIT>' <EOL> instance . obj_reset_changes ( ) <EOL> self . mox . StubOutWithMock ( instance , '<STR_LIT>' ) <EOL> self . mox . StubOutWithMock ( self . compute_api , '<STR_LIT>' ) <EOL> def check_state ( admin_state_reset = True ) : <EOL> self . assertEqual ( set ( expected . keys ( ) ) , <EOL> instance . obj_what_changed ( ) ) <EOL> for k , v in expected . items ( ) : <EOL> self . assertEqual ( v , getattr ( instance , k ) , <EOL> "<STR_LIT>" % k ) <EOL> instance . obj_reset_changes ( ) <EOL> self . compute_api . get ( self . context , instance . uuid , expected_attrs = None , <EOL> want_objects = True ) . AndReturn ( instance ) <EOL> instance . save ( admin_state_reset = True ) . WithSideEffects ( check_state ) <EOL> def test_reset_active ( self ) : <EOL> self . _setup_mock ( dict ( vm_state = vm_states . ACTIVE , <EOL> task_state = None ) ) <EOL> self . mox . ReplayAll ( ) <EOL> body = { "<STR_LIT>" : { "<STR_LIT:state>" : "<STR_LIT>" } } <EOL> result = self . admin_api . _reset_state ( self . request , self . uuid , <EOL> body = body ) <EOL> if isinstance ( self . admin_api , <EOL> admin_actions_v21 . AdminActionsController ) : <EOL> status_int = self . admin_api . _reset_state . wsgi_code <EOL> else : <EOL> status_int = result . status_int <EOL> self . assertEqual ( <NUM_LIT> , status_int ) <EOL> def test_reset_error ( self ) : <EOL> self . _setup_mock ( dict ( vm_state = vm_states . ERROR , <EOL> task_state = None ) ) <EOL> self . mox . ReplayAll ( ) <EOL> body = { "<STR_LIT>" : { "<STR_LIT:state>" : "<STR_LIT:error>" } } <EOL> result = self . admin_api . _reset_state ( self . request , self . uuid , <EOL> body = body ) <EOL> if isinstance ( self . admin_api , <EOL> admin_actions_v21 . AdminActionsController ) : <EOL> status_int = self . admin_api . _reset_state . wsgi_code <EOL> else : <EOL> status_int = result . status_int <EOL> self . assertEqual ( <NUM_LIT> , status_int ) <EOL> class ResetStateTestsV2 ( ResetStateTestsV21 ) : <EOL> admin_act = admin_actions_v2 <EOL> bad_request = webob . exc . HTTPBadRequest </s>
