Use glance to list images 05/36405/5
authorLinda Wang <wangwulin@huawei.com>
Fri, 23 Jun 2017 05:11:31 +0000 (05:11 +0000)
committerLinda Wang <wangwulin@huawei.com>
Sat, 24 Jun 2017 02:21:35 +0000 (02:21 +0000)
1. Images list is not supported by novaclient 8.0.0
2. It moves images list out of nova and into glance

Change-Id: I2a2d40a2ca3a0cf1ebfb55697b3c58af8748a805
Signed-off-by: Linda Wang <wangwulin@huawei.com>
functest/tests/unit/utils/test_openstack_clean.py
functest/tests/unit/utils/test_openstack_snapshot.py
functest/tests/unit/utils/test_openstack_utils.py
functest/utils/openstack_clean.py
functest/utils/openstack_snapshot.py
functest/utils/openstack_utils.py

index fe7b50d..5feed16 100644 (file)
@@ -672,6 +672,7 @@ class OSCleanTesting(unittest.TestCase):
                                               RegexMatch(" Removing "
                                                          "\s*\S+..."))
 
+    @mock.patch('functest.utils.openstack_clean.os_utils.get_glance_client')
     @mock.patch('functest.utils.openstack_clean.os_utils.get_cinder_client')
     @mock.patch('functest.utils.openstack_clean.os_utils'
                 '.get_keystone_client')
@@ -684,7 +685,7 @@ class OSCleanTesting(unittest.TestCase):
     @mock.patch('functest.utils.openstack_clean.logger.debug')
     def test_main_default(self, mock_logger_debug, mock_logger_info,
                           mock_creds, mock_nova, mock_neutron,
-                          mock_keystone, mock_cinder):
+                          mock_keystone, mock_cinder, mock_glance):
 
         with mock.patch('functest.utils.openstack_clean.remove_instances') \
             as mock_remove_instances, \
index d3f9399..8b3635e 100644 (file)
@@ -186,6 +186,7 @@ class OSTackerTesting(unittest.TestCase):
             mock_logger_debug.assert_called_once_with("Getting tenants...")
             self.assertDictEqual(resp, {'tenants': {}})
 
+    @mock.patch('functest.utils.openstack_clean.os_utils.get_glance_client')
     @mock.patch('functest.utils.openstack_snapshot.os_utils.get_cinder_client')
     @mock.patch('functest.utils.openstack_snapshot.os_utils'
                 '.get_keystone_client')
@@ -197,7 +198,7 @@ class OSTackerTesting(unittest.TestCase):
     @mock.patch('functest.utils.openstack_snapshot.logger.debug')
     def test_main_default(self, mock_logger_debug, mock_logger_info,
                           mock_creds, mock_nova, mock_neutron,
-                          mock_keystone, mock_cinder):
+                          mock_keystone, mock_cinder, mock_glance):
         with mock.patch('functest.utils.openstack_snapshot.get_instances',
                         return_value=self.update_list), \
             mock.patch('functest.utils.openstack_snapshot.get_images',
index 0f06b1e..8dab87b 100644 (file)
@@ -1472,7 +1472,7 @@ class OSUtilsTesting(unittest.TestCase):
 
     def test_get_images_default(self):
         self.assertEqual(openstack_utils.
-                         get_images(self.nova_client),
+                         get_images(self.glance_client),
                          [self.image])
 
     @mock.patch('functest.utils.openstack_utils.logger.error')
index 0ce0879..29ebb33 100755 (executable)
@@ -80,22 +80,25 @@ def remove_instances(nova_client, default_instances):
         break
 
 
-def remove_images(nova_client, default_images):
+def remove_images(glance_client, default_images):
     logger.debug("Removing Glance images...")
-    images = os_utils.get_images(nova_client)
-    if images is None or len(images) == 0:
+    images = os_utils.get_images(glance_client)
+    if images is None:
+        return -1
+    images = {image.id: image.name for image in images}
+    if len(images) == 0:
         logger.debug("No images found.")
         return
 
     for image in images:
-        image_name = getattr(image, 'name')
-        image_id = getattr(image, 'id')
+        image_id = image
+        image_name = images.get(image_id)
         logger.debug("'%s', ID=%s " % (image_name, image_id))
         if (image_id not in default_images and
                 image_name not in default_images.values()):
             logger.debug("Removing image '%s', ID=%s ..."
                          % (image_name, image_id))
-            if os_utils.delete_glance_image(nova_client, image_id):
+            if os_utils.delete_glance_image(glance_client, image_id):
                 logger.debug("  > Done!")
             else:
                 logger.error("There has been a problem removing the"
@@ -385,6 +388,7 @@ def main():
     neutron_client = os_utils.get_neutron_client()
     keystone_client = os_utils.get_keystone_client()
     cinder_client = os_utils.get_cinder_client()
+    glance_client = os_utils.get_glance_client()
 
     try:
         with open(OS_SNAPSHOT_FILE) as f:
@@ -411,7 +415,7 @@ def main():
 
     remove_instances(nova_client, default_instances)
     separator()
-    remove_images(nova_client, default_images)
+    remove_images(glance_client, default_images)
     separator()
     remove_volumes(cinder_client, default_volumes)
     separator()
index 233c316..c6c1615 100755 (executable)
@@ -48,13 +48,13 @@ def get_instances(nova_client):
     return {'instances': dic_instances}
 
 
-def get_images(nova_client):
+def get_images(glance_client):
     logger.debug("Getting images...")
     dic_images = {}
-    images = os_utils.get_images(nova_client)
-    if not (images is None or len(images) == 0):
-        for image in images:
-            dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
+    images = os_utils.get_images(glance_client)
+    if images is None:
+        return -1
+    dic_images.update({image.id: image.name for image in images})
     return {'images': dic_images}
 
 
@@ -136,6 +136,7 @@ def main():
     neutron_client = os_utils.get_neutron_client()
     keystone_client = os_utils.get_keystone_client()
     cinder_client = os_utils.get_cinder_client()
+    glance_client = os_utils.get_glance_client()
 
     if not os_utils.check_credentials():
         logger.error("Please source the openrc credentials and run the" +
@@ -144,7 +145,7 @@ def main():
 
     snapshot = {}
     snapshot.update(get_instances(nova_client))
-    snapshot.update(get_images(nova_client))
+    snapshot.update(get_images(glance_client))
     snapshot.update(get_volumes(cinder_client))
     snapshot.update(get_networks(neutron_client))
     snapshot.update(get_routers(neutron_client))
index f155449..3e27d67 100644 (file)
@@ -1192,9 +1192,9 @@ def delete_security_group(neutron_client, secgroup_id):
 # *********************************************
 #   GLANCE
 # *********************************************
-def get_images(nova_client):
+def get_images(glance_client):
     try:
-        images = nova_client.images.list()
+        images = glance_client.images.list()
         return images
     except Exception as e:
         logger.error("Error [get_images]: %s" % e)
@@ -1258,12 +1258,12 @@ def get_or_create_image(name, path, format):
     return image_exists, image_id
 
 
-def delete_glance_image(nova_client, image_id):
+def delete_glance_image(glance_client, image_id):
     try:
-        nova_client.images.delete(image_id)
+        glance_client.images.delete(image_id)
         return True
     except Exception as e:
-        logger.error("Error [delete_glance_image(nova_client, '%s')]: %s"
+        logger.error("Error [delete_glance_image(glance_client, '%s')]: %s"
                      % (image_id, e))
         return False