]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Fix exception on uploading a volume to image with glance v2 API
authorTomoki Sekiyama <tomoki.sekiyama@hds.com>
Tue, 28 Jul 2015 18:08:15 +0000 (14:08 -0400)
committerTomoki Sekiyama <tomoki.sekiyama@hds.com>
Tue, 28 Jul 2015 18:18:10 +0000 (14:18 -0400)
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

cinder/image/glance.py
cinder/tests/unit/glance/stubs.py
cinder/tests/unit/image/test_glance.py

index c4a1afe2de55478233ea51d692725dc9721579c5..b1fc25e69dad2317ba0b9940c51cb9b00e38016a 100644 (file)
@@ -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)
index 6094496c4d6fdaa9913b0cd3b97eb3ed74220828..291687c3a647186c78ce854d1601bd611844aa55 100644 (file)
@@ -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):
index c53c3aae8902750968b716d39b92594b1cfb5969..71fd0f3141cc8a2cee195e418b0df214da106683 100644 (file)
@@ -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')