diff options
-rw-r--r-- | yardstick/benchmark/scenarios/lib/delete_image.py | 36 | ||||
-rw-r--r-- | yardstick/common/exceptions.py | 4 | ||||
-rw-r--r-- | yardstick/common/openstack_utils.py | 19 | ||||
-rw-r--r-- | yardstick/tests/unit/benchmark/scenarios/lib/test_delete_image.py | 51 | ||||
-rw-r--r-- | yardstick/tests/unit/common/test_openstack_utils.py | 27 |
5 files changed, 93 insertions, 44 deletions
diff --git a/yardstick/benchmark/scenarios/lib/delete_image.py b/yardstick/benchmark/scenarios/lib/delete_image.py index 0e3a853e5..008f104b2 100644 --- a/yardstick/benchmark/scenarios/lib/delete_image.py +++ b/yardstick/benchmark/scenarios/lib/delete_image.py @@ -7,13 +7,11 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from __future__ import print_function -from __future__ import absolute_import - 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__) @@ -26,12 +24,14 @@ class DeleteImage(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.image_name = self.options.get("image_name", "TestImage") - self.image_id = None + self.image_name_or_id = self.options["name_or_id"] + self.wait = self.options.get("wait", False) + self.timeout = self.options.get("timeout", 3600) + self.delete_objects = self.options.get("delete_objects", True) - self.glance_client = op_utils.get_glance_client() + self.shade_client = openstack_utils.get_shade_client() self.setup_done = False @@ -46,16 +46,14 @@ class DeleteImage(base.Scenario): if not self.setup_done: self.setup() - self.image_id = op_utils.get_image_id(self.glance_client, self.image_name) - LOG.info("Deleting image: %s", self.image_name) - status = op_utils.delete_image(self.glance_client, self.image_id) + status = openstack_utils.delete_image( + self.shade_client, self.image_name_or_id, wait=self.wait, + timeout=self.timeout, delete_objects=self.delete_objects) - if status: - LOG.info("Delete image successful!") - values = [status] - else: - LOG.info("Delete image failed!") - values = [] + if not status: + result.update({"delete_image": 0}) + LOG.error("Delete image failed!") + raise exceptions.ScenarioDeleteImageError - keys = self.scenario_cfg.get('output', '').split() - return self._push_to_outputs(keys, values) + result.update({"delete_image": 1}) + LOG.info("Delete image successful!") diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py index faa8688d8..1aeee20fe 100644 --- a/yardstick/common/exceptions.py +++ b/yardstick/common/exceptions.py @@ -274,6 +274,10 @@ class ScenarioCreateImageError(YardstickException): message = 'Glance Create Image Scenario failed' +class ScenarioDeleteImageError(YardstickException): + message = 'Glance Delete Image Scenario failed' + + class IxNetworkClientNotConnected(YardstickException): message = 'IxNetwork client not connected to a TCL server' diff --git a/yardstick/common/openstack_utils.py b/yardstick/common/openstack_utils.py index 56cd4958e..666404779 100644 --- a/yardstick/common/openstack_utils.py +++ b/yardstick/common/openstack_utils.py @@ -709,11 +709,6 @@ def create_security_group_full(shade_client, sg_name, # ********************************************* # GLANCE # ********************************************* -def get_image_id(glance_client, image_name): # pragma: no cover - images = glance_client.images.list() - return next((i.id for i in images if i.name == image_name), None) - - def create_image(shade_client, name, filename=None, container='images', md5=None, sha256=None, disk_format=None, container_format=None, disable_vendor_agent=True, @@ -772,15 +767,17 @@ def create_image(shade_client, name, filename=None, container='images', "Exception message: %s", op_exc.orig_message) -def delete_image(glance_client, image_id): # pragma: no cover +def delete_image(shade_client, name_or_id, wait=False, timeout=3600, + delete_objects=True): try: - glance_client.images.delete(image_id) + return shade_client.delete_image(name_or_id, wait=wait, + timeout=timeout, + delete_objects=delete_objects) - except Exception: # pylint: disable=broad-except - log.exception("Error [delete_flavor(glance_client, %s)]", image_id) + except exc.OpenStackCloudException as op_exc: + log.error("Failed to delete_image(shade_client). " + "Exception message: %s", op_exc.orig_message) return False - else: - return True def list_images(shade_client=None): diff --git a/yardstick/tests/unit/benchmark/scenarios/lib/test_delete_image.py b/yardstick/tests/unit/benchmark/scenarios/lib/test_delete_image.py index e382d46fa..8a1d6d695 100644 --- a/yardstick/tests/unit/benchmark/scenarios/lib/test_delete_image.py +++ b/yardstick/tests/unit/benchmark/scenarios/lib/test_delete_image.py @@ -9,21 +9,44 @@ import unittest import mock -from yardstick.benchmark.scenarios.lib.delete_image import DeleteImage +from yardstick.common import openstack_utils +from yardstick.common import exceptions +from yardstick.benchmark.scenarios.lib import delete_image class DeleteImageTestCase(unittest.TestCase): - @mock.patch('yardstick.common.openstack_utils.delete_image') - @mock.patch('yardstick.common.openstack_utils.get_image_id') - @mock.patch('yardstick.common.openstack_utils.get_glance_client') - def test_delete_image(self, mock_get_glance_client, mock_image_id, mock_delete_image): - options = { - 'image_name': 'yardstick_test_image_01' - } - args = {"options": options} - obj = DeleteImage(args, {}) - obj.run({}) - mock_delete_image.assert_called_once() - mock_image_id.assert_called_once() - mock_get_glance_client.assert_called_once() + def setUp(self): + self._mock_delete_image = mock.patch.object( + openstack_utils, 'delete_image') + self.mock_delete_image = ( + self._mock_delete_image.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(delete_image, 'LOG') + self.mock_log = self._mock_log.start() + self.args = {'options': {'name_or_id': 'yardstick_image'}} + self.result = {} + + self.delimg_obj = delete_image.DeleteImage(self.args, mock.ANY) + + self.addCleanup(self._stop_mock) + + def _stop_mock(self): + self._mock_delete_image.stop() + self._mock_get_shade_client.stop() + self._mock_log.stop() + + def test_run(self): + self.mock_delete_image.return_value = True + self.assertIsNone(self.delimg_obj.run(self.result)) + self.assertEqual({'delete_image': 1}, self.result) + self.mock_log.info.assert_called_once_with('Delete image successful!') + + def test_run_fail(self): + self.mock_delete_image.return_value = False + with self.assertRaises(exceptions.ScenarioDeleteImageError): + self.delimg_obj.run(self.result) + self.assertEqual({'delete_image': 0}, self.result) + self.mock_log.error.assert_called_once_with('Delete image failed!') diff --git a/yardstick/tests/unit/common/test_openstack_utils.py b/yardstick/tests/unit/common/test_openstack_utils.py index 9b23c65b8..c9f9129df 100644 --- a/yardstick/tests/unit/common/test_openstack_utils.py +++ b/yardstick/tests/unit/common/test_openstack_utils.py @@ -667,3 +667,30 @@ class CreateImageTestCase(unittest.TestCase): self.name) mock_logger.error.assert_called_once() self.assertIsNone(output) + + +class DeleteImageTestCase(unittest.TestCase): + + def test_delete_image(self): + self.mock_shade_client = mock.Mock() + self.mock_shade_client.delete_image.return_value = True + output = openstack_utils.delete_image(self.mock_shade_client, + 'image_name_or_id') + self.assertTrue(output) + + def test_delete_image_fail(self): + self.mock_shade_client = mock.Mock() + self.mock_shade_client.delete_image.return_value = False + output = openstack_utils.delete_image(self.mock_shade_client, + 'image_name_or_id') + self.assertFalse(output) + + @mock.patch.object(openstack_utils, 'log') + def test_delete_image_exception(self, mock_logger): + self.mock_shade_client = mock.Mock() + self.mock_shade_client.delete_image.side_effect = ( + exc.OpenStackCloudException('error message')) + output = openstack_utils.delete_image(self.mock_shade_client, + 'image_name_or_id') + mock_logger.error.assert_called_once() + self.assertFalse(output) |