You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2015/03/28 15:51:30 UTC

[13/16] libcloud git commit: Update affected tests.

Update affected tests.


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

Branch: refs/heads/trunk
Commit: 37ed7e59e4a549b8abc6d4e7f08838b597420c63
Parents: b034b2f
Author: Tomaz Muraus <to...@apache.org>
Authored: Sat Mar 14 23:38:19 2015 +0100
Committer: Tomaz Muraus <to...@apache.org>
Committed: Sat Mar 14 23:38:19 2015 +0100

----------------------------------------------------------------------
 libcloud/test/common/test_aws.py | 111 +++++++++++++++++++++-------------
 1 file changed, 69 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/37ed7e59/libcloud/test/common/test_aws.py
----------------------------------------------------------------------
diff --git a/libcloud/test/common/test_aws.py b/libcloud/test/common/test_aws.py
index 025b766..6661f7d 100644
--- a/libcloud/test/common/test_aws.py
+++ b/libcloud/test/common/test_aws.py
@@ -1,7 +1,11 @@
-import mock
+import sys
+import unittest
 from datetime import datetime
 
-from libcloud.common.aws import V4SignedAWSConnection
+import mock
+
+from libcloud.common.aws import SignedAWSConnection
+from libcloud.common.aws import AWSRequestSignerAlgorithmV4
 from libcloud.test import LibcloudTestCase
 
 
@@ -9,59 +13,75 @@ class EC2MockDriver(object):
     region_name = 'my_region'
 
 
-class V4SignedAWSConnectionTest(LibcloudTestCase):
+class AWSRequestSignerAlgorithmV4TestCase(LibcloudTestCase):
 
     def setUp(self):
-        V4SignedAWSConnection.service_name = 'my_service'
-        V4SignedAWSConnection.method = 'GET'
-        V4SignedAWSConnection.action = '/my_action/'
-        V4SignedAWSConnection.driver = EC2MockDriver()
+        SignedAWSConnection.driver = EC2MockDriver()
+        SignedAWSConnection.service_name = 'my_service'
+        SignedAWSConnection.version = '2013-10-15'
+        self.connection = SignedAWSConnection('my_key', 'my_secret')
+
+        self.signer = AWSRequestSignerAlgorithmV4(access_key='my_key',
+                                                  access_secret='my_secret',
+                                                  version='2013-10-15',
+                                                  connection=self.connection)
+
+        SignedAWSConnection.action = '/my_action/'
+        SignedAWSConnection.driver = EC2MockDriver()
 
-        self.conn = V4SignedAWSConnection('my_key', 'my_secret')
         self.now = datetime(2015, 3, 4, hour=17, minute=34, second=52)
 
     def test_v4_signature(self):
-        sig = self.conn._get_authorization_v4_header({
+        params = {
             'Action': 'DescribeInstances',
             'Version': '2013-10-15'
-        }, {
+        }
+        headers = {
             'Host': 'ec2.eu-west-1.amazonaws.com',
             'Accept-Encoding': 'gzip,deflate',
             'X-AMZ-Date': '20150304T173452Z',
             'User-Agent': 'libcloud/0.17.0 (Amazon EC2 (eu-central-1)) '
-        }, self.now)
+        }
+        dt = self.now
+        sig = self.signer._get_authorization_v4_header(params=params,
+                                                       headers=headers,
+                                                       dt=dt,
+                                                       method='GET',
+                                                       path='/my_action/')
         self.assertEqual(sig, 'AWS4-HMAC-SHA256 '
                               'Credential=my_key/20150304/my_region/my_service/aws4_request, '
                               'SignedHeaders=accept-encoding;host;user-agent;x-amz-date, '
                               'Signature=f9868f8414b3c3f856c7955019cc1691265541f5162b9b772d26044280d39bd3')
 
     def test_v4_signature_raises_error_if_request_method_not_GET(self):
-        V4SignedAWSConnection.method = 'POST'
-
         with self.assertRaises(Exception):
-            self.conn._get_authorization_v4_header({}, {}, self.now)
+            self.signer._get_authorization_v4_header(params={}, headers={},
+                                                     dt=self.now, method='POST')
 
     def test_v4_signature_contains_user_id(self):
-        sig = self.conn._get_authorization_v4_header({}, {}, self.now)
+        sig = self.signer._get_authorization_v4_header(params={}, headers={},
+                                                       dt=self.now)
         self.assertIn('Credential=my_key/', sig)
 
     def test_v4_signature_contains_credential_scope(self):
