Add pagination to v2 image-list

* Use a recursive generator function to iterate over the image
  container. The presence of next links are indicators to
  continue pagination while their value drives the location of
  the next page.
* A user can pass in --page-size on the command line, or page_size
  when using the controller directly, to control how many images
  are requested with each subsequent paginated request. Default page
  size is 20.
* Add a flag (strict_url_check) for the FakeAPI class to control
  whether it chops off query params when trying to match a request
  to a fixture.
* Related to bp glance-client-v2.

Change-Id: Ib98e912a7af0bb570b4fd738733edd9b837d1a12
This commit is contained in:
Brian Waldon
2012-07-14 03:06:03 +00:00
parent 95a7f9dffe
commit d88d8fc462
4 changed files with 65 additions and 9 deletions
+36 -2
View File
@@ -22,7 +22,7 @@ from tests import utils
fixtures = {
'/v2/images': {
'/v2/images?limit=20': {
'GET': (
{},
{'images': [
@@ -37,6 +37,32 @@ fixtures = {
]},
),
},
'/v2/images?limit=1': {
'GET': (
{},
{
'images': [
{
'id': '3a4560a1-e585-443e-9b39-553b46ec92d1',
'name': 'image-1',
},
],
'next': ('/v2/images?limit=1&'
'marker=3a4560a1-e585-443e-9b39-553b46ec92d1'),
},
),
},
('/v2/images?limit=1&marker=3a4560a1-e585-443e-9b39-553b46ec92d1'): {
'GET': (
{},
{'images': [
{
'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
'name': 'image-2',
},
]},
),
},
'/v2/images/3a4560a1-e585-443e-9b39-553b46ec92d1': {
'GET': (
{},
@@ -58,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)
self.api = utils.FakeAPI(fixtures, strict_url_check=True)
self.controller = images.Controller(self.api, FakeModel)
def test_list_images(self):
@@ -69,6 +95,14 @@ class TestController(unittest.TestCase):
self.assertEqual(images[1].id, '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810')
self.assertEqual(images[1].name, 'image-2')
def test_list_images_paginated(self):
#NOTE(bcwaldon): cast to list since the controller returns a generator
images = list(self.controller.list(page_size=1))
self.assertEqual(images[0].id, '3a4560a1-e585-443e-9b39-553b46ec92d1')
self.assertEqual(images[0].name, 'image-1')
self.assertEqual(images[1].id, '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810')
self.assertEqual(images[1].name, 'image-2')
def test_get_image(self):
image = self.controller.get('3a4560a1-e585-443e-9b39-553b46ec92d1')
self.assertEqual(image.id, '3a4560a1-e585-443e-9b39-553b46ec92d1')