]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Remove _create_volume function from several tests
authorJulia Varlamova <jvarlamova@mirantis.com>
Thu, 29 Aug 2013 07:56:17 +0000 (11:56 +0400)
committerJulia Varlamova <jvarlamova@mirantis.com>
Fri, 30 Aug 2013 07:23:54 +0000 (11:23 +0400)
Remove _create_volume function from
- db/test_transfers.py
- api/contrib/test_backups.py
- test_gpfs.py
- test_volume_transfer.py
- test_volume.py
And use create_volume from tests/utils.py instead

Change-Id: I446220d0cfaa2850a5262a968ed35b7827b90a03

cinder/tests/api/contrib/test_backups.py
cinder/tests/db/test_transfers.py
cinder/tests/test_gpfs.py
cinder/tests/test_volume.py
cinder/tests/test_volume_transfer.py

index fdc57aabc31e49568cc46c99181c006d946bf0fe..06c563639b29ba04692bafd437ad3a67f80fb864 100644 (file)
@@ -31,6 +31,7 @@ from cinder.openstack.common import log as logging
 from cinder.openstack.common import timeutils
 from cinder import test
 from cinder.tests.api import fakes
+from cinder.tests import utils
 # needed for stubs to work
 import cinder.volume
 
@@ -45,6 +46,9 @@ class BackupsAPITestCase(test.TestCase):
         super(BackupsAPITestCase, self).setUp()
         self.volume_api = cinder.volume.API()
         self.backup_api = cinder.backup.API()
+        self.context = context.get_admin_context()
+        self.context.project_id = 'fake'
+        self.context.user_id = 'fake'
 
     def tearDown(self):
         super(BackupsAPITestCase, self).tearDown()
@@ -77,33 +81,14 @@ class BackupsAPITestCase(test.TestCase):
         return db.backup_get(context.get_admin_context(),
                              backup_id)[attrib_name]
 
-    @staticmethod
-    def _create_volume(display_name='test_volume',
-                       display_description='this is a test volume',
-                       status='creating',
-                       availability_zone='fake_az',
-                       host='fake_host',
-                       size=1):
-        """Create a volume object."""
-        vol = {}
-        vol['size'] = size
-        vol['user_id'] = 'fake'
-        vol['project_id'] = 'fake'
-        vol['status'] = status
-        vol['display_name'] = display_name
-        vol['display_description'] = display_description
-        vol['attach_status'] = 'detached'
-        vol['availability_zone'] = availability_zone
-        vol['host'] = host
-        return db.volume_create(context.get_admin_context(), vol)['id']
-
     @staticmethod
     def _stub_service_get_all_by_topic(context, topic):
