summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoss Brattain <ross.b.brattain@intel.com>2017-08-10 03:56:50 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-08-10 03:56:50 +0000
commit2ad96094b99e007f5ef46aced3b69a9ced558da3 (patch)
treeadccfc44324e9feb05e15aaf03a2972277dba13c
parentdaa256f05a1f42f66a84ad96321bc609aeb1f75b (diff)
parent97004997c00dac2e0dcfeef303af4701e78bb909 (diff)
Merge "constants: cache YAML config values"
-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 948f8fc98..f29de5ca0 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 969bb4b38..729bc1db9 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)