Added unit tests for vping.
[functest.git] / functest / ci / prepare_env.py
index 19e3786..64fcc92 100755 (executable)
@@ -11,6 +11,7 @@ import json
 import logging
 import logging.config
 import os
+import pkg_resources
 import re
 import subprocess
 import sys
@@ -34,14 +35,15 @@ handler = None
 pod_arch = None
 arch_filter = ['aarch64']
 
-CONFIG_FUNCTEST_PATH = CONST.__getattribute__('CONFIG_FUNCTEST_YAML')
-CONFIG_PATCH_PATH = os.path.join(os.path.dirname(
-    CONFIG_FUNCTEST_PATH), "config_patch.yaml")
-CONFIG_AARCH64_PATCH_PATH = os.path.join(os.path.dirname(
-    CONFIG_FUNCTEST_PATH), "config_aarch64_patch.yaml")
-RALLY_CONF_PATH = os.path.join("/etc/rally/rally.conf")
-RALLY_AARCH64_PATCH_PATH = os.path.join(os.path.dirname(
-    CONFIG_FUNCTEST_PATH), "rally_aarch64_patch.conf")
+CONFIG_FUNCTEST_PATH = pkg_resources.resource_filename(
+            'functest', 'ci/config_functest.yaml')
+CONFIG_PATCH_PATH = pkg_resources.resource_filename(
+            'functest', 'ci/config_patch.yaml')
+CONFIG_AARCH64_PATCH_PATH = pkg_resources.resource_filename(
+            'functest', 'ci/config_aarch64_patch.yaml')
+RALLY_CONF_PATH = "/etc/rally/rally.conf"
+RALLY_AARCH64_PATCH_PATH = pkg_resources.resource_filename(
+            'functest', 'ci/rally_aarch64_patch.conf')
 
 
 class PrepareEnvParser(object):
@@ -114,9 +116,8 @@ def get_deployment_handler():
     global handler
     global pod_arch
 
-    installer_params_yaml = os.path.join(
-        CONST.__getattribute__('dir_repo_functest'),
-        'functest/ci/installer_params.yaml')
+    installer_params_yaml = pkg_resources.resource_filename(
+            'functest', 'ci/installer_params.yaml')
     if (CONST.__getattribute__('INSTALLER_IP') and
         CONST.__getattribute__('INSTALLER_TYPE') and
             CONST.__getattribute__('INSTALLER_TYPE') in
@@ -210,12 +211,15 @@ def source_rc_file():
                 CONST.__setattr__('OS_PASSWORD', value)
 
 
-def patch_config_file():
+def update_config_file():
     patch_file(CONFIG_PATCH_PATH)
 
     if pod_arch and pod_arch in arch_filter:
         patch_file(CONFIG_AARCH64_PATCH_PATH)
 
+    if "TEST_DB_URL" in os.environ:
+        update_db_url()
+
 
 def patch_file(patch_file_path):
     logger.debug('Updating file: %s', patch_file_path)
@@ -233,14 +237,22 @@ def patch_file(patch_file_path):
         os.remove(CONFIG_FUNCTEST_PATH)
         with open(CONFIG_FUNCTEST_PATH, "w") as f:
             f.write(yaml.dump(new_functest_yaml, default_style='"'))
-        f.close()
+
+
+def update_db_url():
+    with open(CONFIG_FUNCTEST_PATH) as f:
+        functest_yaml = yaml.safe_load(f)
+
+    with open(CONFIG_FUNCTEST_PATH, "w") as f:
+        functest_yaml["results"]["test_db_url"] = os.environ.get('TEST_DB_URL')
+        f.write(yaml.dump(functest_yaml, default_style='"'))
 
 
 def verify_deployment():
     print_separator()
     logger.info("Verifying OpenStack services...")
-    cmd = ("%s/functest/ci/check_os.sh" %
-           CONST.__getattribute__('dir_repo_functest'))
+    cmd = ("%s" % pkg_resources.resource_filename(
+           'functest', 'ci/check_os.sh'))
 
     logger.debug("Executing command: %s" % cmd)
     p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
@@ -354,10 +366,9 @@ def main(**kwargs):
         elif kwargs['action'] == "start":
             logger.info("######### Preparing Functest environment #########\n")
             check_env_variables()
-            get_deployment_handler()
             create_directories()
             source_rc_file()
-            patch_config_file()
+            update_config_file()
             verify_deployment()
             install_rally()
             install_tempest()
@@ -365,7 +376,6 @@ def main(**kwargs):
             with open(CONST.__getattribute__('env_active'), "w") as env_file:
                 env_file.write("1")
             check_environment()
-            print_deployment_info()
         elif kwargs['action'] == "check":
             check_environment()
     except Exception as e:
@@ -375,8 +385,8 @@ def main(**kwargs):
 
 
 if __name__ == '__main__':
-    logging.config.fileConfig(
-        CONST.__getattribute__('dir_functest_logging_cfg'))
+    logging.config.fileConfig(pkg_resources.resource_filename(
+        'functest', 'ci/logging.ini'))
     parser = PrepareEnvParser()
     args = parser.parse_args(sys.argv[1:])
     sys.exit(main(**args))