-        return [{'availability_zone': "fake_az", 'host': 'fake_host',
+        return [{'availability_zone': "fake_az", 'host': 'test_host',
                  'disabled': 0, 'updated_at': timeutils.utcnow()}]
 
     def test_show_backup(self):
-        volume_id = self._create_volume(size=5)
+        volume_id = utils.create_volume(self.context, size=5,
+                                        status='creating')['id']
         backup_id = self._create_backup(volume_id)
         LOG.debug('Created backup with id %s' % backup_id)
         req = webob.Request.blank('/v2/fake/backups/%s' %
@@ -129,7 +114,8 @@ class BackupsAPITestCase(test.TestCase):
         db.volume_destroy(context.get_admin_context(), volume_id)
 
     def test_show_backup_xml_content_type(self):
-        volume_id = self._create_volume(size=5)
+        volume_id = utils.create_volume(self.context, size=5,
+                                        status='creating')['id']
         backup_id = self._create_backup(volume_id)
         req = webob.Request.blank('/v2/fake/backups/%s' % backup_id)
         req.method = 'GET'
@@ -357,7 +343,9 @@ class BackupsAPITestCase(test.TestCase):
     def test_create_backup_json(self):
         self.stubs.Set(cinder.db, 'service_get_all_by_topic',
                        self._stub_service_get_all_by_topic)
-        volume_id = self._create_volume(status='available', size=5)
+
+        volume_id = utils.create_volume(self.context, size=5)['id']
+
         body = {"backup": {"display_name": "nightly001",
                            "display_description":
                            "Nightly Backup 03-Sep-2012",
@@ -382,8 +370,7 @@ class BackupsAPITestCase(test.TestCase):
     def test_create_backup_xml(self):
         self.stubs.Set(cinder.db, 'service_get_all_by_topic',
                        self._stub_service_get_all_by_topic)
-        volume_size = 2
-        volume_id = self._create_volume(status='available', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=2)['id']
 
         req = webob.Request.blank('/v2/fake/backups')
         req.body = ('<backup display_name="backup-001" '
@@ -459,9 +446,8 @@ class BackupsAPITestCase(test.TestCase):
 
     def test_create_backup_with_InvalidVolume(self):
         # need to create the volume referenced below first
-        volume_size = 5
-        volume_id = self._create_volume(status='restoring', size=volume_size)
-
+        volume_id = utils.create_volume(self.context, size=5,
+                                        status='restoring')['id']
         body = {"backup": {"display_name": "nightly001",
                            "display_description":
                            "Nightly Backup 03-Sep-2012",
@@ -489,9 +475,7 @@ class BackupsAPITestCase(test.TestCase):
 
         self.stubs.Set(cinder.db, 'service_get_all_by_topic',
                        stub_empty_service_get_all_by_topic)
-        volume_size = 2
-        volume_id = self._create_volume(status='available', size=volume_size)
-
+        volume_id = utils.create_volume(self.context, size=2)['id']
         req = webob.Request.blank('/v2/fake/backups')
         body = {"backup": {"display_name": "nightly001",
                            "display_description":
@@ -523,12 +507,12 @@ class BackupsAPITestCase(test.TestCase):
 
         #service az not match with volume's az
         def az_not_match(context, topic):
-            return [{'availability_zone': "strange_az", 'host': 'fake_host',
+            return [{'availability_zone': "strange_az", 'host': 'test_host',
                      'disabled': 0, 'updated_at': timeutils.utcnow()}]
 
         #service disabled
         def disabled_service(context, topic):
-            return [{'availability_zone': "fake_az", 'host': 'fake_host',
+            return [{'availability_zone': "fake_az", 'host': 'test_host',
                      'disabled': 1, 'updated_at': timeutils.utcnow()}]
 
         #dead service that last reported at 20th centry
@@ -540,12 +524,10 @@ class BackupsAPITestCase(test.TestCase):
         def multi_services(context, topic):
             return [{'availability_zone': "fake_az", 'host': 'strange_host',
                      'disabled': 0, 'updated_at': timeutils.utcnow()},
-                    {'availability_zone': "fake_az", 'host': 'fake_host',
+                    {'availability_zone': "fake_az", 'host': 'test_host',
                      'disabled': 0, 'updated_at': timeutils.utcnow()}]
 
-        volume_id = self._create_volume(status='available', size=2,
-                                        availability_zone='fake_az',
-                                        host='fake_host')
+        volume_id = utils.create_volume(self.context, size=2)['id']
         volume = self.volume_api.get(context.get_admin_context(), volume_id)
 
         #test empty service
@@ -632,8 +614,7 @@ class BackupsAPITestCase(test.TestCase):
     def test_restore_backup_volume_id_specified_json(self):
         backup_id = self._create_backup(status='available')
         # need to create the volume referenced below first
-        volume_size = 5
-        volume_id = self._create_volume(status='available', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=5)['id']
 
         body = {"restore": {"volume_id": volume_id, }}
         req = webob.Request.blank('/v2/fake/backups/%s/restore' %
@@ -650,8 +631,7 @@ class BackupsAPITestCase(test.TestCase):
 
     def test_restore_backup_volume_id_specified_xml(self):
         backup_id = self._create_backup(status='available')
-        volume_size = 2
-        volume_id = self._create_volume(status='available', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=2)['id']
 
         req = webob.Request.blank('/v2/fake/backups/%s/restore' % backup_id)
         req.body = '<restore volume_id="%s"/>' % volume_id
@@ -716,7 +696,7 @@ class BackupsAPITestCase(test.TestCase):
         # intercept volume creation to ensure created volume
         # has status of available
         def fake_volume_api_create(cls, context, size, name, description):
-            volume_id = self._create_volume(status='available', size=size)
+            volume_id = utils.create_volume(self.context, size=size)['id']
             return db.volume_get(context, volume_id)
 
         self.stubs.Set(cinder.volume.API, 'create',
@@ -749,9 +729,7 @@ class BackupsAPITestCase(test.TestCase):
 
         backup_id = self._create_backup(status='available')
         # need to create the volume referenced below first
-        volume_size = 0
-        volume_id = self._create_volume(status='available', size=volume_size)
-
+        volume_id = utils.create_volume(self.context, size=0)['id']
         body = {"restore": {"volume_id": volume_id, }}
         req = webob.Request.blank('/v2/fake/backups/%s/restore' %
                                   backup_id)
@@ -770,8 +748,8 @@ class BackupsAPITestCase(test.TestCase):
     def test_restore_backup_with_InvalidVolume(self):
         backup_id = self._create_backup(status='available')
         # need to create the volume referenced below first
-        volume_size = 5
-        volume_id = self._create_volume(status='attaching', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=5,
+                                        status='attaching')['id']
 
         body = {"restore": {"volume_id": volume_id, }}
         req = webob.Request.blank('/v2/fake/backups/%s/restore' %
@@ -794,8 +772,7 @@ class BackupsAPITestCase(test.TestCase):
     def test_restore_backup_with_InvalidBackup(self):
         backup_id = self._create_backup(status='restoring')
         # need to create the volume referenced below first
-        volume_size = 5
-        volume_id = self._create_volume(status='available', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=5)['id']
 
         body = {"restore": {"volume_id": volume_id, }}
         req = webob.Request.blank('/v2/fake/backups/%s/restore' %
@@ -816,8 +793,7 @@ class BackupsAPITestCase(test.TestCase):
 
     def test_restore_backup_with_BackupNotFound(self):
         # need to create the volume referenced below first
-        volume_size = 5
-        volume_id = self._create_volume(status='available', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=5)['id']
 
         body = {"restore": {"volume_id": volume_id, }}
         req = webob.Request.blank('/v2/fake/backups/9999/restore')
@@ -866,8 +842,7 @@ class BackupsAPITestCase(test.TestCase):
 
         backup_id = self._create_backup(status='available')
         # need to create the volume referenced below first
-        volume_size = 5
-        volume_id = self._create_volume(status='available', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=5)['id']
 
         body = {"restore": {"volume_id": volume_id, }}
         req = webob.Request.blank('/v2/fake/backups/%s/restore' %
@@ -898,8 +873,7 @@ class BackupsAPITestCase(test.TestCase):
 
         backup_id = self._create_backup(status='available')
         # need to create the volume referenced below first
-        volume_size = 5
-        volume_id = self._create_volume(status='available', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=5)['id']
 
         body = {"restore": {"volume_id": volume_id, }}
         req = webob.Request.blank('/v2/fake/backups/%s/restore' %
@@ -921,7 +895,7 @@ class BackupsAPITestCase(test.TestCase):
         backup_id = self._create_backup(status='available', size=backup_size)
         # need to create the volume referenced below first
         volume_size = 5
-        volume_id = self._create_volume(status='available', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=volume_size)['id']
 
         body = {"restore": {"volume_id": volume_id, }}
         req = webob.Request.blank('/v2/fake/backups/%s/restore' %
@@ -945,8 +919,7 @@ class BackupsAPITestCase(test.TestCase):
     def test_restore_backup_to_oversized_volume(self):
         backup_id = self._create_backup(status='available', size=10)
         # need to create the volume referenced below first
-        volume_size = 15
-        volume_id = self._create_volume(status='available', size=volume_size)
+        volume_id = utils.create_volume(self.context, size=15)['id']
 
         body = {"restore": {"volume_id": volume_id, }}
         req = webob.Request.blank('/v2/fake/backups/%s/restore' %
index 8f38cedfa063802a4c24986201708fbcfe54dab8..55cffe5137ee070ab74c09f75181f12f09e498b7 100644 (file)
@@ -20,6 +20,7 @@ from cinder import db
 from cinder import exception
 from cinder.openstack.common import log as logging
 from cinder import test
+from cinder.tests import utils
 
 
 LOG = logging.getLogger(__name__)
@@ -36,22 +37,6 @@ class TransfersTableTestCase(test.TestCase):
     def tearDown(self):
         super(TransfersTableTestCase, self).tearDown()
 
-    def _create_volume(self,
-                       display_name='test_volume',
-                       display_description='this is a test volume',
-                       status='available',
-                       size=1):
-        """Create a volume object."""
-        vol = {}
-        vol['size'] = size
-        vol['user_id'] = self.ctxt.user_id
-        vol['project_id'] = self.ctxt.project_id
-        vol['status'] = status
-        vol['display_name'] = display_name
-        vol['display_description'] = display_description
-        vol['attach_status'] = 'detached'
-        return db.volume_create(self.ctxt, vol)['id']
-
     def _create_transfer(self, volume_id=None):
         """Create a transfer object."""
         transfer = {'display_name': 'display_name',
@@ -66,11 +51,11 @@ class TransfersTableTestCase(test.TestCase):
         self.assertRaises(KeyError,
                           self._create_transfer)
 
-        volume_id = self._create_volume(size=1)
+        volume_id = utils.create_volume(self.ctxt)['id']
         self._create_transfer(volume_id)
 
     def test_transfer_get(self):
-        volume_id1 = self._create_volume(size=1)
+        volume_id1 = utils.create_volume(self.ctxt)['id']
         xfer_id1 = self._create_transfer(volume_id1)
 
         xfer = db.transfer_get(self.ctxt, xfer_id1)
@@ -85,8 +70,8 @@ class TransfersTableTestCase(test.TestCase):
         self.assertEquals(xfer.volume_id, volume_id1, "Unexpected volume_id")
 
     def test_transfer_get_all(self):
-        volume_id1 = self._create_volume(size=1)
-        volume_id2 = self._create_volume(size=1)
+        volume_id1 = utils.create_volume(self.ctxt)['id']
+        volume_id2 = utils.create_volume(self.ctxt)['id']
         self._create_transfer(volume_id1)
         self._create_transfer(volume_id2)
 
@@ -112,8 +97,8 @@ class TransfersTableTestCase(test.TestCase):
                           "Unexpected number of transfer records")
 
     def test_transfer_destroy(self):
-        volume_id = self._create_volume(size=1)
-        volume_id2 = self._create_volume(size=1)
+        volume_id = utils.create_volume(self.ctxt)['id']
+        volume_id2 = utils.create_volume(self.ctxt)['id']
         xfer_id1 = self._create_transfer(volume_id)
         xfer_id2 = self._create_transfer(volume_id2)
 
index 398092b3c89f8129f63cb716bbbe131745a2da03..6490adf1ae8942d4edcac90129615f9d9756188c 100644 (file)
@@ -27,6 +27,7 @@ from cinder.openstack.common import importutils
 from cinder.openstack.common import log as logging
 from cinder.openstack.common import processutils
 from cinder import test
+from cinder.tests import utils as test_utils
 from cinder import utils
 from cinder.volume import configuration as conf
 from cinder.volume.drivers.gpfs import GPFSDriver
@@ -112,6 +113,8 @@ class GPFSDriverTestCase(test.TestCase):
                        self._fake_qemu_image_resize)
 
         self.context = context.get_admin_context()
+        self.context.user_id = 'fake'
+        self.context.project_id = 'fake'
         CONF.gpfs_images_dir = self.images_dir
 
     def tearDown(self):
@@ -122,27 +125,10 @@ class GPFSDriverTestCase(test.TestCase):
             pass
         super(GPFSDriverTestCase, self).tearDown()
 
-    def _create_volume(self, size=0, snapshot_id=None, image_id=None,
-                       metadata=None):
-        """Create a volume object."""
-        vol = {}
-        vol['size'] = size
-        vol['snapshot_id'] = snapshot_id
-        vol['image_id'] = image_id
-        vol['user_id'] = 'fake'
-        vol['project_id'] = 'fake'
-        vol['availability_zone'] = CONF.storage_availability_zone
-        vol['status'] = "creating"
-        vol['attach_status'] = "detached"
-        vol['host'] = CONF.host
-        if metadata is not None:
-            vol['metadata'] = metadata
-        return db.volume_create(context.get_admin_context(), vol)
-
     def test_create_delete_volume_full_backing_file(self):
         """create and delete vol with full creation method"""
         CONF.gpfs_sparse_volumes = False
-        vol = self._create_volume(size=1)
+        vol = test_utils.create_volume(self.context, host=CONF.host)
         volume_id = vol['id']
         self.assertTrue(os.path.exists(self.volumes_path))
         self.volume.create_volume(self.context, volume_id)
@@ -154,7 +140,7 @@ class GPFSDriverTestCase(test.TestCase):
     def test_create_delete_volume_sparse_backing_file(self):
         """create and delete vol with default sparse creation method"""
         CONF.gpfs_sparse_volumes = True
-        vol = self._create_volume(size=1)
+        vol = test_utils.create_volume(self.context, host=CONF.host)
         volume_id = vol['id']
         self.assertTrue(os.path.exists(self.volumes_path))
         self.volume.create_volume(self.context, volume_id)
@@ -171,7 +157,8 @@ class GPFSDriverTestCase(test.TestCase):
                       'block_group_factor': '1',
                       'write_affinity_failure-group':
                       '1,1,1:2;2,1,1:2;2,0,3:4'}
-        vol = self._create_volume(size=1, metadata=attributes)
+        vol = test_utils.create_volume(self.context, host=CONF.host,
+                                       metadata=attributes)
         volume_id = vol['id']
         self.assertTrue(os.path.exists(self.volumes_path))
         self.volume.create_volume(self.context, volume_id)
@@ -191,7 +178,7 @@ class GPFSDriverTestCase(test.TestCase):
         return db.snapshot_create(context.get_admin_context(), snap)
 
     def test_create_delete_snapshot(self):
-        volume_src = self._create_volume()
+        volume_src = test_utils.create_volume(self.context, host=CONF.host)
         self.volume.create_volume(self.context, volume_src['id'])
         snapCount = len(db.snapshot_get_all_for_volume(self.context,
                                                        volume_src['id']))
@@ -214,7 +201,7 @@ class GPFSDriverTestCase(test.TestCase):
         self.assertTrue(snapCount == 0)
 
     def test_create_volume_from_snapshot(self):
-        volume_src = self._create_volume()
+        volume_src = test_utils.create_volume(self.context, host=CONF.host)
         self.volume.create_volume(self.context, volume_src['id'])
         snapshot = self._create_snapshot(volume_src['id'])
         snapshot_id = snapshot['id']
@@ -222,7 +209,8 @@ class GPFSDriverTestCase(test.TestCase):
                                     snapshot_id)
         self.assertTrue(os.path.exists(os.path.join(self.volumes_path,
                                                     snapshot['name'])))
-        volume_dst = self._create_volume(0, snapshot_id)
+        volume_dst = test_utils.create_volume(self.context, host=CONF.host,
+                                              snapshot_id=snapshot_id)
         self.volume.create_volume(self.context, volume_dst['id'], snapshot_id)
         self.assertEqual(volume_dst['id'], db.volume_get(
                          context.get_admin_context(),
@@ -236,10 +224,10 @@ class GPFSDriverTestCase(test.TestCase):
         self.volume.delete_snapshot(self.context, snapshot_id)
 
     def test_create_cloned_volume(self):
-        volume_src = self._create_volume()
+        volume_src = test_utils.create_volume(self.context, host=CONF.host)
         self.volume.create_volume(self.context, volume_src['id'])
 
-        volume_dst = self._create_volume()
+        volume_dst = test_utils.create_volume(self.context, host=CONF.host)
         volumepath = os.path.join(self.volumes_path, volume_dst['name'])
         self.assertFalse(os.path.exists(volumepath))
 
@@ -254,14 +242,14 @@ class GPFSDriverTestCase(test.TestCase):
         self.volume.delete_volume(self.context, volume_dst['id'])
 
     def test_create_volume_from_snapshot_method(self):
-        volume_src = self._create_volume()
+        volume_src = test_utils.create_volume(self.context, host=CONF.host)
         self.volume.create_volume(self.context, volume_src['id'])
 
         snapshot = self._create_snapshot(volume_src['id'])
         snapshot_id = snapshot['id']
         self.volume.create_snapshot(self.context, volume_src['id'],
                                     snapshot_id)
-        volume_dst = self._create_volume()
+        volume_dst = test_utils.create_volume(self.context, host=CONF.host)
         self.driver.create_volume_from_snapshot(volume_dst, snapshot)
         self.assertEqual(volume_dst['id'], db.volume_get(
                          context.get_admin_context(),
@@ -284,7 +272,7 @@ class GPFSDriverTestCase(test.TestCase):
         self.stubs.Set(image_utils, 'qemu_img_info',
                        self._fake_qemu_raw_image_info)
 
-        volume = self._create_volume()
+        volume = test_utils.create_volume(self.context, host=CONF.host)
         volumepath = os.path.join(self.volumes_path, volume['name'])
         CONF.gpfs_images_share_mode = 'copy_on_write'
         self.driver.clone_image(volume,
@@ -305,7 +293,7 @@ class GPFSDriverTestCase(test.TestCase):
         self.stubs.Set(image_utils, 'qemu_img_info',
                        self._fake_qemu_raw_image_info)
 
-        volume = self._create_volume()
+        volume = test_utils.create_volume(self.context, host=CONF.host)
         volumepath = os.path.join(self.volumes_path, volume['name'])
         CONF.gpfs_images_share_mode = 'copy'
         self.driver.clone_image(volume,
@@ -325,7 +313,7 @@ class GPFSDriverTestCase(test.TestCase):
                        self._fake_qemu_raw_image_info)
 
         for share_mode in ['copy_on_write', 'copy']:
-            volume = self._create_volume()
+            volume = test_utils.create_volume(self.context, host=CONF.host)
             volumepath = os.path.join(self.volumes_path, volume['name'])
             CONF.gpfs_images_share_mode = share_mode
             CONF.gpfs_images_dir = None
@@ -345,7 +333,7 @@ class GPFSDriverTestCase(test.TestCase):
         self.stubs.Set(image_utils, 'qemu_img_info',
                        self._fake_qemu_qcow2_image_info)
 
-        volume = self._create_volume()
+        volume = test_utils.create_volume(self.context, host=CONF.host)
         CONF.gpfs_images_share_mode = 'copy'
         CONF.gpfs_images_dir = self.images_dir
         self.assertRaises(exception.ImageUnacceptable,
index 4c358c5e0cfafec653537d595bd66dac04dd2830..1673f9a1ed438f0a3dc3d85d1e14520af5ced215 100644 (file)
@@ -91,6 +91,12 @@ class BaseVolumeTestCase(test.TestCase):
                    notification_driver=[test_notifier.__name__])
         self.volume = importutils.import_object(CONF.volume_manager)
         self.context = context.get_admin_context()
+        self.context.user_id = 'fake'
+        self.context.project_id = 'fake'
+        self.volume_params = {
+            'status': 'creating',
+            'host': CONF.host,
+            'size': 0}
         self.stubs.Set(iscsi.TgtAdm, '_get_target', self.fake_get_target)
         self.stubs.Set(brick_lvm.LVM,
                        'get_all_volume_groups',
@@ -117,35 +123,12 @@ class BaseVolumeTestCase(test.TestCase):
                  'lv_count': '2',
                  'uuid': 'vR1JU3-FAKE-C4A9-PQFh-Mctm-9FwA-Xwzc1m'}]
 
-    @staticmethod
-    def _create_volume(size=0, snapshot_id=None, image_id=None,
-                       source_volid=None, metadata=None, admin_metadata=None,
-                       status="creating", migration_status=None,
-                       availability_zone=None):
-        """Create a volume object."""
-        vol = {}
-        vol['size'] = size
-        vol['snapshot_id'] = snapshot_id
-        vol['image_id'] = image_id
-        vol['source_volid'] = source_volid
-        vol['user_id'] = 'fake'
-        vol['project_id'] = 'fake'
-        vol['availability_zone'] = \
-            availability_zone or CONF.storage_availability_zone
-        vol['status'] = status
-        vol['attach_status'] = "detached"
-        vol['host'] = CONF.host
-        if metadata is not None:
-            vol['metadata'] = metadata
-        if admin_metadata is not None:
-            vol['admin_metadata'] = admin_metadata
-        return db.volume_create(context.get_admin_context(), vol)
-
 
 class VolumeTestCase(BaseVolumeTestCase):
     def test_init_host_clears_downloads(self):
         """Test that init_host will unwedge a volume stuck in downloading."""
-        volume = self._create_volume(status='downloading')
+        volume = tests_utils.create_volume(self.context, status='downloading',
+                                           size=0, host=CONF.host)
         volume_id = volume['id']
         self.volume.init_host()
         volume = db.volume_get(context.get_admin_context(), volume_id)
@@ -168,7 +151,10 @@ class VolumeTestCase(BaseVolumeTestCase):
         self.stubs.Set(QUOTAS, "commit", fake_commit)
         self.stubs.Set(QUOTAS, "rollback", fake_rollback)
 
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(
+            self.context,
+            availability_zone=CONF.storage_availability_zone,
+            **self.volume_params)
         volume_id = volume['id']
         self.assertIsNone(volume['encryption_key_id'])
         self.assertEquals(len(test_notifier.NOTIFICATIONS), 0)
@@ -178,7 +164,7 @@ class VolumeTestCase(BaseVolumeTestCase):
         self.assertEqual(msg['event_type'], 'volume.create.start')
         expected = {
             'status': 'creating',
-            'display_name': None,
+            'display_name': 'test_volume',
             'availability_zone': 'nova',
             'tenant_id': 'fake',
             'created_at': 'DONTCARE',
@@ -216,7 +202,8 @@ class VolumeTestCase(BaseVolumeTestCase):
     def test_create_delete_volume_with_metadata(self):
         """Test volume can be created with metadata and deleted."""
         test_meta = {'fake_key': 'fake_value'}
-        volume = self._create_volume(0, None, metadata=test_meta)
+        volume = tests_utils.create_volume(self.context, metadata=test_meta,
+                                           **self.volume_params)
         volume_id = volume['id']
         self.volume.create_volume(self.context, volume_id)
         result_meta = {
@@ -327,7 +314,7 @@ class VolumeTestCase(BaseVolumeTestCase):
 
     def test_delete_busy_volume(self):
         """Test volume survives deletion if driver reports it as busy."""
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         volume_id = volume['id']
         self.volume.create_volume(self.context, volume_id)
 
@@ -348,7 +335,7 @@ class VolumeTestCase(BaseVolumeTestCase):
     def test_delete_volume_in_error_extending(self):
         """Test volume can be deleted in error_extending stats."""
         # create a volume
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         self.volume.create_volume(self.context, volume['id'])
 
         # delete 'error_extending' volume
@@ -360,12 +347,15 @@ class VolumeTestCase(BaseVolumeTestCase):
 
     def test_create_volume_from_snapshot(self):
         """Test volume can be created from a snapshot."""
-        volume_src = self._create_volume()
+        volume_src = tests_utils.create_volume(self.context,
+                                               **self.volume_params)
         self.volume.create_volume(self.context, volume_src['id'])
         snapshot_id = self._create_snapshot(volume_src['id'])['id']
         self.volume.create_snapshot(self.context, volume_src['id'],
                                     snapshot_id)
-        volume_dst = self._create_volume(0, snapshot_id)
+        volume_dst = tests_utils.create_volume(self.context,
+                                               snapshot_id=snapshot_id,
+                                               **self.volume_params)
         self.volume.create_volume(self.context, volume_dst['id'], snapshot_id)
         self.assertEqual(volume_dst['id'],
                          db.volume_get(
@@ -506,7 +496,9 @@ class VolumeTestCase(BaseVolumeTestCase):
                        'list_availability_zones',
                        fake_list_availability_zones)
 
-        volume_src = self._create_volume(availability_zone='az2')
+        volume_src = tests_utils.create_volume(self.context,
+                                               availability_zone='az2',
+                                               **self.volume_params)
         self.volume.create_volume(self.context, volume_src['id'])
         snapshot = self._create_snapshot(volume_src['id'])
         self.volume.create_snapshot(self.context, volume_src['id'],
@@ -548,7 +540,9 @@ class VolumeTestCase(BaseVolumeTestCase):
         #              volume_create
         return True
         try:
-            volume = self._create_volume(1001)
+            volume = tests_utils.create_volume(self.context, size=1001,
+                                               status='creating',
+                                               host=CONF.host)
             self.volume.create_volume(self.context, volume)
             self.fail("Should have thrown TypeError")
         except TypeError:
@@ -559,7 +553,9 @@ class VolumeTestCase(BaseVolumeTestCase):
         mountpoint = "/dev/sdf"
         # attach volume to the instance then to detach
         instance_uuid = '12345678-1234-5678-1234-567812345678'
-        volume = self._create_volume(admin_metadata={'readonly': 'True'})
+        volume = tests_utils.create_volume(self.context,
+                                           admin_metadata={'readonly': 'True'},
+                                           **self.volume_params)
         volume_id = volume['id']
         self.volume.create_volume(self.context, volume_id)
         self.volume.attach_volume(self.context, volume_id, instance_uuid,
@@ -598,7 +594,10 @@ class VolumeTestCase(BaseVolumeTestCase):
     def test_run_attach_detach_volume_for_host(self):
         """Make sure volume can be attached and detached from host."""
         mountpoint = "/dev/sdf"
-        volume = self._create_volume(admin_metadata={'readonly': 'False'})
+        volume = tests_utils.create_volume(
+            self.context,
+            admin_metadata={'readonly': 'False'},
+            **self.volume_params)
         volume_id = volume['id']
         self.volume.create_volume(self.context, volume_id)
         self.volume.attach_volume(self.context, volume_id, None,
@@ -638,7 +637,9 @@ class VolumeTestCase(BaseVolumeTestCase):
     def test_run_attach_detach_volume_with_attach_mode(self):
         instance_uuid = '12345678-1234-5678-1234-567812345678'
         mountpoint = "/dev/sdf"
-        volume = self._create_volume(admin_metadata={'readonly': 'True'})
+        volume = tests_utils.create_volume(self.context,
+                                           admin_metadata={'readonly': 'True'},
+                                           **self.volume_params)
         volume_id = volume['id']
         db.volume_update(self.context, volume_id, {'status': 'available',
                                                    'mountpoint': None,
@@ -717,7 +718,9 @@ class VolumeTestCase(BaseVolumeTestCase):
         # Not allow using 'read-write' mode attach readonly volume
         instance_uuid = '12345678-1234-5678-1234-567812345678'
         mountpoint = "/dev/sdf"
-        volume = self._create_volume(admin_metadata={'readonly': 'True'})
+        volume = tests_utils.create_volume(self.context,
+                                           admin_metadata={'readonly': 'True'},
+                                           **self.volume_params)
         volume_id = volume['id']
         self.volume.create_volume(self.context, volume_id)
         self.assertRaises(exception.InvalidVolumeAttachMode,
@@ -761,7 +764,9 @@ class VolumeTestCase(BaseVolumeTestCase):
         # Not allow using 'read-write' mode attach readonly volume
         instance_uuid = '12345678-1234-5678-1234-567812345678'
         mountpoint = "/dev/sdf"
-        volume = self._create_volume(admin_metadata={'readonly': 'True'})
+        volume = tests_utils.create_volume(self.context,
+                                           admin_metadata={'readonly': 'True'},
+                                           **self.volume_params)
         volume_id = volume['id']
         self.volume.create_volume(self.context, volume_id)
         volume_api = cinder.volume.api.API()
@@ -812,7 +817,7 @@ class VolumeTestCase(BaseVolumeTestCase):
 
         total_slots = CONF.iscsi_num_targets
         for _index in xrange(total_slots):
-            self._create_volume()
+            tests_utils.create_volume(self.context, **self.volume_params)
         for volume_id in volume_ids:
             self.volume.delete_volume(self.context, volume_id)
 
@@ -837,7 +842,10 @@ class VolumeTestCase(BaseVolumeTestCase):
 
     def test_create_delete_snapshot(self):
         """Test snapshot can be created and deleted."""
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(
+            self.context,
+            availability_zone=CONF.storage_availability_zone,
+            **self.volume_params)
         self.assertEquals(len(test_notifier.NOTIFICATIONS), 0)
         self.volume.create_volume(self.context, volume['id'])
         self.assertEquals(len(test_notifier.NOTIFICATIONS), 2)
@@ -888,7 +896,7 @@ class VolumeTestCase(BaseVolumeTestCase):
     def test_create_delete_snapshot_with_metadata(self):
         """Test snapshot can be created with metadata and deleted."""
         test_meta = {'fake_key': 'fake_value'}
-        volume = self._create_volume(0, None)
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         snapshot = self._create_snapshot(volume['id'], metadata=test_meta)
         snapshot_id = snapshot['id']
 
@@ -908,7 +916,7 @@ class VolumeTestCase(BaseVolumeTestCase):
     def test_cant_delete_volume_in_use(self):
         """Test volume can't be deleted in invalid stats."""
         # create a volume and assign to host
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         self.volume.create_volume(self.context, volume['id'])
         volume['status'] = 'in-use'
         volume['host'] = 'fakehost'
@@ -927,7 +935,7 @@ class VolumeTestCase(BaseVolumeTestCase):
     def test_force_delete_volume(self):
         """Test volume can be forced to delete."""
         # create a volume and assign to host
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         self.volume.create_volume(self.context, volume['id'])
         volume['status'] = 'error_deleting'
         volume['host'] = 'fakehost'
@@ -952,7 +960,7 @@ class VolumeTestCase(BaseVolumeTestCase):
 
     def test_cant_force_delete_attached_volume(self):
         """Test volume can't be force delete in attached state"""
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         self.volume.create_volume(self.context, volume['id'])
         volume['status'] = 'in-use'
         volume['attach_status'] = 'attached'
@@ -970,7 +978,7 @@ class VolumeTestCase(BaseVolumeTestCase):
 
     def test_cant_delete_volume_with_snapshots(self):
         """Test volume can't be deleted with dependent snapshots."""
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         self.volume.create_volume(self.context, volume['id'])
         snapshot_id = self._create_snapshot(volume['id'])['id']
         self.volume.create_snapshot(self.context, volume['id'], snapshot_id)
@@ -992,7 +1000,7 @@ class VolumeTestCase(BaseVolumeTestCase):
 
     def test_can_delete_errored_snapshot(self):
         """Test snapshot can be created and deleted."""
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         self.volume.create_volume(self.context, volume['id'])
         snapshot_id = self._create_snapshot(volume['id'])['id']
         self.volume.create_snapshot(self.context, volume['id'], snapshot_id)
@@ -1019,7 +1027,7 @@ class VolumeTestCase(BaseVolumeTestCase):
         self.stubs.Set(rpc, 'cast', fake_cast)
         instance_uuid = '12345678-1234-5678-1234-567812345678'
         # create volume and attach to the instance
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         self.volume.create_volume(self.context, volume['id'])
         db.volume_attached(self.context, volume['id'], instance_uuid,
                            None, '/dev/sda1')
@@ -1038,7 +1046,7 @@ class VolumeTestCase(BaseVolumeTestCase):
         db.volume_destroy(self.context, volume['id'])
 
         # create volume and attach to the host
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         self.volume.create_volume(self.context, volume['id'])
         db.volume_attached(self.context, volume['id'], None,
                            'fake_host', '/dev/sda1')
@@ -1064,7 +1072,7 @@ class VolumeTestCase(BaseVolumeTestCase):
                                              None,
                                              'default')
 
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         volume_id = volume['id']
         self.volume.create_volume(self.context, volume_id)
         snapshot_id = self._create_snapshot(volume_id)['id']
@@ -1117,7 +1125,8 @@ class VolumeTestCase(BaseVolumeTestCase):
                            fake_copy_image_to_volume)
 
         image_id = 'c905cedb-7281-47e4-8a62-f26bc5fc4c77'
-        volume_id = self._create_volume(status='creating')['id']
+        volume_id = tests_utils.create_volume(self.context,
+                                              **self.volume_params)['id']
         # creating volume testdata
         try:
             self.volume.create_volume(self.context,
@@ -1289,7 +1298,7 @@ class VolumeTestCase(BaseVolumeTestCase):
 
     def test_begin_roll_detaching_volume(self):
         """Test begin_detaching and roll_detaching functions."""
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         volume_api = cinder.volume.api.API()
         volume_api.begin_detaching(self.context, volume)
         volume = db.volume_get(self.context, volume['id'])
@@ -1300,7 +1309,7 @@ class VolumeTestCase(BaseVolumeTestCase):
 
     def test_volume_api_update(self):
         # create a raw vol
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         # use volume.api to update name
         volume_api = cinder.volume.api.API()
         update_dict = {'display_name': 'test update name'}
@@ -1311,7 +1320,7 @@ class VolumeTestCase(BaseVolumeTestCase):
 
     def test_volume_api_update_snapshot(self):
         # create raw snapshot
-        volume = self._create_volume()
+        volume = tests_utils.create_volume(self.context, **self.volume_params)
         snapshot = self._create_snapshot(volume['id'])
         self.assertEquals(snapshot['display_name'], None)
         # use volume.api to update name
@@ -1325,7 +1334,8 @@ class VolumeTestCase(BaseVolumeTestCase):
     def test_extend_volume(self):
         """Test volume can be extended at API level."""
         # create a volume and assign to host
-        volume = self._create_volume(2)
+        volume = tests_utils.create_volume(self.context, size=2,
+                                           status='creating', host=CONF.host)
         self.volume.create_volume(self.context, volume['id'])
         volume['status'] = 'in-use'
         volume['host'] = 'fakehost'
@@ -1377,7 +1387,8 @@ class VolumeTestCase(BaseVolumeTestCase):
         def fake_extend_exc(volume, new_size):
             raise exception.CinderException('fake exception')
 
-        volume = self._create_volume(2)
+        volume = tests_utils.create_volume(self.context, size=2,
+                                           status='creating', host=CONF.host)
         self.volume.create_volume(self.context, volume['id'])
 
         # Test quota exceeded
@@ -1435,7 +1446,8 @@ class VolumeTestCase(BaseVolumeTestCase):
                        fake_reschedule_or_error)
         self.stubs.Set(self.volume.driver, 'create_volume', fake_create_volume)
 
-        volume_src = self._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'])
 
@@ -1446,9 +1458,12 @@ class VolumeTestCase(BaseVolumeTestCase):
 
         self.stubs.Set(self.volume.driver, 'create_cloned_volume',
                        fake_create_cloned_volume)
-        volume_src = self._create_volume()
+        volume_src = tests_utils.create_volume(self.context,
+                                               **self.volume_params)
         self.volume.create_volume(self.context, volume_src['id'])
-        volume_dst = self._create_volume(source_volid=volume_src['id'])
+        volume_dst = tests_utils.create_volume(self.context,
+                                               source_volid=volume_src['id'],
+                                               **self.volume_params)
         self.volume.create_volume(self.context, volume_dst['id'],
                                   source_volid=volume_src['id'])
         self.assertEqual('available',
@@ -1469,7 +1484,9 @@ class VolumeTestCase(BaseVolumeTestCase):
                        'list_availability_zones',
                        fake_list_availability_zones)
 
-        volume_src = self._create_volume(availability_zone='az2')
+        volume_src = tests_utils.create_volume(self.context,
+                                               availability_zone='az2',
+                                               **self.volume_params)
         self.volume.create_volume(self.context, volume_src['id'])
 
         volume_src = db.volume_get(self.context, volume_src['id'])
@@ -1499,7 +1516,9 @@ class VolumeTestCase(BaseVolumeTestCase):
                        fake_create_cloned_volume)
         volume_src = self._create_volume_from_image()
         self.volume.create_volume(self.context, volume_src['id'])
-        volume_dst = self._create_volume(source_volid=volume_src['id'])
+        volume_dst = tests_utils.create_volume(self.context,
+                                               source_volid=volume_src['id'],
+                                               **self.volume_params)
         self.volume.create_volume(self.context, volume_dst['id'],
                                   source_volid=volume_src['id'])
         self.assertEqual('available',
@@ -1524,9 +1543,12 @@ class VolumeTestCase(BaseVolumeTestCase):
 
         self.stubs.Set(self.volume.driver, 'create_cloned_volume',
                        fake_error_create_cloned_volume)
-        volume_src = self._create_volume()
+        volume_src = tests_utils.create_volume(self.context,
+                                               **self.volume_params)
         self.volume.create_volume(self.context, volume_src['id'])
-        volume_dst = self._create_volume(0, source_volid=volume_src['id'])
+        volume_dst = tests_utils.create_volume(self.context,
+                                               source_volid=volume_src['id'],
+                                               **self.volume_params)
         self.assertRaises(exception.CinderException,
                           self.volume.create_volume,
                           self.context,
@@ -1567,8 +1589,9 @@ class VolumeTestCase(BaseVolumeTestCase):
         self.stubs.Set(self.volume.driver, 'migrate_volume',
                        lambda x, y, z: (True, {'user_id': 'foo'}))
 
-        volume = self._create_volume(status='available',
-                                     migration_status='migrating')
+        volume = tests_utils.create_volume(self.context, size=0,
+                                           host=CONF.host,
+                                           migration_status='migrating')
         host_obj = {'host': 'newhost', 'capabilities': {}}
         self.volume.migrate_volume(self.context, volume['id'],
                                    host_obj, False)
@@ -1597,7 +1620,8 @@ class VolumeTestCase(BaseVolumeTestCase):
         self.stubs.Set(volume_rpcapi.VolumeAPI, 'delete_volume',
                        fake_delete_volume_rpc)
 
-        volume = self._create_volume(status='available')
+        volume = tests_utils.create_volume(self.context, size=0,
+                                           host=CONF.host)
         host_obj = {'host': 'newhost', 'capabilities': {}}
         self.volume.migrate_volume(self.context, volume['id'],
                                    host_obj, True)
@@ -1608,7 +1632,9 @@ class VolumeTestCase(BaseVolumeTestCase):
     def test_update_volume_readonly_flag(self):
         """Test volume readonly flag can be updated at API level."""
         # create a volume and assign to host
-        volume = self._create_volume(admin_metadata={'readonly': 'True'})
+        volume = tests_utils.create_volume(self.context,
+                                           admin_metadata={'readonly': 'True'},
+                                           **self.volume_params)
         self.volume.create_volume(self.context, volume['id'])
         volume['status'] = 'in-use'
 
index 034fe51d68796ab28f10ead82e4b9163124af180..75cf8be48b3b84e536937f473e2a1357d36f5da1 100644 (file)
@@ -22,6 +22,7 @@ from cinder import db
 from cinder import exception
 from cinder.openstack.common import log as logging
 from cinder import test
+from cinder.tests import utils
 from cinder.transfer import api as transfer_api
 
 
@@ -34,27 +35,12 @@ class VolumeTransferTestCase(test.TestCase):
         super(VolumeTransferTestCase, self).setUp()
         self.ctxt = context.RequestContext(user_id='user_id',
                                            project_id='project_id')
-
-    def _create_volume(self, volume_id, status='available',
-                       user_id=None, project_id=None):
-        if user_id is None:
-            user_id = self.ctxt.user_id
-        if project_id is None:
-            project_id = self.ctxt.project_id
-        vol = {'id': volume_id,
-               'updated_at': datetime.datetime(1, 1, 1, 1, 1, 1),
-               'user_id': user_id,
-               'project_id': project_id,
-               'display_name': 'Display Name',
-               'display_description': 'Display Description',
-               'size': 1,
-               'status': status}
-        volume = db.volume_create(self.ctxt, vol)
-        return volume
+        self.updated_at = datetime.datetime(1, 1, 1, 1, 1, 1)
 
     def test_transfer_volume_create_delete(self):
         tx_api = transfer_api.API()
-        volume = self._create_volume('1')
+        volume = utils.create_volume(self.ctxt, id='1',
+                                     updated_at=self.updated_at)
         response = tx_api.create(self.ctxt, '1', 'Description')
         volume = db.volume_get(self.ctxt, '1')
         self.assertEquals('awaiting-transfer', volume['status'],
@@ -67,7 +53,8 @@ class VolumeTransferTestCase(test.TestCase):
 
     def test_transfer_invalid_volume(self):
         tx_api = transfer_api.API()
-        volume = self._create_volume('1', status='in-use')
+        volume = utils.create_volume(self.ctxt, id='1', status='in-use',
+                                     updated_at=self.updated_at)
         self.assertRaises(exception.InvalidVolume,
                           tx_api.create,
                           self.ctxt, '1', 'Description')
@@ -77,7 +64,8 @@ class VolumeTransferTestCase(test.TestCase):
 
     def test_transfer_accept(self):
         tx_api = transfer_api.API()
-        volume = self._create_volume('1')
+        volume = utils.create_volume(self.ctxt, id='1',
+                                     updated_at=self.updated_at)
         transfer = tx_api.create(self.ctxt, '1', 'Description')
         volume = db.volume_get(self.ctxt, '1')
         self.assertEquals('awaiting-transfer', volume['status'],
@@ -115,7 +103,8 @@ class VolumeTransferTestCase(test.TestCase):
 
     def test_transfer_get(self):
         tx_api = transfer_api.API()
-        volume = self._create_volume('1')
+        volume = utils.create_volume(self.ctxt, id='1',
+                                     updated_at=self.updated_at)
         transfer = tx_api.create(self.ctxt, volume['id'], 'Description')
         t = tx_api.get(self.ctxt, transfer['id'])
         self.assertEquals(t['id'], transfer['id'], 'Unexpected transfer id')