Check if rally image already exist and don't create it again if so
[functest.git] / testcases / VIM / OpenStack / CI / libraries / run_rally.py
index 09ed41d..a7f1db1 100644 (file)
@@ -21,14 +21,16 @@ import argparse
 import logging
 import yaml
 import requests
+import subprocess
 import sys
-import novaclient.v2.client as novaclient
+from novaclient import client as novaclient
+from keystoneclient.v2_0 import client as keystoneclient
+from glanceclient import client as glanceclient
 
 """ tests configuration """
 tests = ['authenticate', 'glance', 'cinder', 'ceilometer', 'heat', 'keystone',
          'neutron', 'nova', 'quotas', 'requests', 'vm', 'all']
 parser = argparse.ArgumentParser()
-parser.add_argument("repo_path", help="Path to the repository")
 parser.add_argument("test_name",
                     help="Module name to be tested"
                          "Possible values are : "
@@ -45,9 +47,8 @@ parser.add_argument("-r", "--report",
 
 args = parser.parse_args()
 
-sys.path.append(args.repo_path + "testcases/")
-import functest_utils
 
+FNULL = open(os.devnull, 'w')
 """ logging configuration """
 logger = logging.getLogger("run_rally")
 logger.setLevel(logging.DEBUG)
@@ -63,16 +64,22 @@ formatter = logging.Formatter("%(asctime)s - %(name)s - "
 ch.setFormatter(formatter)
 logger.addHandler(ch)
 
-with open(args.repo_path+"testcases/config_functest.yaml") as f:
+REPO_PATH=os.environ['repos_dir']+'/functest/'
+if not os.path.exists(REPO_PATH):
+    logger.error("Functest repository directory not found '%s'" % REPO_PATH)
+    exit(-1)
+sys.path.append(REPO_PATH + "testcases/")
+import functest_utils
+
+with open("/home/opnfv/functest/conf/config_functest.yaml") as f:
     functest_yaml = yaml.safe_load(f)
 f.close()
 
 HOME = os.environ['HOME']+"/"
-REPO_PATH = args.repo_path
 SCENARIOS_DIR = REPO_PATH + functest_yaml.get("general"). \
     get("directories").get("dir_rally_scn")
-RESULTS_DIR = HOME + functest_yaml.get("general").get("directories"). \
-    get("dir_rally_res") + "/rally/"
+RESULTS_DIR = functest_yaml.get("general").get("directories"). \
+    get("dir_rally_res")
 TEST_DB = functest_yaml.get("results").get("test_db_url")
 
 GLANCE_IMAGE_NAME = "functest-img-rally"
@@ -84,14 +91,15 @@ GLANCE_IMAGE_PATH = functest_yaml.get("general"). \
     get("directories").get("dir_functest_data") + "/" + GLANCE_IMAGE_FILENAME
 
 
-def push_results_to_db(payload, module):
+def push_results_to_db(payload):
 
     url = TEST_DB + "/results"
     installer = functest_utils.get_installer_type(logger)
-    git_version = functest_utils.get_git_branch(args.repo_path)
+    git_version = functest_utils.get_git_branch(REPO_PATH)
+    pod_name = functest_utils.get_pod_name(logger)
     # TODO pod_name hardcoded, info shall come from Jenkins
     params = {"project_name": "functest", "case_name": "Rally",
-              "pod_name": "opnfv-jump-2", "installer": installer,
+              "pod_name": pod_name, "installer": installer,
               "version": git_version, "details": payload}
 
     headers = {'Content-Type': 'application/json'}
@@ -114,16 +122,6 @@ def get_task_id(cmd_raw):
     return None
 
 
-def create_glance_image(path, name, disk_format):
-    """
-    Create a glance image given the absolute path of the image, its name and the disk format
-    """
-    cmd = ("glance image-create --name " + name + "  --visibility public "
-           "--disk-format " + disk_format + " --container-format bare --file " + path)
-    functest_utils.execute_command(cmd, logger)
-    return True
-
-
 def task_succeed(json_raw):
     """
     Parse JSON from rally JSON results
@@ -166,8 +164,14 @@ def run_task(test_name):
         logger.debug('Scenario fetched from : {}'.format(test_file_name))
         cmd_line = "rally task start --abort-on-sla-failure {}".format(test_file_name)
         logger.debug('running command line : {}'.format(cmd_line))
-        cmd = os.popen(cmd_line)
-        task_id = get_task_id(cmd.read())
+        p = subprocess.Popen(cmd_line, stdout=subprocess.PIPE, stderr=FNULL, shell=True)
+        result = ""
+        while p.poll() is None:
+            l = p.stdout.readline()
+            print l.replace('\n', '')
+            result += l
+
+        task_id = get_task_id(result)
         logger.debug('task_id : {}'.format(task_id))
 
         if task_id is None:
@@ -203,7 +207,7 @@ def run_task(test_name):
         # Push results in payload of testcase
         if args.report:
             logger.debug("Push result into DB")
-            push_results_to_db(json_data, test_name)
+            push_results_to_db(json_data)
 
         """ parse JSON operation result """
         if task_succeed(json_results):
@@ -215,20 +219,6 @@ def run_task(test_name):
                      .format(test_name))
 
 
-def delete_glance_image(name):
-    cmd = ("glance image-delete $(glance image-list | grep %s "
-           "| awk '{print $2}' | head -1)" % name)
-    functest_utils.execute_command(cmd, logger)
-    return True
-
-
-def cleanup(nova):
-    logger.info("Cleaning up...")
-    logger.debug("Deleting image...")
-    delete_glance_image(GLANCE_IMAGE_NAME)
-    return True
-
-
 def main():
     # configure script
     if not (args.test_name in tests):
@@ -236,19 +226,32 @@ def main():
         exit(-1)
 
     creds_nova = functest_utils.get_credentials("nova")
-    nova_client = novaclient.Client(**creds_nova)
-
-    logger.debug("Creating image '%s' from '%s'..." % (GLANCE_IMAGE_NAME, GLANCE_IMAGE_PATH))
-    create_glance_image(GLANCE_IMAGE_PATH, GLANCE_IMAGE_NAME, GLANCE_IMAGE_FORMAT)
-
-    # Check if the given image exists
-    try:
-        nova_client.images.find(name=GLANCE_IMAGE_NAME)
-        logger.info("Glance image found '%s'" % GLANCE_IMAGE_NAME)
-    except:
-        logger.error("ERROR: Glance image '%s' not found." % GLANCE_IMAGE_NAME)
-        logger.info("Available images are: ")
-        exit(-1)
+    nova_client = novaclient.Client('2',**creds_nova)
+    creds_keystone = functest_utils.get_credentials("keystone")
+    keystone_client = keystoneclient.Client(**creds_keystone)
+    glance_endpoint = keystone_client.service_catalog.url_for(service_type='image',
+                                                   endpoint_type='publicURL')
+    glance_client = glanceclient.Client(1, glance_endpoint,
+                                        token=keystone_client.auth_token)
+
+
+    image_id = functest_utils.get_image_id(glance_client, GLANCE_IMAGE_NAME)
+
+    if image_id == '':
+        logger.debug("Creating image '%s' from '%s'..." % (GLANCE_IMAGE_NAME, \
+                                                           GLANCE_IMAGE_PATH))
+        image_id = functest_utils.create_glance_image(glance_client,\
+                                                GLANCE_IMAGE_NAME,GLANCE_IMAGE_PATH)
+        if not image_id:
+            logger.error("Failed to create the Glance image...")
+            exit(-1)
+        else:
+            logger.debug("Image '%s' with ID '%s' created succesfully ." \
+                         % (GLANCE_IMAGE_NAME, image_id))
+    else:
+        logger.debug("Using existing image '%s' with ID '%s'..." \
+                     % (GLANCE_IMAGE_NAME,image_id))
+
 
     if args.test_name == "all":
         for test_name in tests:
@@ -263,7 +266,10 @@ def main():
         print(args.test_name)
         run_task(args.test_name)
 
-    cleanup(nova_client)
+    logger.debug("Deleting image '%s' with ID '%s'..." \
+                         % (GLANCE_IMAGE_NAME, image_id))
+    if not functest_utils.delete_glance_image(nova_client, image_id):
+        logger.error("Error deleting the glance image")
 
 if __name__ == '__main__':
     main()