constants: cache YAML config values 43/36343/9
authorRoss Brattain <ross.b.brattain@intel.com>
Thu, 22 Jun 2017 02:25:22 +0000 (19:25 -0700)
committerRoss Brattain <ross.b.brattain@intel.com>
Wed, 9 Aug 2017 04:24:27 +0000 (21:24 -0700)
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>
tests/unit/common/test_utils.py
yardstick/common/constants.py
yardstick/common/utils.py

index 53b5652..094ab2b 100644 (file)
@@ -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__))
index 8e8114f..822d3b4 100644 (file)
@@ -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')
index 1059e1c..d749d26 100644 (file)
@@ -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)