Create a constants.py to manage constant variable consistently 95/23795/3
authorchenjiankun <chenjiankun1@huawei.com>
Mon, 31 Oct 2016 12:56:38 +0000 (12:56 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Tue, 8 Nov 2016 09:14:26 +0000 (09:14 +0000)
JIRA: YARDSTICK-378

Change-Id: I527d4f60f2a2081730118bdbbea6c19fc093672f
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
setup.py
tests/unit/common/config_sample.yaml [new file with mode: 0644]
tests/unit/common/test_utils.py
yardstick/common/constants.py [new file with mode: 0644]
yardstick/common/utils.py

index 4fe2596..9bf656b 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -66,5 +66,5 @@ setup(
     scripts=['tools/yardstick-img-modify',
              'tools/yardstick-img-lxd-modify',
              'tools/yardstick-img-dpdk-modify'
-            ]
+             ]
 )
diff --git a/tests/unit/common/config_sample.yaml b/tests/unit/common/config_sample.yaml
new file mode 100644 (file)
index 0000000..8caa19e
--- /dev/null
@@ -0,0 +1,2 @@
+releng:
+    dir:                                /home/opnfv/repos/releng
index 002d049..a64c1f1 100644 (file)
@@ -83,6 +83,33 @@ class ImportModulesFromPackageTestCase(unittest.TestCase):
         mock_importutils.import_module.assert_called_with('bar.baz')
 
 
+class GetParaFromYaml(unittest.TestCase):
+
+    def test_get_para_from_yaml_file_not_exist(self):
+        file_path = '/etc/yardstick/hello.yaml'
+        args = 'hello.world'
+        para = utils.get_para_from_yaml(file_path, args)
+        self.assertIsNone(para)
+
+    def test_get_para_from_yaml_para_not_found(self):
+        file_path = 'config_sample.yaml'
+        file_path = self._get_file_abspath(file_path)
+        args = 'releng.file'
+        self.assertIsNone(utils.get_para_from_yaml(file_path, args))
+
+    def test_get_para_from_yaml_para_exists(self):
+        file_path = 'config_sample.yaml'
+        file_path = self._get_file_abspath(file_path)
+        args = 'releng.dir'
+        para = '/home/opnfv/repos/releng'
+        self.assertEqual(para, utils.get_para_from_yaml(file_path, args))
+
+    def _get_file_abspath(self, filename):
+        curr_path = os.path.dirname(os.path.abspath(__file__))
+        file_path = os.path.join(curr_path, filename)
+        return file_path
+
+
 def main():
     unittest.main()
 
diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py
new file mode 100644 (file)
index 0000000..40b29a7
--- /dev/null
@@ -0,0 +1,3 @@
+CONFIG_SAMPLE = '/etc/yardstick/config.yaml'
+
+RELENG_DIR = 'releng.dir'
index c482b4d..d639fb6 100644 (file)
@@ -17,6 +17,7 @@
 
 import os
 import sys
+import yaml
 from oslo_utils import importutils
 
 import yardstick
@@ -68,3 +69,25 @@ def import_modules_from_package(package):
             new_package = ".".join(root.split(os.sep)).split("....")[1]
             module_name = "%s.%s" % (new_package, filename[:-3])
             try_append_module(module_name, sys.modules)
+
+
+def get_para_from_yaml(file_path, args):
+
+    def func(a, b):
+        if a is None:
+            return None
+        return a.get(b)
+
+    if os.path.exists(file_path):
+        with open(file_path) as f:
+            value = yaml.safe_load(f)
+            value = reduce(func, args.split('.'), value)
+
+            if value is None:
+                print 'parameter not found'
+                return None
+
+            return value
+    else:
+        print 'file not exist'
+        return None