VMware store.add to return the image size uploaded

This patch fixes a tempest test failing for the VMware datastore.
When the size provided to add() is zero but the image size is actually
larger than zero, add() should return the size of the image uploaded
and not zero.

Tempest failure:
api.image.v1.test_images.CreateRegisterImagesTest.test_register_then_upload

Change-Id: I16524c0f25aa4124c24ca25ac014819267dd72c3
Closes-Bug: #1292764
This commit is contained in:
Arnaud Legendre 2014-03-14 20:14:01 -07:00
parent ddae276b32
commit 3bbdc70d4e
2 changed files with 42 additions and 4 deletions

View File

@ -111,12 +111,18 @@ class _Reader(object):
def __init__(self, data, checksum):
self.data = data
self.checksum = checksum
self._size = 0
def read(self, len):
result = self.data.read(len)
def read(self, length):
result = self.data.read(length)
self._size += len(result)
self.checksum.update(result)
return result
@property
def size(self):
return self._size
class StoreLocation(glance.store.location.StoreLocation):
"""Class describing an VMware URI.
@ -285,7 +291,7 @@ class Store(glance.store.base.Store):
raise exception.UnexpectedStatus(status=res.status,
body=res.read())
return (loc.get_uri(), image_size, checksum.hexdigest(), {})
return (loc.get_uri(), image_file.size, checksum.hexdigest(), {})
def get(self, location):
"""Takes a `glance.store.location.Location` object that indicates

View File

@ -24,6 +24,7 @@ import six
from glance.common import exception
from glance.openstack.common import units
from glance.store.location import get_location_from_uri
import glance.store.vmware_datastore as vm_store
from glance.store.vmware_datastore import Store
from glance.tests.unit import base
from glance.tests import utils
@ -160,13 +161,15 @@ class TestStore(base.StoreClearingUnitTest):
HttpConn.return_value = FakeHTTPConnection(status=404)
self.assertRaises(exception.NotFound, self.store.get, loc)
def test_add(self):
@mock.patch.object(vm_store._Reader, 'size')
def test_add(self, fake_size):
"""Test that we can add an image via the VMware backend"""
expected_image_id = str(uuid.uuid4())
expected_size = FIVE_KB
expected_contents = "*" * expected_size
hash_code = hashlib.md5(expected_contents)
expected_checksum = hash_code.hexdigest()
fake_size.__get__ = mock.Mock(return_value=expected_size)
with mock.patch('hashlib.md5') as md5:
md5.return_value = hash_code
expected_location = format_location(
@ -185,6 +188,35 @@ class TestStore(base.StoreClearingUnitTest):
self.assertEqual(expected_size, size)
self.assertEqual(expected_checksum, checksum)
@mock.patch.object(vm_store._Reader, 'size')
def test_add_size_zero(self, fake_size):
"""
Test that when specifying size zero for the image to add,
the actual size of the image is returned.
"""
expected_image_id = str(uuid.uuid4())
expected_size = FIVE_KB
expected_contents = "*" * expected_size
hash_code = hashlib.md5(expected_contents)
expected_checksum = hash_code.hexdigest()
fake_size.__get__ = mock.Mock(return_value=expected_size)
with mock.patch('hashlib.md5') as md5:
md5.return_value = hash_code
expected_location = format_location(
VMWARE_DATASTORE_CONF['vmware_server_host'],
VMWARE_DATASTORE_CONF['vmware_store_image_dir'],
expected_image_id,
VMWARE_DATASTORE_CONF['vmware_datacenter_path'],
VMWARE_DATASTORE_CONF['vmware_datastore_name'])
image = six.StringIO(expected_contents)
with mock.patch('httplib.HTTPConnection') as HttpConn:
HttpConn.return_value = FakeHTTPConnection()
location, size, checksum, _ = self.store.add(expected_image_id,
image, 0)
self.assertEqual(expected_location, location)
self.assertEqual(expected_size, size)
self.assertEqual(expected_checksum, checksum)
def test_delete(self):
"""Test we can delete an existing image in the VMware store"""
loc = get_location_from_uri(