diff --git a/ceilometer/image/glance.py b/ceilometer/image/glance.py index e96a9a239..9efe2559e 100644 --- a/ceilometer/image/glance.py +++ b/ceilometer/image/glance.py @@ -40,8 +40,7 @@ class _Base(plugin.PollsterBase): return glanceclient.Client('1', endpoint, token=ksclient.auth_token) - def iter_images(self, ksclient): - """Iterate over all images.""" + def _get_images(self, ksclient): client = self.get_glance_client(ksclient) #TODO(eglynn): use pagination to protect against unbounded # memory usage @@ -70,6 +69,12 @@ class _Base(plugin.PollsterBase): imageIdSet -= set([image.id]) 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 def extract_image_metadata(image): return dict((k, getattr(image, k)) @@ -100,7 +105,7 @@ class ImagePollster(_Base): return ['image', 'image.size'] 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( name='image', type=counter.TYPE_GAUGE, diff --git a/tests/image/test_glance.py b/tests/image/test_glance.py index f1618b8b6..6d00f03a9 100644 --- a/tests/image/test_glance.py +++ b/tests/image/test_glance.py @@ -133,12 +133,21 @@ class TestImagePollster(base.TestCase): self.stubs.Set(glance._Base, 'get_glance_client', self.fake_get_glance_client) - # Tests whether the iter_images method returns an unique image list. 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(). - iter_images(self.manager.keystone)) + _iter_images(self.manager.keystone, {})) 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): counters = list(glance.ImagePollster().get_counters(self.manager, {})) self.assertEqual(len(counters), 6)