refactor create or get image process to eliminate reduplicate 29/19429/3
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Tue, 23 Aug 2016 05:44:36 +0000 (13:44 +0800)
committerSerenaFeng <feng.xiaowei@zte.com.cn>
Fri, 26 Aug 2016 01:21:06 +0000 (09:21 +0800)
Some places when create image is referred, they check if image exist,
if exist use it directly, else create a new one.
Abstract a method to integrate the process

JIRA: FUNCTEST-432

Change-Id: I83d1112aa5cb0d3cdfab92fd49cd5f2c1cceff82
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
testcases/OpenStack/rally/run_rally-cert.py
testcases/OpenStack/tempest/run_tempest.py
testcases/OpenStack/vPing/vping_util.py
utils/openstack_utils.py

index 92dbddf..279bcde 100755 (executable)
@@ -376,7 +376,6 @@ def main():
 
     nova_client = os_utils.get_nova_client()
     neutron_client = os_utils.get_neutron_client()
-    glance_client = os_utils.get_glance_client()
     cinder_client = os_utils.get_cinder_client()
 
     start_time = time.time()
@@ -402,26 +401,11 @@ def main():
     else:
         logger.debug("Using existing volume type(s)...")
 
-    image_id = os_utils.get_image_id(glance_client, GLANCE_IMAGE_NAME)
-    image_exists = False
-
-    if image_id == '':
-        logger.debug("Creating image '%s' from '%s'..." % (GLANCE_IMAGE_NAME,
-                                                           GLANCE_IMAGE_PATH))
-        image_id = os_utils.create_glance_image(glance_client,
-                                                GLANCE_IMAGE_NAME,
-                                                GLANCE_IMAGE_PATH,
-                                                GLANCE_IMAGE_FORMAT)
-        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))
-        image_exists = True
+    image_exists, image_id = os_utils.get_or_create_image(GLANCE_IMAGE_NAME,
+                                                          GLANCE_IMAGE_PATH,
+                                                          GLANCE_IMAGE_FORMAT)
+    if not image_id:
+        exit(-1)
 
     logger.debug("Creating network '%s'..." % PRIVATE_NET_NAME)
     network_dict = os_utils.create_network_full(neutron_client,
index 64a5ed7..10b22c9 100755 (executable)
@@ -126,7 +126,6 @@ def get_info(file_result):
 def create_tempest_resources():
     keystone_client = os_utils.get_keystone_client()
     neutron_client = os_utils.get_neutron_client()
-    glance_client = os_utils.get_glance_client()
 
     logger.debug("Creating tenant and user for Tempest suite")
     tenant_id = os_utils.create_tenant(keystone_client,
@@ -159,22 +158,11 @@ def create_tempest_resources():
         exit(-1)
 
     logger.debug("Creating image for Tempest suite")
-    # Check if the given image exists
-    image_id = os_utils.get_image_id(glance_client, GLANCE_IMAGE_NAME)
-    if image_id != '':
-        logger.info("Using existing image '%s'..." % GLANCE_IMAGE_NAME)
-    else:
-        logger.info("Creating image '%s' from '%s'..." % (GLANCE_IMAGE_NAME,
-                                                          GLANCE_IMAGE_PATH))
-        image_id = os_utils.create_glance_image(glance_client,
-                                                GLANCE_IMAGE_NAME,
-                                                GLANCE_IMAGE_PATH,
-                                                GLANCE_IMAGE_FORMAT)
-        if not image_id:
-            logger.error("Failed to create a Glance image...")
-            exit(-1)
-        logger.debug("Image '%s' with ID=%s created successfully."
-                     % (GLANCE_IMAGE_NAME, image_id))
+    _, image_id = os_utils.get_or_create_image(GLANCE_IMAGE_NAME,
+                                               GLANCE_IMAGE_PATH,
+                                               GLANCE_IMAGE_FORMAT)
+    if not image_id:
+        exit(-1)
 
 
 def configure_tempest(deployment_dir):
index c16c5d6..3f4adae 100644 (file)
@@ -147,24 +147,11 @@ def create_security_group():
 
 
 def create_image():
-    EXIT_CODE = -1
-
-    # Check if the given image exists
-    image_id = os_utils.get_image_id(glance_client, GLANCE_IMAGE_NAME)
-    if image_id != '':
-        logger.info("Using existing image '%s'..." % GLANCE_IMAGE_NAME)
-    else:
-        logger.info("Creating image '%s' from '%s'..." % (GLANCE_IMAGE_NAME,
-                                                          GLANCE_IMAGE_PATH))
-        image_id = os_utils.create_glance_image(glance_client,
-                                                GLANCE_IMAGE_NAME,
-                                                GLANCE_IMAGE_PATH,
-                                                GLANCE_IMAGE_FORMAT)
-        if not image_id:
-            logger.error("Failed to create a Glance image...")
-            exit(EXIT_CODE)
-        logger.debug("Image '%s' with ID=%s created successfully."
-                     % (GLANCE_IMAGE_NAME, image_id))
+    _, image_id = os_utils.get_or_create_image(GLANCE_IMAGE_NAME,
+                                               GLANCE_IMAGE_PATH,
+                                               GLANCE_IMAGE_FORMAT)
+    if not image_id:
+        exit(-1)
 
     return image_id
 
index d30ca62..a7bc899 100755 (executable)
@@ -889,6 +889,26 @@ def create_glance_image(glance_client, image_name, file_path, disk="qcow2",
         return None
 
 
+def get_or_create_image(name, path, format):
+    image_exists = False
+    glance_client = get_glance_client()
+
+    image_id = get_image_id(glance_client, name)
+    if image_id != '':
+        logger.info("Using existing image '%s'..." % name)
+        image_exists = True
+    else:
+        logger.info("Creating image '%s' from '%s'..." % (name, path))
+        image_id = create_glance_image(glance_client, name, path, format)
+        if not image_id:
+            logger.error("Failed to create a Glance image...")
+        else:
+            logger.debug("Image '%s' with ID=%s created successfully."
+                         % (name, image_id))
+
+    return image_exists, image_id
+
+
 def delete_glance_image(nova_client, image_id):
     try:
         nova_client.images.delete(image_id)