Convert nova.volume.api.API to use volume objects

Change-Id: If6b78f7de814116bc93b273ec300dba02e63593d
This commit is contained in:
Brian Waldon
2012-01-13 11:22:50 -08:00
parent 4f8dcf6345
commit 70f300ef40
6 changed files with 58 additions and 84 deletions

View File

@@ -2828,42 +2828,6 @@ class ComputeAPITestCase(BaseTestCase):
None, None,
'/dev/invalid') '/dev/invalid')
def test_attach_volume(self):
instance_id = 1
instance_uuid = utils.gen_uuid()
volume_id = 1
for device in ('/dev/sda', '/dev/xvda'):
# creating mocks
self.mox.StubOutWithMock(self.compute_api.volume_api,
'check_attach')
self.mox.StubOutWithMock(self.compute_api, 'get')
self.mox.StubOutWithMock(rpc, 'cast')
rpc.cast(
mox.IgnoreArg(),
mox.IgnoreArg(), {"method": "attach_volume",
"args": {'volume_id': volume_id,
'instance_uuid': instance_uuid,
'mountpoint': device}})
self.compute_api.volume_api.check_attach(
mox.IgnoreArg(),
volume_id=volume_id).AndReturn(
{'id': volume_id, 'status': 'available',
'attach_status': 'detached'})
self.compute_api.get(
mox.IgnoreArg(),
mox.IgnoreArg()).AndReturn({
'id': instance_id,
'uuid': instance_uuid,
'host': 'fake'})
self.mox.ReplayAll()
self.compute_api.attach_volume(None, None, volume_id, device)
self.mox.UnsetStubs()
def test_vnc_console(self): def test_vnc_console(self):
"""Make sure we can a vnc console for an instance.""" """Make sure we can a vnc console for an instance."""
def vnc_rpc_call_wrapper(*args, **kwargs): def vnc_rpc_call_wrapper(*args, **kwargs):
@@ -2896,6 +2860,10 @@ class ComputeAPITestCase(BaseTestCase):
def fake_check_attach(*args, **kwargs): def fake_check_attach(*args, **kwargs):
pass pass
def fake_volume_get(self, context, volume_id):
return {'id': volume_id}
self.stubs.Set(nova.volume.api.API, 'get', fake_volume_get)
self.stubs.Set(nova.volume.api.API, 'check_attach', fake_check_attach) self.stubs.Set(nova.volume.api.API, 'check_attach', fake_check_attach)
instance = self._create_fake_instance() instance = self._create_fake_instance()

View File

