Remove logger as input parameter of functions in openstack_utils
[functest.git] / utils / functest_utils.py
index 2e7a122..b51a1c8 100644 (file)
@@ -8,23 +8,25 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 #
 
+""" global variables """
+
 from datetime import datetime as dt
 import json
 import os
 import os.path
 import re
-import requests
 import shutil
 import socket
 import subprocess
 import sys
 import urllib2
-import yaml
-from git import Repo
+
 import functest.ci.tier_builder as tb
+from git import Repo
+import requests
+import yaml
 
 
-""" global variables """
 REPOS_DIR = os.getenv('repos_dir')
 FUNCTEST_REPO = ("%s/functest/" % REPOS_DIR)
 
@@ -242,7 +244,8 @@ def execute_command(cmd, logger=None,
             print(msg_exec)
     p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
     while True:
-        line = p.stdout.readline().replace('\n', '')
+        output = p.stdout.readline()
+        line = output.replace('\n', '')
         if not line:
             break
         if logger:
@@ -300,3 +303,28 @@ def get_criteria_by_test(testname):
                 criteria = test.get_criteria()
 
     return criteria
+
+
+# ----------------------------------------------------------
+#
+#               YAML UTILS
+#
+# -----------------------------------------------------------
+def get_parameter_from_yaml(parameter, file=None):
+    """
+    Returns the value of a given parameter in config_functest.yaml
+    parameter must be given in string format with dots
+    Example: general.openstack.image_name
+    """
+    if file is None:
+        file = os.environ["CONFIG_FUNCTEST_YAML"]
+    with open(file) as f:
+        functest_yaml = yaml.safe_load(f)
+    f.close()
+    value = functest_yaml
+    for element in parameter.split("."):
+        value = value.get(element)
+        if value is None:
+            raise ValueError("The parameter %s is not defined in"
+                             " config_functest.yaml" % parameter)
+    return value