Fix imports for py34 compatibility

Fix imports of:
    urllib2
    httplib
    __builtin__
    StringIO
    UserDict

Partially-Implements: bp py3-compatibility

Change-Id: I0da74d504456cbb9680973bdabc091cf8786bc9a
This commit is contained in:
Julia Varlamova
2015-07-29 04:54:28 -04:00
committed by Valeriy Ponomaryov
parent f53613f5ff
commit c3e749dafd
15 changed files with 122 additions and 110 deletions

View File

@@ -14,7 +14,10 @@
# under the License. # under the License.
import re 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 from oslo_log import log
import paste.urlmap import paste.urlmap
@@ -65,7 +68,7 @@ def parse_list_header(value):
:return: :class:`list` :return: :class:`list`
""" """
result = [] result = []
for item in urllib2.parse_http_list(value): for item in parse_http_list(value):
if item[:1] == item[-1:] == '"': if item[:1] == item[-1:] == '"':
item = unquote_header_value(item[1:-1]) item = unquote_header_value(item[1:-1])
result.append(item) result.append(item)

View File

@@ -19,13 +19,13 @@ Module dedicated functions/classes dealing with rate limiting requests.
import collections import collections
import copy import copy
import httplib
import math import math
import re import re
import time import time
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from oslo_utils import importutils from oslo_utils import importutils
from six.moves import http_client
import webob.dec import webob.dec
import webob.exc import webob.exc
@@ -406,7 +406,7 @@ class WsgiLimiterProxy(object):
body = jsonutils.dumps({"verb": verb, "path": path}) body = jsonutils.dumps({"verb": verb, "path": path})
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
conn = httplib.HTTPConnection(self.limiter_address) conn = http_client.HTTPConnection(self.limiter_address)
if username: if username:
conn.request("POST", "/%s" % (username), body, headers) conn.request("POST", "/%s" % (username), body, headers)

View File

@@ -21,7 +21,10 @@ Manage hosts in the current zone.
""" """
import re 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_config import cfg
from oslo_log import log from oslo_log import log
@@ -59,7 +62,7 @@ CONF.import_opt('max_over_subscription_ratio', 'manila.share.driver')
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
class ReadOnlyDict(UserDict.IterableUserDict): class ReadOnlyDict(IterableUserDict):
"""A read-only dict.""" """A read-only dict."""
def __init__(self, source=None): def __init__(self, source=None):
self.data = {} self.data = {}
@@ -83,7 +86,7 @@ class ReadOnlyDict(UserDict.IterableUserDict):
def update(self, source=None): def update(self, source=None):
if source is None: if source is None:
return return
elif isinstance(source, UserDict.UserDict): elif isinstance(source, IterableUserDict):
self.data = source.data self.data = source.data
elif isinstance(source, type({})): elif isinstance(source, type({})):
self.data = source self.data = source

View File

@@ -12,7 +12,6 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import cookielib
import random import random
import re import re
@@ -21,6 +20,7 @@ from lxml import builder
from lxml import etree as ET from lxml import etree as ET
from oslo_log import log from oslo_log import log
import six 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 error as url_error # pylint: disable=E0611
from six.moves.urllib import request as url_request # 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 self._url = ('https://' + self.storage_ip
+ '/servlets/CelerraManagementServices') + '/servlets/CelerraManagementServices')
https_handler = url_request.HTTPSHandler() https_handler = url_request.HTTPSHandler()
cookie_jar = cookielib.CookieJar() cookie_jar = http_cookiejar.CookieJar()
cookie_handler = url_request.HTTPCookieProcessor(cookie_jar) cookie_handler = url_request.HTTPCookieProcessor(cookie_jar)
self.url_opener = url_request.build_opener(https_handler, self.url_opener = url_request.build_opener(https_handler,
cookie_handler) cookie_handler)

View File

