Replace cinder create volume with shade client. 05/55405/14
authorShobhi Jain <shobhi.jain@intel.com>
Fri, 6 Apr 2018 13:44:33 +0000 (14:44 +0100)
committerShobhi Jain <shobhi.jain@intel.com>
Tue, 8 May 2018 08:21:16 +0000 (08:21 +0000)
Adds get_volume function.
Function create volume now uses shade client.

JIRA: YARDSTICK-891

Change-Id: I0b2fae5f2cf52eaf2e4a0062c858d49bc4ce9ccd
Signed-off-by: Shobhi Jain <shobhi.jain@intel.com>
yardstick/benchmark/scenarios/lib/create_volume.py
yardstick/common/exceptions.py
yardstick/common/openstack_utils.py
yardstick/tests/unit/benchmark/scenarios/lib/test_create_volume.py
yardstick/tests/unit/common/test_openstack_utils.py

index df523a5..b667490 100644 (file)
@@ -7,14 +7,12 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
-from __future__ import print_function
-from __future__ import absolute_import
-
 import time
 import logging
 
 from yardstick.benchmark.scenarios import base
-import yardstick.common.openstack_utils as op_utils
+from yardstick.common import openstack_utils
+from yardstick.common import exceptions
 
 LOG = logging.getLogger(__name__)
 
@@ -27,15 +25,16 @@ class CreateVolume(base.Scenario):
     def __init__(self, scenario_cfg, context_cfg):
         self.scenario_cfg = scenario_cfg
         self.context_cfg = context_cfg
-        self.options = self.scenario_cfg['options']
+        self.options = self.scenario_cfg["options"]
 
-        self.volume_name = self.options.get("volume_name", "TestVolume")
-        self.volume_size = self.options.get("size", 100)
-        self.image_name = self.options.get("image", None)
-        self.image_id = None
+        self.size = self.options["size_gb"]
+        self.wait = self.options.get("wait", True)
+        self.timeout = self.options.get("timeout")
+        self.image = self.options.get("image")
+        self.name = self.options.get("name")
+        self.description = self.options.get("description")
 
-        self.glance_client = op_utils.get_glance_client()
-        self.cinder_client = op_utils.get_cinder_client()
+        self.shade_client = openstack_utils.get_shade_client()
 
         self.setup_done = False
 
@@ -44,27 +43,29 @@ class CreateVolume(base.Scenario):
 
         self.setup_done = True
 
-    def run(self):
+    def run(self, result):
         """execute the test"""
 
         if not self.setup_done:
             self.setup()
 
-        self.image_id = op_utils.get_image_id(self.glance_client,
-                                              self.image_name)
+        volume = openstack_utils.create_volume(
+            self.shade_client, self.size, wait=self.wait, timeout=self.timeout,
+            image=self.image, name=self.name, description=self.description)
 
-        volume = op_utils.create_volume(self.cinder_client, self.volume_name,
-                                        self.volume_size, self.image_id)
+        if not volume:
+            result.update({"volume_create": 0})
+            LOG.error("Create volume failed!")
+            raise exceptions.ScenarioCreateVolumeError
 
-        status = volume.status
-        while(status == 'creating' or status == 'downloading'):
+        status = volume["status"]
+        while status == "creating" or status == "downloading":
             LOG.info("Volume status is: %s", status)
             time.sleep(5)
-            volume = op_utils.get_volume_by_name(self.volume_name)
-            status = volume.status
-
+            volume = openstack_utils.get_volume(self.shade_client, self.name)
+            status = volume["status"]
+        result.update({"volume_create": 1})
         LOG.info("Create volume successful!")
-
-        values = [volume.id]
-        keys = self.scenario_cfg.get('output', '').split()
+        values = [volume["id"]]
+        keys = self.scenario_cfg.get("output", '').split()
         return self._push_to_outputs(keys, values)
index 0492094..1aa734a 100644 (file)
@@ -230,3 +230,7 @@ class ScenarioGetServerError(YardstickException):
 
 class ScenarioGetFlavorError(YardstickException):
     message = 'Nova Get Falvor Scenario failed'
+
+
+class ScenarioCreateVolumeError(YardstickException):
+    message = 'Cinder Create Volume Scenario failed'
index 5a83ddb..026ec62 100644 (file)
@@ -788,21 +788,38 @@ def get_volume_id(shade_client, volume_name):
     return shade_client.get_volume_id(volume_name)
 
 
