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