Merge "Refactor configure_tempest() function"
[functest.git] / functest / opnfv_tests / openstack / tempest / conf_utils.py
index 6aa39ea..f013b44 100644 (file)
@@ -11,12 +11,15 @@ import ConfigParser
 import os
 import re
 import shutil
+import subprocess
 
 import opnfv.utils.constants as releng_constants
 
+from functest.utils.constants import CONST
+import functest.utils.functest_logger as ft_logger
 import functest.utils.functest_utils as ft_utils
 import functest.utils.openstack_utils as os_utils
-from functest.utils.constants import CONST
+
 
 IMAGE_ID_ALT = None
 FLAVOR_ID_ALT = None
@@ -38,26 +41,92 @@ TEMPEST_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_list.txt')
 CI_INSTALLER_TYPE = CONST.INSTALLER_TYPE
 CI_INSTALLER_IP = CONST.INSTALLER_IP
 
+""" logging configuration """
+logger = ft_logger.Logger("Tempest").getLogger()
 
-def configure_tempest(logger, deployment_dir, IMAGE_ID=None, FLAVOR_ID=None):
+
+def get_verifier_id():
     """
-    Add/update needed parameters into tempest.conf file generated by Rally
+    Returns verifer id for current Tempest
     """
-    tempest_conf_file = deployment_dir + "/tempest.conf"
-    if os.path.isfile(tempest_conf_file):
-        logger.debug("Deleting old tempest.conf file...")
-        os.remove(tempest_conf_file)
+    cmd = ("rally verify list-verifiers | awk '/" +
+           CONST.tempest_deployment_name +
+           "/ {print $2}'")
+    p = subprocess.Popen(cmd, shell=True,
+                         stdout=subprocess.PIPE,
+                         stderr=subprocess.STDOUT)
+    deployment_uuid = p.stdout.readline().rstrip()
+    if deployment_uuid == "":
+        logger.error("Tempest verifier not found.")
+        raise Exception('Error with command:%s' % cmd)
+    return deployment_uuid
 
-    logger.debug("Generating new tempest.conf file...")
-    cmd = "rally verify genconfig"
-    ft_utils.execute_command(cmd)
 
-    logger.debug("Finding tempest.conf file...")
-    if not os.path.isfile(tempest_conf_file):
-        logger.error("Tempest configuration file %s NOT found."
-                     % tempest_conf_file)
+def get_verifier_deployment_id():
+    """
+    Returns deployment id for active Rally deployment
+    """
+    cmd = ("rally deployment list | awk '/" +
+           CONST.rally_deployment_name +
+           "/ {print $2}'")
+    p = subprocess.Popen(cmd, shell=True,
+                         stdout=subprocess.PIPE,
+                         stderr=subprocess.STDOUT)
+    deployment_uuid = p.stdout.readline().rstrip()
+    if deployment_uuid == "":
+        logger.error("Rally deployment not found.")
+        raise Exception('Error with command:%s' % cmd)
+    return deployment_uuid
+
+
+def get_verifier_repo_dir(verifier_id):
+    """
+    Returns installed verfier repo directory for Tempest
+    """
+    if not verifier_id:
+        verifier_id = get_verifier_id()
+
+    return os.path.join(CONST.dir_rally_inst,
+                        'verification',
+                        'verifier-{}'.format(verifier_id),
+                        'repo')
+
+
+def get_verifier_deployment_dir(verifier_id, deployment_id):
+    """
+    Returns Rally deployment directory for current verifier
+    """
+    if not verifier_id:
+        verifier_id = get_verifier_id()
+
+    if not deployment_id:
+        deployment_id = get_verifier_deployment_id()
+
+    return os.path.join(CONST.dir_rally_inst,
+                        'verification',
+                        'verifier-{}'.format(verifier_id),
+                        'for-deployment-{}'.format(deployment_id))
+
+
+def configure_tempest(deployment_dir, IMAGE_ID=None, FLAVOR_ID=None):
+    """
+    Calls rally verify and updates the generated tempest.conf with
+    given parameters
+    """
+    conf_verifier_result = configure_verifier(deployment_dir)
+    if conf_verifier_result == releng_constants.EXIT_RUN_ERROR:
         return releng_constants.EXIT_RUN_ERROR
+    else:
+        configure_tempest_update_params(conf_verifier_result,
+                                        IMAGE_ID, FLAVOR_ID)
+        return releng_constants.EXIT_OK
+
 
