1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 from influxdb import client as influxdb_client
12 from six.moves import configparser
14 from api.utils import influx
15 from yardstick.common import constants
16 from yardstick.common import exceptions
17 from yardstick import dispatcher
18 from yardstick.tests.unit import base
21 class GetDataDbClientTestCase(base.BaseUnitTestCase):
23 @mock.patch.object(influx, '_get_influxdb_client',
24 return_value='fake_client')
25 @mock.patch.object(influx.ConfigParser, 'ConfigParser')
26 def test_get_data_db_client(self, mock_parser, mock_get_client):
27 _mock_parser = mock.Mock()
28 mock_parser.return_value = _mock_parser
30 self.assertEqual('fake_client', influx.get_data_db_client())
31 _mock_parser.read.assert_called_once_with(constants.CONF_FILE)
32 mock_get_client.assert_called_once_with(_mock_parser)
34 @mock.patch.object(influx, '_get_influxdb_client',
35 return_value='fake_client')
36 @mock.patch.object(influx.ConfigParser, 'ConfigParser')
37 def test_get_data_db_client_parsing_error(self, mock_parser,
39 _mock_parser = mock.Mock()
40 mock_parser.return_value = _mock_parser
41 mock_parser.NoOptionError = configparser.NoOptionError
42 mock_get_client.side_effect = configparser.NoOptionError('option', 'section')
43 with self.assertRaises(configparser.NoOptionError):
44 influx.get_data_db_client()
46 _mock_parser.read.assert_called_once_with(constants.CONF_FILE)
47 mock_get_client.assert_called_once_with(_mock_parser)
50 class GetIpTestCase(base.BaseUnitTestCase):
52 def test_get_url(self):
53 url = 'http://localhost:8086/hello'
54 output = influx._get_ip(url)
57 self.assertEqual(result, output)
60 class GetInfluxdbTestCase(base.BaseUnitTestCase):
62 @mock.patch.object(influxdb_client, 'InfluxDBClient',
63 return_value='idb_client')
64 @mock.patch.object(influx, '_get_ip', return_value='fake_ip')
65 def test_get_influxdb_client(self, mock_get_ip, mock_client):
66 mock_parser = mock.Mock()
67 mock_parser.get.side_effect = [dispatcher.INFLUXDB, 'target', 'user',
70 self.assertEqual('idb_client',
71 influx._get_influxdb_client(mock_parser))
72 mock_client.assert_called_once_with('fake_ip', constants.INFLUXDB_PORT,
73 'user', 'pass', 'db_name')
74 mock_get_ip.assert_called_once_with('target')
75 mock_parser.get.assert_has_calls([
76 mock.call('DEFAULT', 'dispatcher'),
77 mock.call('dispatcher_influxdb', 'target'),
78 mock.call('dispatcher_influxdb', 'username'),
79 mock.call('dispatcher_influxdb', 'password'),
80 mock.call('dispatcher_influxdb', 'db_name')])
82 def test_get_influxdb_client_no_influxdb_client(self):
83 mock_parser = mock.Mock()
84 mock_parser.get.return_value = dispatcher.FILE
86 with self.assertRaises(exceptions.InfluxDBConfigurationMissing):
87 influx._get_influxdb_client(mock_parser)
88 mock_parser.get.assert_called_once_with('DEFAULT', 'dispatcher')