You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by an...@apache.org on 2017/04/13 09:33:18 UTC

[12/46] libcloud git commit: fix s3 tests to use proper response classes

fix s3 tests to use proper response classes


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/d5ff8eb3
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/d5ff8eb3
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/d5ff8eb3

Branch: refs/heads/trunk
Commit: d5ff8eb348da8dace81c35d5af7908905d077eb6
Parents: 67f55ee
Author: Anthony Shaw <an...@apache.org>
Authored: Tue Apr 11 14:07:51 2017 +1000
Committer: Anthony Shaw <an...@apache.org>
Committed: Tue Apr 11 14:07:51 2017 +1000

----------------------------------------------------------------------
 libcloud/http.py                 |  3 +++
 libcloud/test/__init__.py        | 19 +++++++++----------
 libcloud/test/storage/test_s3.py | 30 ++++++++++++------------------
 3 files changed, 24 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/d5ff8eb3/libcloud/http.py
----------------------------------------------------------------------
diff --git a/libcloud/http.py b/libcloud/http.py
index e78d92c..4e47589 100644
--- a/libcloud/http.py
+++ b/libcloud/http.py
@@ -201,6 +201,9 @@ class LibcloudConnection(LibcloudBaseConnection):
     def request(self, method, url, body=None, headers=None, raw=False,
                 stream=False):
         url = urlparse.urljoin(self.host, url)
+        # all headers should be strings
+        if 'Content-Length' in headers and isinstance(headers['Content-Length'], int):
+            headers['Content-Length'] = str(headers['Content-Length'])
         self.response = self.session.request(
             method=method.lower(),
             url=url,

http://git-wip-us.apache.org/repos/asf/libcloud/blob/d5ff8eb3/libcloud/test/__init__.py
----------------------------------------------------------------------
diff --git a/libcloud/test/__init__.py b/libcloud/test/__init__.py
index 2f97f7c..0c6e5fc 100644
--- a/libcloud/test/__init__.py
+++ b/libcloud/test/__init__.py
@@ -16,7 +16,8 @@
 import sys
 import random
 import requests
-
+from libcloud.common.base import Response
+from libcloud.http import HttpLibResponseProxy
 from libcloud.http import LibcloudConnection
 from libcloud.utils.py3 import PY2
 
@@ -196,15 +197,7 @@ class MockHttp(LibcloudConnection):
         return meth_name
 
 
-class MockHttpTestCase(MockHttp, unittest.TestCase):
-    # Same as the MockHttp class, but you can also use assertions in the
-    # classes which inherit from this one.
-    def __init__(self, *args, **kwargs):
-        unittest.TestCase.__init__(self)
-
-        if kwargs.get('host', None) and kwargs.get('port', None):
-            MockHttp.__init__(self, *args, **kwargs)
-
+class MockHttpTestCase(unittest.TestCase):
     def runTest(self):
         pass
 
@@ -244,6 +237,12 @@ class MockConnection(object):
 StorageMockHttp = MockHttp
 
 
+def make_response(status=200, headers={}, connection=None):
+    response = requests.Response()
+    response.status_code = status
+    response.headers = headers
+    return Response(response, connection)
+
 if __name__ == "__main__":
     import doctest
     doctest.testmod()

http://git-wip-us.apache.org/repos/asf/libcloud/blob/d5ff8eb3/libcloud/test/storage/test_s3.py
----------------------------------------------------------------------
diff --git a/libcloud/test/storage/test_s3.py b/libcloud/test/storage/test_s3.py
index 44ee4a6..429f5bb 100644
--- a/libcloud/test/storage/test_s3.py
+++ b/libcloud/test/storage/test_s3.py
@@ -50,7 +50,7 @@ from libcloud.utils.py3 import b
 
 from libcloud.test import MockHttp # pylint: disable-msg=E0611
 from libcloud.test import MockHttpTestCase  # pylint: disable-msg=E0611
-from libcloud.test import unittest
+from libcloud.test import unittest, make_response
 from libcloud.test.file_fixtures import StorageFileFixtures  # pylint: disable-msg=E0611
 from libcloud.test.secrets import STORAGE_S3_PARAMS
 
@@ -80,7 +80,7 @@ class S3MockHttp(MockHttp):
                 httplib.responses[httplib.OK])
 
     def _list_containers_TOKEN(self, method, url, body, headers):