-        with mock.patch('libcloud.common.aws.V4SignedAWSConnection._get_credential_scope') as mock_get_creds:
+        with mock.patch('libcloud.common.aws.AWSRequestSignerAlgorithmV4._get_credential_scope') as mock_get_creds:
             mock_get_creds.return_value = 'my_credential_scope'
-            sig = self.conn._get_authorization_v4_header({}, {}, self.now)
+            sig = self.signer._get_authorization_v4_header(params={}, headers={}, dt=self.now)
 
         self.assertIn('Credential=my_key/my_credential_scope, ', sig)
 
     def test_v4_signature_contains_signed_headers(self):
-        with mock.patch('libcloud.common.aws.V4SignedAWSConnection._get_signed_headers') as mock_get_headers:
+        with mock.patch('libcloud.common.aws.AWSRequestSignerAlgorithmV4._get_signed_headers') as mock_get_headers:
             mock_get_headers.return_value = 'my_signed_headers'
-            sig = self.conn._get_authorization_v4_header({}, {}, self.now)
+            sig = self.signer._get_authorization_v4_header({}, {}, self.now,
+                                                           method='GET',
+                                                           path='/')
         self.assertIn('SignedHeaders=my_signed_headers, ', sig)
 
     def test_v4_signature_contains_signature(self):
-        with mock.patch('libcloud.common.aws.V4SignedAWSConnection._get_signature') as mock_get_signature:
+        with mock.patch('libcloud.common.aws.AWSRequestSignerAlgorithmV4._get_signature') as mock_get_signature:
             mock_get_signature.return_value = 'my_signature'
-            sig = self.conn._get_authorization_v4_header({}, {}, self.now)
+            sig = self.signer._get_authorization_v4_header({}, {}, self.now)
         self.assertIn('Signature=my_signature', sig)
 
     def test_get_signature_(self):
@@ -71,19 +91,21 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
             else:
                 return '%s|%s' % (key, msg)
 
-        with mock.patch('libcloud.common.aws.V4SignedAWSConnection._get_key_to_sign_with') as mock_get_key:
-            with mock.patch('libcloud.common.aws.V4SignedAWSConnection._get_string_to_sign') as mock_get_string:
+        with mock.patch('libcloud.common.aws.AWSRequestSignerAlgorithmV4._get_key_to_sign_with') as mock_get_key:
+            with mock.patch('libcloud.common.aws.AWSRequestSignerAlgorithmV4._get_string_to_sign') as mock_get_string:
                 with mock.patch('libcloud.common.aws._sign', new=_sign):
                     mock_get_key.return_value = 'my_signing_key'
                     mock_get_string.return_value = 'my_string_to_sign'
-                    sig = self.conn._get_signature({}, {}, self.now)
+                    sig = self.signer._get_signature({}, {}, self.now,
+                                                     method='GET', path='/')
 
         self.assertEqual(sig, 'H|my_signing_key|my_string_to_sign')
 
     def test_get_string_to_sign(self):
         with mock.patch('hashlib.sha256') as mock_sha256:
             mock_sha256.return_value.hexdigest.return_value = 'chksum_of_canonical_request'
-            to_sign = self.conn._get_string_to_sign({}, {}, self.now)
+            to_sign = self.signer._get_string_to_sign({}, {}, self.now,
+                                                      method='GET', path='/')
 
         self.assertEqual(to_sign,
                          'AWS4-HMAC-SHA256\n'
@@ -96,13 +118,13 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
             return '%s|%s' % (key, msg)
 
         with mock.patch('libcloud.common.aws._sign', new=_sign):
-            key = self.conn._get_key_to_sign_with(self.now)
+            key = self.signer._get_key_to_sign_with(self.now)
 
         self.assertEqual(key, 'AWS4my_secret|20150304|my_region|my_service|aws4_request')
 
     def test_get_signed_headers_contains_all_headers_lowercased(self):
         headers = {'Content-Type': 'text/plain', 'Host': 'my_host', 'X-Special-Header': ''}
-        signed_headers = self.conn._get_signed_headers(headers)
+        signed_headers = self.signer._get_signed_headers(headers)
 
         self.assertIn('content-type', signed_headers)
         self.assertIn('host', signed_headers)
@@ -110,12 +132,12 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
 
     def test_get_signed_headers_concats_headers_sorted_lexically(self):
         headers = {'Host': 'my_host', 'X-Special-Header': '', '1St-Header': '2', 'Content-Type': 'text/plain'}
-        signed_headers = self.conn._get_signed_headers(headers)
+        signed_headers = self.signer._get_signed_headers(headers)
 
         self.assertEqual(signed_headers, '1st-header;content-type;host;x-special-header')
 
     def test_get_credential_scope(self):
-        scope = self.conn._get_credential_scope(self.now)
+        scope = self.signer._get_credential_scope(self.now)
         self.assertEqual(scope, '20150304/my_region/my_service/aws4_request')
 
     def test_get_canonical_headers_joins_all_headers(self):
@@ -123,7 +145,7 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
             'accept-encoding': 'gzip,deflate',
             'host': 'my_host',
         }
