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
diff --git a/tempest/api/compute/images/test_image_metadata.py b/tempest/api/compute/images/test_image_metadata.py
index ab21ad7..52d47dd 100644
--- a/tempest/api/compute/images/test_image_metadata.py
+++ b/tempest/api/compute/images/test_image_metadata.py
@@ -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 @@
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')
diff --git a/tempest/api/compute/images/test_list_image_filters.py b/tempest/api/compute/images/test_list_image_filters.py
index 2c6d2df..430ca35 100644
--- a/tempest/api/compute/images/test_list_image_filters.py
+++ b/tempest/api/compute/images/test_list_image_filters.py
@@ -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 @@
# 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)
diff --git a/tempest/api/compute/test_authorization.py b/tempest/api/compute/test_authorization.py
index f9ee75b..2baf608 100644
--- a/tempest/api/compute/test_authorization.py
+++ b/tempest/api/compute/test_authorization.py
@@ -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 @@
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)
diff --git a/tempest/api/image/base.py b/tempest/api/image/base.py
index d513b0c..74044dc 100644
--- a/tempest/api/image/base.py
+++ b/tempest/api/image/base.py
@@ -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 @@
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,
diff --git a/tempest/api/image/v1/test_images.py b/tempest/api/image/v1/test_images.py
index bd672c9..49e167b 100644
--- a/tempest/api/image/v1/test_images.py
+++ b/tempest/api/image/v1/test_images.py
@@ -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 @@
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 @@
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 @@
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,
diff --git a/tempest/api/image/v2/test_images.py b/tempest/api/image/v2/test_images.py
index a00296c..ef0b5f1 100644
--- a/tempest/api/image/v2/test_images.py
+++ b/tempest/api/image/v2/test_images.py
@@ -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 @@
# 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 @@
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 @@
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,
diff --git a/tempest/api/object_storage/test_object_services.py b/tempest/api/object_storage/test_object_services.py
index 2091eb5..5797e7f 100644
--- a/tempest/api/object_storage/test_object_services.py
+++ b/tempest/api/object_storage/test_object_services.py
@@ -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 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 @@
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')
diff --git a/tempest/common/glance_http.py b/tempest/common/glance_http.py
index c6b8ba3..ee07e73 100644
--- a/tempest/common/glance_http.py
+++ b/tempest/common/glance_http.py
@@ -22,13 +22,13 @@
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 @@
# 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)
diff --git a/tempest/common/ssh.py b/tempest/common/ssh.py
index fe67ff8..d0e484c 100644
--- a/tempest/common/ssh.py
+++ b/tempest/common/ssh.py
@@ -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 @@
from oslo_log import log as logging
import six
+from six import moves
from tempest import exceptions
@@ -43,7 +42,7 @@
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
diff --git a/tempest/tests/test_ssh.py b/tempest/tests/test_ssh.py
index 27cd6b5..aaacaab 100644
--- a/tempest/tests/test_ssh.py
+++ b/tempest/tests/test_ssh.py
@@ -29,7 +29,7 @@
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)
diff --git a/tempest/tests/test_wrappers.py b/tempest/tests/test_wrappers.py
index ae7860d..a4ef699 100644
--- a/tempest/tests/test_wrappers.py
+++ b/tempest/tests/test_wrappers.py
@@ -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 @@
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)