diff options
author | Rex Lee <limingjiang@huawei.com> | 2018-03-21 08:13:02 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2018-03-21 08:13:02 +0000 |
commit | a854a1dde960b630c0c0d854dee9232e4c4e9ac8 (patch) | |
tree | 731a85dbebaae7b8e5ece8559e58eec512b3d19d | |
parent | c4228f65829e86abadb7d93246d0b6de9dab2d51 (diff) | |
parent | 16aa0b60244b742c2b6478cea93b1eebce4c335f (diff) |
Merge "Replace neutron router creation with shade."
-rw-r--r-- | yardstick/benchmark/scenarios/lib/create_router.py | 46 | ||||
-rw-r--r-- | yardstick/common/exceptions.py | 4 | ||||
-rw-r--r-- | yardstick/common/openstack_utils.py | 28 | ||||
-rw-r--r-- | yardstick/tests/unit/benchmark/scenarios/lib/test_create_router.py | 57 | ||||
-rw-r--r-- | yardstick/tests/unit/common/test_openstack_utils.py | 24 |
5 files changed, 115 insertions, 44 deletions
diff --git a/yardstick/benchmark/scenarios/lib/create_router.py b/yardstick/benchmark/scenarios/lib/create_router.py index 9aa57ebb2..34252f603 100644 --- a/yardstick/benchmark/scenarios/lib/create_router.py +++ b/yardstick/benchmark/scenarios/lib/create_router.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__) @@ -28,9 +26,14 @@ class CreateRouter(base.Scenario): self.context_cfg = context_cfg self.options = self.scenario_cfg['options'] - self.openstack = self.options.get("openstack_paras", None) + self.name = self.options.get('name') + self.admin_state_up = self.options.get('admin_state_up', True) + self.ext_gateway_net_id = self.options.get('ext_gateway_net_id') + self.enable_snat = self.options.get('enable_snat') + self.ext_fixed_ips = self.options.get('ext_fixed_ips') + self.project_id = self.options.get('project_id') - self.neutron_client = op_utils.get_neutron_client() + self.shade_client = openstack_utils.get_shade_client() self.setup_done = False @@ -45,22 +48,19 @@ class CreateRouter(base.Scenario): if not self.setup_done: self.setup() - openstack_paras = {'router': self.openstack} - router_id = op_utils.create_neutron_router(self.neutron_client, - openstack_paras) - if router_id: - result.update({"network_create": 1}) - LOG.info("Create router successful!") - else: - result.update({"network_create": 0}) + router_id = openstack_utils.create_neutron_router( + self.shade_client, name=self.name, + admin_state_up=self.admin_state_up, + ext_gateway_net_id=self.ext_gateway_net_id, + enable_snat=self.enable_snat, ext_fixed_ips=self.ext_fixed_ips, + project_id=self.project_id) + if not router_id: + result.update({"router_create": 0}) LOG.error("Create router failed!") + raise exceptions.ScenarioCreateRouterError - check_result = router_id - - try: - keys = self.scenario_cfg.get('output', '').split() - except KeyError: - pass - else: - values = [check_result] - return self._push_to_outputs(keys, values) + result.update({"router_create": 1}) + LOG.info("Create router successful!") + keys = self.scenario_cfg.get('output', '').split() + values = [router_id] + return self._push_to_outputs(keys, values) diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py index 633b36f91..8160c5b0c 100644 --- a/yardstick/common/exceptions.py +++ b/yardstick/common/exceptions.py @@ -120,3 +120,7 @@ class MissingPodInfoError(YardstickException): class UnsupportedPodFormatError(YardstickException): message = 'Failed to load pod info, unsupported format' + + +class ScenarioCreateRouterError(YardstickException): + message = 'Create Neutron Router Scenario failed' diff --git a/yardstick/common/openstack_utils.py b/yardstick/common/openstack_utils.py index 84bfbbbb1..a4fd4e550 100644 --- a/yardstick/common/openstack_utils.py +++ b/yardstick/common/openstack_utils.py @@ -519,13 +519,29 @@ def create_neutron_subnet(shade_client, network_name_or_id, cidr=None, return None -def create_neutron_router(neutron_client, json_body): # pragma: no cover +def create_neutron_router(shade_client, name=None, admin_state_up=True, + ext_gateway_net_id=None, enable_snat=None, + ext_fixed_ips=None, project_id=None): + """Create a logical router. + + :param name:(string) the router name. + :param admin_state_up:(bool) the administrative state of the router. + :param ext_gateway_net_id:(string) network ID for the external gateway. + :param enable_snat:(bool) enable Source NAT (SNAT) attribute. + :param ext_fixed_ips: List of dictionaries of desired IP and/or subnet + on the external network. + :param project_id:(string) project ID for the router. + + :returns:(string) the router id. + """ try: - router = neutron_client.create_router(json_body) - return router['router']['id'] - except Exception: # pylint: disable=broad-except - log.error("Error [create_neutron_router(neutron_client)]") - raise Exception("operation error") + router = shade_client.create_router( + name, admin_state_up, ext_gateway_net_id, enable_snat, + ext_fixed_ips, project_id) + return router['id'] + except exc.OpenStackCloudException as o_exc: + log.error("Error [create_neutron_router(shade_client)]. " + "Exception message: %s", o_exc.orig_message) def delete_neutron_router(shade_client, router_id): diff --git a/yardstick/tests/unit/benchmark/scenarios/lib/test_create_router.py b/yardstick/tests/unit/benchmark/scenarios/lib/test_create_router.py index 3469a2a06..8d6f119ab 100644 --- a/yardstick/tests/unit/benchmark/scenarios/lib/test_create_router.py +++ b/yardstick/tests/unit/benchmark/scenarios/lib/test_create_router.py @@ -6,25 +6,52 @@ # 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.create_router import CreateRouter +from yardstick.common import openstack_utils +from yardstick.common import exceptions +from yardstick.benchmark.scenarios.lib import create_router class CreateRouterTestCase(unittest.TestCase): - @mock.patch('yardstick.common.openstack_utils.get_neutron_client') - @mock.patch('yardstick.common.openstack_utils.create_neutron_router') - def test_create_router(self, mock_get_neutron_client, mock_create_neutron_router): - options = { - 'openstack_paras': { - 'admin_state_up': 'True', - 'name': 'yardstick_router' - } - } - args = {"options": options} - obj = CreateRouter(args, {}) - obj.run({}) - mock_get_neutron_client.assert_called_once() - mock_create_neutron_router.assert_called_once() + def setUp(self): + + self._mock_create_neutron_router = mock.patch.object( + openstack_utils, 'create_neutron_router') + self.mock_create_neutron_router = ( + self._mock_create_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(create_router, 'LOG') + self.mock_log = self._mock_log.start() + self.args = {'options': {'name': 'yardstick_net'}} + self.result = {} + + self.crouter_obj = create_router.CreateRouter(self.args, mock.ANY) + self.addCleanup(self._stop_mock) + + def _stop_mock(self): + self._mock_create_neutron_router.stop() + self._mock_get_shade_client.stop() + self._mock_log.stop() + + def test_run(self): + _uuid = uuidutils.generate_uuid() + self.crouter_obj.scenario_cfg = {'output': 'id'} + self.mock_create_neutron_router.return_value = _uuid + output = self.crouter_obj.run(self.result) + self.assertEqual({"router_create": 1}, self.result) + self.assertEqual({'id': _uuid}, output) + self.mock_log.info.asset_called_once_with('Create router successful!') + + def test_run_fail(self): + self.mock_create_neutron_router.return_value = None + with self.assertRaises(exceptions.ScenarioCreateRouterError): + self.crouter_obj.run(self.result) + self.assertEqual({"router_create": 0}, self.result) + self.mock_log.error.assert_called_once_with('Create router failed!') diff --git a/yardstick/tests/unit/common/test_openstack_utils.py b/yardstick/tests/unit/common/test_openstack_utils.py index c6b0f46b2..e39a13f1b 100644 --- a/yardstick/tests/unit/common/test_openstack_utils.py +++ b/yardstick/tests/unit/common/test_openstack_utils.py @@ -161,3 +161,27 @@ class DeleteNeutronRouterTestCase(unittest.TestCase): 'router_id') mock_logger.error.assert_called_once() self.assertFalse(output) + + +class CreateNeutronRouterTestCase(unittest.TestCase): + + def setUp(self): + self.mock_shade_client = mock.Mock() + self.mock_shade_client.create_subnet = mock.Mock() + + def test_create_neutron_router(self): + _uuid = uuidutils.generate_uuid() + self.mock_shade_client.create_router.return_value = {'id': _uuid} + output = openstack_utils.create_neutron_router( + self.mock_shade_client) + self.assertEqual(_uuid, output) + + @mock.patch.object(openstack_utils, 'log') + def test_create_neutron_subnet_exception(self, mock_logger): + self.mock_shade_client.create_router.side_effect = ( + exc.OpenStackCloudException('error message')) + + output = openstack_utils.create_neutron_router( + self.mock_shade_client) + mock_logger.error.assert_called_once() + self.assertIsNone(output) |