Context cleanup

Before the fix of bug #1386932 (285cfaf095)
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
This commit is contained in:
Zhiteng Huang
2014-11-22 02:53:40 +08:00
parent ca37fbaea9
commit f8dc04fe3f
3 changed files with 6 additions and 30 deletions

View File

@@ -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):

View File

@@ -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

View File

@@ -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: