Fixing bug in paginated calls caused after linting, added tests to cover cases


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/5fb0b924
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/5fb0b924
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/5fb0b924

Branch: refs/heads/trunk
Commit: 5fb0b924a66b9473fa490d7e74643fead9b0ec00
Parents: 418b4e0
Author: Jeffrey Dunham <jeffrey.a.dun...@gmail.com>
Authored: Thu May 5 16:26:15 2016 -0400
Committer: Mark S. Maglana <mmagl...@gmail.com>
Committed: Fri Sep 16 11:21:47 2016 -0700

----------------------------------------------------------------------
 libcloud/common/dimensiondata.py                | 15 +++----
 .../server_server_paginated_empty.xml           |  1 +
 libcloud/test/compute/test_dimensiondata.py     | 41 ++++++++++++++++++++
 3 files changed, 50 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/5fb0b924/libcloud/common/dimensiondata.py
----------------------------------------------------------------------
diff --git a/libcloud/common/dimensiondata.py b/libcloud/common/dimensiondata.py
index 4ba1bca..9562a9f 100644
--- a/libcloud/common/dimensiondata.py
+++ b/libcloud/common/dimensiondata.py
@@ -480,13 +480,14 @@ class DimensionDataConnection(ConnectionUserAndKey):
         params['pageSize'] = page_size
 
         paged_resp = self.request_with_orgId_api_2(action, params,
-                                                   data, headers,
-                                                   method).object
-        yield paged_resp
-        paged_resp = paged_resp or {}
-
-        while int(paged_resp.get('pageCount')) >= \
-                int(paged_resp.get('pageSize')):
+                                                   data, headers,
+                                                   method).object
+        yield paged_resp
+        if len(paged_resp) <= 0:
+            raise StopIteration
+
+        while int(paged_resp.get('pageCount')) >= \
+                int(paged_resp.get('pageSize')):
             params['pageNumber'] = int(paged_resp.get('pageNumber')) + 1
             paged_resp = self.request_with_orgId_api_2(action, params,
                                                        data, headers,

http://git-wip-us.apache.org/repos/asf/libcloud/blob/5fb0b924/libcloud/test/compute/fixtures/dimensiondata/server_server_paginated_empty.xml
----------------------------------------------------------------------
diff --git 
a/libcloud/test/compute/fixtures/dimensiondata/server_server_paginated_empty.xml
 
b/libcloud/test/compute/fixtures/dimensiondata/server_server_paginated_empty.xml
new file mode 100644
index 0000000..31f4c55
--- /dev/null
+++ 
b/libcloud/test/compute/fixtures/dimensiondata/server_server_paginated_empty.xml
@@ -0,0 +1 @@
+<?xml version="1.0" encoding="UTF-8"?><servers 
xmlns="urn:didata.com:api:cloud:types" pageNumber="1" pageCount="0" 
totalCount="0" pageSize="250"/>

http://git-wip-us.apache.org/repos/asf/libcloud/blob/5fb0b924/libcloud/test/compute/test_dimensiondata.py
----------------------------------------------------------------------
diff --git a/libcloud/test/compute/test_dimensiondata.py 
b/libcloud/test/compute/test_dimensiondata.py
index 5b5d755..40bce15 100644
--- a/libcloud/test/compute/test_dimensiondata.py
+++ b/libcloud/test/compute/test_dimensiondata.py
@@ -104,6 +104,26 @@ class DimensionDataTests(unittest.TestCase, TestCaseMixin):
         ret = self.driver.list_nodes()
         self.assertEqual(len(ret), 9)
 
+    def test_paginated_mcp2_call_EMPTY(self):
+        # cache org
+        self.driver.connection._get_orgId()
+        DimensionDataMockHttp.type = 'EMPTY'
+        node_list_generator = 
self.driver.connection.paginated_request_with_orgId_api_2('server/server')
+        empty_node_list = []
+        for node_list in node_list_generator:
+            empty_node_list.extend(node_list)
+        self.assertTrue(len(empty_node_list) == 0)
+
+    def test_paginated_mcp2_call_PAGED_THEN_EMPTY(self):
+        # cache org
+        self.driver.connection._get_orgId()
+        DimensionDataMockHttp.type = 'PAGED_THEN_EMPTY'
+        node_list_generator = 
self.driver.connection.paginated_request_with_orgId_api_2('server/server')
+        final_node_list = []
+        for node_list in node_list_generator:
+            final_node_list.extend(node_list)
+        self.assertTrue(len(final_node_list) == 2)
+
     def test_paginated_mcp2_call_with_page_size(self):
         # cache org
         self.driver.connection._get_orgId()
@@ -1407,6 +1427,21 @@ class DimensionDataMockHttp(StorageMockHttp, MockHttp):
             'server_server.xml')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
+    def 
_caas_2_2_8a8f6abc_2745_4d8a_9cbc_8dabe5a7d0e4_server_server_EMPTY(self, 
method, url, body, headers):
+        body = self.fixtures.load(
+            'server_server_paginated_empty.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
+    def 
_caas_2_2_8a8f6abc_2745_4d8a_9cbc_8dabe5a7d0e4_server_server_PAGED_THEN_EMPTY(self,
 method, url, body, headers):
+        if 'pageNumber=2' in url:
+            body = self.fixtures.load(
+                'server_server_paginated_empty.xml')
+            return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+        else:
+            body = self.fixtures.load(
+                'server_server_paginated.xml')
+            return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
     def 
_caas_2_2_8a8f6abc_2745_4d8a_9cbc_8dabe5a7d0e4_server_server_PAGINATED(self, 
method, url, body, headers):
         if 'pageNumber=2' in url:
             body = self.fixtures.load(
@@ -1417,6 +1452,12 @@ class DimensionDataMockHttp(StorageMockHttp, MockHttp):
                 'server_server_paginated.xml')
             return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
+    def 
_caas_2_2_8a8f6abc_2745_4d8a_9cbc_8dabe5a7d0e4_server_server_PAGINATEDEMPTY(self,
 method, url, body, headers):
+        print("In empty")
+        body = self.fixtures.load(
+            'server_server_paginated_empty.xml')
+        return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+
     def 
_caas_2_2_8a8f6abc_2745_4d8a_9cbc_8dabe5a7d0e4_server_server_ALLFILTERS(self, 
method, url, body, headers):
         (_, params) = url.split('?')
         parameters = params.split('&')

Reply via email to