Replace neutron router deletion with shade. 61/52361/22
authorShobhi Jain <shobhi.jain@intel.com>
Mon, 19 Feb 2018 16:50:51 +0000 (16:50 +0000)
committerShobhi Jain <shobhi.jain@intel.com>
Mon, 12 Mar 2018 10:58:47 +0000 (10:58 +0000)
Function delete_neutron_router now uses shade client instead of neutron
client.

JIRA: YARDSTICK-890

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

index 358fd40..5e7467b 100644 (file)
@@ -7,13 +7,12 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
-from __future__ import print_function
-from __future__ import absolute_import
-
 import logging
 
+from yardstick.common import openstack_utils
+from yardstick.common import exceptions
 from yardstick.benchmark.scenarios import base
-import yardstick.common.openstack_utils as op_utils
+
 
 LOG = logging.getLogger(__name__)
 
@@ -28,9 +27,9 @@ class DeleteRouter(base.Scenario):
         self.context_cfg = context_cfg
         self.options = self.scenario_cfg['options']
 
-        self.router_id = self.options.get("router_id", None)
+        self.router_id = self.options["router_id"]
 
-        self.neutron_client = op_utils.get_neutron_client()
+        self.shade_client = openstack_utils.get_shade_client()
 
         self.setup_done = False
 
@@ -45,11 +44,12 @@ class DeleteRouter(base.Scenario):
         if not self.setup_done:
             self.setup()
 
-        status = op_utils.delete_neutron_router(self.neutron_client,
-                                                router_id=self.router_id)
-        if status:
-            result.update({"delete_router": 1})
-            LOG.info("Delete router successful!")
-        else:
+        status = openstack_utils.delete_neutron_router(self.shade_client,
+                                                       self.router_id)
+        if not status:
             result.update({"delete_router": 0})
             LOG.error("Delete router failed!")
+            raise exceptions.ScenarioDeleteRouterError
+
+        result.update({"delete_router": 1})
+        LOG.info("Delete router successful!")
index 4245ce1..a0edd3b 100644 (file)
@@ -108,3 +108,7 @@ class ScenarioCreateNetworkError(YardstickException):
 
 class ScenarioCreateSubnetError(YardstickException):
     message = 'Create Neutron Subnet Scenario failed'
+
+
+class ScenarioDeleteRouterError(YardstickException):
+    message = 'Delete Neutron Router Scenario failed'
index 0421346..84bfbbb 100644 (file)
@@ -528,13 +528,12 @@ def create_neutron_router(neutron_client, json_body):      # pragma: no cover
         raise Exception("operation error")
 
 
-def delete_neutron_router(neutron_client, router_id):      # pragma: no cover
+def delete_neutron_router(shade_client, router_id):
     try:
-        neutron_client.delete_router(router=router_id)
-        return True
-    except Exception:  # pylint: disable=broad-except
-        log.error("Error [delete_neutron_router(neutron_client, '%s')]",
-                  router_id)
+        return shade_client.delete_router(router_id)
+    except exc.OpenStackCloudException as o_exc:
+        log.error("Error [delete_neutron_router(shade_client, '%s')]. "
+                  "Exception message: %s", router_id, o_exc.orig_message)
         return False
 
 
index d075108..b76100f 100644 (file)
@@ -6,22 +6,49 @@
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
+
+from oslo_utils import uuidutils
 import unittest
 import mock
 
-from yardstick.benchmark.scenarios.lib.delete_router import DeleteRouter
+from yardstick.common import openstack_utils
+from yardstick.common import exceptions
+from yardstick.benchmark.scenarios.lib import delete_router
 
 
 class DeleteRouterTestCase(unittest.TestCase):
 
-    @mock.patch('yardstick.common.openstack_utils.get_neutron_client')
-    @mock.patch('yardstick.common.openstack_utils.delete_neutron_router')
-    def test_delete_router(self, mock_get_neutron_client, mock_delete_neutron_router):
-        options = {
-            'router_id': '123-123-123'
-        }
-        args = {"options": options}
-        obj = DeleteRouter(args, {})
-        obj.run({})
-        mock_get_neutron_client.assert_called_once()
-        mock_delete_neutron_router.assert_called_once()
+    def setUp(self):
+        self._mock_delete_neutron_router = mock.patch.object(
+            openstack_utils, 'delete_neutron_router')
+        self.mock_delete_neutron_router = (
+            self._mock_delete_neutron_router.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_router, 'LOG')
+        self.mock_log = self._mock_log.start()
+        self.args = {'options': {'router_id': uuidutils.generate_uuid()}}
+        self.result = {"delete_router": 0}
+
+        self._del_obj = delete_router.DeleteRouter(self.args, mock.ANY)
+
+        self.addCleanup(self._stop_mock)
+
+    def _stop_mock(self):
+        self._mock_delete_neutron_router.stop()
+        self._mock_get_shade_client.stop()
+        self._mock_log.stop()
+
+    def test_run(self):
+        self.mock_delete_neutron_router.return_value = True
+        self.assertIsNone(self._del_obj.run(self.result))
+        self.assertEqual({"delete_router": 1}, self.result)
+        self.mock_log.info.assert_called_once_with("Delete router successful!")
+
+    def test_run_fail(self):
+        self.mock_delete_neutron_router.return_value = False
+        with self.assertRaises(exceptions.ScenarioDeleteRouterError):
+            self._del_obj.run(self.result)
+        self.assertEqual({"delete_router": 0}, self.result)
+        self.mock_log.error.assert_called_once_with("Delete router failed!")
index cf5c206..c6b0f46 100644 (file)
@@ -133,3 +133,31 @@ class CreateNeutronSubnetTestCase(unittest.TestCase):
             self.mock_shade_client, self.network_name_or_id)
         mock_logger.error.assert_called_once()
         self.assertIsNone(output)
+
+
+class DeleteNeutronRouterTestCase(unittest.TestCase):
+
+    def setUp(self):
+        self.mock_shade_client = mock.Mock()
+        self.mock_shade_client.delete_router = mock.Mock()
+
+    def test_delete_neutron_router(self):
+        self.mock_shade_client.delete_router.return_value = True
+        output = openstack_utils.delete_neutron_router(self.mock_shade_client,
+                                                       'router_id')
+        self.assertTrue(output)
+
+    def test_delete_neutron_router_fail(self):
+        self.mock_shade_client.delete_router.return_value = False
+        output = openstack_utils.delete_neutron_router(self.mock_shade_client,
+                                                       'router_id')
+        self.assertFalse(output)
+
+    @mock.patch.object(openstack_utils, 'log')
+    def test_delete_neutron_router_exception(self, mock_logger):
+        self.mock_shade_client.delete_router.side_effect = (
+            exc.OpenStackCloudException('error message'))
+        output = openstack_utils.delete_neutron_router(self.mock_shade_client,
+                                                       'router_id')
+        mock_logger.error.assert_called_once()
+        self.assertFalse(output)