Use six.StringIO/BytesIO instead of StringIO.StringIO

to keep Python 3.x compatibility, use six.StringIO/BytesIO to
replace StringIO.StringIO

StringIO works for unicode
BytesIO works for bytes

Change-Id: I93e043c633e1de9e4dedcb0a313032403b6a70fb
Closes-Bug: #1280100
This commit is contained in:
He Yongli 2014-02-17 13:10:18 +08:00
parent 6165966203
commit 01903933db
17 changed files with 83 additions and 85 deletions

View File

@ -13,10 +13,10 @@
# 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 StringIO
import uuid import uuid
from oslo.config import cfg from oslo.config import cfg
import six
#NOTE(bcwaldon): importing this to get the default_store option #NOTE(bcwaldon): importing this to get the default_store option
import glance.api.v1.images import glance.api.v1.images
@ -76,7 +76,7 @@ class BaseTestCase(object):
store = self.get_store() store = self.get_store()
image_id = str(uuid.uuid4()) image_id = str(uuid.uuid4())
image_data = StringIO.StringIO('XXX') image_data = six.StringIO('XXX')
image_checksum = 'bc9189406be84ec297464a514221406d' image_checksum = 'bc9189406be84ec297464a514221406d'
try: try:
uri, add_size, add_checksum, _ = store.add(image_id, image_data, 3) uri, add_size, add_checksum, _ = store.add(image_id, image_data, 3)

View File

