Switch all uses of StringIO to use it from six

As part of enabling python3 support in tempest we need to be able to
handle StringIO usage in both python2 and python3. Six provides a
compat layer for doing this, so this commit moves all uses of StringIO
and cStringIO to get it through six.

Change-Id: Ie6ac86b3b5ed2c307dc2cc41386da9e5ba1ee23e
This commit is contained in:
Matthew Treinish 2015-04-23 09:09:41 -04:00
parent 628c965d62
commit b0c65f2820
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
11 changed files with 27 additions and 29 deletions

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import StringIO
import six
from tempest_lib.common.utils import data_utils
@ -51,7 +51,7 @@ class ImagesMetadataTestJSON(base.BaseV2ComputeTest):
is_public=False)
cls.image_id = body['id']
cls.images.append(cls.image_id)
image_file = StringIO.StringIO(('*' * 1024))
image_file = six.StringIO(('*' * 1024))
cls.glance_client.update_image(cls.image_id, data=image_file)
cls.client.wait_for_image_status(cls.image_id, 'ACTIVE')

View File

@ -13,10 +13,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import StringIO
import time
from oslo_log import log as logging
import six
from tempest_lib.common.utils import data_utils
import testtools
@ -59,7 +59,7 @@ class ListImageFiltersTestJSON(base.BaseV2ComputeTest):
# Wait 1 second between creation and upload to ensure a delta
# between created_at and updated_at.
time.sleep(1)
image_file = StringIO.StringIO(('*' * 1024))
image_file = six.StringIO(('*' * 1024))
cls.glance_client.update_image(image_id, data=image_file)
cls.client.wait_for_image_status(image_id, 'ACTIVE')
body = cls.client.get_image(image_id)

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import StringIO
import six
from oslo_log import log as logging
from tempest_lib.common.utils import data_utils
@ -75,7 +75,7 @@ class AuthorizationTestJSON(base.BaseV2ComputeTest):
disk_format='raw',
is_public=False)
image_id = body['id']
image_file = StringIO.StringIO(('*' * 1024))
image_file = six.StringIO(('*' * 1024))
body = cls.glance_client.update_image(image_id, data=image_file)
cls.glance_client.wait_for_image_status(image_id, 'active')
cls.image = cls.images_client.get_image(image_id)

View File

