Sfoglia il codice sorgente

Fixed bug in iteration and added tests

Nuwan Goonasekera 8 anni fa
parent
commit
5ea71f5d10

+ 2 - 5
cloudbridge/cloud/base/resources.py

@@ -198,17 +198,14 @@ class BasePageableObjectMixin(PageableObjectMixin):
     """
 
     def __iter__(self):
-        marker = None
-
-        result_list = self.list(marker=marker)
+        result_list = self.list()
         if result_list.supports_server_paging:
             for result in result_list:
                 yield result
             while result_list.is_truncated:
-                result_list = self.list(marker=marker)
+                result_list = self.list(marker=result_list.marker)
                 for result in result_list:
                     yield result
-                marker = result_list.marker
         else:
             for result in result_list.data:
                 yield result

+ 3 - 7
cloudbridge/cloud/providers/openstack/services.py

@@ -288,13 +288,9 @@ class OpenStackImageService(BaseImageService):
         """
         List all images.
         """
-        if marker is None:
-            os_images = self.provider.nova.images.list(
-                limit=oshelpers.os_result_limit(self.provider, limit))
-        else:
-            os_images = self.provider.nova.images.list(
-                limit=oshelpers.os_result_limit(self.provider, limit),
-                marker=marker)
+        os_images = self.provider.nova.images.list(
+            limit=oshelpers.os_result_limit(self.provider, limit),
+            marker=marker)
 
         cb_images = [
             OpenStackMachineImage(self.provider, img)

+ 16 - 13
test/helpers/standard_interface_tests.py

@@ -16,23 +16,26 @@ def check_repr(test, obj):
 
 
 def check_list(test, service, obj):
-    objects = service.list()
-    list_objs = [o for o in objects if o.id == obj.id]
+    list_objs = [o for o in service.list() if o.id == obj.id]
     test.assertTrue(
         len(list_objs) == 1,
-        "List objects for %s does not return the expected object id %s"
-        % (type(obj).__name__, obj.id))
+        "List objects for %s does not return the expected object id %s. Got %s"
+        % (type(obj).__name__, obj.id, list_objs))
     return list_objs
 
 
 def check_iter(test, service, obj):
     # check iteration
-    iter_objs = [o for o in service if o.id == obj.id]
+    iter_objs = list(service)
+    iter_ids = [o.id for o in service]
+    test.assertEqual(len(set(iter_ids)), len(iter_ids),
+                     "Iteration should not return duplicates")
+    match_objs = [o for o in iter_objs if o.id == obj.id]
     test.assertTrue(
-        len(iter_objs) == 1,
-        "Iter objects for %s does not return the expected object id %s"
-        % (type(obj).__name__, obj.id))
-    return iter_objs
+        len(match_objs) == 1,
+        "Iter objects for %s does not return the expected object id %s. Got %s"
+        % (type(obj).__name__, obj.id, match_objs))
+    return match_objs
 
 
 def check_find(test, service, obj):
@@ -40,8 +43,8 @@ def check_find(test, service, obj):
     find_objs = service.find(name=obj.name)
     test.assertTrue(
         len(find_objs) == 1,
-        "Find objects for %s does not return the expected object: %s"
-        % (type(obj).__name__, obj.name))
+        "Find objects for %s does not return the expected object: %s. Got %s"
+        % (type(obj).__name__, obj.name, find_objs))
     return find_objs
 
 
@@ -65,11 +68,11 @@ def check_delete(test, service, obj, perform_delete=False):
         obj.delete()
 
     objs = service.list()
-    found_objs = [o for o in objs if obj.id == obj.id]
+    found_objs = [o for o in objs if o.id == obj.id]
     test.assertTrue(
         len(found_objs) == 0,
         "Object %s in service %s should have been deleted but still exists."
-        % (type(obj).__name__, found_objs))
+        % (found_objs, type(service).__name__))
 
 
 def check_standard_behaviour(test, service, obj):