@@ -19,11 +19,11 @@ Contains classes required to issue API calls to Data ONTAP and OnCommand DFM.
""" """
import copy import copy
import urllib2
from lxml import etree from lxml import etree
from oslo_log import log from oslo_log import log
import six import six
from six.moves import urllib
from manila import exception from manila import exception
from manila.i18n import _ from manila.i18n import _
@@ -228,7 +228,7 @@ class NaServer(object):
response = self._opener.open(request, timeout=self._timeout) response = self._opener.open(request, timeout=self._timeout)
else: else:
response = self._opener.open(request) response = self._opener.open(request)
except urllib2.HTTPError as e: except urllib.error.HTTPError as e:
raise NaApiError(e.code, e.msg) raise NaApiError(e.code, e.msg)
except Exception as e: except Exception as e:
raise NaApiError('Unexpected error', e) raise NaApiError('Unexpected error', e)
@@ -273,7 +273,7 @@ class NaServer(object):
self._enable_tunnel_request(netapp_elem) self._enable_tunnel_request(netapp_elem)
netapp_elem.add_child_elem(na_element) netapp_elem.add_child_elem(na_element)
request_d = netapp_elem.to_string() request_d = netapp_elem.to_string()
request = urllib2.Request( request = urllib.request.Request(
self._get_url(), data=request_d, self._get_url(), data=request_d,
headers={'Content-Type': 'text/xml', 'charset': 'utf-8'}) headers={'Content-Type': 'text/xml', 'charset': 'utf-8'})
return request, netapp_elem return request, netapp_elem
@@ -320,14 +320,14 @@ class NaServer(object):
auth_handler = self._create_basic_auth_handler() auth_handler = self._create_basic_auth_handler()
else: else:
auth_handler = self._create_certificate_auth_handler() auth_handler = self._create_certificate_auth_handler()
opener = urllib2.build_opener(auth_handler) opener = urllib.request.build_opener(auth_handler)
self._opener = opener self._opener = opener
def _create_basic_auth_handler(self): 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, password_man.add_password(None, self._get_url(), self._username,
self._password) self._password)
auth_handler = urllib2.HTTPBasicAuthHandler(password_man) auth_handler = urllib.request.HTTPBasicAuthHandler(password_man)
return auth_handler return auth_handler
def _create_certificate_auth_handler(self): def _create_certificate_auth_handler(self):

View File