@ -22,10 +22,10 @@ RBD backend. This backend must be running Ceph Bobtail (0.56) or later.
import ConfigParser import ConfigParser
import os import os
import StringIO
import uuid import uuid
import oslo.config.cfg import oslo.config.cfg
import six
import testtools import testtools
from glance.common import exception from glance.common import exception
@ -135,7 +135,7 @@ class TestRBDStore(store_tests.BaseTestCase, testtools.TestCase):
image_id = unicode(str(uuid.uuid4())) image_id = unicode(str(uuid.uuid4()))
image_size = 300 image_size = 300
image_data = StringIO.StringIO('X' * image_size) image_data = six.StringIO('X' * image_size)
image_checksum = '41757066eaff7c4c6c965556b4d3c6c5' image_checksum = '41757066eaff7c4c6c965556b4d3c6c5'
uri, add_size, add_checksum = store.add(image_id, uri, add_size, add_checksum = store.add(image_id,

View File

@ -26,10 +26,10 @@ import os
import os.path import os.path
import random import random
import string import string
import StringIO
import uuid import uuid
import oslo.config.cfg import oslo.config.cfg
import six
import six.moves.urllib.parse as urlparse import six.moves.urllib.parse as urlparse
import testtools import testtools
@ -194,7 +194,7 @@ class TestSwiftStore(store_tests.BaseTestCase, testtools.TestCase):
store = self.get_store() store = self.get_store()
image_id = str(uuid.uuid4()) image_id = str(uuid.uuid4())
image_size = 5242880 # 5 MB image_size = 5242880 # 5 MB
image_data = StringIO.StringIO('X' * image_size) image_data = six.StringIO('X' * image_size)
image_checksum = 'eb7f8c3716b9f059cee7617a4ba9d0d3' image_checksum = 'eb7f8c3716b9f059cee7617a4ba9d0d3'
uri, add_size, add_checksum, _ = store.add(image_id, uri, add_size, add_checksum, _ = store.add(image_id,
image_data, image_data,
@ -276,7 +276,7 @@ class TestSwiftStore(store_tests.BaseTestCase, testtools.TestCase):
non_image_obj) non_image_obj)
# Simulate exceeding 'image_size_cap' setting # Simulate exceeding 'image_size_cap' setting
image_data = StringIO.StringIO('X' * image_size) image_data = six.StringIO('X' * image_size)
image_data = common_utils.LimitingReader(image_data, image_size - 1) image_data = common_utils.LimitingReader(image_data, image_size - 1)
image_id = str(uuid.uuid4()) image_id = str(uuid.uuid4())
self.assertRaises(exception.ImageSizeLimitExceeded, self.assertRaises(exception.ImageSizeLimitExceeded,
@ -455,7 +455,7 @@ class TestSwiftStore(store_tests.BaseTestCase, testtools.TestCase):
store = self.get_store(context=context) store = self.get_store(context=context)
image_id = str(uuid.uuid4()) image_id = str(uuid.uuid4())
image_data = StringIO.StringIO('XXX') image_data = six.StringIO('XXX')
uri, _, _, _ = store.add(image_id, image_data, 3) uri, _, _, _ = store.add(image_id, image_data, 3)
location = glance.store.location.Location( location = glance.store.location.Location(
@ -512,7 +512,7 @@ class TestSwiftStore(store_tests.BaseTestCase, testtools.TestCase):
store = self.get_store(context=context) store = self.get_store(context=context)
image_id = str(uuid.uuid4()) image_id = str(uuid.uuid4())
image_data = StringIO.StringIO('data') image_data = six.StringIO('data')
uri, _, _, _ = store.add(image_id, image_data, 4) uri, _, _, _ = store.add(image_id, image_data, 4)
location = glance.store.location.Location( location = glance.store.location.Location(

View File

@ -11,9 +11,10 @@
# under the License. # under the License.
import mock import mock
import StringIO
import sys import sys
import six
import glance.cmd.api import glance.cmd.api
import glance.cmd.cache_cleaner import glance.cmd.cache_cleaner
import glance.cmd.cache_pruner import glance.cmd.cache_pruner
@ -41,7 +42,7 @@ class TestGlanceApiCmd(test_utils.BaseTestCase):
super(TestGlanceApiCmd, self).setUp() super(TestGlanceApiCmd, self).setUp()
self.__argv_backup = sys.argv self.__argv_backup = sys.argv
sys.argv = ['glance-api'] sys.argv = ['glance-api']
self.stderr = StringIO.StringIO() self.stderr = six.StringIO()
sys.stderr = self.stderr sys.stderr = self.stderr
self.stubs.Set(glance.common.config, 'load_paste_app', self.stubs.Set(glance.common.config, 'load_paste_app',

View File

@ -14,9 +14,9 @@
# under the License. # under the License.
import os import os
import StringIO
import tempfile import tempfile
import six
import webob import webob
from glance.common import exception from glance.common import exception
@ -77,14 +77,14 @@ class TestUtils(test_utils.BaseTestCase):
"""Ensure limiting reader class accesses all bytes of file""" """Ensure limiting reader class accesses all bytes of file"""
BYTES = 1024 BYTES = 1024
bytes_read = 0 bytes_read = 0
data = StringIO.StringIO("*" * BYTES) data = six.StringIO("*" * BYTES)
for chunk in utils.LimitingReader(data, BYTES): for chunk in utils.LimitingReader(data, BYTES):
bytes_read += len(chunk) bytes_read += len(chunk)
self.assertEqual(bytes_read, BYTES) self.assertEqual(bytes_read, BYTES)
bytes_read = 0 bytes_read = 0
data = StringIO.StringIO("*" * BYTES) data = six.StringIO("*" * BYTES)
reader = utils.LimitingReader(data, BYTES) reader = utils.LimitingReader(data, BYTES)
byte = reader.read(1) byte = reader.read(1)
while len(byte) != 0: while len(byte) != 0:
@ -99,7 +99,7 @@ class TestUtils(test_utils.BaseTestCase):
def _consume_all_iter(): def _consume_all_iter():
bytes_read = 0 bytes_read = 0
data = StringIO.StringIO("*" * BYTES) data = six.StringIO("*" * BYTES)
for chunk in utils.LimitingReader(data, BYTES - 1): for chunk in utils.LimitingReader(data, BYTES - 1):
bytes_read += len(chunk) bytes_read += len(chunk)
@ -107,7 +107,7 @@ class TestUtils(test_utils.BaseTestCase):
def _consume_all_read(): def _consume_all_read():
bytes_read = 0 bytes_read = 0
data = StringIO.StringIO("*" * BYTES) data = six.StringIO("*" * BYTES)
reader = utils.LimitingReader(data, BYTES - 1) reader = utils.LimitingReader(data, BYTES - 1)
byte = reader.read(1) byte = reader.read(1)
while len(byte) != 0: while len(byte) != 0:

View File

@ -20,12 +20,12 @@ import errno
import hashlib import hashlib
import json import json
import os import os
import StringIO
import uuid import uuid
import fixtures import fixtures
import mox import mox
from oslo.config import cfg from oslo.config import cfg
import six
from glance.common import exception from glance.common import exception
from glance.openstack.common import units from glance.openstack.common import units
@ -157,7 +157,7 @@ class TestStore(base.IsolatedUnitTest):
# First add an image... # First add an image...
image_id = str(uuid.uuid4()) image_id = str(uuid.uuid4())
file_contents = "chunk00000remainder" file_contents = "chunk00000remainder"
image_file = StringIO.StringIO(file_contents) image_file = six.StringIO(file_contents)
location, size, checksum, _ = self.store.add(image_id, location, size, checksum, _ = self.store.add(image_id,
image_file, image_file,
@ -198,7 +198,7 @@ class TestStore(base.IsolatedUnitTest):
expected_checksum = hashlib.md5(expected_file_contents).hexdigest() expected_checksum = hashlib.md5(expected_file_contents).hexdigest()
expected_location = "file://%s/%s" % (self.test_dir, expected_location = "file://%s/%s" % (self.test_dir,
expected_image_id) expected_image_id)
image_file = StringIO.StringIO(expected_file_contents) image_file = six.StringIO(expected_file_contents)
location, size, checksum, _ = self.store.add(expected_image_id, location, size, checksum, _ = self.store.add(expected_image_id,
image_file, image_file,
@ -239,7 +239,7 @@ class TestStore(base.IsolatedUnitTest):
expected_checksum = hashlib.md5(expected_file_contents).hexdigest() expected_checksum = hashlib.md5(expected_file_contents).hexdigest()
expected_location = "file://%s/%s" % (store_map[1], expected_location = "file://%s/%s" % (store_map[1],
expected_image_id) expected_image_id)
image_file = StringIO.StringIO(expected_file_contents) image_file = six.StringIO(expected_file_contents)
location, size, checksum, _ = self.store.add(expected_image_id, location, size, checksum, _ = self.store.add(expected_image_id,
image_file, image_file,
@ -283,7 +283,7 @@ class TestStore(base.IsolatedUnitTest):
expected_image_id = str(uuid.uuid4()) expected_image_id = str(uuid.uuid4())
expected_file_size = 5 * units.Ki # 5K expected_file_size = 5 * units.Ki # 5K
expected_file_contents = "*" * expected_file_size expected_file_contents = "*" * expected_file_size
image_file = StringIO.StringIO(expected_file_contents) image_file = six.StringIO(expected_file_contents)
self.assertRaises(exception.StorageFull, self.store.add, self.assertRaises(exception.StorageFull, self.store.add,
expected_image_id, image_file, expected_file_size) expected_image_id, image_file, expected_file_size)
@ -299,7 +299,7 @@ class TestStore(base.IsolatedUnitTest):
json.dump(in_metadata, fptr) json.dump(in_metadata, fptr)
expected_file_size = 10 expected_file_size = 10
expected_file_contents = "*" * expected_file_size expected_file_contents = "*" * expected_file_size
image_file = StringIO.StringIO(expected_file_contents) image_file = six.StringIO(expected_file_contents)
location, size, checksum, metadata = self.store.add(expected_image_id, location, size, checksum, metadata = self.store.add(expected_image_id,
image_file, image_file,
@ -318,7 +318,7 @@ class TestStore(base.IsolatedUnitTest):
json.dump(in_metadata, fptr) json.dump(in_metadata, fptr)
expected_file_size = 10 expected_file_size = 10
expected_file_contents = "*" * expected_file_size expected_file_contents = "*" * expected_file_size
image_file = StringIO.StringIO(expected_file_contents) image_file = six.StringIO(expected_file_contents)
location, size, checksum, metadata = self.store.add(expected_image_id, location, size, checksum, metadata = self.store.add(expected_image_id,
image_file, image_file,
@ -334,7 +334,7 @@ class TestStore(base.IsolatedUnitTest):
self.config(filesystem_store_metadata_file=jsonfilename) self.config(filesystem_store_metadata_file=jsonfilename)
expected_file_size = 10 expected_file_size = 10
expected_file_contents = "*" * expected_file_size expected_file_contents = "*" * expected_file_size
image_file = StringIO.StringIO(expected_file_contents) image_file = six.StringIO(expected_file_contents)
location, size, checksum, metadata = self.store.add(expected_image_id, location, size, checksum, metadata = self.store.add(expected_image_id,
image_file, image_file,
@ -351,12 +351,12 @@ class TestStore(base.IsolatedUnitTest):
image_id = str(uuid.uuid4()) image_id = str(uuid.uuid4())
file_size = 5 * units.Ki # 5K file_size = 5 * units.Ki # 5K
file_contents = "*" * file_size file_contents = "*" * file_size
image_file = StringIO.StringIO(file_contents) image_file = six.StringIO(file_contents)
location, size, checksum, _ = self.store.add(image_id, location, size, checksum, _ = self.store.add(image_id,
image_file, image_file,
file_size) file_size)
image_file = StringIO.StringIO("nevergonnamakeit") image_file = six.StringIO("nevergonnamakeit")
self.assertRaises(exception.Duplicate, self.assertRaises(exception.Duplicate,
self.store.add, self.store.add,
image_id, image_file, 0) image_id, image_file, 0)
@ -367,7 +367,7 @@ class TestStore(base.IsolatedUnitTest):
file_size = 5 * units.Ki # 5K file_size = 5 * units.Ki # 5K
file_contents = "*" * file_size file_contents = "*" * file_size
path = os.path.join(self.test_dir, image_id) path = os.path.join(self.test_dir, image_id)
image_file = StringIO.StringIO(file_contents) image_file = six.StringIO(file_contents)
m = mox.Mox() m = mox.Mox()
m.StubOutWithMock(__builtin__, 'open') m.StubOutWithMock(__builtin__, 'open')
@ -424,7 +424,7 @@ class TestStore(base.IsolatedUnitTest):
file_size = 5 * units.Ki # 5K file_size = 5 * units.Ki # 5K
file_contents = "*" * file_size file_contents = "*" * file_size
path = os.path.join(self.test_dir, image_id) path = os.path.join(self.test_dir, image_id)
image_file = StringIO.StringIO(file_contents) image_file = six.StringIO(file_contents)
def fake_Error(size): def fake_Error(size):
raise AttributeError() raise AttributeError()
@ -444,7 +444,7 @@ class TestStore(base.IsolatedUnitTest):
image_id = str(uuid.uuid4()) image_id = str(uuid.uuid4())
file_size = 5 * units.Ki # 5K file_size = 5 * units.Ki # 5K
file_contents = "*" * file_size file_contents = "*" * file_size
image_file = StringIO.StringIO(file_contents) image_file = six.StringIO(file_contents)
location, size, checksum, _ = self.store.add(image_id, location, size, checksum, _ = self.store.add(image_id,
image_file, image_file,

View File

@ -14,12 +14,12 @@
import copy import copy
import os import os
import StringIO
import sys import sys
import UserDict import UserDict
import uuid import uuid
import fixtures import fixtures
import six
from glance.cmd import replicator as glance_replicator from glance.cmd import replicator as glance_replicator
from glance.openstack.common import jsonutils from glance.openstack.common import jsonutils
@ -106,7 +106,7 @@ class FakeHTTPConnection(object):
def getresponse(self): def getresponse(self):
class FakeResponse(object): class FakeResponse(object):
def __init__(self, (code, body, headers)): def __init__(self, (code, body, headers)):
self.body = StringIO.StringIO(body) self.body = six.StringIO(body)
self.headers = headers self.headers = headers
self.status = code self.status = code
@ -241,7 +241,7 @@ class ImageServiceTestCase(test_utils.BaseTestCase):
class FakeHttpResponse(object): class FakeHttpResponse(object):
def __init__(self, headers, data): def __init__(self, headers, data):
self.headers = headers self.headers = headers
self.data = StringIO.StringIO(data) self.data = six.StringIO(data)
def getheaders(self): def getheaders(self):
return self.headers return self.headers
@ -339,7 +339,7 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
stdout = sys.stdout stdout = sys.stdout
orig_img_service = glance_replicator.get_image_service orig_img_service = glance_replicator.get_image_service
sys.stdout = StringIO.StringIO() sys.stdout = six.StringIO()
try: try:
glance_replicator.get_image_service = get_image_service glance_replicator.get_image_service = get_image_service
glance_replicator.replication_size(options, args) glance_replicator.replication_size(options, args)

View File

@ -13,8 +13,7 @@
# 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 StringIO import six
import stubout import stubout
from glance.common import exception from glance.common import exception
@ -91,7 +90,7 @@ class TestStore(base.StoreClearingUnitTest):
self.assertRaises(exception.ImageSizeLimitExceeded, self.assertRaises(exception.ImageSizeLimitExceeded,
self.store.add, self.store.add,
'fake_image_id', 'fake_image_id',
utils.LimitingReader(StringIO.StringIO('xx'), 1), utils.LimitingReader(six.StringIO('xx'), 1),
2) 2)
self.assertEqual(self.store.fs.called_commands, self.assertEqual(self.store.fs.called_commands,
['exists', 'put', 'delete']) ['exists', 'put', 'delete'])

View File

@ -17,11 +17,11 @@ from contextlib import contextmanager
import datetime import datetime
import hashlib import hashlib
import os import os
import StringIO
import tempfile import tempfile
import time import time
import fixtures import fixtures
import six
from six.moves import xrange from six.moves import xrange
import stubout import stubout
@ -43,7 +43,7 @@ FIXTURE_DATA = '*' * FIXTURE_LENGTH
class ImageCacheTestCase(object): class ImageCacheTestCase(object):
def _setup_fixture_file(self): def _setup_fixture_file(self):
FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA) FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
self.assertFalse(self.cache.is_cached(1)) self.assertFalse(self.cache.is_cached(1))
@ -66,7 +66,7 @@ class ImageCacheTestCase(object):
""" """
self._setup_fixture_file() self._setup_fixture_file()
buff = StringIO.StringIO() buff = six.StringIO()
with self.cache.open_for_read(1) as cache_file: with self.cache.open_for_read(1) as cache_file:
for chunk in cache_file: for chunk in cache_file:
buff.write(chunk) buff.write(chunk)
@ -80,7 +80,7 @@ class ImageCacheTestCase(object):
""" """
self._setup_fixture_file() self._setup_fixture_file()
buff = StringIO.StringIO() buff = six.StringIO()
with self.cache.open_for_read(1) as cache_file: with self.cache.open_for_read(1) as cache_file:
for chunk in cache_file: for chunk in cache_file:
buff.write(chunk) buff.write(chunk)
@ -114,7 +114,7 @@ class ImageCacheTestCase(object):
self.assertFalse(self.cache.is_cached(image_id)) self.assertFalse(self.cache.is_cached(image_id))
for image_id in (1, 2): for image_id in (1, 2):
FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA) FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
self.assertTrue(self.cache.cache_image_file(image_id, self.assertTrue(self.cache.cache_image_file(image_id,
FIXTURE_FILE)) FIXTURE_FILE))
@ -183,7 +183,7 @@ class ImageCacheTestCase(object):
# pruning, and the images that are least recently accessed # pruning, and the images that are least recently accessed
# should be the ones pruned... # should be the ones pruned...
for x in xrange(10): for x in xrange(10):
FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA) FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
self.assertTrue(self.cache.cache_image_file(x, self.assertTrue(self.cache.cache_image_file(x,
FIXTURE_FILE)) FIXTURE_FILE))
@ -191,7 +191,7 @@ class ImageCacheTestCase(object):
# OK, hit the images that are now cached... # OK, hit the images that are now cached...
for x in xrange(10): for x in xrange(10):
buff = StringIO.StringIO() buff = six.StringIO()
with self.cache.open_for_read(x) as cache_file: with self.cache.open_for_read(x) as cache_file:
for chunk in cache_file: for chunk in cache_file:
buff.write(chunk) buff.write(chunk)
@ -216,13 +216,13 @@ class ImageCacheTestCase(object):
""" """
self.assertEqual(0, self.cache.get_cache_size()) self.assertEqual(0, self.cache.get_cache_size())
FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA) FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
self.assertTrue(self.cache.cache_image_file('xxx', FIXTURE_FILE)) self.assertTrue(self.cache.cache_image_file('xxx', FIXTURE_FILE))
self.assertEqual(1024, self.cache.get_cache_size()) self.assertEqual(1024, self.cache.get_cache_size())
# OK, hit the image that is now cached... # OK, hit the image that is now cached...
buff = StringIO.StringIO() buff = six.StringIO()
with self.cache.open_for_read('xxx') as cache_file: with self.cache.open_for_read('xxx') as cache_file:
for chunk in cache_file: for chunk in cache_file:
buff.write(chunk) buff.write(chunk)
@ -242,7 +242,7 @@ class ImageCacheTestCase(object):
self.assertFalse(self.cache.is_cached(1)) self.assertFalse(self.cache.is_cached(1))
self.assertFalse(self.cache.is_queued(1)) self.assertFalse(self.cache.is_queued(1))
FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA) FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
self.assertTrue(self.cache.queue_image(1)) self.assertTrue(self.cache.queue_image(1))

View File

@ -13,9 +13,8 @@
# 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 StringIO
import mock import mock
import six
from glance.common import exception from glance.common import exception
from glance.common import utils from glance.common import utils
@ -43,7 +42,7 @@ class TestStore(base.StoreClearingUnitTest):
self.location = StoreLocation(self.store_specs) self.location = StoreLocation(self.store_specs)
# Provide enough data to get more than one chunk iteration. # Provide enough data to get more than one chunk iteration.
self.data_len = 3 * units.Ki self.data_len = 3 * units.Ki
self.data_iter = StringIO.StringIO('*' * self.data_len) self.data_iter = six.StringIO('*' * self.data_len)
def test_add_w_image_size_zero(self): def test_add_w_image_size_zero(self):
"""Assert that correct size is returned even though 0 was provided.""" """Assert that correct size is returned even though 0 was provided."""

View File

@ -16,10 +16,10 @@
"""Tests the S3 backend store""" """Tests the S3 backend store"""
import hashlib import hashlib
import StringIO
import uuid import uuid
import boto.s3.connection import boto.s3.connection
import six
import stubout import stubout
from glance.common import exception from glance.common import exception
@ -80,7 +80,7 @@ def stub_out_s3(stubs):
return checksum_hex, None return checksum_hex, None
def set_contents_from_file(self, fp, replace=False, **kwargs): def set_contents_from_file(self, fp, replace=False, **kwargs):
self.data = StringIO.StringIO() self.data = six.StringIO()
for bytes in fp: for bytes in fp:
self.data.write(bytes) self.data.write(bytes)
self.size = self.data.len self.size = self.data.len
@ -122,7 +122,7 @@ def stub_out_s3(stubs):
fixture_buckets = {'glance': FakeBucket('glance')} fixture_buckets = {'glance': FakeBucket('glance')}
b = fixture_buckets['glance'] b = fixture_buckets['glance']
k = b.new_key(FAKE_UUID) 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): def fake_connection_constructor(self, *args, **kwargs):
host = kwargs.get('host') host = kwargs.get('host')
@ -237,7 +237,7 @@ class TestStore(base.StoreClearingUnitTest):
S3_CONF['s3_store_host'], S3_CONF['s3_store_host'],
S3_CONF['s3_store_bucket'], S3_CONF['s3_store_bucket'],
expected_image_id) expected_image_id)
image_s3 = StringIO.StringIO(expected_s3_contents) image_s3 = six.StringIO(expected_s3_contents)
location, size, checksum, _ = self.store.add(expected_image_id, location, size, checksum, _ = self.store.add(expected_image_id,
image_s3, image_s3,
@ -249,7 +249,7 @@ class TestStore(base.StoreClearingUnitTest):
loc = get_location_from_uri(expected_location) loc = get_location_from_uri(expected_location)
(new_image_s3, new_image_size) = self.store.get(loc) (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: for chunk in new_image_s3:
new_image_contents.write(chunk) new_image_contents.write(chunk)
new_image_s3_size = new_image_contents.len new_image_s3_size = new_image_contents.len
@ -285,7 +285,7 @@ class TestStore(base.StoreClearingUnitTest):
new_conf['s3_store_host'], new_conf['s3_store_host'],
new_conf['s3_store_bucket'], new_conf['s3_store_bucket'],
expected_image_id) expected_image_id)
image_s3 = StringIO.StringIO(expected_s3_contents) image_s3 = six.StringIO(expected_s3_contents)
self.config(**new_conf) self.config(**new_conf)
self.store = Store() self.store = Store()
@ -310,7 +310,7 @@ class TestStore(base.StoreClearingUnitTest):
Tests that adding an image with an existing identifier Tests that adding an image with an existing identifier
raises an appropriate exception raises an appropriate exception
""" """
image_s3 = StringIO.StringIO("nevergonnamakeit") image_s3 = six.StringIO("nevergonnamakeit")
self.assertRaises(exception.Duplicate, self.assertRaises(exception.Duplicate,
self.store.add, self.store.add,
FAKE_UUID, image_s3, 0) FAKE_UUID, image_s3, 0)

View File

@ -13,8 +13,7 @@
# 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 StringIO import six
import stubout import stubout
from glance.common import exception from glance.common import exception
@ -55,6 +54,6 @@ class TestStore(base.StoreClearingUnitTest):
self.assertRaises(exception.ImageSizeLimitExceeded, self.assertRaises(exception.ImageSizeLimitExceeded,
self.store.add, self.store.add,
'fake_image_id', 'fake_image_id',
utils.LimitingReader(StringIO.StringIO('xx'), 1), utils.LimitingReader(six.StringIO('xx'), 1),
2) 2)
self.assertEqual(called_commands, ['list -r', 'create', 'delete']) self.assertEqual(called_commands, ['list -r', 'create', 'delete'])

View File

@ -18,11 +18,11 @@
import hashlib import hashlib
import httplib import httplib
import mock import mock
import StringIO
import tempfile import tempfile
import uuid import uuid
from oslo.config import cfg from oslo.config import cfg
import six
import six.moves.urllib.parse as urlparse import six.moves.urllib.parse as urlparse
import stubout import stubout
import swiftclient import swiftclient
@ -69,7 +69,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
} }
} }
fixture_objects = {'glance/%s' % FAKE_UUID: fixture_objects = {'glance/%s' % FAKE_UUID:
StringIO.StringIO("*" * FIVE_KB)} six.StringIO("*" * FIVE_KB)}
def fake_head_container(url, token, container, **kwargs): def fake_head_container(url, token, container, **kwargs):
if container not in fixture_containers: if container not in fixture_containers:
@ -99,7 +99,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
'etag': etag} 'etag': etag}
return etag return etag
if hasattr(contents, 'read'): if hasattr(contents, 'read'):
fixture_object = StringIO.StringIO() fixture_object = six.StringIO()
chunk = contents.read(CHUNKSIZE) chunk = contents.read(CHUNKSIZE)
checksum = hashlib.md5() checksum = hashlib.md5()
while chunk: while chunk:
@ -108,7 +108,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
chunk = contents.read(CHUNKSIZE) chunk = contents.read(CHUNKSIZE)
etag = checksum.hexdigest() etag = checksum.hexdigest()
else: else:
fixture_object = StringIO.StringIO(contents) fixture_object = six.StringIO(contents)
etag = hashlib.md5(fixture_object.getvalue()).hexdigest() etag = hashlib.md5(fixture_object.getvalue()).hexdigest()
read_len = fixture_object.len read_len = fixture_object.len
if read_len > MAX_SWIFT_OBJECT_SIZE: if read_len > MAX_SWIFT_OBJECT_SIZE:
@ -149,7 +149,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
chunk_keys = sorted([k for k in fixture_headers.keys() chunk_keys = sorted([k for k in fixture_headers.keys()
if k.startswith(fixture_key) and if k.startswith(fixture_key) and
k != fixture_key]) k != fixture_key])
result = StringIO.StringIO() result = six.StringIO()
for key in chunk_keys: for key in chunk_keys:
result.write(fixture_objects[key].getvalue()) result.write(fixture_objects[key].getvalue())
else: else:
@ -157,7 +157,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
if byte_range is not None: if byte_range is not None:
start = int(byte_range.split('=')[1].strip('-')) start = int(byte_range.split('=')[1].strip('-'))
result = StringIO.StringIO(result.getvalue()[start:]) result = six.StringIO(result.getvalue()[start:])
fixture_headers[fixture_key]['content-length'] = len( fixture_headers[fixture_key]['content-length'] = len(
result.getvalue()) result.getvalue())
@ -321,7 +321,7 @@ class SwiftTests(object):
loc = 'swift+https://%s:key@localhost:8080/glance/%s' loc = 'swift+https://%s:key@localhost:8080/glance/%s'
expected_location = loc % (self.swift_store_user, expected_location = loc % (self.swift_store_user,
expected_image_id) expected_image_id)
image_swift = StringIO.StringIO(expected_swift_contents) image_swift = six.StringIO(expected_swift_contents)
global SWIFT_PUT_OBJECT_CALLS global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0 SWIFT_PUT_OBJECT_CALLS = 0
@ -378,7 +378,7 @@ class SwiftTests(object):
expected_checksum = \ expected_checksum = \
hashlib.md5(expected_swift_contents).hexdigest() hashlib.md5(expected_swift_contents).hexdigest()
image_swift = StringIO.StringIO(expected_swift_contents) image_swift = six.StringIO(expected_swift_contents)
global SWIFT_PUT_OBJECT_CALLS global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0 SWIFT_PUT_OBJECT_CALLS = 0
@ -410,7 +410,7 @@ class SwiftTests(object):
swift_store_container='noexist') swift_store_container='noexist')
self.store = Store() self.store = Store()
image_swift = StringIO.StringIO("nevergonnamakeit") image_swift = six.StringIO("nevergonnamakeit")
global SWIFT_PUT_OBJECT_CALLS global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0 SWIFT_PUT_OBJECT_CALLS = 0
@ -440,7 +440,7 @@ class SwiftTests(object):
loc = 'swift+https://%s:key@localhost:8080/noexist/%s' loc = 'swift+https://%s:key@localhost:8080/noexist/%s'
expected_location = loc % (self.swift_store_user, expected_location = loc % (self.swift_store_user,
expected_image_id) expected_image_id)
image_swift = StringIO.StringIO(expected_swift_contents) image_swift = six.StringIO(expected_swift_contents)
global SWIFT_PUT_OBJECT_CALLS global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0 SWIFT_PUT_OBJECT_CALLS = 0
@ -479,7 +479,7 @@ class SwiftTests(object):
loc = 'swift+https://%s:key@localhost:8080/glance/%s' loc = 'swift+https://%s:key@localhost:8080/glance/%s'
expected_location = loc % (self.swift_store_user, expected_location = loc % (self.swift_store_user,
expected_image_id) expected_image_id)
image_swift = StringIO.StringIO(expected_swift_contents) image_swift = six.StringIO(expected_swift_contents)
global SWIFT_PUT_OBJECT_CALLS global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0 SWIFT_PUT_OBJECT_CALLS = 0
@ -532,7 +532,7 @@ class SwiftTests(object):
loc = 'swift+https://%s:key@localhost:8080/glance/%s' loc = 'swift+https://%s:key@localhost:8080/glance/%s'
expected_location = loc % (self.swift_store_user, expected_location = loc % (self.swift_store_user,
expected_image_id) expected_image_id)
image_swift = StringIO.StringIO(expected_swift_contents) image_swift = six.StringIO(expected_swift_contents)
global SWIFT_PUT_OBJECT_CALLS global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0 SWIFT_PUT_OBJECT_CALLS = 0
@ -578,7 +578,7 @@ class SwiftTests(object):
Tests that adding an image with an existing identifier Tests that adding an image with an existing identifier
raises an appropriate exception raises an appropriate exception
""" """
image_swift = StringIO.StringIO("nevergonnamakeit") image_swift = six.StringIO("nevergonnamakeit")
self.assertRaises(exception.Duplicate, self.assertRaises(exception.Duplicate,
self.store.add, self.store.add,
FAKE_UUID, image_swift, 0) FAKE_UUID, image_swift, 0)
@ -597,7 +597,7 @@ class SwiftTests(object):
except Exception: except Exception:
pass pass
image_swift = StringIO.StringIO(swift_contents) image_swift = six.StringIO(swift_contents)
connection.put_object.side_effect = exception.ClientConnectionError connection.put_object.side_effect = exception.ClientConnectionError
self.store._delete_stale_chunks = fake_delete_chunk self.store._delete_stale_chunks = fake_delete_chunk

View File

@ -16,10 +16,10 @@
"""Tests the VMware Datastore backend store""" """Tests the VMware Datastore backend store"""
import hashlib import hashlib
import StringIO
import uuid import uuid
import mock import mock
import six
from glance.common import exception from glance.common import exception
from glance.openstack.common import units from glance.openstack.common import units
@ -167,7 +167,7 @@ class TestStore(base.StoreClearingUnitTest):
expected_image_id, expected_image_id,
VMWARE_DATASTORE_CONF['vmware_datacenter_path'], VMWARE_DATASTORE_CONF['vmware_datacenter_path'],
VMWARE_DATASTORE_CONF['vmware_datastore_name']) VMWARE_DATASTORE_CONF['vmware_datastore_name'])
image = StringIO.StringIO(expected_contents) image = six.StringIO(expected_contents)
with mock.patch('httplib.HTTPConnection') as HttpConn: with mock.patch('httplib.HTTPConnection') as HttpConn:
HttpConn.return_value = FakeHTTPConnection() HttpConn.return_value = FakeHTTPConnection()
location, size, checksum, _ = self.store.add(expected_image_id, location, size, checksum, _ = self.store.add(expected_image_id,

View File

@ -18,12 +18,12 @@
import copy import copy
import datetime import datetime
import hashlib import hashlib
import StringIO
import uuid import uuid
import mock import mock
from oslo.config import cfg from oslo.config import cfg
import routes import routes
import six
import webob import webob
import glance.api import glance.api
@ -532,7 +532,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
req = webob.Request.blank("/images") req = webob.Request.blank("/images")
req.method = 'POST' req.method = 'POST'
req.body_file = StringIO.StringIO('X' * (CONF.image_size_cap + 1)) req.body_file = six.StringIO('X' * (CONF.image_size_cap + 1))
for k, v in fixture_headers.iteritems(): for k, v in fixture_headers.iteritems():
req.headers[k] = v req.headers[k] = v
@ -979,7 +979,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
req = webob.Request.blank("/images") req = webob.Request.blank("/images")
req.method = 'POST' req.method = 'POST'
req.body_file = StringIO.StringIO('X' * (CONF.image_size_cap)) req.body_file = six.StringIO('X' * (CONF.image_size_cap))
for k, v in fixture_headers.iteritems(): for k, v in fixture_headers.iteritems():
req.headers[k] = v req.headers[k] = v
@ -1034,7 +1034,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
req.headers['transfer-encoding'] = 'chunked' req.headers['transfer-encoding'] = 'chunked'
req.headers['x-image-disk-format'] = 'vhd' req.headers['x-image-disk-format'] = 'vhd'
req.headers['x-image-container-format'] = 'ovf' req.headers['x-image-container-format'] = 'ovf'
req.body_file = StringIO.StringIO('X' * (CONF.image_size_cap)) req.body_file = six.StringIO('X' * (CONF.image_size_cap))
res = req.get_response(self.api) res = req.get_response(self.api)
self.assertEqual(res.status_int, 403) self.assertEqual(res.status_int, 403)
@ -1655,7 +1655,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
req = webob.Request.blank("/images/%s" % image_id) req = webob.Request.blank("/images/%s" % image_id)
req.method = 'PUT' req.method = 'PUT'
req.body_file = StringIO.StringIO('X' * (CONF.image_size_cap + 1)) req.body_file = six.StringIO('X' * (CONF.image_size_cap + 1))
for k, v in fixture_headers.iteritems(): for k, v in fixture_headers.iteritems():
req.headers[k] = v req.headers[k] = v

View File

@ -14,9 +14,9 @@
# under the License. # under the License.
import mock import mock
import StringIO
import uuid import uuid
import six
import webob import webob
import glance.api.v2.image_data import glance.api.v2.image_data
@ -327,7 +327,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
request.headers['Content-Type'] = 'application/octet-stream' request.headers['Content-Type'] = 'application/octet-stream'
# If we use body_file, webob assumes we want to do a chunked upload, # If we use body_file, webob assumes we want to do a chunked upload,
# ignoring the Content-Length header # ignoring the Content-Length header
request.body_file = StringIO.StringIO('YYY') request.body_file = six.StringIO('YYY')
output = self.deserializer.upload(request) output = self.deserializer.upload(request)
data = output.pop('data') data = output.pop('data')
self.assertEqual(data.read(), 'YYY') self.assertEqual(data.read(), 'YYY')
@ -337,7 +337,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
def test_upload_chunked_with_content_length(self): def test_upload_chunked_with_content_length(self):
request = unit_test_utils.get_fake_request() request = unit_test_utils.get_fake_request()
request.headers['Content-Type'] = 'application/octet-stream' request.headers['Content-Type'] = 'application/octet-stream'
request.body_file = StringIO.StringIO('YYY') request.body_file = six.StringIO('YYY')
# The deserializer shouldn't care if the Content-Length is # The deserializer shouldn't care if the Content-Length is
# set when the user is attempting to send chunked data. # set when the user is attempting to send chunked data.
request.headers['Content-Length'] = 3 request.headers['Content-Length'] = 3

View File

@ -21,12 +21,12 @@ import os
import shlex import shlex
import shutil import shutil
import socket import socket
import StringIO
import subprocess import subprocess
import uuid import uuid
import fixtures import fixtures
from oslo.config import cfg from oslo.config import cfg
import six
import stubout import stubout
import testtools import testtools
import webob import webob
@ -508,7 +508,7 @@ class FakeAuthMiddleware(wsgi.Middleware):
class FakeHTTPResponse(object): class FakeHTTPResponse(object):
def __init__(self, status=200, headers=None, data=None, *args, **kwargs): def __init__(self, status=200, headers=None, data=None, *args, **kwargs):
data = data or 'I am a teapot, short and stout\n' 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.read = self.data.read
self.status = status self.status = status
self.headers = headers or {'content-length': len(data)} self.headers = headers or {'content-length': len(data)}