Handle image cache hashing on shared storage.
Resolves bug 1075018. Change-Id: Iddb092468fae1e7af18134bc433708410d15184d
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
import contextlib
|
import contextlib
|
||||||
import cStringIO
|
import cStringIO
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
@@ -52,7 +53,7 @@ class ImageCacheManagerTestCase(test.TestCase):
|
|||||||
|
|
||||||
def test_read_stored_checksum_missing(self):
|
def test_read_stored_checksum_missing(self):
|
||||||
self.stubs.Set(os.path, 'exists', lambda x: False)
|
self.stubs.Set(os.path, 'exists', lambda x: False)
|
||||||
csum = imagecache.read_stored_checksum('/tmp/foo')
|
csum = imagecache.read_stored_checksum('/tmp/foo', timestamped=False)
|
||||||
self.assertEquals(csum, None)
|
self.assertEquals(csum, None)
|
||||||
|
|
||||||
def test_read_stored_checksum(self):
|
def test_read_stored_checksum(self):
|
||||||
@@ -68,7 +69,8 @@ class ImageCacheManagerTestCase(test.TestCase):
|
|||||||
f.write(csum_input)
|
f.write(csum_input)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
csum_output = imagecache.read_stored_checksum(fname)
|
csum_output = imagecache.read_stored_checksum(fname,
|
||||||
|
timestamped=False)
|
||||||
self.assertEquals(csum_input.rstrip(),
|
self.assertEquals(csum_input.rstrip(),
|
||||||
'{"sha1": "%s"}' % csum_output)
|
'{"sha1": "%s"}' % csum_output)
|
||||||
|
|
||||||
@@ -84,7 +86,8 @@ class ImageCacheManagerTestCase(test.TestCase):
|
|||||||
f.write('fdghkfhkgjjksfdgjksjkghsdf')
|
f.write('fdghkfhkgjjksfdgjksjkghsdf')
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
csum_output = imagecache.read_stored_checksum(fname)
|
csum_output = imagecache.read_stored_checksum(fname,
|
||||||
|
timestamped=False)
|
||||||
self.assertEquals(csum_output, 'fdghkfhkgjjksfdgjksjkghsdf')
|
self.assertEquals(csum_output, 'fdghkfhkgjjksfdgjksjkghsdf')
|
||||||
self.assertFalse(os.path.exists(old_fname))
|
self.assertFalse(os.path.exists(old_fname))
|
||||||
self.assertTrue(os.path.exists(virtutils.get_info_filename(fname)))
|
self.assertTrue(os.path.exists(virtutils.get_info_filename(fname)))
|
||||||
@@ -350,9 +353,8 @@ class ImageCacheManagerTestCase(test.TestCase):
|
|||||||
fname = os.path.join(tmpdir, 'aaa')
|
fname = os.path.join(tmpdir, 'aaa')
|
||||||
info_fname = virtutils.get_info_filename(fname)
|
info_fname = virtutils.get_info_filename(fname)
|
||||||
|
|
||||||
f = open(fname, 'w')
|
with open(fname, 'w') as f:
|
||||||
f.write(testdata)
|
f.write(testdata)
|
||||||
f.close()
|
|
||||||
|
|
||||||
return fname, info_fname, testdata
|
return fname, info_fname, testdata
|
||||||
|
|
||||||
@@ -479,26 +481,23 @@ class ImageCacheManagerTestCase(test.TestCase):
|
|||||||
self.assertNotEqual(log.find('image verification failed'), -1)
|
self.assertNotEqual(log.find('image verification failed'), -1)
|
||||||
|
|
||||||
def test_verify_checksum_file_missing(self):
|
def test_verify_checksum_file_missing(self):
|
||||||
img = {'container_format': 'ami', 'id': '42'}
|
|
||||||
|
|
||||||
self.flags(checksum_base_images=True)
|
self.flags(checksum_base_images=True)
|
||||||
|
|
||||||
with self._intercept_log_messages() as stream:
|
with utils.tempdir() as tmpdir:
|
||||||
with utils.tempdir() as tmpdir:
|
self.flags(instances_path=tmpdir)
|
||||||
self.flags(instances_path=tmpdir)
|
self.flags(image_info_filename_pattern=('$instances_path/'
|
||||||
self.flags(image_info_filename_pattern=('$instances_path/'
|
'%(image)s.info'))
|
||||||
'%(image)s.info'))
|
|
||||||
|
|
||||||
fname, info_fname, testdata = self._make_checksum(tmpdir)
|
fname, info_fname, testdata = self._make_checksum(tmpdir)
|
||||||
|
|
||||||
# Checksum file missing
|
# Checksum file missing
|
||||||
image_cache_manager = imagecache.ImageCacheManager()
|
image_cache_manager = imagecache.ImageCacheManager()
|
||||||
res = image_cache_manager._verify_checksum(img, fname)
|
res = image_cache_manager._verify_checksum('aaa', fname)
|
||||||
self.assertEquals(res, None)
|
self.assertEquals(res, None)
|
||||||
|
|
||||||
# Checksum requests for a file with no checksum now have the
|
# Checksum requests for a file with no checksum now have the
|
||||||
# side effect of creating the checksum
|
# side effect of creating the checksum
|
||||||
self.assertTrue(os.path.exists(info_fname))
|
self.assertTrue(os.path.exists(info_fname))
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def _make_base_file(self, checksum=True):
|
def _make_base_file(self, checksum=True):
|
||||||
@@ -682,9 +681,12 @@ class ImageCacheManagerTestCase(test.TestCase):
|
|||||||
img = '123'
|
img = '123'
|
||||||
|
|
||||||
with self._make_base_file() as fname:
|
with self._make_base_file() as fname:
|
||||||
f = open(fname, 'w')
|
with open(fname, 'w') as f:
|
||||||
f.write('banana')
|
f.write('banana')
|
||||||
f.close()
|
|
||||||
|
d = {'sha1': '21323454'}
|
||||||
|
with open('%s.info' % fname, 'w') as f:
|
||||||
|
f.write(json.dumps(d))
|
||||||
|
|
||||||
image_cache_manager = imagecache.ImageCacheManager()
|
image_cache_manager = imagecache.ImageCacheManager()
|
||||||
image_cache_manager.unexplained_images = [fname]
|
image_cache_manager.unexplained_images = [fname]
|
||||||
|
|||||||
Reference in New Issue
Block a user