@@ -30,7 +30,7 @@ from nova import log as logging
from nova import rpc from nova import rpc
from nova import test from nova import test
from nova import utils from nova import utils
from nova import volume import nova.volume.api
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.volume') LOG = logging.getLogger('nova.tests.volume')
@@ -62,11 +62,12 @@ class VolumeTestCase(test.TestCase):
vol['availability_zone'] = FLAGS.storage_availability_zone vol['availability_zone'] = FLAGS.storage_availability_zone
vol['status'] = "creating" vol['status'] = "creating"
vol['attach_status'] = "detached" vol['attach_status'] = "detached"
return db.volume_create(context.get_admin_context(), vol)['id'] return db.volume_create(context.get_admin_context(), vol)
def test_create_delete_volume(self): def test_create_delete_volume(self):
"""Test volume can be created and deleted.""" """Test volume can be created and deleted."""
volume_id = self._create_volume() volume = self._create_volume()
volume_id = volume['id']
self.volume.create_volume(self.context, volume_id) self.volume.create_volume(self.context, volume_id)
self.assertEqual(volume_id, db.volume_get(context.get_admin_context(), self.assertEqual(volume_id, db.volume_get(context.get_admin_context(),
volume_id).id) volume_id).id)
@@ -79,22 +80,24 @@ class VolumeTestCase(test.TestCase):
def test_create_volume_from_snapshot(self): def test_create_volume_from_snapshot(self):
"""Test volume can be created from a snapshot.""" """Test volume can be created from a snapshot."""
volume_src_id = self._create_volume() volume_src = self._create_volume()
self.volume.create_volume(self.context, volume_src_id) self.volume.create_volume(self.context, volume_src['id'])
snapshot_id = self._create_snapshot(volume_src_id) snapshot_id = self._create_snapshot(volume_src['id'])
self.volume.create_snapshot(self.context, volume_src_id, snapshot_id) self.volume.create_snapshot(self.context, volume_src['id'],
volume_dst_id = self._create_volume(0, snapshot_id) snapshot_id)
self.volume.create_volume(self.context, volume_dst_id, snapshot_id) volume_dst = self._create_volume(0, snapshot_id)
self.assertEqual(volume_dst_id, db.volume_get( self.volume.create_volume(self.context, volume_dst['id'], snapshot_id)
context.get_admin_context(), self.assertEqual(volume_dst['id'],
volume_dst_id).id) db.volume_get(
context.get_admin_context(),
volume_dst['id']).id)
self.assertEqual(snapshot_id, db.volume_get( self.assertEqual(snapshot_id, db.volume_get(
context.get_admin_context(), context.get_admin_context(),
volume_dst_id).snapshot_id) volume_dst['id']).snapshot_id)
self.volume.delete_volume(self.context, volume_dst_id) self.volume.delete_volume(self.context, volume_dst['id'])
self.volume.delete_snapshot(self.context, snapshot_id) self.volume.delete_snapshot(self.context, snapshot_id)
self.volume.delete_volume(self.context, volume_src_id) self.volume.delete_volume(self.context, volume_src['id'])
def test_too_big_volume(self): def test_too_big_volume(self):
"""Ensure failure if a too large of a volume is requested.""" """Ensure failure if a too large of a volume is requested."""
@@ -102,8 +105,8 @@ class VolumeTestCase(test.TestCase):
# volume_create # volume_create
return True return True
try: try:
volume_id = self._create_volume('1001') volume = self._create_volume('1001')
self.volume.create_volume(self.context, volume_id) self.volume.create_volume(self.context, volume)
self.fail("Should have thrown TypeError") self.fail("Should have thrown TypeError")
except TypeError: except TypeError:
pass pass
@@ -113,15 +116,15 @@ class VolumeTestCase(test.TestCase):
vols = [] vols = []
total_slots = FLAGS.iscsi_num_targets total_slots = FLAGS.iscsi_num_targets
for _index in xrange(total_slots): for _index in xrange(total_slots):
volume_id = self._create_volume() volume = self._create_volume()
self.volume.create_volume(self.context, volume_id) self.volume.create_volume(self.context, volume['id'])
vols.append(volume_id) vols.append(volume['id'])
volume_id = self._create_volume() volume = self._create_volume()
self.assertRaises(db.NoMoreTargets, self.assertRaises(db.NoMoreTargets,
self.volume.create_volume, self.volume.create_volume,
self.context, self.context,
volume_id) volume['id'])
db.volume_destroy(context.get_admin_context(), volume_id) db.volume_destroy(context.get_admin_context(), volume['id'])
for volume_id in vols: for volume_id in vols:
self.volume.delete_volume(self.context, volume_id) self.volume.delete_volume(self.context, volume_id)
@@ -137,7 +140,8 @@ class VolumeTestCase(test.TestCase):
inst['ami_launch_index'] = 0 inst['ami_launch_index'] = 0
instance_id = db.instance_create(self.context, inst)['id'] instance_id = db.instance_create(self.context, inst)['id']
mountpoint = "/dev/sdf" mountpoint = "/dev/sdf"
volume_id = self._create_volume() volume = self._create_volume()
volume_id = volume['id']
self.volume.create_volume(self.context, volume_id) self.volume.create_volume(self.context, volume_id)
if FLAGS.fake_tests: if FLAGS.fake_tests:
db.volume_attached(self.context, volume_id, instance_id, db.volume_attached(self.context, volume_id, instance_id,
@@ -190,8 +194,8 @@ class VolumeTestCase(test.TestCase):
LOG.debug(_("Target %s allocated"), iscsi_target) LOG.debug(_("Target %s allocated"), iscsi_target)
total_slots = FLAGS.iscsi_num_targets total_slots = FLAGS.iscsi_num_targets
for _index in xrange(total_slots): for _index in xrange(total_slots):
volume_id = self._create_volume() volume = self._create_volume()
d = self.volume.create_volume(self.context, volume_id) d = self.volume.create_volume(self.context, volume['id'])
_check(d) _check(d)
for volume_id in volume_ids: for volume_id in volume_ids:
self.volume.delete_volume(self.context, volume_id) self.volume.delete_volume(self.context, volume_id)
@@ -215,10 +219,10 @@ class VolumeTestCase(test.TestCase):
def test_create_delete_snapshot(self): def test_create_delete_snapshot(self):
"""Test snapshot can be created and deleted.""" """Test snapshot can be created and deleted."""
volume_id = self._create_volume() volume = self._create_volume()
self.volume.create_volume(self.context, volume_id) self.volume.create_volume(self.context, volume['id'])
snapshot_id = self._create_snapshot(volume_id) snapshot_id = self._create_snapshot(volume['id'])
self.volume.create_snapshot(self.context, volume_id, snapshot_id) self.volume.create_snapshot(self.context, volume['id'], snapshot_id)
self.assertEqual(snapshot_id, self.assertEqual(snapshot_id,
db.snapshot_get(context.get_admin_context(), db.snapshot_get(context.get_admin_context(),
snapshot_id).id) snapshot_id).id)
@@ -228,7 +232,7 @@ class VolumeTestCase(test.TestCase):
db.snapshot_get, db.snapshot_get,
self.context, self.context,
snapshot_id) snapshot_id)
self.volume.delete_volume(self.context, volume_id) self.volume.delete_volume(self.context, volume['id'])
def test_create_snapshot_force(self): def test_create_snapshot_force(self):
"""Test snapshot in use can be created forcibly.""" """Test snapshot in use can be created forcibly."""
@@ -237,22 +241,23 @@ class VolumeTestCase(test.TestCase):
pass pass
self.stubs.Set(rpc, 'cast', fake_cast) self.stubs.Set(rpc, 'cast', fake_cast)
volume_id = self._create_volume() volume = self._create_volume()
self.volume.create_volume(self.context, volume_id) self.volume.create_volume(self.context, volume['id'])
db.volume_attached(self.context, volume_id, self.instance_id, db.volume_attached(self.context, volume['id'], self.instance_id,
'/dev/sda1') '/dev/sda1')
volume_api = volume.api.API() volume_api = nova.volume.api.API()
volume = volume_api.get(self.context, volume['id'])
self.assertRaises(exception.ApiError, self.assertRaises(exception.ApiError,
volume_api.create_snapshot, volume_api.create_snapshot,
self.context, volume_id, self.context, volume,
'fake_name', 'fake_description') 'fake_name', 'fake_description')
snapshot_ref = volume_api.create_snapshot_force(self.context, snapshot_ref = volume_api.create_snapshot_force(self.context,
volume_id, volume,
'fake_name', 'fake_name',
'fake_description') 'fake_description')
db.snapshot_destroy(self.context, snapshot_ref['id']) db.snapshot_destroy(self.context, snapshot_ref['id'])
db.volume_destroy(self.context, volume_id) db.volume_destroy(self.context, volume['id'])
class DriverTestCase(test.TestCase): class DriverTestCase(test.TestCase):

