Add ability to get data from different DBs in influx 39/66739/8
authorEmma Foley <emma.l.foley@intel.com>
Tue, 18 Dec 2018 18:26:23 +0000 (18:26 +0000)
committerEmma Foley <emma.l.foley@intel.com>
Mon, 11 Feb 2019 11:53:27 +0000 (11:53 +0000)
In order to get data from Barometer, the influx helper need
to be updated to recognise different DBs. This patch adds
an optional arg to specify which database to use.
By default, the configured dispatcher for Yardstick is
used.
Limitation: The new change hard codes barometer DB name to
be used, and assumes that it is in the same instance of
Influx as the Yardstick dispatcher.
This can be extended later to make it configurable,
however, a new config option would have to be introduced.

JIRA: YARDSTICK-1593
Change-Id: Idee9c3491d6bf7b549e014edd9ff031312cf0ec2
Signed-off-by: Emma Foley <emma.l.foley@intel.com>
api/utils/influx.py
yardstick/tests/unit/apiserver/utils/test_influx.py

index f391ad9..8f36047 100644 (file)
@@ -1,5 +1,6 @@
 ##############################################################################
 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
+# Copyright (c) 2019 Intel Corporation
 #
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Apache License, Version 2.0
@@ -22,25 +23,27 @@ from yardstick import dispatcher
 
 logger = logging.getLogger(__name__)
 
-
-def get_data_db_client():
+def get_data_db_client(db=None):
     parser = ConfigParser.ConfigParser()
     try:
         parser.read(consts.CONF_FILE)
-        return _get_influxdb_client(parser)
+        return _get_influxdb_client(parser, db)
     except ConfigParser.NoOptionError:
         logger.error('Can not find the key')
         raise
 
-
-def _get_influxdb_client(parser):
+def _get_influxdb_client(parser, db=None):
     if dispatcher.INFLUXDB not in parser.get('DEFAULT', 'dispatcher'):
         raise exceptions.InfluxDBConfigurationMissing()
 
     ip = _get_ip(parser.get('dispatcher_influxdb', 'target'))
     user = parser.get('dispatcher_influxdb', 'username')
     password = parser.get('dispatcher_influxdb', 'password')
-    db_name = parser.get('dispatcher_influxdb', 'db_name')
+    if db is None:
+        db_name = parser.get('dispatcher_influxdb', 'db_name')
+    else:
+        db_name = db
+
     return influxdb_client.InfluxDBClient(ip, consts.INFLUXDB_PORT, user,
                                           password, db_name)
 
@@ -49,9 +52,9 @@ def _get_ip(url):
     return urlsplit(url).hostname
 
 
-def query(query_sql):
+def query(query_sql, db=None):
     try:
-        client = get_data_db_client()
+        client = get_data_db_client(db)
         logger.debug('Start to query: %s', query_sql)
         return list(client.query(query_sql).get_points())
     except RuntimeError:
index 6021d35..3a97ff2 100644 (file)
@@ -1,5 +1,6 @@
 ##############################################################################
 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
+# Copyright (c) 2019 Intel Corporation.
 #
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Apache License, Version 2.0
@@ -29,7 +30,7 @@ class GetDataDbClientTestCase(base.BaseUnitTestCase):
 
         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_get_client.assert_called_once_with(_mock_parser, None)
 
     @mock.patch.object(influx.logger, 'error')
     @mock.patch.object(influx, '_get_influxdb_client',
@@ -46,7 +47,7 @@ class GetDataDbClientTestCase(base.BaseUnitTestCase):
             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_get_client.assert_called_once_with(_mock_parser, None)
 
 
 class GetIpTestCase(base.BaseUnitTestCase):