diff --git a/manila/api/urlmap.py b/manila/api/urlmap.py index 09493db064..bccaa67c3d 100644 --- a/manila/api/urlmap.py +++ b/manila/api/urlmap.py @@ -14,7 +14,10 @@ # under the License. import re -import urllib2 +try: + from urllib.request import parse_http_list # noqa +except ImportError: + from urllib2 import parse_http_list # noqa from oslo_log import log import paste.urlmap @@ -65,7 +68,7 @@ def parse_list_header(value): :return: :class:`list` """ result = [] - for item in urllib2.parse_http_list(value): + for item in parse_http_list(value): if item[:1] == item[-1:] == '"': item = unquote_header_value(item[1:-1]) result.append(item) diff --git a/manila/api/v1/limits.py b/manila/api/v1/limits.py index fae0dfcd1d..153fc0786f 100644 --- a/manila/api/v1/limits.py +++ b/manila/api/v1/limits.py @@ -19,13 +19,13 @@ Module dedicated functions/classes dealing with rate limiting requests. import collections import copy -import httplib import math import re import time from oslo_serialization import jsonutils from oslo_utils import importutils +from six.moves import http_client import webob.dec import webob.exc @@ -406,7 +406,7 @@ class WsgiLimiterProxy(object): body = jsonutils.dumps({"verb": verb, "path": path}) headers = {"Content-Type": "application/json"} - conn = httplib.HTTPConnection(self.limiter_address) + conn = http_client.HTTPConnection(self.limiter_address) if username: conn.request("POST", "/%s" % (username), body, headers) diff --git a/manila/scheduler/host_manager.py b/manila/scheduler/host_manager.py index d0796226e9..714696a8df 100644 --- a/manila/scheduler/host_manager.py +++ b/manila/scheduler/host_manager.py @@ -21,7 +21,10 @@ Manage hosts in the current zone. """ import re -import UserDict +try: + from UserDict import IterableUserDict # noqa +except ImportError: + from collections import UserDict as IterableUserDict # noqa from oslo_config import cfg from oslo_log import log @@ -59,7 +62,7 @@ CONF.import_opt('max_over_subscription_ratio', 'manila.share.driver') LOG = log.getLogger(__name__) -class ReadOnlyDict(UserDict.IterableUserDict): +class ReadOnlyDict(IterableUserDict): """A read-only dict.""" def __init__(self, source=None): self.data = {} @@ -83,7 +86,7 @@ class ReadOnlyDict(UserDict.IterableUserDict): def update(self, source=None): if source is None: return - elif isinstance(source, UserDict.UserDict): + elif isinstance(source, IterableUserDict): self.data = source.data elif isinstance(source, type({})): self.data = source diff --git a/manila/share/drivers/emc/plugins/vnx/helper.py b/manila/share/drivers/emc/plugins/vnx/helper.py index 23fbbca745..3e62f59922 100644 --- a/manila/share/drivers/emc/plugins/vnx/helper.py +++ b/manila/share/drivers/emc/plugins/vnx/helper.py @@ -12,7 +12,6 @@ # 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 cookielib import random import re @@ -21,6 +20,7 @@ from lxml import builder from lxml import etree as ET from oslo_log import log import six +from six.moves import http_cookiejar from six.moves.urllib import error as url_error # pylint: disable=E0611 from six.moves.urllib import request as url_request # pylint: disable=E0611 @@ -49,7 +49,7 @@ class XMLAPIConnector(object): self._url = ('https://' + self.storage_ip + '/servlets/CelerraManagementServices') https_handler = url_request.HTTPSHandler() - cookie_jar = cookielib.CookieJar() + cookie_jar = http_cookiejar.CookieJar() cookie_handler = url_request.HTTPCookieProcessor(cookie_jar) self.url_opener = url_request.build_opener(https_handler, cookie_handler) diff --git a/manila/share/drivers/netapp/dataontap/client/api.py b/manila/share/drivers/netapp/dataontap/client/api.py index 2839b534d5..240b082846 100644 --- a/manila/share/drivers/netapp/dataontap/client/api.py +++ b/manila/share/drivers/netapp/dataontap/client/api.py @@ -19,11 +19,11 @@ Contains classes required to issue API calls to Data ONTAP and OnCommand DFM. """ import copy -import urllib2 from lxml import etree from oslo_log import log import six +from six.moves import urllib from manila import exception from manila.i18n import _ @@ -228,7 +228,7 @@ class NaServer(object): response = self._opener.open(request, timeout=self._timeout) else: response = self._opener.open(request) - except urllib2.HTTPError as e: + except urllib.error.HTTPError as e: raise NaApiError(e.code, e.msg) except Exception as e: raise NaApiError('Unexpected error', e) @@ -273,7 +273,7 @@ class NaServer(object): self._enable_tunnel_request(netapp_elem) netapp_elem.add_child_elem(na_element) request_d = netapp_elem.to_string() - request = urllib2.Request( + request = urllib.request.Request( self._get_url(), data=request_d, headers={'Content-Type': 'text/xml', 'charset': 'utf-8'}) return request, netapp_elem @@ -320,14 +320,14 @@ class NaServer(object): auth_handler = self._create_basic_auth_handler() else: auth_handler = self._create_certificate_auth_handler() - opener = urllib2.build_opener(auth_handler) + opener = urllib.request.build_opener(auth_handler) self._opener = opener def _create_basic_auth_handler(self): - password_man = urllib2.HTTPPasswordMgrWithDefaultRealm() + password_man = urllib.request.HTTPPasswordMgrWithDefaultRealm() password_man.add_password(None, self._get_url(), self._username, self._password) - auth_handler = urllib2.HTTPBasicAuthHandler(password_man) + auth_handler = urllib.request.HTTPBasicAuthHandler(password_man) return auth_handler def _create_certificate_auth_handler(self): diff --git a/manila/share/drivers/quobyte/jsonrpc.py b/manila/share/drivers/quobyte/jsonrpc.py index e2c8afb4fb..e07573a2d4 100644 --- a/manila/share/drivers/quobyte/jsonrpc.py +++ b/manila/share/drivers/quobyte/jsonrpc.py @@ -19,7 +19,6 @@ Control Quobyte over its JSON RPC API. """ import base64 -import httplib import socket import ssl import time @@ -27,6 +26,7 @@ import time from oslo_log import log from oslo_serialization import jsonutils import six +from six.moves import http_client import six.moves.urllib.parse as urlparse from manila import exception @@ -55,15 +55,15 @@ class BasicAuthCredentials(object): return 'BASIC %s' % auth -class HTTPSConnectionWithCaVerification(httplib.HTTPConnection): +class HTTPSConnectionWithCaVerification(http_client.HTTPConnection): """Verify server cert against a given CA certificate.""" - default_port = httplib.HTTPS_PORT + default_port = http_client.HTTPS_PORT def __init__(self, host, port=None, key_file=None, cert_file=None, ca_file=None, strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT): - httplib.HTTPConnection.__init__(self, host, port, strict, timeout) + http_client.HTTPConnection.__init__(self, host, port, strict, timeout) self.key_file = key_file self.cert_file = cert_file self.ca_file = ca_file @@ -79,7 +79,7 @@ class HTTPSConnectionWithCaVerification(httplib.HTTPConnection): ca_certs=self.ca_file, cert_reqs=ssl.CERT_REQUIRED) - httplib.__all__.append("HTTPSConnectionWithCaVerification") + http_client.__all__.append("HTTPSConnectionWithCaVerification") class JsonRpc(object): @@ -94,12 +94,12 @@ class JsonRpc(object): self._netloc, ca_file=self._ca_file.name) else: - self._connection = httplib.HTTPSConnection(self._netloc) + self._connection = http_client.HTTPSConnection(self._netloc) LOG.warning(_LW( "Will not verify the server certificate of the API service" " because the CA certificate is not available.")) else: - self._connection = httplib.HTTPConnection(self._netloc) + self._connection = http_client.HTTPConnection(self._netloc) self._id = 0 self._fail_fast = True self._credentials = BasicAuthCredentials( @@ -117,7 +117,7 @@ class JsonRpc(object): 'params': parameters, 'id': six.text_type(self._id)} self.call_counter = 0 - self._connection.connect() # prevents httplib timing issue + self._connection.connect() # prevents http_client timing issue while self.call_counter < CONNECTION_RETRIES: self.call_counter += 1 @@ -146,18 +146,19 @@ class JsonRpc(object): "API service against CA.")) self._connection.close() # Core HTTPSConnection does no certificate verification. - self._connection = httplib.HTTPSConnection(self._netloc) + self._connection = http_client.HTTPSConnection( + self._netloc) self._disabled_cert_verification = True else: raise exception.QBException(_( "Client SSL subsystem returned error: %s") % e) - except httplib.BadStatusLine as e: + except http_client.BadStatusLine as e: raise exception.QBException(_( "If SSL is enabled for the API service, the URL must" " start with 'https://' for the URL. Failed to parse" " status code from server response. Error was %s") % e) - except (httplib.HTTPException, socket.error) as e: + except (http_client.HTTPException, socket.error) as e: if self._fail_fast: raise exception.QBException(msg=six.text_type(e)) else: diff --git a/manila/share/drivers/zfssa/restclient.py b/manila/share/drivers/zfssa/restclient.py index c850c9ce91..4a1fb716dc 100644 --- a/manila/share/drivers/zfssa/restclient.py +++ b/manila/share/drivers/zfssa/restclient.py @@ -18,11 +18,11 @@ TODO(diemtran): this module needs to be placed in a library common to OpenStack base and imported from the relevant library. """ -import httplib import time from oslo_serialization import jsonutils import six +from six.moves import http_client # pylint: disable=E0611,F0401 from six.moves.urllib import error as urlerror from six.moves.urllib import request as urlrequest @@ -37,40 +37,40 @@ class Status(object): """Result HTTP Status.""" #: Request return OK - OK = httplib.OK # pylint: disable=invalid-name + OK = http_client.OK # pylint: disable=invalid-name #: New resource created successfully - CREATED = httplib.CREATED + CREATED = http_client.CREATED #: Command accepted - ACCEPTED = httplib.ACCEPTED + ACCEPTED = http_client.ACCEPTED #: Command returned OK but no data will be returned - NO_CONTENT = httplib.NO_CONTENT + NO_CONTENT = http_client.NO_CONTENT #: Bad Request - BAD_REQUEST = httplib.BAD_REQUEST + BAD_REQUEST = http_client.BAD_REQUEST #: User is not authorized - UNAUTHORIZED = httplib.UNAUTHORIZED + UNAUTHORIZED = http_client.UNAUTHORIZED #: The request is not allowed - FORBIDDEN = httplib.FORBIDDEN + FORBIDDEN = http_client.FORBIDDEN #: The requested resource was not found - NOT_FOUND = httplib.NOT_FOUND + NOT_FOUND = http_client.NOT_FOUND #: The request is not allowed - NOT_ALLOWED = httplib.METHOD_NOT_ALLOWED + NOT_ALLOWED = http_client.METHOD_NOT_ALLOWED #: Request timed out - TIMEOUT = httplib.REQUEST_TIMEOUT + TIMEOUT = http_client.REQUEST_TIMEOUT #: Invalid request - CONFLICT = httplib.CONFLICT + CONFLICT = http_client.CONFLICT #: Service Unavailable - BUSY = httplib.SERVICE_UNAVAILABLE + BUSY = http_client.SERVICE_UNAVAILABLE class RestResult(object): @@ -96,7 +96,7 @@ class RestResult(object): if self.error: self.status = self.error.code - self.data = httplib.responses[self.status] + self.data = http_client.responses[self.status] log_debug_msg(self, 'Response code: %s' % self.status) log_debug_msg(self, 'Response data: %s' % self.data) @@ -127,8 +127,8 @@ class RestClientError(Exception): self.code = status self.name = name self.msg = message - if status in httplib.responses: - self.msg = httplib.responses[status] + if status in http_client.responses: + self.msg = http_client.responses[status] def __str__(self): return "%d %s %s" % (self.code, self.name, self.msg) @@ -180,14 +180,14 @@ class RestClientURL(object): # pylint: disable=R0902 try: result = self.post("/access/v1") del self.headers['authorization'] - if result.status == httplib.CREATED: + if result.status == http_client.CREATED: self.headers['x-auth-session'] = \ result.get_header('x-auth-session') self.do_logout = True log_debug_msg(self, ('ZFSSA version: %s') % result.get_header('x-zfssa-version')) - elif result.status == httplib.NOT_FOUND: + elif result.status == http_client.NOT_FOUND: raise RestClientError(result.status, name="ERR_RESTError", message=("REST Not Available:" "Please Upgrade")) @@ -285,20 +285,20 @@ class RestClientURL(object): # pylint: disable=R0902 try: response = urlrequest.urlopen(req, timeout=self.timeout) except urlerror.HTTPError as err: - if err.code == httplib.NOT_FOUND: + if err.code == http_client.NOT_FOUND: log_debug_msg(self, 'REST Not Found: %s' % err.code) else: log_debug_msg(self, ('REST Not Available: %s') % err.code) - if (err.code == httplib.SERVICE_UNAVAILABLE and + if (err.code == http_client.SERVICE_UNAVAILABLE and retry < maxreqretries): retry += 1 time.sleep(1) log_debug_msg(self, ('Server Busy retry request: %s') % retry) continue - if ((err.code == httplib.UNAUTHORIZED or - err.code == httplib.INTERNAL_SERVER_ERROR) and + if ((err.code == http_client.UNAUTHORIZED or + err.code == http_client.INTERNAL_SERVER_ERROR) and '/access/v1' not in zfssaurl): try: log_debug_msg(self, ('Authorizing request: ' @@ -324,7 +324,7 @@ class RestClientURL(object): # pylint: disable=R0902 break if ((response and - response.getcode() == httplib.SERVICE_UNAVAILABLE) and + response.getcode() == http_client.SERVICE_UNAVAILABLE) and retry >= maxreqretries): raise RestClientError(response.getcode(), name="ERR_HTTPError", message="REST Not Available: Disabled") diff --git a/manila/tests/__init__.py b/manila/tests/__init__.py index 3b8468ac01..edb653e682 100644 --- a/manila/tests/__init__.py +++ b/manila/tests/__init__.py @@ -29,9 +29,10 @@ """ import eventlet +from six.moves import builtins + eventlet.monkey_patch() # See http://code.google.com/p/python-nose/issues/detail?id=373 # The code below enables nosetests to work with i18n _() blocks -import __builtin__ -setattr(__builtin__, '_', lambda x: x) +setattr(builtins, '_', lambda x: x) diff --git a/manila/tests/api/v1/test_limits.py b/manila/tests/api/v1/test_limits.py index f8354b81c3..8a433892b3 100644 --- a/manila/tests/api/v1/test_limits.py +++ b/manila/tests/api/v1/test_limits.py @@ -17,11 +17,10 @@ Tests dealing with HTTP rate-limiting. """ -import httplib - from oslo_serialization import jsonutils import six from six import moves +from six.moves import http_client import webob from manila.api.v1 import limits @@ -596,7 +595,7 @@ class WsgiLimiterTest(BaseLimitTestSuite): class FakeHttplibSocket(object): - """Fake `httplib.HTTPResponse` replacement.""" + """Fake `http_client.HTTPResponse` replacement.""" def __init__(self, response_string): """Initialize new `FakeHttplibSocket`.""" @@ -608,7 +607,7 @@ class FakeHttplibSocket(object): class FakeHttplibConnection(object): - """Fake `httplib.HTTPConnection`.""" + """Fake `http_client.HTTPConnection`.""" def __init__(self, app, host): """Initialize `FakeHttplibConnection`.""" @@ -620,7 +619,7 @@ class FakeHttplibConnection(object): Requests made via this connection actually get translated and routed into our WSGI app, we then wait for the response and turn it back into - an `httplib.HTTPResponse`. + an `http_client.HTTPResponse`. """ if not headers: headers = {} @@ -634,7 +633,7 @@ class FakeHttplibConnection(object): resp = str(req.get_response(self.app)) resp = "HTTP/1.0 %s" % resp sock = FakeHttplibSocket(resp) - self.http_response = httplib.HTTPResponse(sock) + self.http_response = http_client.HTTPResponse(sock) self.http_response.begin() def getresponse(self): @@ -650,7 +649,7 @@ def wire_HTTPConnection_to_WSGI(host, app): After calling this method, when any code calls - httplib.HTTPConnection(host) + http_client.HTTPConnection(host) the connection object will be a fake. Its requests will be sent directly to the given WSGI app rather than through a socket. @@ -680,8 +679,9 @@ def wire_HTTPConnection_to_WSGI(host, app): else: return self.wrapped(connection_host, *args, **kwargs) - oldHTTPConnection = httplib.HTTPConnection - httplib.HTTPConnection = HTTPConnectionDecorator(httplib.HTTPConnection) + oldHTTPConnection = http_client.HTTPConnection + http_client.HTTPConnection = HTTPConnectionDecorator( + http_client.HTTPConnection) return oldHTTPConnection @@ -692,7 +692,7 @@ class WsgiLimiterProxyTest(BaseLimitTestSuite): """Set up HTTP/WSGI magic. Do some nifty HTTP/WSGI magic which allows for WSGI to be called - directly by something like the `httplib` library. + directly by something like the `http_client` library. """ super(WsgiLimiterProxyTest, self).setUp() self.app = limits.WsgiLimiter(TEST_LIMITS) @@ -720,7 +720,7 @@ class WsgiLimiterProxyTest(BaseLimitTestSuite): def tearDown(self): # restore original HTTPConnection object - httplib.HTTPConnection = self.oldHTTPConnection + http_client.HTTPConnection = self.oldHTTPConnection super(WsgiLimiterProxyTest, self).tearDown() diff --git a/manila/tests/cmd/test_manage.py b/manila/tests/cmd/test_manage.py index 7f1a521654..c7323a87b5 100644 --- a/manila/tests/cmd/test_manage.py +++ b/manila/tests/cmd/test_manage.py @@ -15,12 +15,12 @@ import code import readline -import StringIO import sys import ddt import mock from oslo_config import cfg +import six from manila.cmd import manage as manila_manage from manila import context @@ -162,7 +162,7 @@ class ManilaCmdManageTestCase(test.TestCase): readline.parse_and_bind.assert_called_once_with("tab:complete") code.interact.assert_called_once_with() - @mock.patch('__builtin__.print') + @mock.patch('six.moves.builtins.print') def test_list(self, print_mock): serv_1 = { 'host': 'fake_host1', @@ -184,7 +184,7 @@ class ManilaCmdManageTestCase(test.TestCase): mock.call(u'host \tzone '), mock.call('fake_host1 \tavail_zone1 ')]) - @mock.patch('__builtin__.print') + @mock.patch('six.moves.builtins.print') def test_list_zone_is_none(self, print_mock): serv_1 = { 'host': 'fake_host1', @@ -235,7 +235,7 @@ class ManilaCmdManageTestCase(test.TestCase): def test_version_commands_list(self): self.mock_object(version, 'version_string', mock.Mock(return_value='123')) - with mock.patch('sys.stdout', new=StringIO.StringIO()) as fake_out: + with mock.patch('sys.stdout', new=six.StringIO()) as fake_out: self.version_commands.list() version.version_string.assert_called_once_with() self.assertEqual('123\n', fake_out.getvalue()) @@ -243,13 +243,13 @@ class ManilaCmdManageTestCase(test.TestCase): def test_version_commands_call(self): self.mock_object(version, 'version_string', mock.Mock(return_value='123')) - with mock.patch('sys.stdout', new=StringIO.StringIO()) as fake_out: + with mock.patch('sys.stdout', new=six.StringIO()) as fake_out: self.version_commands() version.version_string.assert_called_once_with() self.assertEqual('123\n', fake_out.getvalue()) def test_get_log_commands_no_errors(self): - with mock.patch('sys.stdout', new=StringIO.StringIO()) as fake_out: + with mock.patch('sys.stdout', new=six.StringIO()) as fake_out: CONF.set_override('log_dir', None) expected_out = 'No errors in logfiles!\n' @@ -257,14 +257,14 @@ class ManilaCmdManageTestCase(test.TestCase): self.assertEqual(expected_out, fake_out.getvalue()) - @mock.patch('__builtin__.open') + @mock.patch('six.moves.builtins.open') @mock.patch('os.listdir') def test_get_log_commands_errors(self, listdir, open): CONF.set_override('log_dir', 'fake-dir') listdir.return_value = ['fake-error.log'] - with mock.patch('sys.stdout', new=StringIO.StringIO()) as fake_out: - open.return_value = StringIO.StringIO( + with mock.patch('sys.stdout', new=six.StringIO()) as fake_out: + open.return_value = six.StringIO( '[ ERROR ] fake-error-message') expected_out = ('fake-dir/fake-error.log:-\n' 'Line 1 : [ ERROR ] fake-error-message\n') @@ -274,7 +274,7 @@ class ManilaCmdManageTestCase(test.TestCase): open.assert_called_once_with('fake-dir/fake-error.log', 'r') listdir.assert_called_once_with(CONF.log_dir) - @mock.patch('__builtin__.open') + @mock.patch('six.moves.builtins.open') @mock.patch('os.path.exists') def test_get_log_commands_syslog_no_log_file(self, path_exists, open): path_exists.return_value = False @@ -297,7 +297,7 @@ class ManilaCmdManageTestCase(test.TestCase): 'disabled': False} service_get_all.return_value = [service] service_is_up.return_value = True - with mock.patch('sys.stdout', new=StringIO.StringIO()) as fake_out: + with mock.patch('sys.stdout', new=six.StringIO()) as fake_out: format = "%-16s %-36s %-16s %-10s %-5s %-10s" print_format = format % ('Binary', 'Host', diff --git a/manila/tests/integrated/api/client.py b/manila/tests/integrated/api/client.py index 99844025b4..7f977cac89 100644 --- a/manila/tests/integrated/api/client.py +++ b/manila/tests/integrated/api/client.py @@ -12,10 +12,9 @@ # License for the specific language governing permissions and limitations # under the License. -import httplib - from oslo_log import log from oslo_serialization import jsonutils +from six.moves import http_client from six.moves.urllib import parse LOG = log.getLogger(__name__) @@ -91,11 +90,11 @@ class TestOpenStackClient(object): scheme = parsed_url.scheme if scheme == 'http': - conn = httplib.HTTPConnection(hostname, - port=port) + conn = http_client.HTTPConnection(hostname, + port=port) elif scheme == 'https': - conn = httplib.HTTPSConnection(hostname, - port=port) + conn = http_client.HTTPSConnection(hostname, + port=port) else: raise OpenStackApiException("Unknown scheme: %s" % url) @@ -139,7 +138,9 @@ class TestOpenStackClient(object): def api_request(self, relative_uri, check_response_status=None, **kwargs): auth_result = self._authenticate() - # NOTE(justinsb): httplib 'helpfully' converts headers to lower case + # NOTE(justinsb): http_client 'helpfully' converts headers + # to lower case + base_uri = auth_result['x-server-management-url'] full_uri = '%s/%s' % (base_uri, relative_uri) diff --git a/manila/tests/share/drivers/quobyte/test_jsonrpc.py b/manila/tests/share/drivers/quobyte/test_jsonrpc.py index 92c24d8200..a17cf8498a 100644 --- a/manila/tests/share/drivers/quobyte/test_jsonrpc.py +++ b/manila/tests/share/drivers/quobyte/test_jsonrpc.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import httplib import socket import ssl import tempfile @@ -22,6 +21,7 @@ import time import mock from oslo_serialization import jsonutils import six +from six.moves import http_client from manila import exception from manila.share.drivers.quobyte import jsonrpc @@ -74,7 +74,7 @@ class QuobyteHttpsConnectionWithCaVerificationTestCase(test.TestCase): ca_certs=ca_file, cert_reqs=mock.ANY) - @mock.patch.object(httplib.HTTPConnection, "_tunnel") + @mock.patch.object(http_client.HTTPConnection, "_tunnel") @mock.patch.object(socket, "create_connection", return_value="fake_socket") @mock.patch.object(ssl, "wrap_socket") @@ -155,7 +155,7 @@ class QuobyteJsonRpcTestCase(test.TestCase): "Will not verify the server certificate of the API service" " because the CA certificate is not available.") - @mock.patch.object(httplib.HTTPConnection, + @mock.patch.object(http_client.HTTPConnection, '__init__', return_value=None) def test_jsonrpc_init_no_ssl(self, mock_init): @@ -181,12 +181,12 @@ class QuobyteJsonRpcTestCase(test.TestCase): 'request', mock.Mock(side_effect=ssl.SSLError)) self.mock_object(jsonrpc.LOG, 'warning') - self.mock_object(httplib, 'HTTPSConnection') + self.mock_object(http_client, 'HTTPSConnection') self.assertRaises(exception.QBException, self.rpc.call, 'method', {'param': 'value'}) - httplib.HTTPSConnection.assert_called_once_with(self.rpc._netloc) + http_client.HTTPSConnection.assert_called_once_with(self.rpc._netloc) self.assertTrue(self.rpc._disabled_cert_verification) jsonrpc.LOG.warning.assert_called_once_with( "Could not verify server certificate of " @@ -220,7 +220,7 @@ class QuobyteJsonRpcTestCase(test.TestCase): self.mock_object( self.rpc._connection, 'getresponse', - mock.Mock(side_effect=httplib.BadStatusLine("fake_line"))) + mock.Mock(side_effect=http_client.BadStatusLine("fake_line"))) self.assertRaises(exception.QBException, self.rpc.call, @@ -230,7 +230,7 @@ class QuobyteJsonRpcTestCase(test.TestCase): self.mock_object( self.rpc._connection, 'getresponse', - mock.Mock(side_effect=httplib.HTTPException)) + mock.Mock(side_effect=http_client.HTTPException)) self.mock_object(jsonrpc.LOG, 'warning') self.assertRaises(exception.QBException, @@ -243,7 +243,7 @@ class QuobyteJsonRpcTestCase(test.TestCase): self.mock_object( self.rpc._connection, 'getresponse', - mock.Mock(side_effect=httplib.HTTPException)) + mock.Mock(side_effect=http_client.HTTPException)) self.mock_object(jsonrpc.LOG, 'warning') self.rpc._fail_fast = False diff --git a/manila/tests/test_api.py b/manila/tests/test_api.py index 86460a3c1a..53f3612b82 100644 --- a/manila/tests/test_api.py +++ b/manila/tests/test_api.py @@ -16,14 +16,13 @@ """Unit tests for the API endpoint.""" -import httplib - import six +from six.moves import http_client import webob class FakeHttplibSocket(object): - """A fake socket implementation for httplib.HTTPResponse, trivial.""" + """A fake socket implementation for http_client.HTTPResponse, trivial.""" def __init__(self, response_string): self.response_string = response_string self._buffer = six.StringIO(response_string) @@ -34,11 +33,11 @@ class FakeHttplibSocket(object): class FakeHttplibConnection(object): - """A fake httplib.HTTPConnection for boto. + """A fake http_client.HTTPConnection for boto. requests made via this connection actually get translated and routed into our WSGI app, we then wait for the response and turn it back into - the httplib.HTTPResponse that boto expects. + the http_client.HTTPResponse that boto expects. """ def __init__(self, app, host, is_secure=False): self.app = app @@ -57,7 +56,7 @@ class FakeHttplibConnection(object): # guess that's a function the web server usually provides. resp = "HTTP/1.0 %s" % resp self.sock = FakeHttplibSocket(resp) - self.http_response = httplib.HTTPResponse(self.sock) + self.http_response = http_client.HTTPResponse(self.sock) # NOTE(vish): boto is accessing private variables for some reason self._HTTPConnection__response = self.http_response self.http_response.begin() diff --git a/manila/tests/test_utils.py b/manila/tests/test_utils.py index a3c419f95b..e76f9a1272 100644 --- a/manila/tests/test_utils.py +++ b/manila/tests/test_utils.py @@ -14,7 +14,6 @@ # License for the specific language governing permissions and limitations # under the License. -import __builtin__ import datetime import errno import os @@ -29,6 +28,7 @@ import mock from oslo_config import cfg from oslo_utils import timeutils import paramiko +from six.moves import builtins import manila from manila import exception @@ -210,7 +210,7 @@ class GenericUtilsTestCase(test.TestCase): fake_context_manager.__enter__ = mock.Mock(return_value=fake_file) fake_context_manager.__exit__ = mock.Mock() with mock.patch.object( - __builtin__, 'open', + builtins, 'open', mock.Mock(return_value=fake_context_manager)): cache_data = {"data": 1123, "mtime": 1} self.reload_called = False @@ -226,7 +226,7 @@ class GenericUtilsTestCase(test.TestCase): self.assertTrue(self.reload_called) fake_file.read.assert_called_once_with() fake_context_manager.__enter__.assert_any_call() - __builtin__.open.assert_called_once_with("/this/is/a/fake") + builtins.open.assert_called_once_with("/this/is/a/fake") os.path.getmtime.assert_called_once_with("/this/is/a/fake") def test_read_file_as_root(self): @@ -288,8 +288,8 @@ class GenericUtilsTestCase(test.TestCase): def test_is_ipv6_configured0(self): fake_fd = mock.Mock() fake_fd.read.return_value = 'test' - with mock.patch( - '__builtin__.open', mock.Mock(return_value=fake_fd)) as open: + with mock.patch('six.moves.builtins.open', + mock.Mock(return_value=fake_fd)) as open: self.assertTrue(utils.is_ipv6_configured()) open.assert_called_once_with('/proc/net/if_inet6') @@ -299,17 +299,19 @@ class GenericUtilsTestCase(test.TestCase): fake_fd = mock.Mock() fake_fd.read.return_value = '' with mock.patch( - '__builtin__.open', mock.Mock(return_value=fake_fd)): + 'six.moves.builtins.open', mock.Mock(return_value=fake_fd)): self.assertFalse(utils.is_ipv6_configured()) def test_is_ipv6_configured2(self): - with mock.patch('__builtin__.open', mock.Mock(side_effect=IOError( - errno.ENOENT, 'Fake no such file error.'))): + with mock.patch('six.moves.builtins.open', + mock.Mock(side_effect=IOError( + errno.ENOENT, 'Fake no such file error.'))): self.assertFalse(utils.is_ipv6_configured()) def test_is_ipv6_configured3(self): - with mock.patch('__builtin__.open', mock.Mock(side_effect=IOError( - errno.EPERM, 'Fake no such file error.'))): + with mock.patch('six.moves.builtins.open', + mock.Mock(side_effect=IOError( + errno.EPERM, 'Fake no such file error.'))): self.assertRaises(IOError, utils.is_ipv6_configured) def test_is_eventlet_bug105(self): diff --git a/manila/tests/test_wsgi.py b/manila/tests/test_wsgi.py index c7e9c503c0..32ea0ca002 100644 --- a/manila/tests/test_wsgi.py +++ b/manila/tests/test_wsgi.py @@ -19,13 +19,13 @@ import os.path import ssl import tempfile -import urllib2 import ddt import eventlet import mock from oslo_config import cfg import six +from six.moves import urllib import testtools import webob import webob.dec @@ -132,7 +132,7 @@ class TestWSGIServer(test.TestCase): "test_app", hello_world, host="127.0.0.1", port=0) server.start() - response = urllib2.urlopen('http://127.0.0.1:%d/' % server.port) + response = urllib.request.urlopen('http://127.0.0.1:%d/' % server.port) self.assertEqual(greetings, response.read()) # Verify provided parameters to eventlet.spawn func @@ -173,11 +173,12 @@ class TestWSGIServer(test.TestCase): server.start() if hasattr(ssl, '_create_unverified_context'): - response = urllib2.urlopen( + response = urllib.request.urlopen( 'https://127.0.0.1:%d/' % server.port, context=ssl._create_unverified_context()) else: - response = urllib2.urlopen('https://127.0.0.1:%d/' % server.port) + response = urllib.request.urlopen( + 'https://127.0.0.1:%d/' % server.port) self.assertEqual(greetings, response.read()) @@ -206,11 +207,12 @@ class TestWSGIServer(test.TestCase): server.start() if hasattr(ssl, '_create_unverified_context'): - response = urllib2.urlopen( + response = urllib.request.urlopen( 'https://[::1]:%d/' % server.port, context=ssl._create_unverified_context()) else: - response = urllib2.urlopen('https://[::1]:%d/' % server.port) + response = urllib.request.urlopen( + 'https://[::1]:%d/' % server.port) self.assertEqual(greetings, response.read())