diff options
-rw-r--r-- | glance_store/_drivers/filesystem.py | 4 | ||||
-rw-r--r-- | glance_store/_drivers/gridfs.py | 6 | ||||
-rw-r--r-- | glance_store/_drivers/http.py | 15 | ||||
-rw-r--r-- | glance_store/_drivers/rbd.py | 12 | ||||
-rw-r--r-- | glance_store/_drivers/s3.py | 8 | ||||
-rw-r--r-- | glance_store/_drivers/swift/store.py | 28 | ||||
-rw-r--r-- | glance_store/_drivers/swift/utils.py | 6 | ||||
-rw-r--r-- | glance_store/_drivers/vmware_datastore.py | 30 | ||||
-rw-r--r-- | glance_store/common/auth.py | 6 | ||||
-rw-r--r-- | glance_store/exceptions.py | 4 | ||||
-rw-r--r-- | glance_store/location.py | 4 | ||||
-rw-r--r-- | glance_store/tests/utils.py | 15 | ||||
-rw-r--r-- | tests/unit/test_filesystem_store.py | 4 | ||||
-rw-r--r-- | tests/unit/test_http_store.py | 5 | ||||
-rw-r--r-- | tests/unit/test_s3_store.py | 20 | ||||
-rw-r--r-- | tests/unit/test_swift_store.py | 54 | ||||
-rw-r--r-- | tests/unit/test_vmware_store.py | 23 |
17 files changed, 124 insertions, 120 deletions
diff --git a/glance_store/_drivers/filesystem.py b/glance_store/_drivers/filesystem.py index e9f849a..9b2ed50 100644 --- a/glance_store/_drivers/filesystem.py +++ b/glance_store/_drivers/filesystem.py @@ -23,13 +23,13 @@ import hashlib import logging import os import stat -import urlparse import jsonschema from oslo_config import cfg from oslo_serialization import jsonutils from oslo_utils import excutils from oslo_utils import units +from six.moves import urllib import glance_store from glance_store import capabilities @@ -98,7 +98,7 @@ class StoreLocation(glance_store.location.StoreLocation): in the URL are interpreted differently in Python 2.6.1+ than prior versions of Python. """ - pieces = urlparse.urlparse(uri) + pieces = urllib.parse.urlparse(uri) assert pieces.scheme in ('file', 'filesystem') self.scheme = pieces.scheme path = (pieces.netloc + pieces.path).strip() diff --git a/glance_store/_drivers/gridfs.py b/glance_store/_drivers/gridfs.py index b4b7e7d..5ce5370 100644 --- a/glance_store/_drivers/gridfs.py +++ b/glance_store/_drivers/gridfs.py @@ -17,10 +17,10 @@ from __future__ import absolute_import import logging -import urlparse from oslo_config import cfg from oslo_utils import excutils +from six.moves import urllib from glance_store import capabilities import glance_store.driver @@ -73,7 +73,7 @@ class StoreLocation(glance_store.location.StoreLocation): :param uri: Current set URI """ - parsed = urlparse.urlparse(uri) + parsed = urllib.parse.urlparse(uri) assert parsed.scheme in ('gridfs',) self.specs["image_id"] = parsed.netloc @@ -160,7 +160,7 @@ class Store(glance_store.driver.Store): store_location = location.store_location try: - parsed = urlparse.urlparse(store_location.get_uri()) + parsed = urllib.parse.urlparse(store_location.get_uri()) return self.fs.get(parsed.netloc) except gridfs.errors.NoFile: msg = _("Could not find %s image in GridFS") % \ diff --git a/glance_store/_drivers/http.py b/glance_store/_drivers/http.py index 73946f7..f539905 100644 --- a/glance_store/_drivers/http.py +++ b/glance_store/_drivers/http.py @@ -13,10 +13,11 @@ # License for the specific language governing permissions and limitations # under the License. -import httplib import logging import socket -import urlparse + +from six.moves import http_client +from six.moves import urllib from glance_store import capabilities import glance_store.driver @@ -60,7 +61,7 @@ class StoreLocation(glance_store.location.StoreLocation): in the URL are interpreted differently in Python 2.6.1+ than prior versions of Python. """ - pieces = urlparse.urlparse(uri) + pieces = urllib.parse.urlparse(uri) assert pieces.scheme in ('https', 'http') self.scheme = pieces.scheme netloc = pieces.netloc @@ -98,7 +99,7 @@ def http_response_iterator(conn, response, size): Return an iterator for a file-like object. :param conn: HTTP(S) Connection - :param response: httplib.HTTPResponse object + :param response: http_client.HTTPResponse object :param size: Chunk size to iterate with """ chunk = response.read(size) @@ -183,7 +184,7 @@ class Store(glance_store.driver.Store): # Check for bad status codes if resp.status >= 400: - if resp.status == httplib.NOT_FOUND: + if resp.status == http_client.NOT_FOUND: reason = _("HTTP datastore could not find image at URI.") LOG.debug(reason) raise exceptions.NotFound(message=reason) @@ -218,5 +219,5 @@ class Store(glance_store.driver.Store): Returns connection class for accessing the resource. Useful for dependency injection and stubouts in testing... """ - return {'http': httplib.HTTPConnection, - 'https': httplib.HTTPSConnection}[loc.scheme] + return {'http': http_client.HTTPConnection, + 'https': http_client.HTTPSConnection}[loc.scheme] diff --git a/glance_store/_drivers/rbd.py b/glance_store/_drivers/rbd.py index d286487..4d86b68 100644 --- a/glance_store/_drivers/rbd.py +++ b/glance_store/_drivers/rbd.py @@ -21,11 +21,11 @@ from __future__ import with_statement import hashlib import logging import math -import urllib from oslo_config import cfg from oslo_utils import units import six +from six.moves import urllib from glance_store import capabilities from glance_store.common import utils @@ -94,10 +94,10 @@ class StoreLocation(location.StoreLocation): def get_uri(self): if self.fsid and self.pool and self.snapshot: # ensure nothing contains / or any other url-unsafe character - safe_fsid = urllib.quote(self.fsid, '') - safe_pool = urllib.quote(self.pool, '') - safe_image = urllib.quote(self.image, '') - safe_snapshot = urllib.quote(self.snapshot, '') + safe_fsid = urllib.parse.quote(self.fsid, '') + safe_pool = urllib.parse.quote(self.pool, '') + safe_image = urllib.parse.quote(self.image, '') + safe_snapshot = urllib.parse.quote(self.snapshot, '') return "rbd://%s/%s/%s/%s" % (safe_fsid, safe_pool, safe_image, safe_snapshot) else: @@ -126,7 +126,7 @@ class StoreLocation(location.StoreLocation): (None, None, pieces[0], None) elif len(pieces) == 4: self.fsid, self.pool, self.image, self.snapshot = \ - map(urllib.unquote, pieces) + map(urllib.parse.unquote, pieces) else: reason = _('URI must have exactly 1 or 4 components') msg = _LI("Invalid URI: %s") % reason diff --git a/glance_store/_drivers/s3.py b/glance_store/_drivers/s3.py index 5212a9e..0f0e3d0 100644 --- a/glance_store/_drivers/s3.py +++ b/glance_store/_drivers/s3.py @@ -16,18 +16,18 @@ """Storage backend for S3 or Storage Servers that follow the S3 Protocol""" import hashlib -import httplib import logging import math import re import tempfile -import urlparse import eventlet from oslo_config import cfg from oslo_utils import netutils from oslo_utils import units import six +from six.moves import http_client +from six.moves import urllib import glance_store from glance_store import capabilities @@ -207,7 +207,7 @@ class StoreLocation(glance_store.location.StoreLocation): LOG.info(_LI("Invalid store uri: %s") % reason) raise exceptions.BadStoreUri(message=reason) - pieces = urlparse.urlparse(uri) + pieces = urllib.parse.urlparse(uri) assert pieces.scheme in ('s3', 's3+http', 's3+https') self.scheme = pieces.scheme path = pieces.path.strip('/') @@ -740,7 +740,7 @@ def create_bucket_if_missing(conf, bucket, s3_conn): try: s3_conn.get_bucket(bucket) except S3ResponseError as e: - if e.status == httplib.NOT_FOUND: + if e.status == http_client.NOT_FOUND: if conf.glance_store.s3_store_create_bucket_on_put: host = conf.glance_store.s3_store_host location = get_s3_location(host) diff --git a/glance_store/_drivers/swift/store.py b/glance_store/_drivers/swift/store.py index c7f97c0..f3fd7ff 100644 --- a/glance_store/_drivers/swift/store.py +++ b/glance_store/_drivers/swift/store.py @@ -16,7 +16,6 @@ """Storage backend for SWIFT""" import hashlib -import httplib import logging import math @@ -24,9 +23,9 @@ from oslo_config import cfg from oslo_utils import excutils from oslo_utils import units import six -import six.moves.urllib.parse as urlparse +from six.moves import http_client +from six.moves import urllib import swiftclient -import urllib import glance_store from glance_store._drivers.swift import utils as sutils @@ -195,7 +194,8 @@ class StoreLocation(location.StoreLocation): def _get_credstring(self): if self.user and self.key: - return '%s:%s' % (urllib.quote(self.user), urllib.quote(self.key)) + return '%s:%s' % (urllib.parse.quote(self.user), + urllib.parse.quote(self.key)) return '' def get_uri(self, credentials_included=True): @@ -270,12 +270,12 @@ class StoreLocation(location.StoreLocation): raise exceptions.BadStoreUri(message=reason) key = cred_parts.pop() user = ':'.join(cred_parts) - creds = urllib.unquote(creds) + creds = urllib.parse.unquote(creds) try: self.user, self.key = creds.rsplit(':', 1) except exceptions.BadStoreConfiguration: - self.user = urllib.unquote(user) - self.key = urllib.unquote(key) + self.user = urllib.parse.unquote(user) + self.key = urllib.parse.unquote(key) else: self.user = None self.key = None @@ -319,7 +319,7 @@ class StoreLocation(location.StoreLocation): LOG.info(_LI("Invalid store URI: %(reason)s"), {'reason': reason}) raise exceptions.BadStoreUri(message=reason) - pieces = urlparse.urlparse(uri) + pieces = urllib.parse.urlparse(uri) assert pieces.scheme in ('swift', 'swift+http', 'swift+https', 'swift+config') @@ -421,7 +421,7 @@ class BaseStore(driver.Store): container=location.container, obj=location.obj, resp_chunk_size=self.CHUNKSIZE, headers=headers) except swiftclient.ClientException as e: - if e.http_status == httplib.NOT_FOUND: + if e.http_status == http_client.NOT_FOUND: msg = _("Swift could not find object %s.") % location.obj LOG.warn(msg) raise exceptions.NotFound(message=msg) @@ -602,7 +602,7 @@ class BaseStore(driver.Store): return (location.get_uri(credentials_included=include_creds), image_size, obj_etag, {}) except swiftclient.ClientException as e: - if e.http_status == httplib.CONFLICT: + if e.http_status == http_client.CONFLICT: msg = _("Swift already has an image at this location") raise exceptions.Duplicate(message=msg) @@ -630,7 +630,7 @@ class BaseStore(driver.Store): dlo_manifest = headers.get('x-object-manifest') slo_manifest = headers.get('x-static-large-object') except swiftclient.ClientException as e: - if e.http_status != httplib.NOT_FOUND: + if e.http_status != http_client.NOT_FOUND: raise if _is_slo(slo_manifest): @@ -662,7 +662,7 @@ class BaseStore(driver.Store): connection.delete_object(location.container, location.obj) except swiftclient.ClientException as e: - if e.http_status == httplib.NOT_FOUND: + if e.http_status == http_client.NOT_FOUND: msg = _("Swift could not find image at URI.") raise exceptions.NotFound(message=msg) else: @@ -679,7 +679,7 @@ class BaseStore(driver.Store): try: connection.head_container(container) except swiftclient.ClientException as e: - if e.http_status == httplib.NOT_FOUND: + if e.http_status == http_client.NOT_FOUND: if self.conf.glance_store.swift_store_create_container_on_put: try: msg = (_LI("Creating swift container %(container)s") % @@ -888,7 +888,7 @@ class MultiTenantStore(BaseStore): try: connection.post_container(location.container, headers=headers) except swiftclient.ClientException as e: - if e.http_status == httplib.NOT_FOUND: + if e.http_status == http_client.NOT_FOUND: msg = _("Swift could not find image at URI.") raise exceptions.NotFound(message=msg) else: diff --git a/glance_store/_drivers/swift/utils.py b/glance_store/_drivers/swift/utils.py index 3b2801a..9489c17 100644 --- a/glance_store/_drivers/swift/utils.py +++ b/glance_store/_drivers/swift/utils.py @@ -12,12 +12,12 @@ # License for the specific language governing permissions and limitations # under the License. -import ConfigParser import logging from collections import OrderedDict from oslo_config import cfg +from six.moves import configparser from glance_store import exceptions from glance_store import i18n @@ -43,7 +43,7 @@ swift_opts = [ # NOTE(bourke): The default dict_type is collections.OrderedDict in py27, but # we must set manually for compatibility with py26 -CONFIG = ConfigParser.SafeConfigParser(dict_type=OrderedDict) +CONFIG = configparser.SafeConfigParser(dict_type=OrderedDict) LOG = logging.getLogger(__name__) @@ -99,7 +99,7 @@ class SwiftParams(object): reference['user'] = CONFIG.get(ref, 'user') reference['key'] = CONFIG.get(ref, 'key') account_params[ref] = reference - except (ValueError, SyntaxError, ConfigParser.NoOptionError) as e: + except (ValueError, SyntaxError, configparser.NoOptionError) as e: LOG.exception(i18n._("Invalid format of swift store config" "cfg")) return account_params diff --git a/glance_store/_drivers/vmware_datastore.py b/glance_store/_drivers/vmware_datastore.py index bbf9037..b7e8756 100644 --- a/glance_store/_drivers/vmware_datastore.py +++ b/glance_store/_drivers/vmware_datastore.py @@ -16,7 +16,6 @@ """Storage backend for VMware Datastore""" import hashlib -import httplib import logging import os @@ -32,11 +31,12 @@ try: from oslo_vmware import vim_util except ImportError: api = None +from six.moves import http_client +from six.moves import urllib import six # NOTE(jokke): simplified transition to py3, behaves like py2 xrange from six.moves import range -import six.moves.urllib.parse as urlparse import glance_store from glance_store import capabilities @@ -121,7 +121,7 @@ def http_response_iterator(conn, response, size): """Return an iterator for a file-like object. :param conn: HTTP(S) Connection - :param response: httplib.HTTPResponse object + :param response: http_client.HTTPResponse object :param size: Chunk size to iterate with """ try: @@ -210,7 +210,7 @@ class StoreLocation(location.StoreLocation): param_list = {'dsName': self.datstore_name} if self.datacenter_path: param_list['dcPath'] = self.datacenter_path - self.query = urlparse.urlencode(param_list) + self.query = urllib.parse.urlencode(param_list) def get_uri(self): if netutils.is_valid_ipv6(self.server_host): @@ -236,7 +236,7 @@ class StoreLocation(location.StoreLocation): LOG.info(reason) raise exceptions.BadStoreUri(message=reason) (self.scheme, self.server_host, - path, params, query, fragment) = urlparse.urlparse(uri) + path, params, query, fragment) = urllib.parse.urlparse(uri) if not query: path, query = path.split('?') @@ -246,7 +246,7 @@ class StoreLocation(location.StoreLocation): # reason = 'Badly formed VMware datastore URI %(uri)s.' % {'uri': uri} # LOG.debug(reason) # raise exceptions.BadStoreUri(reason) - parts = urlparse.parse_qs(self.query) + parts = urllib.parse.parse_qs(self.query) dc_path = parts.get('dcPath') if dc_path: self.datacenter_path = dc_path[0] @@ -494,12 +494,12 @@ class Store(glance_store.Store): headers['Cookie'] = cookie conn_class = self._get_http_conn_class() conn = conn_class(loc.server_host) - url = urlparse.quote('%s?%s' % (loc.path, loc.query)) + url = urllib.parse.quote('%s?%s' % (loc.path, loc.query)) try: conn.request('PUT', url, image_file, headers) except IOError as e: # When a session is not authenticated, the socket is closed by - # the server after sending the response. httplib has an open + # the server after sending the response. http_client has an open # issue with https that raises Broken Pipe # error instead of returning the response. # See http://bugs.python.org/issue16062. Here, we log the error @@ -515,12 +515,12 @@ class Store(glance_store.Store): LOG.exception(_LE('Failed to upload content of image ' '%(image)s'), {'image': image_id}) res = conn.getresponse() - if res.status == httplib.CONFLICT: + if res.status == http_client.CONFLICT: raise exceptions.Duplicate(_("Image file %(image_id)s already " "exists!") % {'image_id': image_id}) - if res.status not in (httplib.CREATED, httplib.OK): + if res.status not in (http_client.CREATED, http_client.OK): msg = (_LE('Failed to upload content of image %(image)s. ' 'The request returned an unexpected status: %(status)s.' '\nThe response body:\n%(body)s') % @@ -614,10 +614,10 @@ class Store(glance_store.Store): 'content.') % {'image': location.image_id}) if resp.status >= 400: - if resp.status == httplib.UNAUTHORIZED: + if resp.status == http_client.UNAUTHORIZED: self.reset_session() continue - if resp.status == httplib.NOT_FOUND: + if resp.status == http_client.NOT_FOUND: reason = _('VMware datastore could not find image at URI.') LOG.info(reason) raise exceptions.NotFound(message=reason) @@ -648,12 +648,12 @@ class Store(glance_store.Store): def _get_http_conn(self, method, loc, headers, content=None): conn_class = self._get_http_conn_class() conn = conn_class(loc.server_host) - url = urlparse.quote('%s?%s' % (loc.path, loc.query)) + url = urllib.parse.quote('%s?%s' % (loc.path, loc.query)) conn.request(method, url, content, headers) return conn def _get_http_conn_class(self): if self.api_insecure: - return httplib.HTTPConnection - return httplib.HTTPSConnection + return http_client.HTTPConnection + return http_client.HTTPSConnection diff --git a/glance_store/common/auth.py b/glance_store/common/auth.py index 3cf1a72..7a48da4 100644 --- a/glance_store/common/auth.py +++ b/glance_store/common/auth.py @@ -34,7 +34,7 @@ import logging from oslo_serialization import jsonutils # NOTE(jokke): simplified transition to py3, behaves like py2 xrange from six.moves import range -import six.moves.urllib.parse as urlparse +from six.moves import urllib from glance_store import exceptions @@ -115,7 +115,7 @@ class KeystoneStrategy(BaseStrategy): # If OS_AUTH_URL is missing a trailing slash add one if not auth_url.endswith('/'): auth_url += '/' - token_url = urlparse.urljoin(auth_url, "tokens") + token_url = urllib.parse.urljoin(auth_url, "tokens") # 1. Check Keystone version is_v2 = auth_url.rstrip('/').endswith('v2.0') if is_v2: @@ -136,7 +136,7 @@ class KeystoneStrategy(BaseStrategy): # v2.0 keystone endpoint. Also, new location does not # contain real endpoint, only hostname and port. if 'v2.0' not in auth_url: - auth_url = urlparse.urljoin(auth_url, 'v2.0/') + auth_url = urllib.parse.urljoin(auth_url, 'v2.0/') else: # If we successfully auth'd, then memorize the correct auth_url # for future use. diff --git a/glance_store/exceptions.py b/glance_store/exceptions.py index 089e23c..79af298 100644 --- a/glance_store/exceptions.py +++ b/glance_store/exceptions.py @@ -15,7 +15,7 @@ """Glance Store exception subclasses""" -import six.moves.urllib.parse as urlparse +from six.moves import urllib from glance_store import i18n @@ -32,7 +32,7 @@ class UnsupportedBackend(BackendException): class RedirectException(Exception): def __init__(self, url): - self.url = urlparse.urlparse(url) + self.url = urllib.parse.urlparse(url) class GlanceStoreException(Exception): diff --git a/glance_store/location.py b/glance_store/location.py index b60c485..2dab343 100644 --- a/glance_store/location.py +++ b/glance_store/location.py @@ -38,9 +38,9 @@ credentials and is **not** user-facing. """ import logging -import urlparse from oslo_config import cfg +from six.moves import urllib from glance_store import exceptions @@ -70,7 +70,7 @@ def get_location_from_uri(uri, conf=CONF): file:///var/lib/glance/images/1 cinder://volume-id """ - pieces = urlparse.urlparse(uri) + pieces = urllib.parse.urlparse(uri) if pieces.scheme not in SCHEME_TO_CLS_MAP.keys(): raise exceptions.UnknownScheme(scheme=pieces.scheme) scheme_info = SCHEME_TO_CLS_MAP[pieces.scheme] diff --git a/glance_store/tests/utils.py b/glance_store/tests/utils.py index d73470e..9dbba11 100644 --- a/glance_store/tests/utils.py +++ b/glance_store/tests/utils.py @@ -13,9 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. -import StringIO -import urllib -import urlparse +import six +from six.moves import urllib def sort_url_by_qs_keys(url): @@ -24,26 +23,26 @@ def sort_url_by_qs_keys(url): # returns '/v2/tasks?limit=10&sort_dir=asc&sort_key=id'. This is to prevent # non-deterministic ordering of the query string causing problems with unit # tests. - parsed = urlparse.urlparse(url) + parsed = urllib.parse.urlparse(url) # In python2.6, for arbitrary url schemes, query string # is not parsed from url. http://bugs.python.org/issue9374 path = parsed.path query = parsed.query if not query: path, query = parsed.path.split('?', 1) - queries = urlparse.parse_qsl(query, True) + queries = urllib.parse.parse_qsl(query, True) sorted_query = sorted(queries, key=lambda x: x[0]) - encoded_sorted_query = urllib.urlencode(sorted_query, True) + encoded_sorted_query = urllib.parse.urlencode(sorted_query, True) url_parts = (parsed.scheme, parsed.netloc, path, parsed.params, encoded_sorted_query, parsed.fragment) - return urlparse.urlunparse(url_parts) + return urllib.parse.urlunparse(url_parts) class FakeHTTPResponse(object): def __init__(self, status=200, headers=None, data=None, *args, **kwargs): data = data or 'I am a teapot, short and stout\n' - self.data = StringIO.StringIO(data) + self.data = six.StringIO(data) self.read = self.data.read self.status = status self.headers = headers or {'content-length': len(data)} diff --git a/tests/unit/test_filesystem_store.py b/tests/unit/test_filesystem_store.py index 65f60e2..870b4e3 100644 --- a/tests/unit/test_filesystem_store.py +++ b/tests/unit/test_filesystem_store.py @@ -15,7 +15,6 @@ """Tests the filesystem backend store""" -import __builtin__ import errno import hashlib import json @@ -27,6 +26,7 @@ import uuid import fixtures from oslo_utils import units import six +from six.moves import builtins # NOTE(jokke): simplified transition to py3, behaves like py2 xrange from six.moves import range @@ -245,7 +245,7 @@ class TestStore(base.StoreBaseTest, path = os.path.join(self.test_dir, image_id) image_file = six.BytesIO(file_contents) - with mock.patch.object(__builtin__, 'open') as popen: + with mock.patch.object(builtins, 'open') as popen: e = IOError() e.errno = errno popen.side_effect = e diff --git a/tests/unit/test_http_store.py b/tests/unit/test_http_store.py index 780ddf6..1cbe8c9 100644 --- a/tests/unit/test_http_store.py +++ b/tests/unit/test_http_store.py @@ -40,12 +40,13 @@ class TestHttpStore(base.StoreBaseTest, Should be called when need to mock httplib response and request objects. """ - response = mock.patch('httplib.HTTPConnection.getresponse') + response = mock.patch('six.moves.http_client' + '.HTTPConnection.getresponse') self.response = response.start() self.response.return_value = utils.FakeHTTPResponse() self.addCleanup(response.stop) - request = mock.patch('httplib.HTTPConnection.request') + request = mock.patch('six.moves.http_client.HTTPConnection.request') self.request = request.start() self.request.side_effect = lambda w, x, y, z: None self.addCleanup(request.stop) diff --git a/tests/unit/test_s3_store.py b/tests/unit/test_s3_store.py index f68e409..5d58573 100644 --- a/tests/unit/test_s3_store.py +++ b/tests/unit/test_s3_store.py @@ -16,13 +16,13 @@ """Tests the S3 backend store""" import hashlib -import StringIO import uuid import xml.etree.ElementTree import boto.s3.connection import mock from oslo_utils import units +import six from glance_store._drivers import s3 from glance_store import capabilities @@ -78,7 +78,7 @@ class FakeKey(object): def set_contents_from_file(self, fp, replace=False, **kwargs): max_read = kwargs.get('size') - self.data = StringIO.StringIO() + self.data = six.StringIO() checksum = hashlib.md5() while True: if max_read is None or max_read > self.BufferSize: @@ -149,7 +149,7 @@ class FakeMPU(object): Complete the parts into one big FakeKey """ key = FakeKey(self.bucket, self.key_name) - key.data = StringIO.StringIO() + key.data = six.StringIO() checksum = hashlib.md5() cnt = 0 for pnum in sorted(self.parts.keys()): @@ -230,7 +230,7 @@ def fakers(): fixture_buckets = {'glance': FakeBucket('glance')} b = fixture_buckets['glance'] k = b.new_key(FAKE_UUID) - k.set_contents_from_file(StringIO.StringIO("*" * FIVE_KB)) + k.set_contents_from_file(six.StringIO("*" * FIVE_KB)) def fake_connection_constructor(self, *args, **kwargs): host = kwargs.get('host') @@ -367,7 +367,7 @@ class TestStore(base.StoreBaseTest, S3_CONF['s3_store_host'], S3_CONF['s3_store_bucket'], expected_image_id) - image_s3 = StringIO.StringIO(expected_s3_contents) + image_s3 = six.StringIO(expected_s3_contents) loc, size, checksum, _ = self.store.add(expected_image_id, image_s3, @@ -380,7 +380,7 @@ class TestStore(base.StoreBaseTest, loc = location.get_location_from_uri(expected_location, conf=self.conf) (new_image_s3, new_image_size) = self.store.get(loc) - new_image_contents = StringIO.StringIO() + new_image_contents = six.StringIO() for chunk in new_image_s3: new_image_contents.write(chunk) new_image_s3_size = new_image_contents.len @@ -409,7 +409,7 @@ class TestStore(base.StoreBaseTest, S3_CONF['s3_store_host'], S3_CONF['s3_store_bucket'], expected_image_id) - image_s3 = StringIO.StringIO(expected_s3_contents) + image_s3 = six.StringIO(expected_s3_contents) # add image loc, size, chksum, _ = self.store.add(expected_image_id, @@ -424,7 +424,7 @@ class TestStore(base.StoreBaseTest, loc = location.get_location_from_uri(expected_location, conf=self.conf) (new_image_s3, new_image_s3_size) = self.store.get(loc) - new_image_contents = StringIO.StringIO() + new_image_contents = six.StringIO() for chunk in new_image_s3: new_image_contents.write(chunk) new_image_size = new_image_contents.len @@ -457,7 +457,7 @@ class TestStore(base.StoreBaseTest, new_conf['s3_store_host'], new_conf['s3_store_bucket'], expected_image_id) - image_s3 = StringIO.StringIO(expected_s3_contents) + image_s3 = six.StringIO(expected_s3_contents) self.config(**new_conf) self.store = s3.Store(self.conf) @@ -484,7 +484,7 @@ class TestStore(base.StoreBaseTest, Tests that adding an image with an existing identifier raises an appropriate exception """ - image_s3 = StringIO.StringIO("nevergonnamakeit") + image_s3 = six.StringIO("nevergonnamakeit") self.assertRaises(exceptions.Duplicate, self.store.add, FAKE_UUID, image_s3, 0) diff --git a/tests/unit/test_swift_store.py b/tests/unit/test_swift_store.py index 40f3253..bdfc8aa 100644 --- a/tests/unit/test_swift_store.py +++ b/tests/unit/test_swift_store.py @@ -18,7 +18,6 @@ import copy import fixtures import hashlib -import httplib import mock import tempfile import uuid @@ -28,9 +27,10 @@ from oslo_utils import units from oslotest import moxstubout import requests_mock import six +from six import moves +from six.moves import http_client # NOTE(jokke): simplified transition to py3, behaves like py2 xrange from six.moves import range -import StringIO import swiftclient from glance_store._drivers.swift import store as swift @@ -85,8 +85,8 @@ def stub_out_swiftclient(stubs, swift_store_auth_version): def fake_head_container(url, token, container, **kwargs): if container not in fixture_containers: msg = "No container %s found" % container - raise swiftclient.ClientException(msg, - http_status=httplib.NOT_FOUND) + status = http_client.NOT_FOUND + raise swiftclient.ClientException(msg, http_status=status) return fixture_container_headers def fake_put_container(url, token, container, **kwargs): @@ -129,7 +129,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version): msg = ('Image size:%d exceeds Swift max:%d' % (read_len, MAX_SWIFT_OBJECT_SIZE)) raise swiftclient.ClientException( - msg, http_status=httplib.REQUEST_ENTITY_TOO_LARGE) + msg, http_status=http_client.REQUEST_ENTITY_TOO_LARGE) fixture_objects[fixture_key] = fixture_object fixture_headers[fixture_key] = { 'content-length': read_len, @@ -139,15 +139,15 @@ def stub_out_swiftclient(stubs, swift_store_auth_version): msg = ("Object PUT failed - Object with key %s already exists" % fixture_key) raise swiftclient.ClientException(msg, - http_status=httplib.CONFLICT) + http_status=http_client.CONFLICT) def fake_get_object(url, token, container, name, **kwargs): # GET returns the tuple (list of headers, file object) fixture_key = "%s/%s" % (container, name) if fixture_key not in fixture_headers: msg = "Object GET failed" - raise swiftclient.ClientException(msg, - http_status=httplib.NOT_FOUND) + status = http_client.NOT_FOUND + raise swiftclient.ClientException(msg, http_status=status) byte_range = None headers = kwargs.get('headers', dict()) @@ -184,16 +184,16 @@ def stub_out_swiftclient(stubs, swift_store_auth_version): return fixture_headers[fixture_key] except KeyError: msg = "Object HEAD failed - Object does not exist" - raise swiftclient.ClientException(msg, - http_status=httplib.NOT_FOUND) + status = http_client.NOT_FOUND + raise swiftclient.ClientException(msg, http_status=status) def fake_delete_object(url, token, container, name, **kwargs): # DELETE returns nothing fixture_key = "%s/%s" % (container, name) if fixture_key not in fixture_headers: msg = "Object DELETE failed - Object does not exist" - raise swiftclient.ClientException(msg, - http_status=httplib.NOT_FOUND) + status = http_client.NOT_FOUND + raise swiftclient.ClientException(msg, http_status=status) else: del fixture_headers[fixture_key] del fixture_objects[fixture_key] @@ -373,7 +373,7 @@ class SwiftTests(object): conf = copy.deepcopy(SWIFT_CONF) conf['default_swift_reference'] = 'store_2' self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() @@ -423,7 +423,7 @@ class SwiftTests(object): SWIFT_PUT_OBJECT_CALLS = 0 conf['default_swift_reference'] = variation self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() loc, size, checksum, _ = self.store.add(image_id, image_swift, @@ -453,7 +453,7 @@ class SwiftTests(object): conf['swift_store_create_container_on_put'] = False conf['swift_store_container'] = 'noexist' self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() @@ -499,7 +499,7 @@ class SwiftTests(object): conf['swift_store_create_container_on_put'] = True conf['swift_store_container'] = 'noexist' self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() loc, size, checksum, _ = self.store.add(expected_image_id, @@ -544,7 +544,7 @@ class SwiftTests(object): conf['swift_store_container'] = 'randomname' conf['swift_store_multiple_containers_seed'] = 2 self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() loc, size, checksum, _ = self.store.add(expected_image_id, @@ -578,7 +578,7 @@ class SwiftTests(object): conf['swift_store_container'] = 'randomname' conf['swift_store_multiple_containers_seed'] = 2 self.config(**conf) - reload(swift) + moves.reload_module(swift) expected_image_id = str(uuid.uuid4()) expected_container = 'randomname_' + expected_image_id[:2] @@ -800,7 +800,7 @@ class SwiftTests(object): """ conf = copy.deepcopy(SWIFT_CONF) self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() @@ -818,7 +818,7 @@ class SwiftTests(object): """ conf = copy.deepcopy(SWIFT_CONF) self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() @@ -839,7 +839,7 @@ class SwiftTests(object): """ conf = copy.deepcopy(SWIFT_CONF) self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() @@ -858,7 +858,7 @@ class SwiftTests(object): """ conf = copy.deepcopy(SWIFT_CONF) self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() @@ -875,7 +875,7 @@ class SwiftTests(object): """ conf = copy.deepcopy(SWIFT_CONF) self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() @@ -914,7 +914,7 @@ class SwiftTests(object): conf = copy.deepcopy(SWIFT_CONF) self.config(**conf) - reload(swift) + moves.reload_module(swift) self.store = Store(self.conf) self.store.configure() @@ -1281,7 +1281,7 @@ class TestMultiTenantStoreContext(base.StoreBaseTest): self.config(swift_store_multi_tenant=True) store = Store(self.conf) store.configure() - pseudo_file = StringIO.StringIO('Some data') + pseudo_file = six.StringIO('Some data') ctx = mock.MagicMock( service_catalog=self.service_catalog, user='tenant:user1', tenant='tenant', auth_token='0123') @@ -1314,7 +1314,7 @@ class TestCreatingLocations(base.StoreBaseTest): conf = copy.deepcopy(SWIFT_CONF) self.store = Store(self.conf) self.config(**conf) - reload(swift) + moves.reload_module(swift) self.addCleanup(self.conf.reset) def test_single_tenant_location(self): @@ -1325,7 +1325,7 @@ class TestCreatingLocations(base.StoreBaseTest): conf.update({'swift_store_config_file': self.swift_config_file}) conf['default_swift_reference'] = 'ref1' self.config(**conf) - reload(swift) + moves.reload_module(swift) store = swift.SingleTenantStore(self.conf) store.configure() diff --git a/tests/unit/test_vmware_store.py b/tests/unit/test_vmware_store.py index deb77b4..40d96d5 100644 --- a/tests/unit/test_vmware_store.py +++ b/tests/unit/test_vmware_store.py @@ -119,6 +119,9 @@ class TestStore(base.StoreBaseTest, self.store.store_image_dir = ( VMWARE_DS['vmware_store_image_dir']) + def _mock_http_connection(self): + return mock.patch('six.moves.http_client.HTTPConnection') + @mock.patch('oslo_vmware.api.VMwareAPISession') def test_get(self, mock_api_session): """Test a "normal" retrieval of an image in chunks.""" @@ -127,7 +130,7 @@ class TestStore(base.StoreBaseTest, loc = location.get_location_from_uri( "vsphere://127.0.0.1/folder/openstack_glance/%s" "?dsName=ds1&dcPath=dc1" % FAKE_UUID, conf=self.conf) - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.return_value = FakeHTTPConnection() (image_file, image_size) = self.store.get(loc) self.assertEqual(image_size, expected_image_size) @@ -143,7 +146,7 @@ class TestStore(base.StoreBaseTest, loc = location.get_location_from_uri( "vsphere://127.0.0.1/folder/openstack_glan" "ce/%s?dsName=ds1&dcPath=dc1" % FAKE_UUID, conf=self.conf) - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.return_value = FakeHTTPConnection(status=404) self.assertRaises(exceptions.NotFound, self.store.get, loc) @@ -168,7 +171,7 @@ class TestStore(base.StoreBaseTest, VMWARE_DS['vmware_datacenter_path'], VMWARE_DS['vmware_datastore_name']) image = six.StringIO(expected_contents) - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.return_value = FakeHTTPConnection() location, size, checksum, _ = self.store.add(expected_image_id, image, @@ -203,7 +206,7 @@ class TestStore(base.StoreBaseTest, VMWARE_DS['vmware_datacenter_path'], VMWARE_DS['vmware_datastore_name']) image = six.StringIO(expected_contents) - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.return_value = FakeHTTPConnection() location, size, checksum, _ = self.store.add(expected_image_id, image, 0) @@ -218,11 +221,11 @@ class TestStore(base.StoreBaseTest, loc = location.get_location_from_uri( "vsphere://127.0.0.1/folder/openstack_glance/%s?" "dsName=ds1&dcPath=dc1" % FAKE_UUID, conf=self.conf) - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.return_value = FakeHTTPConnection() vm_store.Store._service_content = mock.Mock() self.store.delete(loc) - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.return_value = FakeHTTPConnection(status=404) self.assertRaises(exceptions.NotFound, self.store.get, loc) @@ -247,7 +250,7 @@ class TestStore(base.StoreBaseTest, loc = location.get_location_from_uri( "vsphere://127.0.0.1/folder/openstack_glance/%s" "?dsName=ds1&dcPath=dc1" % FAKE_UUID, conf=self.conf) - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.return_value = FakeHTTPConnection() image_size = self.store.get_size(loc) self.assertEqual(image_size, 31) @@ -261,7 +264,7 @@ class TestStore(base.StoreBaseTest, loc = location.get_location_from_uri( "vsphere://127.0.0.1/folder/openstack_glan" "ce/%s?dsName=ds1&dcPath=dc1" % FAKE_UUID, conf=self.conf) - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.return_value = FakeHTTPConnection(status=404) self.assertRaises(exceptions.NotFound, self.store.get_size, loc) @@ -431,7 +434,7 @@ class TestStore(base.StoreBaseTest, expected_contents = "*" * expected_size image = six.StringIO(expected_contents) self.session = mock.Mock() - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.return_value = FakeHTTPConnection(status=401) self.assertRaises(exceptions.BackendException, self.store.add, @@ -472,7 +475,7 @@ class TestStore(base.StoreBaseTest, expected_contents = "*" * expected_size image = six.StringIO(expected_contents) self.session = mock.Mock() - with mock.patch('httplib.HTTPConnection') as HttpConn: + with self._mock_http_connection() as HttpConn: HttpConn.request.side_effect = IOError self.assertRaises(exceptions.BackendException, self.store.add, |