Author: tomaz
Date: Sat Jul 2 19:22:48 2011
New Revision: 1142275
URL: http://svn.apache.org/viewvc?rev=1142275&view=rev
Log:
1. Default connection class for StorageDriver should be ConnectionUserAndKey
2. Throw an exception is user passes an invalid value for the iterator argument
Added:
libcloud/trunk/test/storage/test_base.py
Modified:
libcloud/trunk/libcloud/storage/base.py
Modified: libcloud/trunk/libcloud/storage/base.py
URL:
http://svn.apache.org/viewvc/libcloud/trunk/libcloud/storage/base.py?rev=1142275&r1=1142274&r2=1142275&view=diff
==============================================================================
--- libcloud/trunk/libcloud/storage/base.py (original)
+++ libcloud/trunk/libcloud/storage/base.py Sat Jul 2 19:22:48 2011
@@ -27,7 +27,7 @@ from os.path import join as pjoin
from libcloud import utils
from libcloud.common.types import LibcloudError
-from libcloud.common.base import ConnectionKey
+from libcloud.common.base import ConnectionUserAndKey
from libcloud.storage.types import ObjectDoesNotExistError
CHUNK_SIZE = 8096
@@ -156,7 +156,7 @@ class StorageDriver(object):
A base StorageDriver to derive from.
"""
- connectionCls = ConnectionKey
+ connectionCls = ConnectionUserAndKey
name = None
hash_type = 'md5'
@@ -508,6 +508,10 @@ class StorageDriver(object):
if file_path and not os.path.exists(file_path):
raise OSError('File %s does not exist' % (file_path))
+ if iterator is not None and not hasattr(iterator, 'next'):
+ raise AttributeError('iterator object must implement next() ' +
+ 'method.')
+
if not content_type:
if file_path:
name = file_path
Added: libcloud/trunk/test/storage/test_base.py
URL:
http://svn.apache.org/viewvc/libcloud/trunk/test/storage/test_base.py?rev=1142275&view=auto
==============================================================================
--- libcloud/trunk/test/storage/test_base.py (added)
+++ libcloud/trunk/test/storage/test_base.py Sat Jul 2 19:22:48 2011
@@ -0,0 +1,67 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import sys
+import unittest
+from StringIO import StringIO
+
+from libcloud.storage.base import StorageDriver
+
+from test import StorageMockHttp # pylint: disable-msg=E0611
+
+class BaseStorageTests(unittest.TestCase):
+ def setUp(self):
+ StorageDriver.connectionCls.conn_classes = (None, StorageMockHttp)
+ self.driver = StorageDriver('username', 'key', host='localhost')
+
+ def test__upload_object_iterator_must_have_next_method(self):
+ class Iterator(object):
+ def next(self):
+ pass
+
+ class Iterator2(file):
+ def __init__(self):
+ pass
+
+ class SomeClass(object):
+ pass
+
+ valid_iterators = [ Iterator(), Iterator2(), StringIO('bar') ]
+ invalid_iterators = [ 'foobar', '', False, True, 1, object() ]
+
+ def upload_func(*args, **kwargs):
+ return True, 'barfoo', 100
+
+ kwargs = {'object_name': 'foo', 'content_type': 'foo/bar',
+ 'upload_func': upload_func, 'upload_func_kwargs': {},
+ 'request_path': '/', 'headers': {}}
+
+ for value in valid_iterators:
+ kwargs['iterator'] = value
+ self.driver._upload_object(**kwargs)
+
+ for value in invalid_iterators:
+ kwargs['iterator'] = value
+
+ try:
+ self.driver._upload_object(**kwargs)
+ except AttributeError:
+ pass
+ else:
+ self.fail('Exception was not thrown')
+
+
+if __name__ == '__main__':
+ sys.exit(unittest.main())