View File

@@ -24,7 +24,6 @@ from nova import flags
from nova import log as logging from nova import log as logging
from nova import test from nova import test
from nova import vsa from nova import vsa
from nova import volume
from nova.volume import volume_types from nova.volume import volume_types
from nova.vsa import utils as vsa_utils from nova.vsa import utils as vsa_utils
@@ -40,7 +39,6 @@ class VsaTestCase(test.TestCase):
super(VsaTestCase, self).setUp() super(VsaTestCase, self).setUp()
self.stubs = stubout.StubOutForTesting() self.stubs = stubout.StubOutForTesting()
self.vsa_api = vsa.API() self.vsa_api = vsa.API()
self.volume_api = volume.API()
FLAGS.quota_volumes = 100 FLAGS.quota_volumes = 100
FLAGS.quota_gigabytes = 10000 FLAGS.quota_gigabytes = 10000

View File

@@ -56,7 +56,7 @@ class VsaVolumesTestCase(test.TestCase):
def _default_volume_param(self): def _default_volume_param(self):
return { return {
'size': 1, 'size': 1,
'snapshot_id': None, 'snapshot': None,
'name': 'Test volume name', 'name': 'Test volume name',
'description': 'Test volume desc name', 'description': 'Test volume desc name',
'volume_type': self.default_vol_type, 'volume_type': self.default_vol_type,
@@ -95,8 +95,10 @@ class VsaVolumesTestCase(test.TestCase):
'creating') 'creating')
self.volume_api.update(self.context, self.volume_api.update(self.context,
volume_ref['id'], {'status': 'available'}) volume_ref,
self.volume_api.delete(self.context, volume_ref['id']) {'status': 'available'})
volume_ref = self.volume_api.get(self.context, volume_ref['id'])
self.volume_api.delete(self.context, volume_ref)
vols3 = self._get_all_volumes_by_vsa() vols3 = self._get_all_volumes_by_vsa()
self.assertEqual(1, len(vols2)) self.assertEqual(1, len(vols2))
@@ -110,10 +112,11 @@ class VsaVolumesTestCase(test.TestCase):
volume_ref = self.volume_api.create(self.context, **volume_param) volume_ref = self.volume_api.create(self.context, **volume_param)
self.volume_api.update(self.context, self.volume_api.update(self.context,
volume_ref['id'], {'status': 'in-use'}) volume_ref,
{'status': 'in-use'})
self.assertRaises(exception.ApiError, self.assertRaises(exception.ApiError,
self.volume_api.delete, self.volume_api.delete,
self.context, volume_ref['id']) self.context, volume_ref)
def test_vsa_volume_delete_vsa_with_volumes(self): def test_vsa_volume_delete_vsa_with_volumes(self):
""" Check volume deleton in different states. """ """ Check volume deleton in different states. """

