Fix exception on uploading a volume to image with glance v2 API

This fixes an exception on uploading a volume to image when
the glance v2 api is enabled. Also, this adds the unit tests
for updating metadata and data both with glance API v1 and v2.

Change-Id: Id70eff82837c0af1f033a2bb749ef367cd9e32fb
Closes-bug: #1478737
changes/45/206645/1
Tomoki Sekiyama 7 years ago
parent d620337353
commit beef71d0f8
  1. 4
      cinder/image/glance.py
  2. 30
      cinder/tests/unit/glance/stubs.py
  3. 18
      cinder/tests/unit/image/test_glance.py

@ -323,8 +323,8 @@ class GlanceImageService(object):
try:
# NOTE(dosaboy): the v2 api separates update from upload
if data and CONF.glance_api_version > 1:
image_meta = self._client.call(context, 'upload', image_id,
image_meta['data'])
self._client.call(context, 'upload', image_id, data)
image_meta = self._client.call(context, 'get', image_id)
else:
image_meta = self._client.call(context, 'update', image_id,
**image_meta)

@ -34,9 +34,13 @@ class StubGlanceClient(object):
# NOTE(bcwaldon): HACK to get client.images.* to work
self.images = lambda: None
for fn in ('list', 'get', 'data', 'create', 'update', 'delete'):
for fn in ('list', 'get', 'data', 'create', 'update', 'upload',
'delete'):
setattr(self.images, fn, getattr(self, fn))
self.schemas = lambda: None
setattr(self.schemas, 'get', getattr(self, 'schemas_get'))
# TODO(bcwaldon): implement filters
def list(self, filters=None, marker=None, limit=30):
if marker is None:
@ -58,8 +62,11 @@ class StubGlanceClient(object):
raise glanceclient.exc.NotFound(image_id)
def data(self, image_id):
self.get(image_id)
return []
image = self.get(image_id)
if getattr(image, 'size', 0):
return ['*' * image.size]
else:
return []
def create(self, **metadata):
metadata['created_at'] = NOW_GLANCE_FORMAT
@ -81,7 +88,10 @@ class StubGlanceClient(object):
for i, image in enumerate(self._images):
if image.id == str(image_id):
for k, v in metadata.items():
setattr(self._images[i], k, v)
if k == 'data':
setattr(self._images[i], 'size', len(v))
else:
setattr(self._images[i], k, v)
return self._images[i]
raise glanceclient.exc.NotFound(image_id)
@ -92,6 +102,18 @@ class StubGlanceClient(object):
return
raise glanceclient.exc.NotFound(image_id)
def upload(self, image_id, data):
for i, image in enumerate(self._images):
if image.id == image_id:
setattr(self._images[i], 'size', len(data))
return
raise glanceclient.exc.NotFound(image_id)
def schemas_get(self, schema_name):
if schema_name != 'image':
raise glanceclient.exc.NotFound()
return FakeSchema()
class FakeImage(object):
def __init__(self, metadata):

@ -334,6 +334,24 @@ class TestGlanceImageService(test.TestCase):
new_image_data = self.service.show(self.context, image_id)
self.assertEqual('new image name', new_image_data['name'])
def test_update_v2(self):
self.flags(glance_api_version=2)
self.test_update()
def test_update_with_data(self):
fixture = self._make_fixture(name='test image')
image = self.service.create(self.context, fixture)
image_id = image['id']
data = '*' * 256
self.service.update(self.context, image_id, fixture, data=data)
new_image_data = self.service.show(self.context, image_id)
self.assertEqual(256, new_image_data['size'])
def test_update_with_data_v2(self):
self.flags(glance_api_version=2)
self.test_update_with_data()
def test_delete(self):
fixture1 = self._make_fixture(name='test image 1')
fixture2 = self._make_fixture(name='test image 2')

Loading…
Cancel
Save