-        self.assertEqual(headers['x-amz-security-token'], 'asdf')
+        assert headers['x-amz-security-token'] == 'asdf'
         body = self.fixtures.load('list_containers_empty.xml')
         return (httplib.OK,
                 body,
@@ -134,7 +134,7 @@ class S3MockHttp(MockHttp):
         headers = {'content-type': 'application/zip',
                    'etag': '"e31208wqsdoj329jd"',
                    'x-amz-meta-rabbits': 'monkeys',
-                   'content-length': 12345,
+                   'content-length': '12345',
                    'last-modified': 'Thu, 13 Sep 2012 07:13:22 GMT'
                    }
 
@@ -544,7 +544,7 @@ class S3Tests(unittest.TestCase):
 
         self.assertEqual(obj.name, 'test')
         self.assertEqual(obj.container.name, 'test2')
-        self.assertEqual(obj.size, 12345)
+        self.assertEqual(obj.size, '12345' )
         self.assertEqual(obj.hash, 'e31208wqsdoj329jd')
         self.assertEqual(obj.extra['last_modified'],
                          'Thu, 13 Sep 2012 07:13:22 GMT')
@@ -636,7 +636,7 @@ class S3Tests(unittest.TestCase):
         self.assertTrue(result)
 
     def test_download_object_invalid_file_size(self):
-        self.mock_raw_response_klass.type = 'INVALID_SIZE'
+        self.mock_response_klass.type = 'INVALID_SIZE'
         container = Container(name='foo_bar_container', extra={},
                               driver=self.driver)
         obj = Object(name='foo_bar_object', size=1000, hash=None, extra={},
@@ -650,7 +650,7 @@ class S3Tests(unittest.TestCase):
         self.assertFalse(result)
 
     def test_download_object_invalid_file_already_exists(self):
-        self.mock_raw_response_klass.type = 'INVALID_SIZE'
+        self.mock_response_klass.type = 'INVALID_SIZE'
         container = Container(name='foo_bar_container', extra={},
                               driver=self.driver)
         obj = Object(name='foo_bar_object', size=1000, hash=None, extra={},
@@ -713,11 +713,11 @@ class S3Tests(unittest.TestCase):
         def upload_file(self, object_name=None, content_type=None,
                         request_path=None, request_method=None,
                         headers=None, file_path=None, stream=None):
-            return {'response': MockResponse(200),
+            return {'response': make_response(200),
                     'bytes_transferred': 1000,
                     'data_hash': 'hash343hhash89h932439jsaa89'}
 
-        self.mock_raw_response_klass.type = 'INVALID_HASH1'
+        self.mock_response_klass.type = 'INVALID_HASH1'
 
         old_func = self.driver_type._upload_object
         self.driver_type._upload_object = upload_file
@@ -743,11 +743,11 @@ class S3Tests(unittest.TestCase):
         def upload_file(self, object_name=None, content_type=None,
                         request_path=None, request_method=None,
                         headers=None, file_path=None, stream=None):
-            return {'response': MockResponse(200, headers={'etag': 'woopwoopwoop'}),
+            return {'response': make_response(200, headers={'etag': 'woopwoopwoop'}),
                     'bytes_transferred': 1000,
                     'data_hash': '0cc175b9c0f1b6a831c399e269772661'}
 
-        self.mock_raw_response_klass.type = 'INVALID_HASH2'
+        self.mock_response_klass.type = 'INVALID_HASH2'
 
         old_func = self.driver_type._upload_object
         self.driver_type._upload_object = upload_file
@@ -772,7 +772,7 @@ class S3Tests(unittest.TestCase):
         def upload_file(self, object_name=None, content_type=None,
                         request_path=None, request_method=None,
                         headers=None, file_path=None, stream=None):
-            return {'response': MockResponse(200,
+            return {'response': make_response(200,
                                              headers={'etag': '0cc175b9c0f1b6a831c399e269772661'}),
                     'bytes_transferred': 1000,
                     'data_hash': '0cc175b9c0f1b6a831c399e269772661'}
@@ -798,7 +798,7 @@ class S3Tests(unittest.TestCase):
         def upload_file(self, object_name=None, content_type=None,
                         request_path=None, request_method=None,
                         headers=None, file_path=None, stream=None):
-            return {'response': MockResponse(200, headers={'etag': '0cc175b9c0f1b6a831c399e269772661'}),
+            return {'response': make_response(200, headers={'etag': '0cc175b9c0f1b6a831c399e269772661'}),
                     'bytes_transferred': 1000,
                     'data_hash': '0cc175b9c0f1b6a831c399e269772661'}
 
@@ -823,10 +823,8 @@ class S3Tests(unittest.TestCase):
 
     def test_upload_empty_object_via_stream(self):
         if self.driver.supports_s3_multipart_upload:
-            self.mock_raw_response_klass.type = 'MULTIPART'
             self.mock_response_klass.type = 'MULTIPART'
         else:
-            self.mock_raw_response_klass.type = None
             self.mock_response_klass.type = None
 
         container = Container(name='foo_bar_container', extra={},
@@ -844,10 +842,8 @@ class S3Tests(unittest.TestCase):
 
     def test_upload_small_object_via_stream(self):
         if self.driver.supports_s3_multipart_upload:
-            self.mock_raw_response_klass.type = 'MULTIPART'
             self.mock_response_klass.type = 'MULTIPART'
         else:
-            self.mock_raw_response_klass.type = None
             self.mock_response_klass.type = None
 
         container = Container(name='foo_bar_container', extra={},
@@ -865,10 +861,8 @@ class S3Tests(unittest.TestCase):
 
     def test_upload_big_object_via_stream(self):
         if self.driver.supports_s3_multipart_upload:
-            self.mock_raw_response_klass.type = 'MULTIPART'
             self.mock_response_klass.type = 'MULTIPART'
         else:
-            self.mock_raw_response_klass.type = None
             self.mock_response_klass.type = None
 
         container = Container(name='foo_bar_container', extra={},