+def configure_tempest_update_params(tempest_conf_file,
+                                    IMAGE_ID=None, FLAVOR_ID=None):
+    """
+    Add/update needed parameters into tempest.conf file
+    """
     logger.debug("Updating selected tempest.conf parameters...")
     config = ConfigParser.RawConfigParser()
     config.read(tempest_conf_file)
@@ -80,6 +149,8 @@ def configure_tempest(logger, deployment_dir, IMAGE_ID=None, FLAVOR_ID=None):
     config.set('identity', 'password', CONST.tempest_identity_user_password)
     config.set(
         'validation', 'ssh_timeout', CONST.tempest_validation_ssh_timeout)
+    config.set('object-storage', 'operator_role',
+               CONST.tempest_object_storage_operator_role)
 
     if CONST.OS_ENDPOINT_TYPE is not None:
         services_list = ['compute',
@@ -100,21 +171,46 @@ def configure_tempest(logger, deployment_dir, IMAGE_ID=None, FLAVOR_ID=None):
         config.write(config_file)
 
     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
-    shutil.copyfile(
-        tempest_conf_file, TEMPEST_RESULTS_DIR + '/tempest.conf')
+    if not os.path.exists(TEMPEST_RESULTS_DIR):
+        os.makedirs(TEMPEST_RESULTS_DIR)
 
-    return releng_constants.EXIT_OK
+    shutil.copyfile(tempest_conf_file,
+                    os.path.join(TEMPEST_RESULTS_DIR, 'tempest.conf'))
+
+
+def configure_verifier(deployment_dir):
+    """
+    Execute rally verify configure-verifier, which generates tempest.conf
+    """
+    tempest_conf_file = os.path.join(deployment_dir, "tempest.conf")
+    if os.path.isfile(tempest_conf_file):
+        logger.debug("Verifier is already configured.")
+        logger.debug("Reconfiguring the current verifier...")
+        cmd = "rally verify configure-verifier --reconfigure"
+    else:
+        logger.info("Configuring the verifier...")
+        cmd = "rally verify configure-verifier"
+    ft_utils.execute_command(cmd)
+    logger.debug("Looking for tempest.conf file...")
+
+    if not os.path.isfile(tempest_conf_file):
+        logger.error("Tempest configuration file %s NOT found."
+                     % tempest_conf_file)
+        return releng_constants.EXIT_RUN_ERROR
+
+    else:
+        return tempest_conf_file
 
 
-def configure_tempest_multisite(logger, deployment_dir):
+def configure_tempest_multisite(deployment_dir):
     """
     Add/update needed parameters into tempest.conf file generated by Rally
     """
     logger.debug("configure the tempest")
-    configure_tempest(logger, deployment_dir)
+    configure_tempest(deployment_dir)
 
     logger.debug("Finding tempest.conf file...")
-    tempest_conf_old = os.path.join(deployment_dir, '/tempest.conf')
+    tempest_conf_old = os.path.join(deployment_dir, 'tempest.conf')
     if not os.path.isfile(tempest_conf_old):
         logger.error("Tempest configuration file %s NOT found."
                      % tempest_conf_old)
@@ -122,7 +218,7 @@ def configure_tempest_multisite(logger, deployment_dir):
 
     # Copy tempest.conf to /home/opnfv/functest/results/tempest/
     cur_path = os.path.split(os.path.realpath(__file__))[0]
-    tempest_conf_file = os.path.join(cur_path, '/tempest_multisite.conf')
+    tempest_conf_file = os.path.join(cur_path, 'tempest_multisite.conf')
     shutil.copyfile(tempest_conf_old, tempest_conf_file)
 
     logger.debug("Updating selected tempest.conf parameters...")
@@ -133,7 +229,7 @@ def configure_tempest_multisite(logger, deployment_dir):
     # cmd = ("openstack endpoint show kingbird | grep publicurl |"
     #       "awk '{print $4}' | awk -F '/' '{print $4}'")
     # kingbird_api_version = os.popen(cmd).read()
-    kingbird_api_version = os_utils.get_endpoint(service_type='kingbird')
+    kingbird_api_version = os_utils.get_endpoint(service_type='multisite')
 
     if CI_INSTALLER_TYPE == 'fuel':
         # For MOS based setup, the service is accessible