@ -12,9 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import cStringIO as StringIO
from oslo_log import log as logging
from six import moves
from tempest_lib.common.utils import data_utils
from tempest_lib import exceptions as lib_exc
@ -113,7 +112,7 @@ class BaseV1ImageMembersTest(BaseV1ImageTest):
cls.alt_tenant_id = cls.alt_img_cli.tenant_id
def _create_image(self):
image_file = StringIO.StringIO(data_utils.random_bytes())
image_file = moves.cStringIO(data_utils.random_bytes())
image = self.create_image(container_format='bare',
disk_format='raw',
is_public=False,

View File

@ -13,8 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import cStringIO as StringIO
from six import moves
from tempest_lib.common.utils import data_utils
from tempest.api.image import base
@ -46,7 +45,7 @@ class CreateRegisterImagesTest(base.BaseV1ImageTest):
self.assertEqual(val, body.get('properties')[key])
# Now try uploading an image file
image_file = StringIO.StringIO(data_utils.random_bytes())
image_file = moves.cStringIO(data_utils.random_bytes())
body = self.client.update_image(image_id, data=image_file)
self.assertIn('size', body)
self.assertEqual(1024, body.get('size'))
@ -161,7 +160,7 @@ class ListImagesTest(base.BaseV1ImageTest):
image. Note that the size of the new image is a random number between
1024 and 4096
"""
image_file = StringIO.StringIO(data_utils.random_bytes(size))
image_file = moves.cStringIO(data_utils.random_bytes(size))
name = 'New Standard Image %s' % name
image = cls.create_image(name=name,
container_format=container_format,
@ -257,7 +256,7 @@ class UpdateImageMetaTest(base.BaseV1ImageTest):
Create a new standard image and return the ID of the newly-registered
image.
"""
image_file = StringIO.StringIO(data_utils.random_bytes(size))
image_file = moves.cStringIO(data_utils.random_bytes(size))
name = 'New Standard Image %s' % name
image = cls.create_image(name=name,
container_format=container_format,

View File

@ -14,9 +14,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import cStringIO as StringIO
import random
from six import moves
from tempest_lib.common.utils import data_utils
from tempest.api.image import base
@ -55,7 +55,7 @@ class BasicOperationsImagesTest(base.BaseV2ImageTest):
# Now try uploading an image file
file_content = data_utils.random_bytes()
image_file = StringIO.StringIO(file_content)
image_file = moves.cStringIO(file_content)
self.client.store_image(image_id, image_file)
# Now try to get image details
@ -108,7 +108,7 @@ class BasicOperationsImagesTest(base.BaseV2ImageTest):
image_id = body['id']
# Now try uploading an image file
image_file = StringIO.StringIO(data_utils.random_bytes())
image_file = moves.cStringIO(data_utils.random_bytes())
self.client.store_image(image_id, image_file)
# Update Image
@ -149,7 +149,7 @@ class ListImagesTest(base.BaseV2ImageTest):
1024 and 4096
"""
size = random.randint(1024, 4096)
image_file = StringIO.StringIO(data_utils.random_bytes(size))
image_file = moves.cStringIO(data_utils.random_bytes(size))
name = data_utils.rand_name('image')
body = cls.create_image(name=name,
container_format=container_format,

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import cStringIO as StringIO
import hashlib
import random
import re
@ -21,6 +20,7 @@ import time
import zlib
import six
from six import moves
from tempest_lib.common.utils import data_utils
from tempest.api.object_storage import base
@ -216,7 +216,7 @@ class ObjectTest(base.BaseObjectTest):
status, _, resp_headers = self.object_client.put_object_with_chunk(
container=self.container_name,
name=object_name,
contents=StringIO.StringIO(data),
contents=moves.cStringIO(data),
chunk_size=512)
self.assertHeaders(resp_headers, 'Object', 'PUT')

View File

@ -22,13 +22,13 @@ import json
import posixpath
import re
import socket
import StringIO
import struct
import urlparse
import OpenSSL
from oslo_log import log as logging
import six
from six import moves
from tempest_lib import exceptions as lib_exc
@ -129,7 +129,7 @@ class HTTPClient(object):
# Read body into string if it isn't obviously image data
if resp.getheader('content-type', None) != 'application/octet-stream':
body_str = ''.join([body_chunk for body_chunk in body_iter])
body_iter = StringIO.StringIO(body_str)
body_iter = six.StringIO(body_str)
self._log_response(resp, None)
else:
self._log_response(resp, body_iter)

View File

@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import cStringIO
import select
import socket
import time
@ -22,6 +20,7 @@ import warnings
from oslo_log import log as logging
import six
from six import moves
from tempest import exceptions
@ -43,7 +42,7 @@ class Client(object):
self.password = password
if isinstance(pkey, six.string_types):
pkey = paramiko.RSAKey.from_private_key(
cStringIO.StringIO(str(pkey)))
moves.cStringIO(str(pkey)))
self.pkey = pkey
self.look_for_keys = look_for_keys
self.key_filename = key_filename

View File

@ -29,7 +29,7 @@ class TestSshClient(base.TestCase):
def test_pkey_calls_paramiko_RSAKey(self):
with contextlib.nested(
mock.patch('paramiko.RSAKey.from_private_key'),
mock.patch('cStringIO.StringIO')) as (rsa_mock, cs_mock):
mock.patch('six.moves.cStringIO')) as (rsa_mock, cs_mock):
cs_mock.return_value = mock.sentinel.csio
pkey = 'mykey'
ssh.Client('localhost', 'root', pkey=pkey)

View File

@ -14,10 +14,11 @@
import os
import shutil
import StringIO
import subprocess
import tempfile
import six
from tempest.tests import base
DEVNULL = open(os.devnull, 'wb')
@ -50,8 +51,8 @@ class TestWrappers(base.TestCase):
shutil.copy('tools/pretty_tox_serial.sh',
os.path.join(self.directory, 'pretty_tox_serial.sh'))
self.stdout = StringIO.StringIO()
self.stderr = StringIO.StringIO()
self.stdout = six.StringIO()
self.stderr = six.StringIO()
# Change directory, run wrapper and check result
self.addCleanup(os.chdir, os.path.abspath(os.curdir))
os.chdir(self.directory)