From f8dc04fe3fdd8a9b168581ebd2db0e4bcd80fe20 Mon Sep 17 00:00:00 2001 From: Zhiteng Huang Date: Sat, 22 Nov 2014 02:53:40 +0800 Subject: [PATCH] Context cleanup Before the fix of bug #1386932 (285cfaf0954d4c3e320b205c288240c1828476fe) was committed, there are a few hacks in Cinder where an original copy of (un-elevated) context has to be saved before doing context.elevated(). Now that we have the fix in place, it's time to clean up those old hacks. Change-Id: Ie3e5cb7398647b4619d294c572e920e6c3b6b9c9 Related-bug: #1386392 --- cinder/tests/test_volume.py | 26 ++------------------------ cinder/volume/api.py | 3 +-- cinder/volume/manager.py | 7 +++---- 3 files changed, 6 insertions(+), 30 deletions(-) diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index 11e9c25e7..a858668c2 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -1058,7 +1058,6 @@ class VolumeTestCase(BaseVolumeTestCase): orig_elevated = self.context.elevated - ctxt_deepcopy = self.context.deepcopy() gthreads = [] def mock_elevated(*args, **kwargs): @@ -1067,7 +1066,7 @@ class VolumeTestCase(BaseVolumeTestCase): # we expect this to block and then fail t = eventlet.spawn(self.volume.create_volume, - ctxt_deepcopy, + self.context, volume_id=dst_vol_id, source_volid=src_vol_id) gthreads.append(t) @@ -1107,7 +1106,6 @@ class VolumeTestCase(BaseVolumeTestCase): orig_elevated = self.context.elevated - ctxt_deepcopy = self.context.deepcopy() gthreads = [] def mock_elevated(*args, **kwargs): @@ -1115,7 +1113,7 @@ class VolumeTestCase(BaseVolumeTestCase): self.stubs.Set(self.context, 'elevated', orig_elevated) # We expect this to block and then fail - t = eventlet.spawn(self.volume.create_volume, ctxt_deepcopy, + t = eventlet.spawn(self.volume.create_volume, self.context, volume_id=dst_vol_id, snapshot_id=snap_id) gthreads.append(t) @@ -2586,26 +2584,6 @@ class VolumeTestCase(BaseVolumeTestCase): self.assertEqual(volumes_reserved, 100) - def test_create_volume_from_unelevated_context(self): - """Test context does't change after volume creation failure.""" - def fake_create_volume(*args, **kwargs): - raise exception.CinderException('fake exception') - - # create context for testing - ctxt = self.context.deepcopy() - if 'admin' in ctxt.roles: - ctxt.roles.remove('admin') - ctxt.is_admin = False - # create one copy of context for future comparison - self.saved_ctxt = ctxt.deepcopy() - - self.stubs.Set(self.volume.driver, 'create_volume', fake_create_volume) - - volume_src = tests_utils.create_volume(self.context, - **self.volume_params) - self.assertRaises(exception.CinderException, - self.volume.create_volume, ctxt, volume_src['id']) - @mock.patch( 'cinder.volume.driver.VolumeDriver.create_replica_test_volume') def test_create_volume_from_sourcereplica(self, _create_replica_test): diff --git a/cinder/volume/api.py b/cinder/volume/api.py index 0abf67eea..1f2ab593c 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -320,7 +320,6 @@ class API(base.Base): self.db.volume_update(context, volume['id'], fields) def get(self, context, volume_id, viewable_admin_meta=False): - old_ctxt = context.deepcopy() if viewable_admin_meta: ctxt = context.elevated() else: @@ -328,7 +327,7 @@ class API(base.Base): rv = self.db.volume_get(ctxt, volume_id) volume = dict(rv.iteritems()) try: - check_policy(old_ctxt, 'get', volume) + check_policy(context, 'get', volume) except exception.PolicyNotAuthorized: # raise VolumeNotFound instead to make sure Cinder behaves # as it used to diff --git a/cinder/volume/manager.py b/cinder/volume/manager.py index b5a68b5b7..a564ca1cd 100644 --- a/cinder/volume/manager.py +++ b/cinder/volume/manager.py @@ -347,8 +347,7 @@ class VolumeManager(manager.SchedulerDependentManager): source_replicaid=None, consistencygroup_id=None): """Creates the volume.""" - context_saved = context.deepcopy() - context = context.elevated() + context_elevated = context.elevated() if filter_properties is None: filter_properties = {} @@ -356,7 +355,7 @@ class VolumeManager(manager.SchedulerDependentManager): # NOTE(flaper87): Driver initialization is # verified by the task itself. flow_engine = create_volume.get_flow( - context, + context_elevated, self.db, self.driver, self.scheduler_rpcapi, @@ -368,7 +367,7 @@ class VolumeManager(manager.SchedulerDependentManager): source_replicaid=source_replicaid, consistencygroup_id=consistencygroup_id, allow_reschedule=allow_reschedule, - reschedule_context=context_saved, + reschedule_context=context, request_spec=request_spec, filter_properties=filter_properties) except Exception: -- 2.45.2