Extend fake image service to let it hold image data
Make the fake image service able to store image data in memory. This is handy for unit testing, but probably not very useful otherwise (images are pretty big, so holding them in memory is not a very good idea). Change-Id: I2d07baf24cab544e19521b9486feee3272d0407e
This commit is contained in:
parent
26d2bad187
commit
6c0fd03fc3
@ -118,6 +118,7 @@ class _FakeImageService(object):
|
||||
self.create(None, image3)
|
||||
self.create(None, image4)
|
||||
self.create(None, image5)
|
||||
self._imagedata = {}
|
||||
super(_FakeImageService, self).__init__()
|
||||
|
||||
def index(self, context, filters=None, marker=None, limit=None):
|
||||
@ -132,6 +133,11 @@ class _FakeImageService(object):
|
||||
"""Return list of detailed image information."""
|
||||
return copy.deepcopy(self.images.values())
|
||||
|
||||
def get(self, context, image_id, data):
|
||||
metadata = self.show(context, image_id)
|
||||
data.write(self._imagedata.get(image_id, ''))
|
||||
return metadata
|
||||
|
||||
def show(self, context, image_id):
|
||||
"""Get data about specified image.
|
||||
|
||||
@ -164,6 +170,8 @@ class _FakeImageService(object):
|
||||
if image_id in self.images:
|
||||
raise exception.Duplicate()
|
||||
self.images[image_id] = copy.deepcopy(metadata)
|
||||
if data:
|
||||
self._imagedata[image_id] = data.read()
|
||||
return self.images[image_id]
|
||||
|
||||
def update(self, context, image_id, metadata, data=None):
|
||||
|
@ -16,6 +16,7 @@
|
||||
# under the License.
|
||||
|
||||
import datetime
|
||||
import StringIO
|
||||
|
||||
from nova import context
|
||||
from nova import exception
|
||||
@ -128,6 +129,16 @@ class _ImageTestCase(test.TestCase):
|
||||
index = self.image_service.index(self.context)
|
||||
self.assertEquals(len(index), 0)
|
||||
|
||||
def test_create_then_get(self):
|
||||
blob = 'some data'
|
||||
s1 = StringIO.StringIO(blob)
|
||||
self.image_service.create(self.context,
|
||||
{'id': '32', 'foo': 'bar'},
|
||||
data=s1)
|
||||
s2 = StringIO.StringIO()
|
||||
self.image_service.get(self.context, '32', data=s2)
|
||||
self.assertEquals(s2.getvalue(), blob, 'Did not get blob back intact')
|
||||
|
||||
|
||||
class FakeImageTestCase(_ImageTestCase):
|
||||
def setUp(self):
|
||||
|
Loading…
Reference in New Issue
Block a user