Skip to content

Commit

Permalink
Merge "initialize objects with context in block device"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Feb 2, 2015
2 parents 638e920 + fac0e90 commit dc04361
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 26 deletions.
11 changes: 6 additions & 5 deletions nova/tests/unit/compute/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ def store_cinfo(context, *args, **kwargs):
self.stubs.Set(db, 'block_device_mapping_update', store_cinfo)

def test_attach_volume_serial(self):
fake_bdm = objects.BlockDeviceMapping(**self.fake_volume)
fake_bdm = objects.BlockDeviceMapping(context=self.context,
**self.fake_volume)
with (mock.patch.object(cinder.API, 'get_volume_encryption_metadata',
return_value={})):
instance = self._create_fake_instance_obj()
Expand Down Expand Up @@ -553,7 +554,7 @@ def test_boot_volume_serial(self):
})]
prepped_bdm = self.compute._prep_block_device(
self.context, self.instance_object, block_device_mapping)
mock_save.assert_called_once_with(self.context)
mock_save.assert_called_once_with()
volume_driver_bdm = prepped_bdm['block_device_mapping'][0]
self.assertEqual(volume_driver_bdm['connection_info']['serial'],
self.volume_id)
Expand Down Expand Up @@ -9094,7 +9095,7 @@ def fake_rpc_attach_volume(self, context, **kwargs):
def fake_rpc_reserve_block_device_name(self, context, instance, device,
volume_id, **kwargs):
called['fake_rpc_reserve_block_device_name'] = True
bdm = block_device_obj.BlockDeviceMapping()
bdm = block_device_obj.BlockDeviceMapping(context=context)
bdm['device_name'] = '/dev/vdb'
return bdm

Expand Down Expand Up @@ -9216,7 +9217,7 @@ def fake_roll_detaching(*args, **kwargs):
'get_by_volume_id')
objects.BlockDeviceMapping.get_by_volume_id(
self.context, 1).AndReturn(objects.BlockDeviceMapping(
**fake_bdm))
context=self.context, **fake_bdm))
self.mox.ReplayAll()

self.assertRaises(AttributeError, self.compute.detach_volume,
Expand All @@ -9233,7 +9234,7 @@ def test_detach_volume_not_found(self):
{'source_type': 'volume', 'destination_type': 'volume',
'volume_id': 'fake-id', 'device_name': '/dev/vdb',
'connection_info': '{"test": "test"}'})
bdm = objects.BlockDeviceMapping(**fake_bdm)
bdm = objects.BlockDeviceMapping(context=self.context, **fake_bdm)

with contextlib.nested(
mock.patch.object(self.compute.driver, 'detach_volume',
Expand Down
2 changes: 1 addition & 1 deletion nova/tests/unit/virt/libvirt/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7824,7 +7824,7 @@ def fake_obj_load_attr(self, attrname):
if not hasattr(self, attrname):
self[attrname] = {}

def fake_save(self, context):
def fake_save(self):
pass

drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
Expand Down
11 changes: 3 additions & 8 deletions nova/tests/unit/virt/test_block_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,11 @@ def _test_driver_device(self, name):

# Test the save method
with mock.patch.object(test_bdm._bdm_obj, 'save') as save_mock:
test_bdm.save(self.context)
test_bdm.save()
for fld, alias in test_bdm._update_on_save.iteritems():
self.assertEqual(test_bdm[alias or fld],
getattr(test_bdm._bdm_obj, fld))

save_mock.assert_called_once_with(self.context)

# Test the save method with no context passed
with mock.patch.object(test_bdm._bdm_obj, 'save') as save_mock:
test_bdm.save()
save_mock.assert_called_once_with()

def _test_driver_default_size(self, name):
Expand Down Expand Up @@ -383,7 +378,7 @@ def _test_volume_attach(self, driver_bdm, bdm_dict,
self.volume_api.attach(elevated_context, fake_volume['id'],
'fake_uuid', bdm_dict['device_name'],
mode=access_mode).AndReturn(None)
driver_bdm._bdm_obj.save(self.context).AndReturn(None)
driver_bdm._bdm_obj.save().AndReturn(None)
return instance, expected_conn_info

def test_volume_attach(self):
Expand Down Expand Up @@ -494,7 +489,7 @@ def test_refresh_connection(self):
self.volume_api.initialize_connection(
self.context, test_bdm.volume_id,
connector).AndReturn(connection_info)
test_bdm._bdm_obj.save(self.context).AndReturn(None)
test_bdm._bdm_obj.save().AndReturn(None)

self.mox.ReplayAll()

Expand Down
14 changes: 5 additions & 9 deletions nova/virt/block_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def update_db(method):
@functools.wraps(method)
def wrapped(obj, context, *args, **kwargs):
ret_val = method(obj, context, *args, **kwargs)
obj.save(context)
obj.save()
return ret_val
return wrapped

Expand Down Expand Up @@ -131,14 +131,10 @@ def attach(self, **kwargs):
"""
raise NotImplementedError()

def save(self, context=None):
def save(self):
for attr_name, key_name in self._update_on_save.iteritems():
setattr(self._bdm_obj, attr_name, self[key_name or attr_name])

if context:
self._bdm_obj.save(context)
else:
self._bdm_obj.save()
self._bdm_obj.save()


class DriverSwapBlockDevice(DriverBlockDevice):
Expand Down Expand Up @@ -285,15 +281,15 @@ def refresh_connection_info(self, context, instance,
self._preserve_multipath_id(connection_info)
self['connection_info'] = connection_info

def save(self, context=None):
def save(self):
# NOTE(ndipanov): we might want to generalize this by adding it to the
# _update_on_save and adding a transformation function.
try:
self._bdm_obj.connection_info = jsonutils.dumps(
self.get('connection_info'))
except TypeError:
pass
super(DriverVolumeBlockDevice, self).save(context)
super(DriverVolumeBlockDevice, self).save()


class DriverSnapshotBlockDevice(DriverVolumeBlockDevice):
Expand Down
2 changes: 1 addition & 1 deletion nova/virt/libvirt/blockinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def default_device_names(virt_type, context, instance, root_device_name,
[block_device_info['swap']] if
block_device_info['swap'] else [],
block_device_info['block_device_mapping']):
driver_bdm.save(context)
driver_bdm.save()


def has_default_ephemeral(instance, disk_bus, block_device_info, mapping):
Expand Down
4 changes: 2 additions & 2 deletions nova/virt/libvirt/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3182,7 +3182,7 @@ def _get_guest_storage_config(self, instance, image_meta,
cfg = self._get_volume_config(connection_info, info)
devices.append(cfg)
vol['connection_info'] = connection_info
vol.save(nova_context.get_admin_context())
vol.save()

for d in devices:
self._set_cache_mode(d)
Expand Down Expand Up @@ -6145,7 +6145,7 @@ def _delete_instance_files(self, instance):
inst_obj.system_metadata['clean_attempts'] = str(attempts + 1)
if success:
inst_obj.cleaned = True
inst_obj.save(context)
inst_obj.save()

def delete_instance_files(self, instance):
target = libvirt_utils.get_instance_path(instance)
Expand Down

0 comments on commit dc04361

Please sign in to comment.