Add test for cinder_img_volume_type image metadata

When a volume is created from an image with a cinder_img_volume_type
image property, and a volume_type is not specified in the volume-
create call, the resulting volume should be the specified volume_type,
not the default type.

Change-Id: If8edd24ba5183522ebe8202bbf6b62b41b3febe9
diff --git a/cinder_tempest_plugin/api/volume/base.py b/cinder_tempest_plugin/api/volume/base.py
index 9abc5db..418fd33 100644
--- a/cinder_tempest_plugin/api/volume/base.py
+++ b/cinder_tempest_plugin/api/volume/base.py
@@ -165,3 +165,37 @@
 
         cls.admin_volume_types_client = cls.os_admin.volume_types_client_latest
         cls.admin_backups_client = cls.os_admin.backups_client_latest
+        cls.admin_volumes_client = cls.os_admin.volumes_client_latest
+
+    @classmethod
+    def create_volume_type(cls, name=None, **kwargs):
+        """Create a test volume-type"""
+
+        name = name or data_utils.rand_name(cls.__name__ + '-volume-type')
+        volume_type = cls.admin_volume_types_client.create_volume_type(
+            name=name, **kwargs)['volume_type']
+        cls.addClassResourceCleanup(cls._clear_volume_type, volume_type)
+        return volume_type
+
+    @classmethod
+    def _clear_volume_type(cls, volume_type):
+        # If image caching is enabled, we must delete the cached volume
+        # before cinder will allow us to delete the volume_type.  This function
+        # solves that problem by taking the brute-force approach of deleting
+        # any volumes of this volume_type that exist *no matter what project
+        # they are in*.  Since this won't happen until the teardown of the
+        # test class, that should be OK.
+        type_id = volume_type['id']
+        type_name = volume_type['name']
+
+        volumes = cls.admin_volumes_client.list_volumes(
+            detail=True, params={'all_tenants': 1})['volumes']
+        for volume in [v for v in volumes if v['volume_type'] == type_name]:
+            test_utils.call_and_ignore_notfound_exc(
+                cls.admin_volumes_client.delete_volume, volume['id'])
+            cls.admin_volumes_client.wait_for_resource_deletion(volume['id'])
+
+        test_utils.call_and_ignore_notfound_exc(
+            cls.admin_volume_types_client.delete_volume_type, type_id)
+        test_utils.call_and_ignore_notfound_exc(
+            cls.admin_volume_types_client.wait_for_resource_deletion, type_id)
diff --git a/cinder_tempest_plugin/api/volume/test_create_from_image.py b/cinder_tempest_plugin/api/volume/test_create_from_image.py
index 02fbd24..dc296c0 100644
--- a/cinder_tempest_plugin/api/volume/test_create_from_image.py
+++ b/cinder_tempest_plugin/api/volume/test_create_from_image.py
@@ -10,6 +10,8 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+import io
+
 from tempest.common import waiters
 from tempest import config
 from tempest.lib.common.utils import data_utils
@@ -77,3 +79,84 @@
             waiters.wait_for_volume_resource_status(self.volumes_client,
                                                     v['id'],
                                                     'available')
+
+
+class VolumeAndVolumeTypeFromImageTest(base.BaseVolumeAdminTest):
+    # needs AdminTest as superclass to manipulate volume_types
+
+    @classmethod
+    def skip_checks(cls):
+        super(VolumeAndVolumeTypeFromImageTest, cls).skip_checks()
+        if not CONF.service_available.glance:
+            raise cls.skipException("Glance service is disabled")
+
+    @classmethod
+    def create_image_with_data(cls, **kwargs):
+        # we do this as a class method so we can use the
+        # addClassResourceCleanup functionality of tempest.test.BaseTestCase
+        images_client = cls.os_primary.image_client_v2
+        if 'min_disk' not in kwargs:
+            kwargs['min_disk'] = 1
+        response = images_client.create_image(**kwargs)
+        image_id = response['id']
+        cls.addClassResourceCleanup(
+            images_client.wait_for_resource_deletion, image_id)
+        cls.addClassResourceCleanup(
+            test_utils.call_and_ignore_notfound_exc,
+            images_client.delete_image, image_id)
+
+        # upload "data" to image
+        image_file = io.BytesIO(data_utils.random_bytes(size=1024))
+        images_client.store_image_file(image_id, image_file)
+
+        waiters.wait_for_image_status(images_client, image_id, 'active')
+        image = images_client.show_image(image_id)
+        return image
+
+    @decorators.idempotent_id('6e9266ff-a917-4dd5-aa4a-c36e59e7a2a6')
+    def test_create_from_image_with_volume_type_image_property(self):
+        """Verify that the cinder_img_volume_type image property works.
+
+        When a volume is created from an image containing the
+        cinder_img_volume_type property and no volume_type is specified
+        in the volume-create request, the volume_type of the resulting
+        volume should be the one specified by the image property.
+        """
+
+        volume_type_meta = 'cinder_img_volume_type'
+        volume_type_name = 'vol-type-for-6e9266ff-a917-4dd5-aa4a-c36e59e7a2a6'
+        description = ('Generic volume_type for test '
+                       '6e9266ff-a917-4dd5-aa4a-c36e59e7a2a6')
+        proto = CONF.volume.storage_protocol
+        vendor = CONF.volume.vendor_name
+        extra_specs = {"storage_protocol": proto,
+                       "vendor_name": vendor}
+        kwargs = {'description': description,
+                  'extra_specs': extra_specs,
+                  'os-volume-type-access:is_public': True}
+        volume_type = self.create_volume_type(name=volume_type_name,
+                                              **kwargs)
+        # quick sanity check
+        self.assertEqual(volume_type_name, volume_type['name'])
+
+        # create an image in glance
+        kwargs = {'disk_format': 'raw',
+                  'container_format': 'bare',
+                  'name': ('image-for-test-'
+                           '6e9266ff-a917-4dd5-aa4a-c36e59e7a2a6'),
+                  'visibility': 'private',
+                  volume_type_meta: volume_type_name}
+        image = self.create_image_with_data(**kwargs)
+        # quick sanity check
+        self.assertEqual(volume_type_name, image[volume_type_meta])
+
+        # create volume from image
+        kwargs = {'name': ('volume-for-test-'
+                           '6e9266ff-a917-4dd5-aa4a-c36e59e7a2a6'),
+                  'imageRef': image['id']}
+        # this is the whole point of the test, so make sure this is true
+        self.assertNotIn('volume_type', kwargs)
+        volume = self.create_volume(**kwargs)
+
+        found_volume_type = volume['volume_type']
+        self.assertEqual(volume_type_name, found_volume_type)