Replace neutron floating ip deletion with shade. 23/53423/11
authorShobhi Jain <shobhi.jain@intel.com>
Tue, 27 Feb 2018 13:19:00 +0000 (13:19 +0000)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Tue, 27 Mar 2018 15:17:08 +0000 (15:17 +0000)
Function delete_floating_ip now uses shade client.

JIRA: YARDSTICK-890

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

index 4314952..a35445f 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,10 @@ class DeleteFloatingIp(base.Scenario):
         self.context_cfg = context_cfg
         self.options = self.scenario_cfg['options']
 
-        self.floating_ip_id = self.options.get("floating_ip_id", None)
+        self.floating_ip_id = self.options["floating_ip_id"]
+        self.retry = self.options.get("retry", 1)
 
-        self.nova_client = op_utils.get_nova_client()
+        self.shade_client = openstack_utils.get_shade_client()
         self.setup_done = False
 
     def setup(self):
@@ -44,11 +44,13 @@ class DeleteFloatingIp(base.Scenario):
         if not self.setup_done:
             self.setup()
 
-        status = op_utils.delete_floating_ip(nova_client=self.nova_client,
-                                             floatingip_id=self.floating_ip_id)
-        if status:
-            result.update({"delete_floating_ip": 1})
-            LOG.info("Delete floating ip successful!")
-        else:
+        status = openstack_utils.delete_floating_ip(
+            self.shade_client, self.floating_ip_id,
+            retry=self.retry)
+        if not status:
             result.update({"delete_floating_ip": 0})
             LOG.error("Delete floating ip failed!")
+            raise exceptions.ScenarioDeleteFloatingIPError
+
+        result.update({"delete_floating_ip": 1})
+        LOG.info("Delete floating ip successful!")
index 402b5b9..43d1eb5 100644 (file)
@@ -132,3 +132,7 @@ class ScenarioRemoveRouterIntError(YardstickException):
 
 class ScenarioCreateFloatingIPError(YardstickException):
     message = 'Create Neutron Floating IP Scenario failed'
+
+
+class ScenarioDeleteFloatingIPError(YardstickException):
+    message = 'Delete Neutron Floating IP Scenario failed'
index 591a1b3..a6b53a6 100644 (file)
@@ -615,13 +615,13 @@ def create_floating_ip(shade_client, network_name_or_id=None, server=None,
                   "Exception message: %s", o_exc.orig_message)
 
 
-def delete_floating_ip(nova_client, floatingip_id):      # pragma: no cover
+def delete_floating_ip(shade_client, floating_ip_id, retry=1):
     try:
-        nova_client.floating_ips.delete(floatingip_id)
-        return True
-    except Exception:  # pylint: disable=broad-except
-        log.error("Error [delete_floating_ip(nova_client, '%s')]",
-                  floatingip_id)
+        return shade_client.delete_floating_ip(floating_ip_id=floating_ip_id,
+                                               retry=retry)
+    except exc.OpenStackCloudException as o_exc:
+        log.error("Error [delete_floating_ip(shade_client,'%s')]. "
+                  "Exception message: %s", floating_ip_id, o_exc.orig_message)
         return False
 
 
index 3185ec5..45a39eb 100644 (file)
@@ -6,22 +6,50 @@
 # 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_floating_ip import DeleteFloatingIp
+from yardstick.common import openstack_utils
+from yardstick.common import exceptions
+from yardstick.benchmark.scenarios.lib import delete_floating_ip
 
 
 class DeleteFloatingIpTestCase(unittest.TestCase):
 
-    @mock.patch('yardstick.common.openstack_utils.get_nova_client')
-    @mock.patch('yardstick.common.openstack_utils.delete_floating_ip')
-    def test_delete_floating_ip(self, mock_get_nova_client, mock_delete_floating_ip):
-        options = {
-            'floating_ip_id': '123-123-123'
-        }
-        args = {"options": options}
-        obj = DeleteFloatingIp(args, {})
-        obj.run({})
-        mock_get_nova_client.assert_called_once()
-        mock_delete_floating_ip.assert_called_once()
+    def setUp(self):
+        self._mock_delete_floating_ip = mock.patch.object(
+            openstack_utils, 'delete_floating_ip')
+        self.mock_delete_floating_ip = self._mock_delete_floating_ip.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_floating_ip, 'LOG')
+        self.mock_log = self._mock_log.start()
+        self.args = {'options': {'floating_ip_id': uuidutils.generate_uuid()}}
+        self.result = {}
+
+        self.del_obj = delete_floating_ip.DeleteFloatingIp(
+            self.args, mock.ANY)
+
+        self.addCleanup(self._stop_mock)
+
+    def _stop_mock(self):
+        self._mock_delete_floating_ip.stop()
+        self._mock_get_shade_client.stop()
+        self._mock_log.stop()
+
+    def test_run(self):
+        self.mock_delete_floating_ip.return_value = True
+        self.assertIsNone(self.del_obj.run(self.result))
+        self.assertEqual({"delete_floating_ip": 1}, self.result)
+        self.mock_log.info.assert_called_once_with(
+            "Delete floating ip successful!")
+
+    def test_run_fail(self):
+        self.mock_delete_floating_ip.return_value = False
+        with self.assertRaises(exceptions.ScenarioDeleteFloatingIPError):
+            self.del_obj.run(self.result)
+        self.assertEqual({"delete_floating_ip": 0}, self.result)
+        self.mock_log.error.assert_called_once_with(
+            "Delete floating ip failed!")
index eb96aef..b8f85c0 100644 (file)
@@ -210,3 +210,32 @@ class CreateFloatingIpTestCase(unittest.TestCase):
             self.mock_shade_client, self.network_name_or_id)
         mock_logger.error.assert_called_once()
         self.assertIsNone(output)
+
+
+class DeleteFloatingIpTestCase(unittest.TestCase):
+
+    def setUp(self):
+        self.mock_shade_client = mock.Mock()
+        self.floating_ip_id = 'floating_ip_id'
+        self.mock_shade_client.delete_floating_ip = mock.Mock()
+
+    def test_delete_floating_ip(self):
+        self.mock_shade_client.delete_floating_ip.return_value = True
+        output = openstack_utils.delete_floating_ip(self.mock_shade_client,
+                                                    'floating_ip_id')
+        self.assertTrue(output)
+
+    def test_delete_floating_ip_fail(self):
+        self.mock_shade_client.delete_floating_ip.return_value = False
+        output = openstack_utils.delete_floating_ip(self.mock_shade_client,
+                                                    'floating_ip_id')
+        self.assertFalse(output)
+
+    @mock.patch.object(openstack_utils, 'log')
+    def test_delete_floating_ip_exception(self, mock_logger):
+        self.mock_shade_client.delete_floating_ip.side_effect = (
+            exc.OpenStackCloudException('error message'))
+        output = openstack_utils.delete_floating_ip(self.mock_shade_client,
+                                                    'floating_ip_id')
+        mock_logger.error.assert_called_once()
+        self.assertFalse(output)