@@ -19,7 +19,6 @@ Control Quobyte over its JSON RPC API.
""" """
import base64 import base64
import httplib
import socket import socket
import ssl import ssl
import time import time
@@ -27,6 +26,7 @@ import time
from oslo_log import log from oslo_log import log
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
import six import six
from six.moves import http_client
import six.moves.urllib.parse as urlparse import six.moves.urllib.parse as urlparse
from manila import exception from manila import exception
@@ -55,15 +55,15 @@ class BasicAuthCredentials(object):
return 'BASIC %s' % auth return 'BASIC %s' % auth
class HTTPSConnectionWithCaVerification(httplib.HTTPConnection): class HTTPSConnectionWithCaVerification(http_client.HTTPConnection):
"""Verify server cert against a given CA certificate.""" """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, def __init__(self, host, port=None, key_file=None, cert_file=None,
ca_file=None, strict=None, ca_file=None, strict=None,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT): 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.key_file = key_file
self.cert_file = cert_file self.cert_file = cert_file
self.ca_file = ca_file self.ca_file = ca_file
@@ -79,7 +79,7 @@ class HTTPSConnectionWithCaVerification(httplib.HTTPConnection):
ca_certs=self.ca_file, ca_certs=self.ca_file,
cert_reqs=ssl.CERT_REQUIRED) cert_reqs=ssl.CERT_REQUIRED)
httplib.__all__.append("HTTPSConnectionWithCaVerification") http_client.__all__.append("HTTPSConnectionWithCaVerification")
class JsonRpc(object): class JsonRpc(object):
@@ -94,12 +94,12 @@ class JsonRpc(object):
self._netloc, self._netloc,
ca_file=self._ca_file.name) ca_file=self._ca_file.name)
else: else:
self._connection = httplib.HTTPSConnection(self._netloc) self._connection = http_client.HTTPSConnection(self._netloc)
LOG.warning(_LW( LOG.warning(_LW(
"Will not verify the server certificate of the API service" "Will not verify the server certificate of the API service"
" because the CA certificate is not available.")) " because the CA certificate is not available."))
else: else:
self._connection = httplib.HTTPConnection(self._netloc) self._connection = http_client.HTTPConnection(self._netloc)
self._id = 0 self._id = 0
self._fail_fast = True self._fail_fast = True
self._credentials = BasicAuthCredentials( self._credentials = BasicAuthCredentials(
@@ -117,7 +117,7 @@ class JsonRpc(object):
'params': parameters, 'params': parameters,
'id': six.text_type(self._id)} 'id': six.text_type(self._id)}
self.call_counter = 0 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: while self.call_counter < CONNECTION_RETRIES:
self.call_counter += 1 self.call_counter += 1
@@ -146,18 +146,19 @@ class JsonRpc(object):
"API service against CA.")) "API service against CA."))
self._connection.close() self._connection.close()
# Core HTTPSConnection does no certificate verification. # Core HTTPSConnection does no certificate verification.
self._connection = httplib.HTTPSConnection(self._netloc) self._connection = http_client.HTTPSConnection(
self._netloc)
self._disabled_cert_verification = True self._disabled_cert_verification = True
else: else:
raise exception.QBException(_( raise exception.QBException(_(
"Client SSL subsystem returned error: %s") % e) "Client SSL subsystem returned error: %s") % e)
except httplib.BadStatusLine as e: except http_client.BadStatusLine as e:
raise exception.QBException(_( raise exception.QBException(_(
"If SSL is enabled for the API service, the URL must" "If SSL is enabled for the API service, the URL must"
" start with 'https://' for the URL. Failed to parse" " start with 'https://' for the URL. Failed to parse"
" status code from server response. Error was %s") " status code from server response. Error was %s")
% e) % e)
except (httplib.HTTPException, socket.error) as e: except (http_client.HTTPException, socket.error) as e:
if self._fail_fast: if self._fail_fast:
raise exception.QBException(msg=six.text_type(e)) raise exception.QBException(msg=six.text_type(e))
else: else:

View File

@@ -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. base and imported from the relevant library.
""" """
import httplib
import time import time
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
import six import six
from six.moves import http_client
# pylint: disable=E0611,F0401 # pylint: disable=E0611,F0401
from six.moves.urllib import error as urlerror from six.moves.urllib import error as urlerror
from six.moves.urllib import request as urlrequest from six.moves.urllib import request as urlrequest
@@ -37,40 +37,40 @@ class Status(object):
"""Result HTTP Status.""" """Result HTTP Status."""
#: Request return OK #: Request return OK
OK = httplib.OK # pylint: disable=invalid-name OK = http_client.OK # pylint: disable=invalid-name
#: New resource created successfully #: New resource created successfully
CREATED = httplib.CREATED CREATED = http_client.CREATED
#: Command accepted #: Command accepted
ACCEPTED = httplib.ACCEPTED ACCEPTED = http_client.ACCEPTED
#: Command returned OK but no data will be returned #: Command returned OK but no data will be returned
NO_CONTENT = httplib.NO_CONTENT NO_CONTENT = http_client.NO_CONTENT
#: Bad Request #: Bad Request
BAD_REQUEST = httplib.BAD_REQUEST BAD_REQUEST = http_client.BAD_REQUEST
#: User is not authorized #: User is not authorized
UNAUTHORIZED = httplib.UNAUTHORIZED UNAUTHORIZED = http_client.UNAUTHORIZED
#: The request is not allowed #: The request is not allowed
FORBIDDEN = httplib.FORBIDDEN FORBIDDEN = http_client.FORBIDDEN
#: The requested resource was not found #: The requested resource was not found
NOT_FOUND = httplib.NOT_FOUND NOT_FOUND = http_client.NOT_FOUND
#: The request is not allowed #: The request is not allowed
NOT_ALLOWED = httplib.METHOD_NOT_ALLOWED NOT_ALLOWED = http_client.METHOD_NOT_ALLOWED
#: Request timed out #: Request timed out
TIMEOUT = httplib.REQUEST_TIMEOUT TIMEOUT = http_client.REQUEST_TIMEOUT
#: Invalid request #: Invalid request
CONFLICT = httplib.CONFLICT CONFLICT = http_client.CONFLICT
#: Service Unavailable #: Service Unavailable
BUSY = httplib.SERVICE_UNAVAILABLE BUSY = http_client.SERVICE_UNAVAILABLE
class RestResult(object): class RestResult(object):
@@ -96,7 +96,7 @@ class RestResult(object):
if self.error: if self.error:
self.status = self.error.code 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 code: %s' % self.status)
log_debug_msg(self, 'Response data: %s' % self.data) log_debug_msg(self, 'Response data: %s' % self.data)
@@ -127,8 +127,8 @@ class RestClientError(Exception):
self.code = status self.code = status
self.name = name self.name = name
self.msg = message self.msg = message
if status in httplib.responses: if status in http_client.responses:
self.msg = httplib.responses[status] self.msg = http_client.responses[status]
def __str__(self): def __str__(self):
return "%d %s %s" % (self.code, self.name, self.msg) return "%d %s %s" % (self.code, self.name, self.msg)
@@ -180,14 +180,14 @@ class RestClientURL(object): # pylint: disable=R0902
try: try:
result = self.post("/access/v1") result = self.post("/access/v1")
del self.headers['authorization'] del self.headers['authorization']
if result.status == httplib.CREATED: if result.status == http_client.CREATED:
self.headers['x-auth-session'] = \ self.headers['x-auth-session'] = \
result.get_header('x-auth-session') result.get_header('x-auth-session')
self.do_logout = True self.do_logout = True
log_debug_msg(self, ('ZFSSA version: %s') log_debug_msg(self, ('ZFSSA version: %s')
% result.get_header('x-zfssa-version')) % 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", raise RestClientError(result.status, name="ERR_RESTError",
message=("REST Not Available:" message=("REST Not Available:"
"Please Upgrade")) "Please Upgrade"))
@@ -285,20 +285,20 @@ class RestClientURL(object): # pylint: disable=R0902
try: try:
response = urlrequest.urlopen(req, timeout=self.timeout) response = urlrequest.urlopen(req, timeout=self.timeout)
except urlerror.HTTPError as err: 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) log_debug_msg(self, 'REST Not Found: %s' % err.code)
else: else:
log_debug_msg(self, ('REST Not Available: %s') % err.code) 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 < maxreqretries):
retry += 1 retry += 1
time.sleep(1) time.sleep(1)
log_debug_msg(self, ('Server Busy retry request: %s') log_debug_msg(self, ('Server Busy retry request: %s')
% retry) % retry)
continue continue
if ((err.code == httplib.UNAUTHORIZED or if ((err.code == http_client.UNAUTHORIZED or
err.code == httplib.INTERNAL_SERVER_ERROR) and err.code == http_client.INTERNAL_SERVER_ERROR) and
'/access/v1' not in zfssaurl): '/access/v1' not in zfssaurl):
try: try:
log_debug_msg(self, ('Authorizing request: ' log_debug_msg(self, ('Authorizing request: '
@@ -324,7 +324,7 @@ class RestClientURL(object): # pylint: disable=R0902
break break
if ((response and if ((response and
response.getcode() == httplib.SERVICE_UNAVAILABLE) and response.getcode() == http_client.SERVICE_UNAVAILABLE) and
retry >= maxreqretries): retry >= maxreqretries):
raise RestClientError(response.getcode(), name="ERR_HTTPError", raise RestClientError(response.getcode(), name="ERR_HTTPError",
message="REST Not Available: Disabled") message="REST Not Available: Disabled")

View File

@@ -29,9 +29,10 @@
""" """
import eventlet import eventlet
from six.moves import builtins
eventlet.monkey_patch() eventlet.monkey_patch()
# See http://code.google.com/p/python-nose/issues/detail?id=373 # See http://code.google.com/p/python-nose/issues/detail?id=373
# The code below enables nosetests to work with i18n _() blocks # The code below enables nosetests to work with i18n _() blocks
import __builtin__ setattr(builtins, '_', lambda x: x)
setattr(__builtin__, '_', lambda x: x)

View File

@@ -17,11 +17,10 @@
Tests dealing with HTTP rate-limiting. Tests dealing with HTTP rate-limiting.
""" """
import httplib
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
import six import six
from six import moves from six import moves
from six.moves import http_client
import webob import webob
from manila.api.v1 import limits from manila.api.v1 import limits
@@ -596,7 +595,7 @@ class WsgiLimiterTest(BaseLimitTestSuite):
class FakeHttplibSocket(object): class FakeHttplibSocket(object):
"""Fake `httplib.HTTPResponse` replacement.""" """Fake `http_client.HTTPResponse` replacement."""
def __init__(self, response_string): def __init__(self, response_string):
"""Initialize new `FakeHttplibSocket`.""" """Initialize new `FakeHttplibSocket`."""
@@ -608,7 +607,7 @@ class FakeHttplibSocket(object):
class FakeHttplibConnection(object): class FakeHttplibConnection(object):
"""Fake `httplib.HTTPConnection`.""" """Fake `http_client.HTTPConnection`."""
def __init__(self, app, host): def __init__(self, app, host):
"""Initialize `FakeHttplibConnection`.""" """Initialize `FakeHttplibConnection`."""
@@ -620,7 +619,7 @@ class FakeHttplibConnection(object):
Requests made via this connection actually get translated and routed 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 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: if not headers:
headers = {} headers = {}
@@ -634,7 +633,7 @@ class FakeHttplibConnection(object):
resp = str(req.get_response(self.app)) resp = str(req.get_response(self.app))
resp = "HTTP/1.0 %s" % resp resp = "HTTP/1.0 %s" % resp
sock = FakeHttplibSocket(resp) sock = FakeHttplibSocket(resp)
self.http_response = httplib.HTTPResponse(sock) self.http_response = http_client.HTTPResponse(sock)
self.http_response.begin() self.http_response.begin()
def getresponse(self): def getresponse(self):
@@ -650,7 +649,7 @@ def wire_HTTPConnection_to_WSGI(host, app):
After calling this method, when any code calls 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 the connection object will be a fake. Its requests will be sent directly
to the given WSGI app rather than through a socket. to the given WSGI app rather than through a socket.
@@ -680,8 +679,9 @@ def wire_HTTPConnection_to_WSGI(host, app):
else: else:
return self.wrapped(connection_host, *args, **kwargs) return self.wrapped(connection_host, *args, **kwargs)
oldHTTPConnection = httplib.HTTPConnection oldHTTPConnection = http_client.HTTPConnection
httplib.HTTPConnection = HTTPConnectionDecorator(httplib.HTTPConnection) http_client.HTTPConnection = HTTPConnectionDecorator(
http_client.HTTPConnection)
return oldHTTPConnection return oldHTTPConnection
@@ -692,7 +692,7 @@ class WsgiLimiterProxyTest(BaseLimitTestSuite):
"""Set up HTTP/WSGI magic. """Set up HTTP/WSGI magic.
Do some nifty HTTP/WSGI magic which allows for WSGI to be called 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() super(WsgiLimiterProxyTest, self).setUp()
self.app = limits.WsgiLimiter(TEST_LIMITS) self.app = limits.WsgiLimiter(TEST_LIMITS)
@@ -720,7 +720,7 @@ class WsgiLimiterProxyTest(BaseLimitTestSuite):
def tearDown(self): def tearDown(self):
# restore original HTTPConnection object # restore original HTTPConnection object
httplib.HTTPConnection = self.oldHTTPConnection http_client.HTTPConnection = self.oldHTTPConnection
super(WsgiLimiterProxyTest, self).tearDown() super(WsgiLimiterProxyTest, self).tearDown()

View File

@@ -15,12 +15,12 @@
import code import code
import readline import readline
import StringIO
import sys import sys
import ddt import ddt
import mock import mock
from oslo_config import cfg from oslo_config import cfg
import six
from manila.cmd import manage as manila_manage from manila.cmd import manage as manila_manage
from manila import context from manila import context
@@ -162,7 +162,7 @@ class ManilaCmdManageTestCase(test.TestCase):
readline.parse_and_bind.assert_called_once_with("tab:complete") readline.parse_and_bind.assert_called_once_with("tab:complete")
code.interact.assert_called_once_with() code.interact.assert_called_once_with()
@mock.patch('__builtin__.print') @mock.patch('six.moves.builtins.print')
def test_list(self, print_mock): def test_list(self, print_mock):
serv_1 = { serv_1 = {
'host': 'fake_host1', 'host': 'fake_host1',
@@ -184,7 +184,7 @@ class ManilaCmdManageTestCase(test.TestCase):
mock.call(u'host \tzone '), mock.call(u'host \tzone '),
mock.call('fake_host1 \tavail_zone1 ')]) 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): def test_list_zone_is_none(self, print_mock):
serv_1 = { serv_1 = {
'host': 'fake_host1', 'host': 'fake_host1',
@@ -235,7 +235,7 @@ class ManilaCmdManageTestCase(test.TestCase):
def test_version_commands_list(self): def test_version_commands_list(self):
self.mock_object(version, 'version_string', self.mock_object(version, 'version_string',
mock.Mock(return_value='123')) 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() self.version_commands.list()
version.version_string.assert_called_once_with() version.version_string.assert_called_once_with()
self.assertEqual('123\n', fake_out.getvalue()) self.assertEqual('123\n', fake_out.getvalue())
@@ -243,13 +243,13 @@ class ManilaCmdManageTestCase(test.TestCase):
def test_version_commands_call(self): def test_version_commands_call(self):
self.mock_object(version, 'version_string', self.mock_object(version, 'version_string',
mock.Mock(return_value='123')) 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() self.version_commands()
version.version_string.assert_called_once_with() version.version_string.assert_called_once_with()
self.assertEqual('123\n', fake_out.getvalue()) self.assertEqual('123\n', fake_out.getvalue())
def test_get_log_commands_no_errors(self): 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) CONF.set_override('log_dir', None)
expected_out = 'No errors in logfiles!\n' expected_out = 'No errors in logfiles!\n'
@@ -257,14 +257,14 @@ class ManilaCmdManageTestCase(test.TestCase):
self.assertEqual(expected_out, fake_out.getvalue()) self.assertEqual(expected_out, fake_out.getvalue())
@mock.patch('__builtin__.open') @mock.patch('six.moves.builtins.open')
@mock.patch('os.listdir') @mock.patch('os.listdir')
def test_get_log_commands_errors(self, listdir, open): def test_get_log_commands_errors(self, listdir, open):
CONF.set_override('log_dir', 'fake-dir') CONF.set_override('log_dir', 'fake-dir')
listdir.return_value = ['fake-error.log'] listdir.return_value = ['fake-error.log']
with mock.patch('sys.stdout', new=StringIO.StringIO()) as fake_out: with mock.patch('sys.stdout', new=six.StringIO()) as fake_out:
open.return_value = StringIO.StringIO( open.return_value = six.StringIO(
'[ ERROR ] fake-error-message') '[ ERROR ] fake-error-message')
expected_out = ('fake-dir/fake-error.log:-\n' expected_out = ('fake-dir/fake-error.log:-\n'
'Line 1 : [ ERROR ] fake-error-message\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') open.assert_called_once_with('fake-dir/fake-error.log', 'r')
listdir.assert_called_once_with(CONF.log_dir) listdir.assert_called_once_with(CONF.log_dir)
@mock.patch('__builtin__.open') @mock.patch('six.moves.builtins.open')
@mock.patch('os.path.exists') @mock.patch('os.path.exists')
def test_get_log_commands_syslog_no_log_file(self, path_exists, open): def test_get_log_commands_syslog_no_log_file(self, path_exists, open):
path_exists.return_value = False path_exists.return_value = False
@@ -297,7 +297,7 @@ class ManilaCmdManageTestCase(test.TestCase):
'disabled': False} 'disabled': False}
service_get_all.return_value = [service] service_get_all.return_value = [service]
service_is_up.return_value = True 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" format = "%-16s %-36s %-16s %-10s %-5s %-10s"
print_format = format % ('Binary', print_format = format % ('Binary',
'Host', 'Host',

View File

@@ -12,10 +12,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httplib
from oslo_log import log from oslo_log import log
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
from six.moves import http_client
from six.moves.urllib import parse from six.moves.urllib import parse
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@@ -91,10 +90,10 @@ class TestOpenStackClient(object):
scheme = parsed_url.scheme scheme = parsed_url.scheme
if scheme == 'http': if scheme == 'http':
conn = httplib.HTTPConnection(hostname, conn = http_client.HTTPConnection(hostname,
port=port) port=port)
elif scheme == 'https': elif scheme == 'https':
conn = httplib.HTTPSConnection(hostname, conn = http_client.HTTPSConnection(hostname,
port=port) port=port)
else: else:
raise OpenStackApiException("Unknown scheme: %s" % url) raise OpenStackApiException("Unknown scheme: %s" % url)
@@ -139,7 +138,9 @@ class TestOpenStackClient(object):
def api_request(self, relative_uri, check_response_status=None, **kwargs): def api_request(self, relative_uri, check_response_status=None, **kwargs):
auth_result = self._authenticate() 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'] base_uri = auth_result['x-server-management-url']
full_uri = '%s/%s' % (base_uri, relative_uri) full_uri = '%s/%s' % (base_uri, relative_uri)

View File

@@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import httplib
import socket import socket
import ssl import ssl
import tempfile import tempfile
@@ -22,6 +21,7 @@ import time
import mock import mock
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
import six import six
from six.moves import http_client
from manila import exception from manila import exception
from manila.share.drivers.quobyte import jsonrpc from manila.share.drivers.quobyte import jsonrpc
@@ -74,7 +74,7 @@ class QuobyteHttpsConnectionWithCaVerificationTestCase(test.TestCase):
ca_certs=ca_file, ca_certs=ca_file,
cert_reqs=mock.ANY) cert_reqs=mock.ANY)
@mock.patch.object(httplib.HTTPConnection, "_tunnel") @mock.patch.object(http_client.HTTPConnection, "_tunnel")
@mock.patch.object(socket, "create_connection", @mock.patch.object(socket, "create_connection",
return_value="fake_socket") return_value="fake_socket")
@mock.patch.object(ssl, "wrap_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" "Will not verify the server certificate of the API service"
" because the CA certificate is not available.") " because the CA certificate is not available.")
@mock.patch.object(httplib.HTTPConnection, @mock.patch.object(http_client.HTTPConnection,
'__init__', '__init__',
return_value=None) return_value=None)
def test_jsonrpc_init_no_ssl(self, mock_init): def test_jsonrpc_init_no_ssl(self, mock_init):
@@ -181,12 +181,12 @@ class QuobyteJsonRpcTestCase(test.TestCase):
'request', 'request',
mock.Mock(side_effect=ssl.SSLError)) mock.Mock(side_effect=ssl.SSLError))
self.mock_object(jsonrpc.LOG, 'warning') self.mock_object(jsonrpc.LOG, 'warning')
self.mock_object(httplib, 'HTTPSConnection') self.mock_object(http_client, 'HTTPSConnection')
self.assertRaises(exception.QBException, self.assertRaises(exception.QBException,
self.rpc.call, self.rpc.call,
'method', {'param': 'value'}) '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) self.assertTrue(self.rpc._disabled_cert_verification)
jsonrpc.LOG.warning.assert_called_once_with( jsonrpc.LOG.warning.assert_called_once_with(
"Could not verify server certificate of " "Could not verify server certificate of "
@@ -220,7 +220,7 @@ class QuobyteJsonRpcTestCase(test.TestCase):
self.mock_object( self.mock_object(
self.rpc._connection, self.rpc._connection,
'getresponse', 'getresponse',
mock.Mock(side_effect=httplib.BadStatusLine("fake_line"))) mock.Mock(side_effect=http_client.BadStatusLine("fake_line")))
self.assertRaises(exception.QBException, self.assertRaises(exception.QBException,
self.rpc.call, self.rpc.call,
@@ -230,7 +230,7 @@ class QuobyteJsonRpcTestCase(test.TestCase):
self.mock_object( self.mock_object(
self.rpc._connection, self.rpc._connection,
'getresponse', 'getresponse',
mock.Mock(side_effect=httplib.HTTPException)) mock.Mock(side_effect=http_client.HTTPException))
self.mock_object(jsonrpc.LOG, 'warning') self.mock_object(jsonrpc.LOG, 'warning')
self.assertRaises(exception.QBException, self.assertRaises(exception.QBException,
@@ -243,7 +243,7 @@ class QuobyteJsonRpcTestCase(test.TestCase):
self.mock_object( self.mock_object(
self.rpc._connection, self.rpc._connection,
'getresponse', 'getresponse',
mock.Mock(side_effect=httplib.HTTPException)) mock.Mock(side_effect=http_client.HTTPException))
self.mock_object(jsonrpc.LOG, 'warning') self.mock_object(jsonrpc.LOG, 'warning')
self.rpc._fail_fast = False self.rpc._fail_fast = False

View File

@@ -16,14 +16,13 @@
"""Unit tests for the API endpoint.""" """Unit tests for the API endpoint."""
import httplib
import six import six
from six.moves import http_client
import webob import webob
class FakeHttplibSocket(object): 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): def __init__(self, response_string):
self.response_string = response_string self.response_string = response_string
self._buffer = six.StringIO(response_string) self._buffer = six.StringIO(response_string)
@@ -34,11 +33,11 @@ class FakeHttplibSocket(object):
class FakeHttplibConnection(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 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 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): def __init__(self, app, host, is_secure=False):
self.app = app self.app = app
@@ -57,7 +56,7 @@ class FakeHttplibConnection(object):
# guess that's a function the web server usually provides. # guess that's a function the web server usually provides.
resp = "HTTP/1.0 %s" % resp resp = "HTTP/1.0 %s" % resp
self.sock = FakeHttplibSocket(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 # NOTE(vish): boto is accessing private variables for some reason
self._HTTPConnection__response = self.http_response self._HTTPConnection__response = self.http_response
self.http_response.begin() self.http_response.begin()

View File

@@ -14,7 +14,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import __builtin__
import datetime import datetime
import errno import errno
import os import os
@@ -29,6 +28,7 @@ import mock
from oslo_config import cfg from oslo_config import cfg
from oslo_utils import timeutils from oslo_utils import timeutils
import paramiko import paramiko
from six.moves import builtins
import manila import manila
from manila import exception 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.__enter__ = mock.Mock(return_value=fake_file)
fake_context_manager.__exit__ = mock.Mock() fake_context_manager.__exit__ = mock.Mock()
with mock.patch.object( with mock.patch.object(
__builtin__, 'open', builtins, 'open',
mock.Mock(return_value=fake_context_manager)): mock.Mock(return_value=fake_context_manager)):
cache_data = {"data": 1123, "mtime": 1} cache_data = {"data": 1123, "mtime": 1}
self.reload_called = False self.reload_called = False
@@ -226,7 +226,7 @@ class GenericUtilsTestCase(test.TestCase):
self.assertTrue(self.reload_called) self.assertTrue(self.reload_called)
fake_file.read.assert_called_once_with() fake_file.read.assert_called_once_with()
fake_context_manager.__enter__.assert_any_call() 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") os.path.getmtime.assert_called_once_with("/this/is/a/fake")
def test_read_file_as_root(self): def test_read_file_as_root(self):
@@ -288,8 +288,8 @@ class GenericUtilsTestCase(test.TestCase):
def test_is_ipv6_configured0(self): def test_is_ipv6_configured0(self):
fake_fd = mock.Mock() fake_fd = mock.Mock()
fake_fd.read.return_value = 'test' fake_fd.read.return_value = 'test'
with mock.patch( with mock.patch('six.moves.builtins.open',
'__builtin__.open', mock.Mock(return_value=fake_fd)) as open: mock.Mock(return_value=fake_fd)) as open:
self.assertTrue(utils.is_ipv6_configured()) self.assertTrue(utils.is_ipv6_configured())
open.assert_called_once_with('/proc/net/if_inet6') open.assert_called_once_with('/proc/net/if_inet6')
@@ -299,16 +299,18 @@ class GenericUtilsTestCase(test.TestCase):
fake_fd = mock.Mock() fake_fd = mock.Mock()
fake_fd.read.return_value = '' fake_fd.read.return_value = ''
with mock.patch( 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()) self.assertFalse(utils.is_ipv6_configured())
def test_is_ipv6_configured2(self): def test_is_ipv6_configured2(self):
with mock.patch('__builtin__.open', mock.Mock(side_effect=IOError( with mock.patch('six.moves.builtins.open',
mock.Mock(side_effect=IOError(
errno.ENOENT, 'Fake no such file error.'))): errno.ENOENT, 'Fake no such file error.'))):
self.assertFalse(utils.is_ipv6_configured()) self.assertFalse(utils.is_ipv6_configured())
def test_is_ipv6_configured3(self): def test_is_ipv6_configured3(self):
with mock.patch('__builtin__.open', mock.Mock(side_effect=IOError( with mock.patch('six.moves.builtins.open',
mock.Mock(side_effect=IOError(
errno.EPERM, 'Fake no such file error.'))): errno.EPERM, 'Fake no such file error.'))):
self.assertRaises(IOError, utils.is_ipv6_configured) self.assertRaises(IOError, utils.is_ipv6_configured)

View File

@@ -19,13 +19,13 @@
import os.path import os.path
import ssl import ssl
import tempfile import tempfile
import urllib2
import ddt import ddt
import eventlet import eventlet
import mock import mock
from oslo_config import cfg from oslo_config import cfg
import six import six
from six.moves import urllib
import testtools import testtools
import webob import webob
import webob.dec import webob.dec
@@ -132,7 +132,7 @@ class TestWSGIServer(test.TestCase):
"test_app", hello_world, host="127.0.0.1", port=0) "test_app", hello_world, host="127.0.0.1", port=0)
server.start() 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()) self.assertEqual(greetings, response.read())
# Verify provided parameters to eventlet.spawn func # Verify provided parameters to eventlet.spawn func
@@ -173,11 +173,12 @@ class TestWSGIServer(test.TestCase):
server.start() server.start()
if hasattr(ssl, '_create_unverified_context'): if hasattr(ssl, '_create_unverified_context'):
response = urllib2.urlopen( response = urllib.request.urlopen(
'https://127.0.0.1:%d/' % server.port, 'https://127.0.0.1:%d/' % server.port,
context=ssl._create_unverified_context()) context=ssl._create_unverified_context())
else: 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()) self.assertEqual(greetings, response.read())
@@ -206,11 +207,12 @@ class TestWSGIServer(test.TestCase):
server.start() server.start()
if hasattr(ssl, '_create_unverified_context'): if hasattr(ssl, '_create_unverified_context'):
response = urllib2.urlopen( response = urllib.request.urlopen(
'https://[::1]:%d/' % server.port, 'https://[::1]:%d/' % server.port,
context=ssl._create_unverified_context()) context=ssl._create_unverified_context())
else: else:
response = urllib2.urlopen('https://[::1]:%d/' % server.port) response = urllib.request.urlopen(
'https://[::1]:%d/' % server.port)
self.assertEqual(greetings, response.read()) self.assertEqual(greetings, response.read())