diff options
Diffstat (limited to 'yardstick/tests/unit/apiserver')
4 files changed, 107 insertions, 25 deletions
diff --git a/yardstick/tests/unit/apiserver/resources/v2/__init__.py b/yardstick/tests/unit/apiserver/resources/v2/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/yardstick/tests/unit/apiserver/resources/v2/__init__.py diff --git a/yardstick/tests/unit/apiserver/resources/v2/test_images.py b/yardstick/tests/unit/apiserver/resources/v2/test_images.py new file mode 100644 index 000000000..ab131eec5 --- /dev/null +++ b/yardstick/tests/unit/apiserver/resources/v2/test_images.py @@ -0,0 +1,46 @@ +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import mock + +import unittest + +from yardstick.tests.unit.apiserver import APITestCase +from api.resources.v2.images import format_image_info + + +class V2ImagesTestCase(APITestCase): + @mock.patch('yardstick.common.openstack_utils.list_images') + @mock.patch('yardstick.common.utils.source_env') + def test_get(self, _, mock_list_images): + if self.app is None: + unittest.skip('host config error') + return + + single_image = mock.MagicMock() + single_image.name = 'yardstick-image' + single_image.size = 16384 + single_image.status = 'active' + single_image.updated_at = '2018-04-08' + + mock_list_images.return_value = [single_image] + url = 'api/v2/yardstick/images' + resp = self._get(url) + self.assertEqual(resp.get('status'), 1) + + +class FormatImageInfoTestCase(unittest.TestCase): + def test_format_image_info(self): + image = mock.MagicMock() + image.name = 'yardstick-image' + image.size = 1048576 + image.status = 'active' + image.updated_at = '2018-04-08' + + image_dict = format_image_info(image) + self.assertEqual(image_dict.get('size'), 1) diff --git a/yardstick/tests/unit/apiserver/utils/__init__.py b/yardstick/tests/unit/apiserver/utils/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/yardstick/tests/unit/apiserver/utils/__init__.py diff --git a/yardstick/tests/unit/apiserver/utils/test_influx.py b/yardstick/tests/unit/apiserver/utils/test_influx.py index dce6c1cec..95105d8ae 100644 --- a/yardstick/tests/unit/apiserver/utils/test_influx.py +++ b/yardstick/tests/unit/apiserver/utils/test_influx.py @@ -6,28 +6,48 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import unittest + +from influxdb import client as influxdb_client import mock +from six.moves import configparser from api.utils import influx -from six.moves import configparser as ConfigParser +from yardstick.common import constants +from yardstick.common import exceptions +from yardstick import dispatcher +from yardstick.tests.unit import base + +class GetDataDbClientTestCase(base.BaseUnitTestCase): -class GetDataDbClientTestCase(unittest.TestCase): + @mock.patch.object(influx, '_get_influxdb_client', + return_value='fake_client') + @mock.patch.object(influx.ConfigParser, 'ConfigParser') + def test_get_data_db_client(self, mock_parser, mock_get_client): + _mock_parser = mock.Mock() + mock_parser.return_value = _mock_parser - @mock.patch('api.utils.influx.ConfigParser') - def test_get_data_db_client_dispatcher_not_influxdb(self, mock_parser): - mock_parser.ConfigParser().get.return_value = 'file' - # reset exception to avoid - # TypeError: catching classes that do not inherit from BaseException - mock_parser.NoOptionError = ConfigParser.NoOptionError - try: + self.assertEqual('fake_client', influx.get_data_db_client()) + _mock_parser.read.assert_called_once_with(constants.CONF_FILE) + mock_get_client.assert_called_once_with(_mock_parser) + + @mock.patch.object(influx, '_get_influxdb_client', + return_value='fake_client') + @mock.patch.object(influx.ConfigParser, 'ConfigParser') + def test_get_data_db_client_parsing_error(self, mock_parser, + mock_get_client): + _mock_parser = mock.Mock() + mock_parser.return_value = _mock_parser + mock_parser.NoOptionError = configparser.NoOptionError + mock_get_client.side_effect = configparser.NoOptionError('option', 'section') + with self.assertRaises(configparser.NoOptionError): influx.get_data_db_client() - except Exception as e: # pylint: disable=broad-except - self.assertIsInstance(e, RuntimeError) + + _mock_parser.read.assert_called_once_with(constants.CONF_FILE) + mock_get_client.assert_called_once_with(_mock_parser) -class GetIpTestCase(unittest.TestCase): +class GetIpTestCase(base.BaseUnitTestCase): def test_get_url(self): url = 'http://localhost:8086/hello' @@ -37,16 +57,32 @@ class GetIpTestCase(unittest.TestCase): self.assertEqual(result, output) -class QueryTestCase(unittest.TestCase): +class GetInfluxdbTestCase(base.BaseUnitTestCase): + + @mock.patch.object(influxdb_client, 'InfluxDBClient', + return_value='idb_client') + @mock.patch.object(influx, '_get_ip', return_value='fake_ip') + def test_get_influxdb_client(self, mock_get_ip, mock_client): + mock_parser = mock.Mock() + mock_parser.get.side_effect = [dispatcher.INFLUXDB, 'target', 'user', + 'pass', 'db_name'] + + self.assertEqual('idb_client', + influx._get_influxdb_client(mock_parser)) + mock_client.assert_called_once_with('fake_ip', constants.INFLUXDB_PORT, + 'user', 'pass', 'db_name') + mock_get_ip.assert_called_once_with('target') + mock_parser.get.assert_has_calls([ + mock.call('DEFAULT', 'dispatcher'), + mock.call('dispatcher_influxdb', 'target'), + mock.call('dispatcher_influxdb', 'username'), + mock.call('dispatcher_influxdb', 'password'), + mock.call('dispatcher_influxdb', 'db_name')]) + + def test_get_influxdb_client_no_influxdb_client(self): + mock_parser = mock.Mock() + mock_parser.get.return_value = dispatcher.FILE - @mock.patch('api.utils.influx.ConfigParser') - def test_query_dispatcher_not_influxdb(self, mock_parser): - mock_parser.ConfigParser().get.return_value = 'file' - # reset exception to avoid - # TypeError: catching classes that do not inherit from BaseException - mock_parser.NoOptionError = ConfigParser.NoOptionError - try: - sql = 'select * form tasklist' - influx.query(sql) - except Exception as e: # pylint: disable=broad-except - self.assertIsInstance(e, RuntimeError) + with self.assertRaises(exceptions.InfluxDBConfigurationMissing): + influx._get_influxdb_client(mock_parser) + mock_parser.get.assert_called_once_with('DEFAULT', 'dispatcher') |