summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2015-04-02 17:40:33 +0000
committerGerrit Code Review <review@openstack.org>2015-04-02 17:40:33 +0000
commitb9cbb4c8e9a4ef17a8282a27bc02b2c14cf7e5f1 (patch)
tree14320ad352e6a7171f892be33222ed39afebbcba
parentcf9b058163e737dd5beb9f2bc7ca61be8215c592 (diff)
parent95d3561de8afb01251b657b639d028bcf5f13002 (diff)
downloadglance-b9cbb4c8e9a4ef17a8282a27bc02b2c14cf7e5f1.tar.gz
Merge "Glance cache to not prune newly cached images"
-rw-r--r--glance/image_cache/drivers/sqlite.py4
-rw-r--r--glance/tests/unit/test_image_cache.py29
2 files changed, 21 insertions, 12 deletions
diff --git a/glance/image_cache/drivers/sqlite.py b/glance/image_cache/drivers/sqlite.py
index 21730c84c..74c31736f 100644
--- a/glance/image_cache/drivers/sqlite.py
+++ b/glance/image_cache/drivers/sqlite.py
@@ -321,8 +321,8 @@ class Driver(base.Driver):
db.execute("""INSERT INTO cached_images
(image_id, last_accessed, last_modified, hits, size)
- VALUES (?, 0, ?, 0, ?)""",
- (image_id, now, filesize))
+ VALUES (?, ?, ?, 0, ?)""",
+ (image_id, now, now, filesize))
db.commit()
def rollback(e):
diff --git a/glance/tests/unit/test_image_cache.py b/glance/tests/unit/test_image_cache.py
index 2730d244e..260958426 100644
--- a/glance/tests/unit/test_image_cache.py
+++ b/glance/tests/unit/test_image_cache.py
@@ -174,16 +174,15 @@ class ImageCacheTestCase(object):
"""
self.assertEqual(0, self.cache.get_cache_size())
- # Add a bunch of images to the cache. The max cache
- # size for the cache is set to 5KB and each image is
- # 1K. We add 10 images to the cache and then we'll
- # prune it. We should see only 5 images left after
- # pruning, and the images that are least recently accessed
- # should be the ones pruned...
+ # Add a bunch of images to the cache. The max cache size for the cache
+ # is set to 5KB and each image is 1K. We use 11 images in this test.
+ # The first 10 are added to and retrieved from cache in the same order.
+ # Then, the 11th image is added to cache but not retrieved before we
+ # prune. We should see only 5 images left after pruning, and the
+ # images that are least recently accessed should be the ones pruned...
for x in range(10):
FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
- self.assertTrue(self.cache.cache_image_file(x,
- FIXTURE_FILE))
+ self.assertTrue(self.cache.cache_image_file(x, FIXTURE_FILE))
self.assertEqual(10 * units.Ki, self.cache.get_cache_size())
@@ -194,18 +193,28 @@ class ImageCacheTestCase(object):
for chunk in cache_file:
buff.write(chunk)
+ # Add a new image to cache.
+ # This is specifically to test the bug: 1438564
+ FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
+ self.assertTrue(self.cache.cache_image_file(99, FIXTURE_FILE))
+
self.cache.prune()
self.assertEqual(5 * units.Ki, self.cache.get_cache_size())
- for x in range(0, 5):
+ # Ensure images 0, 1, 2, 3, 4 & 5 are not cached anymore
+ for x in range(0, 6):
self.assertFalse(self.cache.is_cached(x),
"Image %s was cached!" % x)
- for x in range(5, 10):
+ # Ensure images 6, 7, 8 and 9 are still cached
+ for x in range(6, 10):
self.assertTrue(self.cache.is_cached(x),
"Image %s was not cached!" % x)
+ # Ensure the newly added image, 99, is still cached
+ self.assertTrue(self.cache.is_cached(99), "Image 99 was not cached!")
+
@skip_if_disabled
def test_prune_to_zero(self):
"""Test that an image_cache_max_size of 0 doesn't kill the pruner