aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoss Brattain <ross.b.brattain@intel.com>2017-06-21 19:25:22 -0700
committerRoss Brattain <ross.b.brattain@intel.com>2017-08-08 21:24:27 -0700
commit97004997c00dac2e0dcfeef303af4701e78bb909 (patch)
treead6b962b8ec15db6296ea8c636e5f23f2b1342a1
parent5ce3b6f8c8b3217091e51a6041455738603d90b8 (diff)
constants: cache YAML config values
don't reparse the yardstick.yaml file each time we lookup an option. Since it is global data, just cache it in a global var Use pkg_resources.resource_filename to lookup the path of yardstick tests, intsead of using __file__ pkg_resources is slightly more proper than __file__ at least for packages Change-Id: I05d9748390a37bd45c53013fc084d23069ab7c51 Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
-rw-r--r--tests/unit/common/test_utils.py5
-rw-r--r--yardstick/common/constants.py26
-rw-r--r--yardstick/common/utils.py14
3 files changed, 27 insertions, 18 deletions
diff --git a/tests/unit/common/test_utils.py b/tests/unit/common/test_utils.py
index 53b56523e..094ab2b19 100644
--- a/tests/unit/common/test_utils.py
+++ b/tests/unit/common/test_utils.py
@@ -21,6 +21,7 @@ import mock
from six.moves import configparser
from yardstick.common import utils
+from yardstick.common import constants
class IterSubclassesTestCase(unittest.TestCase):
@@ -99,7 +100,7 @@ class GetParaFromYaml(unittest.TestCase):
get_env.return_value = self._get_file_abspath(file_path)
args = 'releng.file'
default = 'hello'
- self.assertTrue(utils.get_param(args, default), default)
+ self.assertTrue(constants.get_param(args, default), default)
@mock.patch('yardstick.common.utils.os.environ.get')
def test_get_param_para_exists(self, get_env):
@@ -107,7 +108,7 @@ class GetParaFromYaml(unittest.TestCase):
get_env.return_value = self._get_file_abspath(file_path)
args = 'releng.dir'
para = '/home/opnfv/repos/releng'
- self.assertEqual(para, utils.get_param(args))
+ self.assertEqual(para, constants.get_param(args))
def _get_file_abspath(self, filename):
curr_path = os.path.dirname(os.path.abspath(__file__))
diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py
index 8e8114fbb..822d3b4fa 100644
--- a/yardstick/common/constants.py
+++ b/yardstick/common/constants.py
@@ -8,15 +8,36 @@
##############################################################################
from __future__ import absolute_import
import os
+from functools import reduce
-from yardstick.common.utils import get_param
+import pkg_resources
+from yardstick.common.utils import parse_yaml
dirname = os.path.dirname
abspath = os.path.abspath
join = os.path.join
sep = os.path.sep
+CONF = {}
+
+
+def get_param(key, default=''):
+
+ # we have to defer this to runtime so that we can mock os.environ.get in unittests
+ conf_file = os.environ.get('CONF_FILE', '/etc/yardstick/yardstick.yaml')
+
+ # don't re-parse yaml for each lookup
+ if not CONF:
+ CONF.update(parse_yaml(conf_file))
+ try:
+ return reduce(lambda a, b: a[b], key.split('.'), CONF)
+ except KeyError:
+ if not default:
+ raise
+ return default
+
+
try:
SERVER_IP = get_param('api.server_ip')
except KeyError:
@@ -41,7 +62,8 @@ CONF_DIR = get_param('dir.conf', '/etc/yardstick')
REPOS_DIR = get_param('dir.repos', '/home/opnfv/repos/yardstick')
RELENG_DIR = get_param('dir.releng', '/home/opnfv/repos/releng')
LOG_DIR = get_param('dir.log', '/tmp/yardstick/')
-YARDSTICK_ROOT_PATH = dirname(dirname(dirname(abspath(__file__)))) + sep
+YARDSTICK_ROOT_PATH = dirname(
+ dirname(abspath(pkg_resources.resource_filename(__name__, "")))) + sep
CONF_SAMPLE_DIR = join(REPOS_DIR, 'etc/yardstick/')
ANSIBLE_DIR = join(REPOS_DIR, 'ansible')
SAMPLE_CASE_DIR = join(REPOS_DIR, 'samples')
diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py
index 1059e1ce4..d749d2625 100644
--- a/yardstick/common/utils.py
+++ b/yardstick/common/utils.py
@@ -27,7 +27,6 @@ import collections
import socket
import random
import ipaddress
-from functools import reduce
from contextlib import closing
import yaml
@@ -107,19 +106,6 @@ def parse_yaml(file_path):
return value
-def get_param(key, default=''):
-
- conf_file = os.environ.get('CONF_FILE', '/etc/yardstick/yardstick.yaml')
-
- conf = parse_yaml(conf_file)
- try:
- return reduce(lambda a, b: a[b], key.split('.'), conf)
- except KeyError:
- if not default:
- raise
- return default
-
-
def makedirs(d):
try:
os.makedirs(d)