From 5cee4f41078ff692a37bb4c553f8faa68c782253 Mon Sep 17 00:00:00 2001 From: Steven Kaufer Date: Fri, 7 Mar 2014 04:54:50 +0000 Subject: [PATCH] get volumes API does not handle limit=0 This bug fixes a regression from bug 1288429, where the "next" link is added when the number of volumes returned is the maximum limit (even if the "limit" param is not specified). The regression is hit when a "limit" of 0 is specified; in this case the logic to create the "next" link is still executed and an exception is thrown. The fix is to add back in the "if" check that implictly checks if the max_items is non-0. Test code was also created to verify that a limit of 0 is handled correctly. Change-Id: I92f9afb9b0e2c627d2c77f67fa026b731903384f Closes-bug: 1289124 --- cinder/api/common.py | 2 +- cinder/tests/api/v2/test_volumes.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cinder/api/common.py b/cinder/api/common.py index d38146243..9ad0847f6 100644 --- a/cinder/api/common.py +++ b/cinder/api/common.py @@ -258,7 +258,7 @@ class ViewBuilder(object): max_items = min( int(request.params.get("limit", CONF.osapi_max_limit)), CONF.osapi_max_limit) - if max_items == len(items): + if max_items and max_items == len(items): last_item = items[-1] if id_key in last_item: last_item_id = last_item[id_key] diff --git a/cinder/tests/api/v2/test_volumes.py b/cinder/tests/api/v2/test_volumes.py index 9d7333cc8..64cd68262 100644 --- a/cinder/tests/api/v2/test_volumes.py +++ b/cinder/tests/api/v2/test_volumes.py @@ -747,6 +747,16 @@ class VolumeApiTest(test.TestCase): self.controller.index, req) + def test_volume_with_limit_zero(self): + def stub_volume_get_all(context, marker, limit, + sort_key, sort_dir): + return [] + self.stubs.Set(db, 'volume_get_all', stub_volume_get_all) + req = fakes.HTTPRequest.blank('/v2/volumes?limit=0') + res_dict = self.controller.index(req) + expected = {'volumes': []} + self.assertEqual(res_dict, expected) + def test_volume_default_limit(self): self.stubs.Set(volume_api.API, 'get', stubs.stub_volume_get) -- 2.45.2