Place config_functest.yaml outside the functest repo
[functest.git] / testcases / VIM / OpenStack / CI / libraries / run_tempest.py
index 41fd4b7..e24697c 100644 (file)
@@ -17,6 +17,8 @@ import requests
 import subprocess
 import sys
 import yaml
+import keystoneclient.v2_0.client as ksclient
+from neutronclient.v2_0 import client as neutronclient
 
 modes = ['full', 'smoke', 'baremetal', 'compute', 'data_processing',
          'identity', 'image', 'network', 'object_storage', 'orchestration',
@@ -54,12 +56,18 @@ if not os.path.exists(REPO_PATH):
 sys.path.append(REPO_PATH + "testcases/")
 import functest_utils
 
-with open(REPO_PATH+"testcases/config_functest.yaml") as f:
+with open("/home/opnfv/functest/conf/config_functest.yaml") as f:
     functest_yaml = yaml.safe_load(f)
 f.close()
 TEST_DB = functest_yaml.get("results").get("test_db_url")
 
 MODE = "smoke"
+TENANT_NAME = functest_yaml.get("tempest").get("identity").get("tenant_name")
+TENANT_DESCRIPTION = functest_yaml.get("tempest").get("identity").get("tenant_description")
+USER_NAME = functest_yaml.get("tempest").get("identity").get("user_name")
+USER_PASSWORD = functest_yaml.get("tempest").get("identity").get("user_password")
+DEPLOYMENT_MAME = functest_yaml.get("rally").get("deployment_name")
+RALLY_INSTALLATION_DIR = functest_yaml.get("general").get("directories").get("dir_rally_inst")
 
 
 def get_info(file_result):
@@ -103,6 +111,92 @@ def push_results_to_db(payload, module, pod_name):
     logger.debug(r)
 
 
+def create_tempest_resources():
+    ks_creds = functest_utils.get_credentials("keystone")
+    logger.info("Creating tenant and user for Tempest suite")
+    keystone = ksclient.Client(**ks_creds)
+    tenant_id = functest_utils.create_tenant(keystone, TENANT_NAME, TENANT_DESCRIPTION)
+    if tenant_id == '':
+        logger.error("Error : Failed to create %s tenant" %TENANT_NAME)
+
+    user_id = functest_utils.create_user(keystone, USER_NAME, USER_PASSWORD, None, tenant_id)
+    if user_id == '':
+        logger.error("Error : Failed to create %s user" %USER_NAME)
+
+
+def free_tempest_resources():
+    ks_creds = functest_utils.get_credentials("keystone")
+    logger.info("Deleting tenant and user for Tempest suite)")
+    keystone = ksclient.Client(**ks_creds)
+
+    user_id = functest_utils.get_user_id(keystone, USER_NAME)
+    if user_id == '':
+        logger.error("Error : Failed to get id of %s user" % USER_NAME)
+    else:
+        if not functest_utils.delete_user(keystone, user_id):
+            logger.error("Error : Failed to delete %s user" % USER_NAME)
+
+    tenant_id = functest_utils.get_tenant_id(keystone, TENANT_NAME)
+    if tenant_id == '':
+        logger.error("Error : Failed to get id of %s tenant" % TENANT_NAME)
+    else:
+        if not functest_utils.delete_tenant(keystone, tenant_id):
+            logger.error("Error : Failed to delete %s tenant" % TENANT_NAME)
+
+
+def configure_tempest():
+    """
+    Add/update needed parameters into tempest.conf file generated by Rally
+    """
+
+    logger.debug("Generating tempest.conf file...")
+    cmd = "rally verify genconfig"
+    functest_utils.execute_command(cmd,logger)
+
+    logger.debug("Resolving deployment UUID...")
+    cmd = "rally deployment list | awk '/"+DEPLOYMENT_MAME+"/ {print $2}'"
+    p = subprocess.Popen(cmd, shell=True,
+                         stdout=subprocess.PIPE,
+                         stderr=subprocess.STDOUT);
+    deployment_uuid = p.stdout.readline().rstrip()
+    if deployment_uuid == "":
+        logger.debug("   Rally deployment NOT found")
+        return False
+
+    logger.debug("Finding tempest.conf file...")
+    tempest_conf_file = RALLY_INSTALLATION_DIR+"/tempest/for-deployment-" \
+                        +deployment_uuid+"/tempest.conf"
+    if tempest_conf_file == "":
+        logger.debug("   Tempest configuration file NOT found")
+        return False
+
+    logger.debug("  Updating fixed_network_name...")
+    private_net_name = ""
+    creds_neutron = functest_utils.get_credentials("neutron")
+    neutron_client = neutronclient.Client(**creds_neutron)
+    private_net = functest_utils.get_private_net(neutron_client)
+    if private_net is None:
+        logger.error("No shared private networks found.")
+    else:
+        private_net_name = private_net['name']
+    cmd = "crudini --set "+tempest_conf_file+" compute fixed_network_name " \
+          +private_net_name
+    functest_utils.execute_command(cmd,logger)
+
+    logger.debug("  Updating non-admin credentials...")
+    cmd = "crudini --set "+tempest_conf_file+" identity tenant_name " \
+          +TENANT_NAME
+    functest_utils.execute_command(cmd,logger)
+    cmd = "crudini --set "+tempest_conf_file+" identity username " \
+          +USER_NAME
+    functest_utils.execute_command(cmd,logger)
+    cmd = "crudini --set "+tempest_conf_file+" identity password " \
+          +USER_PASSWORD
+    functest_utils.execute_command(cmd,logger)
+
+    return True
+
+
 def run_tempest(OPTION):
     #
     # the "main" function of the script which launches Rally to run Tempest
@@ -156,7 +250,10 @@ def main():
     else:
         MODE = "--set "+args.mode
 
+    create_tempest_resources()
+    configure_tempest()
     run_tempest(MODE)
+    free_tempest_resources()
 
 
 if __name__ == '__main__':