Merge "Update glance pollster to use cache"
This commit is contained in:
commit
4cb77294cd
@ -40,8 +40,7 @@ class _Base(plugin.PollsterBase):
|
|||||||
return glanceclient.Client('1', endpoint,
|
return glanceclient.Client('1', endpoint,
|
||||||
token=ksclient.auth_token)
|
token=ksclient.auth_token)
|
||||||
|
|
||||||
def iter_images(self, ksclient):
|
def _get_images(self, ksclient):
|
||||||
"""Iterate over all images."""
|
|
||||||
client = self.get_glance_client(ksclient)
|
client = self.get_glance_client(ksclient)
|
||||||
#TODO(eglynn): use pagination to protect against unbounded
|
#TODO(eglynn): use pagination to protect against unbounded
|
||||||
# memory usage
|
# memory usage
|
||||||
@ -70,6 +69,12 @@ class _Base(plugin.PollsterBase):
|
|||||||
imageIdSet -= set([image.id])
|
imageIdSet -= set([image.id])
|
||||||
yield image
|
yield image
|
||||||
|
|
||||||
|
def _iter_images(self, ksclient, cache):
|
||||||
|
"""Iterate over all images."""
|
||||||
|
if 'images' not in cache:
|
||||||
|
cache['images'] = list(self._get_images(ksclient))
|
||||||
|
return iter(cache['images'])
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def extract_image_metadata(image):
|
def extract_image_metadata(image):
|
||||||
return dict((k, getattr(image, k))
|
return dict((k, getattr(image, k))
|
||||||
@ -100,7 +105,7 @@ class ImagePollster(_Base):
|
|||||||
return ['image', 'image.size']
|
return ['image', 'image.size']
|
||||||
|
|
||||||
def get_counters(self, manager, cache):
|
def get_counters(self, manager, cache):
|
||||||
for image in self.iter_images(manager.keystone):
|
for image in self._iter_images(manager.keystone, cache):
|
||||||
yield counter.Counter(
|
yield counter.Counter(
|
||||||
name='image',
|
name='image',
|
||||||
type=counter.TYPE_GAUGE,
|
type=counter.TYPE_GAUGE,
|
||||||
|
@ -133,12 +133,21 @@ class TestImagePollster(base.TestCase):
|
|||||||
self.stubs.Set(glance._Base, 'get_glance_client',
|
self.stubs.Set(glance._Base, 'get_glance_client',
|
||||||
self.fake_get_glance_client)
|
self.fake_get_glance_client)
|
||||||
|
|
||||||
# Tests whether the iter_images method returns an unique image list.
|
|
||||||
def test_iter_images(self):
|
def test_iter_images(self):
|
||||||
|
# Tests whether the iter_images method returns an unique image
|
||||||
|
# list when there is nothing in the cache
|
||||||
images = list(glance.ImagePollster().
|
images = list(glance.ImagePollster().
|
||||||
iter_images(self.manager.keystone))
|
_iter_images(self.manager.keystone, {}))
|
||||||
self.assertEqual(len(images), len(set(image.id for image in images)))
|
self.assertEqual(len(images), len(set(image.id for image in images)))
|
||||||
|
|
||||||
|
def test_iter_images_cached(self):
|
||||||
|
# Tests whether the iter_images method returns the values from
|
||||||
|
# the cache
|
||||||
|
cache = {'images': []}
|
||||||
|
images = list(glance.ImagePollster().
|
||||||
|
_iter_images(self.manager.keystone, cache))
|
||||||
|
self.assertEqual(images, [])
|
||||||
|
|
||||||
def test_glance_image_counter(self):
|
def test_glance_image_counter(self):
|
||||||
counters = list(glance.ImagePollster().get_counters(self.manager, {}))
|
counters = list(glance.ImagePollster().get_counters(self.manager, {}))
|
||||||
self.assertEqual(len(counters), 6)
|
self.assertEqual(len(counters), 6)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user