Add pagination to v1 image-list

* Use recursive generator function to make subsequent requests
  to the v1 detailed images resource
* 'limit' continues to act as the absolute limit of images to return
  from a list call
* 'page_size' indicates how many images to ask for in each subsequent
  pagination request
* Expose --page-size through the cli
* Convert v1 images tests to use strict url comparison
* Drop strict_url_check from FakeAPI kwargs - now the functionality
  is always active and tests must directly match fixture urls
* Fix bug 1024614

Change-Id: Ifa7874d88360e03b5c8aa95bfb9d5e6dc6dc927e
This commit is contained in:
Brian Waldon
2012-07-14 04:39:27 +00:00
parent 570e64d91f
commit 0f628b19cb
5 changed files with 151 additions and 38 deletions
+1 -4
View File
@@ -15,16 +15,13 @@
class FakeAPI(object):
def __init__(self, fixtures, strict_url_check=False):
def __init__(self, fixtures):
self.fixtures = fixtures
self.calls = []
self.strict_url_check = strict_url_check
def _request(self, method, url, headers=None, body=None):
call = (method, url, headers or {}, body)
self.calls.append(call)
if not self.strict_url_check:
url = url.split('?', 1)[0]
return self.fixtures[url][method]
def raw_request(self, *args, **kwargs):
+106 -17
View File
@@ -42,7 +42,87 @@ fixtures = {
),
),
},
'/v1/images/detail': {
'/v1/images/detail?limit=20': {
'GET': (
{},
{'images': [
{
'id': 'a',
'name': 'image-1',
'properties': {'arch': 'x86_64'},
},
{
'id': 'b',
'name': 'image-2',
'properties': {'arch': 'x86_64'},
},
]},
),
},
'/v1/images/detail?marker=a&limit=20': {
'GET': (
{},
{'images': [
{
'id': 'b',
'name': 'image-1',
'properties': {'arch': 'x86_64'},
},
{
'id': 'c',
'name': 'image-2',
'properties': {'arch': 'x86_64'},
},
]},
),
},
'/v1/images/detail?limit=2': {
'GET': (
{},
{'images': [
{
'id': 'a',
'name': 'image-1',
'properties': {'arch': 'x86_64'},
},
{
'id': 'b',
'name': 'image-2',
'properties': {'arch': 'x86_64'},
},
]},
),
},
'/v1/images/detail?marker=b&limit=2': {
'GET': (
{},
{'images': [
{
'id': 'c',
'name': 'image-3',
'properties': {'arch': 'x86_64'},
},
]},
),
},
'/v1/images/detail?limit=20&name=foo': {
'GET': (
{},
{'images': [
{
'id': 'a',
'name': 'image-1',
'properties': {'arch': 'x86_64'},
},
{
'id': 'b',
'name': 'image-2',
'properties': {'arch': 'x86_64'},
},
]},
),
},
'/v1/images/detail?property-ping=pong&limit=20': {
'GET': (
{},
{'images': [
@@ -94,33 +174,42 @@ class ImageManagerTest(unittest.TestCase):
self.api = utils.FakeAPI(fixtures)
self.mgr = glanceclient.v1.images.ImageManager(self.api)
def test_list(self):
images = self.mgr.list()
expect = [('GET', '/v1/images/detail', {}, None)]
def test_paginated_list(self):
images = list(self.mgr.list(page_size=2))
expect = [
('GET', '/v1/images/detail?limit=2', {}, None),
('GET', '/v1/images/detail?marker=b&limit=2', {}, None),
]
self.assertEqual(self.api.calls, expect)
self.assertEqual(len(images), 1)
self.assertEqual(images[0].id, '1')
self.assertEqual(images[0].name, 'image-1')
self.assertEqual(images[0].properties, {'arch': 'x86_64'})
self.assertEqual(len(images), 3)
self.assertEqual(images[0].id, 'a')
self.assertEqual(images[1].id, 'b')
self.assertEqual(images[2].id, 'c')
def test_list_with_limit(self):
self.mgr.list(limit=10)
expect = [('GET', '/v1/images/detail?limit=10', {}, None)]
def test_list_with_limit_less_than_page_size(self):
list(self.mgr.list(page_size=20, limit=10))
expect = [('GET', '/v1/images/detail?limit=20', {}, None)]
self.assertEqual(self.api.calls, expect)
def test_list_with_limit_greater_than_page_size(self):
list(self.mgr.list(page_size=20, limit=30))
expect = [('GET', '/v1/images/detail?limit=20', {}, None)]
self.assertEqual(self.api.calls, expect)
def test_list_with_marker(self):
self.mgr.list(marker=20)
expect = [('GET', '/v1/images/detail?marker=20', {}, None)]
list(self.mgr.list(marker='a'))
expect = [('GET', '/v1/images/detail?marker=a&limit=20', {}, None)]
self.assertEqual(self.api.calls, expect)
def test_list_with_filter(self):
self.mgr.list(filters={'name': "foo"})
expect = [('GET', '/v1/images/detail?name=foo', {}, None)]
list(self.mgr.list(filters={'name': "foo"}))
expect = [('GET', '/v1/images/detail?limit=20&name=foo', {}, None)]
self.assertEqual(self.api.calls, expect)
def test_list_with_property_filters(self):
self.mgr.list(filters={'properties': {'ping': 'pong'}})
expect = [('GET', '/v1/images/detail?property-ping=pong', {}, None)]
list(self.mgr.list(filters={'properties': {'ping': 'pong'}}))
url = '/v1/images/detail?property-ping=pong&limit=20'
expect = [('GET', url, {}, None)]
self.assertEqual(self.api.calls, expect)
def test_get(self):
+1 -1
View File
@@ -84,7 +84,7 @@ FakeModel = warlock.model_factory(fake_schema)
class TestController(unittest.TestCase):
def setUp(self):
super(TestController, self).setUp()
self.api = utils.FakeAPI(fixtures, strict_url_check=True)
self.api = utils.FakeAPI(fixtures)
self.controller = images.Controller(self.api, FakeModel)
def test_list_images(self):