Catch pruner exception when no images are cached

This handles a situation where the pruner would raise an exception when
the image_cache_max_size was set lower than the size of the last image.

Fixes bug 1039854

Change-Id: I4a6c164e9f821f1250314974f829f058fbd02863
This commit is contained in:
Brian Waldon 2012-08-29 21:17:46 -07:00
parent 6275a28abe
commit abcff5b5f5
2 changed files with 32 additions and 1 deletions

View File

@ -272,7 +272,11 @@ class Driver(base.Driver):
with self.get_db() as db:
cur = db.execute("""SELECT image_id FROM cached_images
ORDER BY last_accessed LIMIT 1""")
image_id = cur.fetchone()[0]
try:
image_id = cur.fetchone()[0]
except TypeError:
# There are no more cached images
return None
path = self.get_image_filepath(image_id)
file_info = os.stat(path)

View File

@ -26,6 +26,8 @@ import stubout
from glance.common import utils
from glance import image_cache
#NOTE(bcwaldon): This is imported to load the registry config options
import glance.registry
from glance.tests import utils as test_utils
from glance.tests.utils import skip_if_disabled, xattr_writes_supported
@ -182,6 +184,31 @@ class ImageCacheTestCase(object):
self.assertTrue(self.cache.is_cached(x),
"Image %s was not cached!" % x)
@skip_if_disabled
def test_prune_to_zero(self):
"""Test that an image_cache_max_size of 0 doesn't kill the pruner
This is a test specifically for LP #1039854
"""
self.assertEqual(0, self.cache.get_cache_size())
FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA)
self.assertTrue(self.cache.cache_image_file('xxx', FIXTURE_FILE))
self.assertEqual(1024, self.cache.get_cache_size())
# OK, hit the image that is now cached...
buff = StringIO.StringIO()
with self.cache.open_for_read('xxx') as cache_file:
for chunk in cache_file:
buff.write(chunk)
self.config(image_cache_max_size=0)
self.cache.prune()
self.assertEqual(0, self.cache.get_cache_size())
self.assertFalse(self.cache.is_cached('xxx'))
@skip_if_disabled
def test_queue(self):
"""