View File

@@ -246,9 +246,9 @@ class API(base.Base):
vol_ref = self.volume_api.create(context, vol_ref = self.volume_api.create(context,
vol_size, vol_size,
None,
vol_name, vol_name,
vol['description'], vol['description'],
None,
volume_type=vol_type, volume_type=vol_type,
metadata=dict(to_vsa_id=str(vsa_id)), metadata=dict(to_vsa_id=str(vsa_id)),
availability_zone=availability_zone) availability_zone=availability_zone)
@@ -349,7 +349,7 @@ class API(base.Base):
vol_name = volume['name'] vol_name = volume['name']
LOG.info(_("VSA ID %(vsa_id)s: Deleting %(direction)s "\ LOG.info(_("VSA ID %(vsa_id)s: Deleting %(direction)s "\
"volume %(vol_name)s"), locals()) "volume %(vol_name)s"), locals())
self.volume_api.delete(context, volume['id']) self.volume_api.delete(context, volume)
except exception.ApiError: except exception.ApiError:
LOG.info(_("Unable to delete volume %s"), volume['name']) LOG.info(_("Unable to delete volume %s"), volume['name'])
if force_delete: if force_delete:

View File

@@ -136,7 +136,7 @@ class VsaManager(manager.SchedulerDependentManager):
locals()) locals())
if status == 'available': if status == 'available':
try: try:
# self.volume_api.update(context, volume['id'], # self.volume_api.update(context, volume,
# dict(attach_status="attached")) # dict(attach_status="attached"))
pass pass
except Exception as ex: except Exception as ex: