diff options
author | Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com> | 2018-04-24 16:53:11 +0100 |
---|---|---|
committer | Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com> | 2018-05-04 06:59:26 +0000 |
commit | a833a12d6fea8bc02f01f6ed8f0f84670e924b77 (patch) | |
tree | c3cb41b3ca234adc9999ec4d71b8dfe9e10cc4fd /yardstick/tests/unit/apiserver/utils/test_influx.py | |
parent | e985b356b75c9f4169ab8f1cd761cc3cfd13f6d1 (diff) |
Parse "dispatcher" options correctly from InfluxDB client
The InfluxDB client needs first to retrieve which dispatchers
are available from "DEFAULT:dispatcher". This parameter accepts
comma separated values. "get_data_db_client" method needs to search
for "influxdb" in this string parameter.
JIRA: YARDSTICK-1136
Change-Id: I5efafcffbd3dbf906ca9bebd75b3717df8bbb75d
Signed-off-by: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Diffstat (limited to 'yardstick/tests/unit/apiserver/utils/test_influx.py')
-rw-r--r-- | yardstick/tests/unit/apiserver/utils/test_influx.py | 86 |
1 files changed, 61 insertions, 25 deletions
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') |