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
This commit is contained in:
Steven Kaufer 2014-03-07 04:54:50 +00:00
parent ae9c73f8bf
commit 5cee4f4107
2 changed files with 11 additions and 1 deletions

View File

@ -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]

View File

@ -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)