-def create_volume(cinder_client, volume_name, volume_size,
-                  volume_image=False):    # pragma: no cover
+def get_volume(shade_client, name_or_id, filters=None):
+    """Get a volume by name or ID.
+
+    :param name_or_id: Name or ID of the volume.
+    :param filters: A dictionary of meta data to use for further filtering.
+
+    :returns: A volume ``munch.Munch`` or None if no matching volume is found.
+    """
+    return shade_client.get_volume(name_or_id, filters=filters)
+
+
+def create_volume(shade_client, size, wait=True, timeout=None,
+                  image=None, **kwargs):
+    """Create a volume.
+
+    :param size: Size, in GB of the volume to create.
+    :param name: (optional) Name for the volume.
+    :param description: (optional) Name for the volume.
+    :param wait: If true, waits for volume to be created.
+    :param timeout: Seconds to wait for volume creation. None is forever.
+    :param image: (optional) Image name, ID or object from which to create
+                  the volume.
+
+    :returns: The created volume object.
+
+    """
     try:
-        if volume_image:
-            volume = cinder_client.volumes.create(name=volume_name,
-                                                  size=volume_size,
-                                                  imageRef=volume_image)
-        else:
-            volume = cinder_client.volumes.create(name=volume_name,
-                                                  size=volume_size)
-        return volume
-    except Exception:  # pylint: disable=broad-except
-        log.exception("Error [create_volume(cinder_client, %s)]",
-                      (volume_name, volume_size))
-        return None
+        return shade_client.create_volume(size, wait=wait, timeout=timeout,
+                                          image=image, **kwargs)
+    except (exc.OpenStackCloudException, exc.OpenStackCloudTimeout) as op_exc:
+        log.error("Failed to create_volume(shade_client). "
+                  "Exception message: %s", op_exc.orig_message)
 
 
 def delete_volume(cinder_client, volume_id,
index 30333dd..f91d2c3 100644 (file)
@@ -6,95 +6,53 @@
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
-import mock
+from oslo_utils import uuidutils
 import unittest
+import mock
 
+from yardstick.common import openstack_utils
+from yardstick.common import exceptions
 from yardstick.benchmark.scenarios.lib import create_volume
 
 
 class CreateVolumeTestCase(unittest.TestCase):
 
     def setUp(self):
-        self._mock_cinder_client = mock.patch(
-            'yardstick.common.openstack_utils.get_cinder_client')
-        self.mock_cinder_client = self._mock_cinder_client.start()
-        self._mock_glance_client = mock.patch(
-            'yardstick.common.openstack_utils.get_glance_client')
-        self.mock_glance_client = self._mock_glance_client.start()
-        self.addCleanup(self._stop_mock)
-
-        self.scenario_cfg = {
-            "options" :
-                {
-                    'volume_name': 'yardstick_test_volume_01',
-                    'size': '256',
-                    'image': 'cirros-0.3.5'
-                }
-            }
 
-        self.scenario = create_volume.CreateVolume(
-            scenario_cfg=self.scenario_cfg,
-            context_cfg={})
+        self._mock_create_volume = mock.patch.object(
+            openstack_utils, 'create_volume')
+        self.mock_create_volume = (
+            self._mock_create_volume.start())
+        self._mock_get_shade_client = mock.patch.object(
+            openstack_utils, 'get_shade_client')
+        self.mock_get_shade_client = self._mock_get_shade_client.start()
+        self._mock_log = mock.patch.object(create_volume, 'LOG')
+        self.mock_log = self._mock_log.start()
+        self.args = {'options': {'size_gb': 1}}
+        self.result = {}
+
+        self.cvolume_obj = create_volume.CreateVolume(self.args, mock.ANY)
+        self.addCleanup(self._stop_mock)
 
     def _stop_mock(self):
-        self._mock_cinder_client.stop()
-        self._mock_glance_client.stop()
-
-    def test_init(self):
-        self.mock_cinder_client.return_value = "All volumes are equal"
-        self.mock_glance_client.return_value = "Images are more equal"
-
-        expected_vol_name = self.scenario_cfg["options"]["volume_name"]
-        expected_vol_size = self.scenario_cfg["options"]["size"]
-        expected_im_name = self.scenario_cfg["options"]["image"]
-        expected_im_id = None
-
-        scenario = create_volume.CreateVolume(
-            scenario_cfg=self.scenario_cfg,
-            context_cfg={})
-
-        self.assertEqual(expected_vol_name, scenario.volume_name)
-        self.assertEqual(expected_vol_size, scenario.volume_size)
-        self.assertEqual(expected_im_name, scenario.image_name)
-        self.assertEqual(expected_im_id, scenario.image_id)
-        self.assertEqual("All volumes are equal", scenario.cinder_client)
-        self.assertEqual("Images are more equal", scenario.glance_client)
-
-    def test_setup(self):
-        self.assertFalse(self.scenario.setup_done)
-        self.scenario.setup()
-        self.assertTrue(self.scenario.setup_done)
-
-    @mock.patch('yardstick.common.openstack_utils.create_volume')
-    @mock.patch('yardstick.common.openstack_utils.get_image_id')
-    def test_run(self, mock_image_id, mock_create_volume):
-        self.scenario.run()
-
-        mock_image_id.assert_called_once()
-        mock_create_volume.assert_called_once()
-
-    @mock.patch.object(create_volume.CreateVolume, 'setup')
-    def test_run_no_setup(self, scenario_setup):
-        self.scenario.setup_done = False
-        self.scenario.run()
-        scenario_setup.assert_called_once()
-
-    @mock.patch('yardstick.common.openstack_utils.create_volume')
-    @mock.patch('yardstick.common.openstack_utils.get_image_id')
-    @mock.patch('yardstick.common.openstack_utils.get_cinder_client')
-    @mock.patch('yardstick.common.openstack_utils.get_glance_client')
-    def test_create_volume(self, mock_get_glance_client,
-                           mock_get_cinder_client, mock_image_id,
-                           mock_create_volume):
-        options = {
-            'volume_name': 'yardstick_test_volume_01',
-            'size': '256',
-            'image': 'cirros-0.3.5'
-        }
-        args = {"options": options}
-        scenario = create_volume.CreateVolume(args, {})
-        scenario.run()
-        mock_create_volume.assert_called_once()
-        mock_image_id.assert_called_once()
-        mock_get_glance_client.assert_called_once()
-        mock_get_cinder_client.assert_called_once()
+        self._mock_create_volume.stop()
+        self._mock_get_shade_client.stop()
+        self._mock_log.stop()
+
+    def test_run(self):
+        _uuid = uuidutils.generate_uuid()
+        self.cvolume_obj.scenario_cfg = {'output': 'id'}
+        self.mock_create_volume.return_value = {'name': 'yardstick_volume',
+                                                'id': _uuid,
+                                                'status': 'available'}
+        output = self.cvolume_obj.run(self.result)
+        self.assertDictEqual({'volume_create': 1}, self.result)
+        self.assertDictEqual({'id': _uuid}, output)
+        self.mock_log.info.asset_called_once_with('Create volume successful!')
+
+    def test_run_fail(self):
+        self.mock_create_volume.return_value = None
+        with self.assertRaises(exceptions.ScenarioCreateVolumeError):
+            self.cvolume_obj.run(self.result)
+        self.assertDictEqual({'volume_create': 0}, self.result)
+        self.mock_log.error.assert_called_once_with('Create volume failed!')
index cc19d98..bc3c995 100644 (file)
@@ -558,3 +558,47 @@ class GetVolumeIDTestCase(unittest.TestCase):
         output = openstack_utils.get_volume_id(self.mock_shade_client,
                                                'volume_name')
         self.assertIsNone(output)
+
+
+class GetVolumeTestCase(unittest.TestCase):
+
+    def setUp(self):
+        self.mock_shade_client = mock.Mock()
+        self.mock_shade_client.get_volume = mock.Mock()
+
+    def test_get_volume(self):
+        self.mock_shade_client.get_volume.return_value = {'volume'}
+        output = openstack_utils.get_volume(self.mock_shade_client,
+                                            'volume_name_or_id')
+        self.assertEqual({'volume'}, output)
+
+    def test_get_volume_None(self):
+        self.mock_shade_client.get_volume.return_value = None
+        output = openstack_utils.get_volume(self.mock_shade_client,
+                                            'volume_name_or_id')
+        self.assertIsNone(output)
+
+
+class CreateVolumeTestCase(unittest.TestCase):
+
+    def setUp(self):
+        self.mock_shade_client = mock.Mock()
+        self.size = 1
+
+    def test_create_volume(self):
+        self.mock_shade_client.create_volume.return_value = (
+            {'name': 'volume-name', 'size': self.size})
+        output = openstack_utils.create_volume(
+            self.mock_shade_client, self.size)
+        self.assertEqual(
+            {'name': 'volume-name', 'size': self.size},
+            output)
+
+    @mock.patch.object(openstack_utils, 'log')
+    def test_create_volume_fail(self, mock_logger):
+        self.mock_shade_client.create_volume.side_effect = (
+            exc.OpenStackCloudException('error message'))
+        output = openstack_utils.create_volume(self.mock_shade_client,
+                                               self.size)
+        mock_logger.error.assert_called_once()
+        self.assertIsNone(output)