Use six.moves.urllib.parse instead of urllib

Six urllib parse contains py3's urllib.parse and py2's urllib.
Replace urllib with six.moves.urllib.parse to keep compatibility.

Change-Id: Ie67987e4ffb981c2ee70360f7fa9b3fe873c2a96
Closes-bug: 1280105
This commit is contained in:
Hanxi Liu 2016-09-28 10:58:30 +08:00
parent 2b60c1a790
commit 52473ea392
1 changed files with 11 additions and 11 deletions

View File

@ -18,10 +18,10 @@ import hmac
import json import json
import os import os
import unittest import unittest
import urllib
import mock import mock
from six.moves.urllib import parse as urlparse
from swift.common.middleware.crypto import encrypter from swift.common.middleware.crypto import encrypter
from swift.common.middleware.crypto.crypto_utils import ( from swift.common.middleware.crypto.crypto_utils import (
CRYPTO_KEY_CALLBACK, Crypto) CRYPTO_KEY_CALLBACK, Crypto)
@ -52,7 +52,7 @@ class TestEncrypter(unittest.TestCase):
param = param.strip() param = param.strip()
self.assertTrue(param.startswith('swift_meta=')) self.assertTrue(param.startswith('swift_meta='))
actual_meta = json.loads( actual_meta = json.loads(
urllib.unquote_plus(param[len('swift_meta='):])) urlparse.unquote_plus(param[len('swift_meta='):]))
self.assertEqual(Crypto.cipher, actual_meta['cipher']) self.assertEqual(Crypto.cipher, actual_meta['cipher'])
meta_iv = base64.b64decode(actual_meta['iv']) meta_iv = base64.b64decode(actual_meta['iv'])
self.assertEqual(FAKE_IV, meta_iv) self.assertEqual(FAKE_IV, meta_iv)
@ -61,7 +61,7 @@ class TestEncrypter(unittest.TestCase):
enc_val) enc_val)
# if there is any encrypted user metadata then this header should exist # if there is any encrypted user metadata then this header should exist
self.assertIn('X-Object-Transient-Sysmeta-Crypto-Meta', req_hdrs) self.assertIn('X-Object-Transient-Sysmeta-Crypto-Meta', req_hdrs)
common_meta = json.loads(urllib.unquote_plus( common_meta = json.loads(urlparse.unquote_plus(
req_hdrs['X-Object-Transient-Sysmeta-Crypto-Meta'])) req_hdrs['X-Object-Transient-Sysmeta-Crypto-Meta']))
self.assertDictEqual({'cipher': Crypto.cipher, self.assertDictEqual({'cipher': Crypto.cipher,
'key_id': {'v': 'fake', 'path': '/a/c/fake'}}, 'key_id': {'v': 'fake', 'path': '/a/c/fake'}},
@ -101,7 +101,7 @@ class TestEncrypter(unittest.TestCase):
# verify body crypto meta # verify body crypto meta
actual = req_hdrs['X-Object-Sysmeta-Crypto-Body-Meta'] actual = req_hdrs['X-Object-Sysmeta-Crypto-Body-Meta']
actual = json.loads(urllib.unquote_plus(actual)) actual = json.loads(urlparse.unquote_plus(actual))
self.assertEqual(Crypto().cipher, actual['cipher']) self.assertEqual(Crypto().cipher, actual['cipher'])
self.assertEqual(FAKE_IV, base64.b64decode(actual['iv'])) self.assertEqual(FAKE_IV, base64.b64decode(actual['iv']))
@ -120,7 +120,7 @@ class TestEncrypter(unittest.TestCase):
req_hdrs['X-Object-Sysmeta-Crypto-Etag'].partition('; swift_meta=') req_hdrs['X-Object-Sysmeta-Crypto-Etag'].partition('; swift_meta=')
# verify crypto_meta was appended to this etag # verify crypto_meta was appended to this etag
self.assertTrue(etag_meta) self.assertTrue(etag_meta)
actual_meta = json.loads(urllib.unquote_plus(etag_meta)) actual_meta = json.loads(urlparse.unquote_plus(etag_meta))
self.assertEqual(Crypto().cipher, actual_meta['cipher']) self.assertEqual(Crypto().cipher, actual_meta['cipher'])
# verify encrypted version of plaintext etag # verify encrypted version of plaintext etag
@ -147,7 +147,7 @@ class TestEncrypter(unittest.TestCase):
crypto_meta_tag = 'swift_meta=' crypto_meta_tag = 'swift_meta='
self.assertTrue(param.startswith(crypto_meta_tag), param) self.assertTrue(param.startswith(crypto_meta_tag), param)
actual_meta = json.loads( actual_meta = json.loads(
urllib.unquote_plus(param[len(crypto_meta_tag):])) urlparse.unquote_plus(param[len(crypto_meta_tag):]))
self.assertEqual(Crypto().cipher, actual_meta['cipher']) self.assertEqual(Crypto().cipher, actual_meta['cipher'])
self.assertEqual(fetch_crypto_keys()['id'], actual_meta['key_id']) self.assertEqual(fetch_crypto_keys()['id'], actual_meta['key_id'])
@ -278,7 +278,7 @@ class TestEncrypter(unittest.TestCase):
encrypted_etag, _junk, etag_meta = \ encrypted_etag, _junk, etag_meta = \
req_hdrs['X-Object-Sysmeta-Crypto-Etag'].partition('; swift_meta=') req_hdrs['X-Object-Sysmeta-Crypto-Etag'].partition('; swift_meta=')
self.assertTrue(etag_meta) self.assertTrue(etag_meta)
actual_meta = json.loads(urllib.unquote_plus(etag_meta)) actual_meta = json.loads(urlparse.unquote_plus(etag_meta))
self.assertEqual(Crypto().cipher, actual_meta['cipher']) self.assertEqual(Crypto().cipher, actual_meta['cipher'])
self.assertEqual(ciphertext_etag, req_hdrs['Etag']) self.assertEqual(ciphertext_etag, req_hdrs['Etag'])
@ -298,7 +298,7 @@ class TestEncrypter(unittest.TestCase):
crypto_meta_tag = 'swift_meta=' crypto_meta_tag = 'swift_meta='
self.assertTrue(param.startswith(crypto_meta_tag), param) self.assertTrue(param.startswith(crypto_meta_tag), param)
actual_meta = json.loads( actual_meta = json.loads(
urllib.unquote_plus(param[len(crypto_meta_tag):])) urlparse.unquote_plus(param[len(crypto_meta_tag):]))
self.assertEqual(Crypto().cipher, actual_meta['cipher']) self.assertEqual(Crypto().cipher, actual_meta['cipher'])
cont_key = fetch_crypto_keys()['container'] cont_key = fetch_crypto_keys()['container']
@ -309,7 +309,7 @@ class TestEncrypter(unittest.TestCase):
# verify body crypto meta # verify body crypto meta
actual = req_hdrs['X-Object-Sysmeta-Crypto-Body-Meta'] actual = req_hdrs['X-Object-Sysmeta-Crypto-Body-Meta']
actual = json.loads(urllib.unquote_plus(actual)) actual = json.loads(urlparse.unquote_plus(actual))
self.assertEqual(Crypto().cipher, actual['cipher']) self.assertEqual(Crypto().cipher, actual['cipher'])
self.assertEqual(FAKE_IV, base64.b64decode(actual['iv'])) self.assertEqual(FAKE_IV, base64.b64decode(actual['iv']))
@ -368,7 +368,7 @@ class TestEncrypter(unittest.TestCase):
crypto_meta_tag = 'swift_meta=' crypto_meta_tag = 'swift_meta='
self.assertTrue(param.startswith(crypto_meta_tag), param) self.assertTrue(param.startswith(crypto_meta_tag), param)
actual_meta = json.loads( actual_meta = json.loads(
urllib.unquote_plus(param[len(crypto_meta_tag):])) urlparse.unquote_plus(param[len(crypto_meta_tag):]))
self.assertEqual(Crypto().cipher, actual_meta['cipher']) self.assertEqual(Crypto().cipher, actual_meta['cipher'])
self.assertEqual(fetch_crypto_keys()['id'], actual_meta['key_id']) self.assertEqual(fetch_crypto_keys()['id'], actual_meta['key_id'])
@ -491,7 +491,7 @@ class TestEncrypter(unittest.TestCase):
crypto_meta_tag = 'swift_meta=' crypto_meta_tag = 'swift_meta='
self.assertTrue(param.startswith(crypto_meta_tag), param) self.assertTrue(param.startswith(crypto_meta_tag), param)
actual_meta = json.loads( actual_meta = json.loads(
urllib.unquote_plus(param[len(crypto_meta_tag):])) urlparse.unquote_plus(param[len(crypto_meta_tag):]))
self.assertEqual(Crypto().cipher, actual_meta['cipher']) self.assertEqual(Crypto().cipher, actual_meta['cipher'])
self.assertEqual(fetch_crypto_keys()['id'], actual_meta['key_id']) self.assertEqual(fetch_crypto_keys()['id'], actual_meta['key_id'])