Author: tomaz
Date: Sat Jul 2 22:11:45 2011
New Revision: 1142332
URL: http://svn.apache.org/viewvc?rev=1142332&view=rev
Log:
Allow users to upload 0-bytes long objects
Modified:
libcloud/trunk/libcloud/storage/base.py
libcloud/trunk/test/storage/test_base.py
Modified: libcloud/trunk/libcloud/storage/base.py
URL:
http://svn.apache.org/viewvc/libcloud/trunk/libcloud/storage/base.py?rev=1142332&r1=1142331&r2=1142332&view=diff
==============================================================================
--- libcloud/trunk/libcloud/storage/base.py (original)
+++ libcloud/trunk/libcloud/storage/base.py Sat Jul 2 22:11:45 2011
@@ -581,8 +581,18 @@ class StorageDriver(object):
try:
chunk = generator.next()
except StopIteration:
- # No data?
- return False, None, None
+ # Special case when StopIteration is thrown on the first iteration
-
+ # create a 0-byte long object
+ chunk = ''
+ if chunked:
+ response.connection.connection.send('%X\r\n' %
+ (len(chunk)))
+ response.connection.connection.send(chunk)
+ response.connection.connection.send('\r\n')
+ response.connection.connection.send('0\r\n\r\n')
+ else:
+ response.connection.connection.send(chunk)
+ return True, data_hash.hexdigest(), bytes_transferred
while len(chunk) > 0:
try:
Modified: libcloud/trunk/test/storage/test_base.py
URL:
http://svn.apache.org/viewvc/libcloud/trunk/test/storage/test_base.py?rev=1142332&r1=1142331&r2=1142332&view=diff
==============================================================================
--- libcloud/trunk/test/storage/test_base.py (original)
+++ libcloud/trunk/test/storage/test_base.py Sat Jul 2 22:11:45 2011
@@ -15,7 +15,10 @@
import sys
import unittest
+import hashlib
+
from StringIO import StringIO
+from mock import Mock
from libcloud.storage.base import StorageDriver
@@ -23,6 +26,7 @@ from test import StorageMockHttp # pylin
class BaseStorageTests(unittest.TestCase):
def setUp(self):
+ self.send_called = 0
StorageDriver.connectionCls.conn_classes = (None, StorageMockHttp)
self.driver = StorageDriver('username', 'key', host='localhost')
@@ -62,6 +66,36 @@ class BaseStorageTests(unittest.TestCase
else:
self.fail('Exception was not thrown')
+ def test_upload_zero_bytes_long_object_via_stream(self):
+ iterator = Mock()
+ iterator.next.side_effect = StopIteration()
+
+ def mock_send(data):
+ self.send_called += 1
+
+ response = Mock()
+ response.connection.connection.send = mock_send
+
+ # Normal
+ success, data_hash, bytes_transferred = \
+ self.driver._stream_data(response=response, iterator=iterator,
+ chunked=False, calculate_hash=True)
+
+ self.assertTrue(success)
+ self.assertEqual(data_hash, hashlib.md5('').hexdigest())
+ self.assertEqual(bytes_transferred, 0)
+ self.assertEqual(self.send_called, 1)
+
+ # Chunked
+ success, data_hash, bytes_transferred = \
+ self.driver._stream_data(response=response, iterator=iterator,
+ chunked=True, calculate_hash=True)
+
+ self.assertTrue(success)
+ self.assertEqual(data_hash, hashlib.md5('').hexdigest())
+ self.assertEqual(bytes_transferred, 0)
+ self.assertEqual(self.send_called, 5)
+
if __name__ == '__main__':
sys.exit(unittest.main())