From 97004997c00dac2e0dcfeef303af4701e78bb909 Mon Sep 17 00:00:00 2001 From: Ross Brattain Date: Wed, 21 Jun 2017 19:25:22 -0700 Subject: 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 --- tests/unit/common/test_utils.py | 5 +++-- yardstick/common/constants.py | 26 ++++++++++++++++++++++++-- yardstick/common/utils.py | 14 -------------- 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) -- cgit 1.2.3-korg