-        self.assertEqual(self.conn._get_canonical_headers(headers),
+        self.assertEqual(self.signer._get_canonical_headers(headers),
                          'accept-encoding:gzip,deflate\n'
                          'host:my_host\n')
 
@@ -135,7 +157,7 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
             'x-amz-date': '20150304T173452Z',
             'user-agent': 'my-ua'
         }
-        self.assertEqual(self.conn._get_canonical_headers(headers),
+        self.assertEqual(self.signer._get_canonical_headers(headers),
                          '1st-header:2\n'
                          'accept-encoding:gzip,deflate\n'
                          'host:my_host\n'
@@ -147,7 +169,7 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
             'Accept-Encoding': 'GZIP,DEFLATE',
             'User-Agent': 'My-UA'
         }
-        self.assertEqual(self.conn._get_canonical_headers(headers),
+        self.assertEqual(self.signer._get_canonical_headers(headers),
                          'accept-encoding:GZIP,DEFLATE\n'
                          'user-agent:My-UA\n')
 
@@ -160,12 +182,12 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
             'accept-encoding': '   gzip,deflate',
             'user-agent': 'libcloud/0.17.0 '
         }
-        self.assertEqual(self.conn._get_canonical_headers(headers),
+        self.assertEqual(self.signer._get_canonical_headers(headers),
                          'accept-encoding:gzip,deflate\n'
                          'user-agent:libcloud/0.17.0\n')
 
     def test_get_request_params_joins_params_sorted_lexically(self):
-        self.assertEqual(self.conn._get_request_params({
+        self.assertEqual(self.signer._get_request_params({
             'Action': 'DescribeInstances',
             'Filter.1.Name': 'state',
             'Version': '2013-10-15'
@@ -173,15 +195,15 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
             'Action=DescribeInstances&Filter.1.Name=state&Version=2013-10-15')
 
     def test_get_request_params_allows_integers_as_value(self):
-        self.assertEqual(self.conn._get_request_params({'Action': 'DescribeInstances', 'Port': 22}),
+        self.assertEqual(self.signer._get_request_params({'Action': 'DescribeInstances', 'Port': 22}),
                          'Action=DescribeInstances&Port=22')
 
     def test_get_request_params_urlquotes_params_keys(self):
-        self.assertEqual(self.conn._get_request_params({'Action+Reaction': 'DescribeInstances'}),
+        self.assertEqual(self.signer._get_request_params({'Action+Reaction': 'DescribeInstances'}),
                          'Action%2BReaction=DescribeInstances')
 
     def test_get_request_params_urlquotes_params_values(self):
-        self.assertEqual(self.conn._get_request_params({
+        self.assertEqual(self.signer._get_request_params({
             'Action': 'DescribeInstances&Addresses',
             'Port-Range': '2000 3000'
         }),
@@ -190,17 +212,19 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
     def test_get_request_params_urlquotes_params_values_allows_safe_chars_in_value(self):
         # http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
         self.assertEqual('Action=a~b.c_d-e',
-                         self.conn._get_request_params({'Action': 'a~b.c_d-e'}))
+                         self.signer._get_request_params({'Action': 'a~b.c_d-e'}))
 
     def test_get_payload_hash_returns_digest_of_empty_string_for_GET_requests(self):
-        V4SignedAWSConnection.method = 'GET'
-        self.assertEqual(self.conn._get_payload_hash(),
+        SignedAWSConnection.method = 'GET'
+        self.assertEqual(self.signer._get_payload_hash(),
                          'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
 
     def test_get_canonical_request(self):
-        req = self.conn._get_canonical_request(
+        req = self.signer._get_canonical_request(
             {'Action': 'DescribeInstances', 'Version': '2013-10-15'},
-            {'Accept-Encoding': 'gzip,deflate', 'User-Agent': 'My-UA'}
+            {'Accept-Encoding': 'gzip,deflate', 'User-Agent': 'My-UA'},
+            method='GET',
+            path='/my_action/'
         )
         self.assertEqual(req, 'GET\n'
                               '/my_action/\n'
@@ -210,3 +234,6 @@ class V4SignedAWSConnectionTest(LibcloudTestCase):
                               '\n'
                               'accept-encoding;user-agent\n'
                               'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
+
+if __name__ == '__main__':
+    sys.exit(unittest.main())