diff options
author | Shobhi Jain <shobhi.jain@intel.com> | 2018-03-30 10:45:54 +0100 |
---|---|---|
committer | Emma Foley <emma.l.foley@intel.com> | 2018-06-27 17:14:54 +0100 |
commit | 60aa5d558a80bd3cfa51f962cef794c25a2eb828 (patch) | |
tree | 9ef89db36f033d72ffdf441a397e102bffdf1cea | |
parent | 8442ad76bb4f93eeb87f5bbf0f2292bb2400ea2d (diff) |
Replace nova get server with shade client.
Rename get_server_by_name with get_server.
Function get_server now uses shade client.
JIRA: YARDSTICK-1088
Change-Id: I69c59145cefdb565f3ece27baaaf932905e1b757
Signed-off-by: Shobhi Jain <shobhi.jain@intel.com>
(cherry picked from commit d75a91ebf2937e5cfe5cf2aa5a90db9121f73967)
-rw-r--r-- | yardstick/benchmark/scenarios/lib/get_server.py | 84 | ||||
-rw-r--r-- | yardstick/common/exceptions.py | 4 | ||||
-rw-r--r-- | yardstick/common/openstack_utils.py | 24 | ||||
-rw-r--r-- | yardstick/tests/unit/benchmark/scenarios/lib/test_get_server.py | 69 | ||||
-rw-r--r-- | yardstick/tests/unit/common/test_openstack_utils.py | 22 |
5 files changed, 125 insertions, 78 deletions
diff --git a/yardstick/benchmark/scenarios/lib/get_server.py b/yardstick/benchmark/scenarios/lib/get_server.py index fcf47c80d..f65fa9ebf 100644 --- a/yardstick/benchmark/scenarios/lib/get_server.py +++ b/yardstick/benchmark/scenarios/lib/get_server.py @@ -6,14 +6,11 @@ # which accompanies this distribution, and is available at # 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__) @@ -21,63 +18,58 @@ LOG = logging.getLogger(__name__) class GetServer(base.Scenario): """Get a server instance - Parameters - server_id - ID of the server - type: string - unit: N/A - default: null - server_name - name of the server - type: string - unit: N/A - default: null - - Either server_id or server_name is required. - - Outputs + Parameters: + name_or_id - Name or ID of the server + type: string + filters - meta data to use for further filtering + type: dict + detailed: Whether or not to add detailed additional information. + type: bool + bare: Whether to skip adding any additional information to the server + record. + type: bool + all_projects: Whether to get server from all projects or just the current + auth scoped project. + type: bool + + Outputs: rc - response code of getting server instance - 0 for success - 1 for failure + 1 for success + 0 for failure type: int - unit: N/A server - instance of the server type: dict - unit: N/A + """ - __scenario_type__ = "GetServer" + __scenario_type__ = 'GetServer' def __init__(self, scenario_cfg, context_cfg): self.scenario_cfg = scenario_cfg self.context_cfg = context_cfg - self.options = self.scenario_cfg.get('options', {}) + self.options = self.scenario_cfg['options'] - self.server_id = self.options.get("server_id") - if self.server_id: - LOG.debug('Server id is %s', self.server_id) + self.server_name_or_id = self.options.get('name_or_id') + self.filters = self.options.get('filters') + self.detailed = self.options.get('detailed', False) + self.bare = self.options.get('bare', False) - default_name = self.scenario_cfg.get('host', - self.scenario_cfg.get('target')) - self.server_name = self.options.get('server_name', default_name) - if self.server_name: - LOG.debug('Server name is %s', self.server_name) - - self.nova_client = op_utils.get_nova_client() + self.shade_client = openstack_utils.get_shade_client() def run(self, result): """execute the test""" - if self.server_id: - server = self.nova_client.servers.get(self.server_id) - else: - server = op_utils.get_server_by_name(self.server_name) - - keys = self.scenario_cfg.get('output', '').split() + server = openstack_utils.get_server( + self.shade_client, name_or_id=self.server_name_or_id, + filters=self.filters, detailed=self.detailed, bare=self.bare) - if server: - LOG.info("Get server successful!") - values = [0, self._change_obj_to_dict(server)] - else: - LOG.info("Get server failed!") - values = [1] + if not server: + result.update({'get_server': 0}) + LOG.error('Get Server failed!') + raise exceptions.ScenarioGetServerError + result.update({'get_server': 1}) + LOG.info('Get Server successful!') + keys = self.scenario_cfg.get('output', '').split() + values = [server] return self._push_to_outputs(keys, values) diff --git a/yardstick/common/exceptions.py b/yardstick/common/exceptions.py index 3a51892cc..cb8fabda8 100644 --- a/yardstick/common/exceptions.py +++ b/yardstick/common/exceptions.py @@ -238,6 +238,10 @@ class ScenarioAttachVolumeError(YardstickException): message = 'Nova Attach Volume Scenario failed' +class ScenarioGetServerError(YardstickException): + message = 'Nova Get Server Scenario failed' + + class ApiServerError(YardstickException): message = 'An unkown exception happened to Api Server!' diff --git a/yardstick/common/openstack_utils.py b/yardstick/common/openstack_utils.py index 0a3cfb5fc..0220c4212 100644 --- a/yardstick/common/openstack_utils.py +++ b/yardstick/common/openstack_utils.py @@ -321,12 +321,26 @@ def delete_instance(shade_client, name_or_id, wait=False, timeout=180, return False -def get_server_by_name(name): # pragma: no cover +def get_server(shade_client, name_or_id=None, filters=None, detailed=False, + bare=False): + """Get a server by name or ID. + + :param name_or_id: Name or ID of the server. + :param filters:(dict) A dictionary of meta data to use for further + filtering. + :param detailed:(bool) Whether or not to add detailed additional + information. + :param bare:(bool) Whether to skip adding any additional information to the + server record. + + :returns: A server ``munch.Munch`` or None if no matching server is found. + """ try: - return get_nova_client().servers.list(search_opts={'name': name})[0] - except IndexError: - log.exception('Failed to get nova client') - raise + return shade_client.get_server(name_or_id=name_or_id, filters=filters, + detailed=detailed, bare=bare) + except exc.OpenStackCloudException as o_exc: + log.error("Error [get_server(shade_client, '%s')]. " + "Exception message: %s", name_or_id, o_exc.orig_message) def create_flavor(name, ram, vcpus, disk, **kwargs): # pragma: no cover diff --git a/yardstick/tests/unit/benchmark/scenarios/lib/test_get_server.py b/yardstick/tests/unit/benchmark/scenarios/lib/test_get_server.py index 83ec903bc..5b5329cb0 100644 --- a/yardstick/tests/unit/benchmark/scenarios/lib/test_get_server.py +++ b/yardstick/tests/unit/benchmark/scenarios/lib/test_get_server.py @@ -6,37 +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.get_server import GetServer +from yardstick.common import openstack_utils +from yardstick.common import exceptions +from yardstick.benchmark.scenarios.lib import get_server class GetServerTestCase(unittest.TestCase): - @mock.patch('yardstick.common.openstack_utils.get_server_by_name') - @mock.patch('yardstick.common.openstack_utils.get_nova_client') - def test_get_server_with_name(self, mock_get_nova_client, mock_get_server_by_name): - scenario_cfg = { - 'options': { - 'server_name': 'yardstick_server' - }, - 'output': 'status server' - } - obj = GetServer(scenario_cfg, {}) - obj.run({}) - mock_get_nova_client.assert_called_once() - mock_get_server_by_name.assert_called_once() - - @mock.patch('yardstick.common.openstack_utils.get_nova_client') - def test_get_server_with_id(self, mock_get_nova_client): - scenario_cfg = { - 'options': { - 'server_id': '1' - }, - 'output': 'status server' - } - mock_get_nova_client().servers.get.return_value = None - obj = GetServer(scenario_cfg, {}) - obj.run({}) - mock_get_nova_client.assert_called() + def setUp(self): + + self._mock_get_server = mock.patch.object( + openstack_utils, 'get_server') + self.mock_get_server = self._mock_get_server.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(get_server, 'LOG') + self.mock_log = self._mock_log.start() + self.args = {'options': {'name_or_id': 'yardstick_key'}} + self.result = {} + + self.getserver_obj = get_server.GetServer(self.args, mock.ANY) + self.addCleanup(self._stop_mock) + + def _stop_mock(self): + self._mock_get_server.stop() + self._mock_get_shade_client.stop() + self._mock_log.stop() + + def test_run(self): + _uuid = uuidutils.generate_uuid() + self.getserver_obj.scenario_cfg = {'output': 'server'} + self.mock_get_server.return_value = ( + {'name': 'server-name', 'id': _uuid}) + output = self.getserver_obj.run(self.result) + self.assertDictEqual({'get_server': 1}, self.result) + self.assertDictEqual({'server': {'name': 'server-name', 'id': _uuid}}, + output) + self.mock_log.info.asset_called_once_with('Get Server successful!') + + def test_run_fail(self): + self.mock_get_server.return_value = None + with self.assertRaises(exceptions.ScenarioGetServerError): + self.getserver_obj.run(self.result) + self.assertDictEqual({'get_server': 0}, self.result) + self.mock_log.error.assert_called_once_with('Get Server failed!') diff --git a/yardstick/tests/unit/common/test_openstack_utils.py b/yardstick/tests/unit/common/test_openstack_utils.py index 3441ad264..89a810cf7 100644 --- a/yardstick/tests/unit/common/test_openstack_utils.py +++ b/yardstick/tests/unit/common/test_openstack_utils.py @@ -467,3 +467,25 @@ class AttachVolumeToServerTestCase(unittest.TestCase): self.mock_shade_client, 'server_name_or_id', 'volume_name_or_id') mock_logger.error.assert_called_once() self.assertFalse(output) + + +class GetServerTestCase(unittest.TestCase): + + def test_get_server(self): + self.mock_shade_client = mock.Mock() + _uuid = uuidutils.generate_uuid() + self.mock_shade_client.get_server.return_value = { + 'name': 'server_name', 'id': _uuid} + output = openstack_utils.get_server(self.mock_shade_client, + 'server_name_or_id') + self.assertEqual({'name': 'server_name', 'id': _uuid}, output) + + @mock.patch.object(openstack_utils, 'log') + def test_get_server_exception(self, mock_logger): + self.mock_shade_client = mock.Mock() + self.mock_shade_client.get_server.side_effect = ( + exc.OpenStackCloudException('error message')) + output = openstack_utils.get_server(self.mock_shade_client, + 'server_name_or_id') + mock_logger.error.assert_called_once() + self.assertIsNone(output) |