Replace six.BytesIO, six.StringIO usage

Change-Id: I007da2ba19b4e18f7c4f64d1503a18815a3f8ce4
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2021-12-21 11:56:48 +00:00
parent 6bd7c188ee
commit 67b531b143
9 changed files with 42 additions and 39 deletions

View File

@ -9,13 +9,14 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import io
import sys
from unittest import mock
import glance_store as store
from oslo_config import cfg
from oslo_log import log as logging
import six
import glance.cmd.api
import glance.cmd.cache_cleaner
@ -48,7 +49,7 @@ class TestGlanceApiCmd(test_utils.BaseTestCase):
super(TestGlanceApiCmd, self).setUp()
self.__argv_backup = sys.argv
sys.argv = ['glance-api']
self.stderr = six.StringIO()
self.stderr = io.StringIO()
sys.stderr = self.stderr
store.register_opts(CONF)

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import json
import os
from unittest import mock
@ -20,7 +21,6 @@ from unittest import mock
import glance_store
from oslo_concurrency import processutils
from oslo_config import cfg
import six
from glance.async_.flows import convert
from glance.async_ import taskflow_executor
@ -173,7 +173,7 @@ class TestImportTask(test_utils.BaseTestCase):
return ("", None)
with mock.patch.object(script_utils, 'get_image_data_iter') as dmock:
dmock.return_value = six.BytesIO(b"TEST_IMAGE")
dmock.return_value = io.BytesIO(b"TEST_IMAGE")
with mock.patch.object(processutils, 'execute') as exc_mock:
exc_mock.side_effect = fake_execute

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import json
import os
from unittest import mock
@ -20,7 +21,6 @@ from unittest import mock
import glance_store
from oslo_concurrency import processutils as putils
from oslo_config import cfg
import six
from six.moves import urllib
from taskflow import task
from taskflow.types import failure
@ -123,7 +123,7 @@ class TestImportTask(test_utils.BaseTestCase):
img_factory.new_image.side_effect = create_image
with mock.patch.object(script_utils, 'get_image_data_iter') as dmock:
dmock.return_value = six.BytesIO(b"TEST_IMAGE")
dmock.return_value = io.BytesIO(b"TEST_IMAGE")
with mock.patch.object(putils, 'trycmd') as tmock:
tmock.return_value = (json.dumps({
@ -166,7 +166,7 @@ class TestImportTask(test_utils.BaseTestCase):
img_factory.new_image.side_effect = create_image
with mock.patch.object(script_utils, 'get_image_data_iter') as dmock:
dmock.return_value = six.BytesIO(b"TEST_IMAGE")
dmock.return_value = io.BytesIO(b"TEST_IMAGE")
with mock.patch.object(import_flow._ImportToFS, 'execute') as emk:
executor.begin_processing(self.task.task_id)
@ -237,7 +237,7 @@ class TestImportTask(test_utils.BaseTestCase):
img_factory.new_image.side_effect = create_image
with mock.patch.object(script_utils, 'get_image_data_iter') as dmock:
dmock.return_value = six.BytesIO(b"TEST_IMAGE")
dmock.return_value = io.BytesIO(b"TEST_IMAGE")
with mock.patch.object(putils, 'trycmd') as tmock:
tmock.return_value = (json.dumps({
@ -287,7 +287,7 @@ class TestImportTask(test_utils.BaseTestCase):
img_factory.new_image.side_effect = create_image
with mock.patch.object(script_utils, 'get_image_data_iter') as dmock:
dmock.return_value = six.BytesIO(b"TEST_IMAGE")
dmock.return_value = io.BytesIO(b"TEST_IMAGE")
with mock.patch.object(putils, 'trycmd') as tmock:
tmock.return_value = (json.dumps({
@ -340,7 +340,7 @@ class TestImportTask(test_utils.BaseTestCase):
with mock.patch.object(urllib.request, 'urlopen') as umock:
content = b"TEST_IMAGE"
umock.return_value = six.BytesIO(content)
umock.return_value = io.BytesIO(content)
with mock.patch.object(import_flow, "_get_import_flows") as imock:
imock.return_value = (x for x in [])

View File

@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import tempfile
from unittest import mock
@ -21,7 +22,6 @@ import glance_store as store
from glance_store._drivers import cinder
from oslo_config import cfg
from oslo_log import log as logging
import six
import webob
from glance.common import exception
@ -30,7 +30,6 @@ from glance.common import utils
from glance.tests.unit import base
from glance.tests import utils as test_utils
CONF = cfg.CONF
@ -276,14 +275,14 @@ class TestUtils(test_utils.BaseTestCase):
"""Ensure limiting reader class accesses all bytes of file"""
BYTES = 1024
bytes_read = 0
data = six.StringIO("*" * BYTES)
data = io.StringIO("*" * BYTES)
for chunk in utils.LimitingReader(data, BYTES):
bytes_read += len(chunk)
self.assertEqual(BYTES, bytes_read)
bytes_read = 0
data = six.StringIO("*" * BYTES)
data = io.StringIO("*" * BYTES)
reader = utils.LimitingReader(data, BYTES)
byte = reader.read(1)
while len(byte) != 0:
@ -298,7 +297,7 @@ class TestUtils(test_utils.BaseTestCase):
def _consume_all_iter():
bytes_read = 0
data = six.StringIO("*" * BYTES)
data = io.StringIO("*" * BYTES)
for chunk in utils.LimitingReader(data, BYTES - 1):
bytes_read += len(chunk)
@ -306,7 +305,7 @@ class TestUtils(test_utils.BaseTestCase):
def _consume_all_read():
bytes_read = 0
data = six.StringIO("*" * BYTES)
data = io.StringIO("*" * BYTES)
reader = utils.LimitingReader(data, BYTES - 1)
byte = reader.read(1)
while len(byte) != 0:

View File

@ -11,6 +11,8 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import io
from unittest import mock
import copy
@ -20,7 +22,6 @@ import uuid
import fixtures
from oslo_serialization import jsonutils
import six
from six import moves
from six.moves import http_client as http
import webob
@ -110,7 +111,7 @@ class FakeHTTPConnection(object):
class FakeResponse(object):
def __init__(self, args):
(code, body, headers) = args
self.body = six.StringIO(body)
self.body = io.StringIO(body)
self.headers = headers
self.status = code
@ -246,7 +247,7 @@ class ImageServiceTestCase(test_utils.BaseTestCase):
class FakeHttpResponse(object):
def __init__(self, headers, data):
self.headers = headers
self.data = six.BytesIO(data)
self.data = io.BytesIO(data)
def getheaders(self):
return self.headers
@ -356,7 +357,7 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
stdout = sys.stdout
orig_img_service = glance_replicator.get_image_service
sys.stdout = six.StringIO()
sys.stdout = io.StringIO()
try:
glance_replicator.get_image_service = get_image_service
glance_replicator.replication_size(options, args)

View File

@ -15,6 +15,7 @@
from contextlib import contextmanager
import datetime
import io
import os
import tempfile
import time
@ -25,7 +26,6 @@ import glance_store as store
from oslo_config import cfg
from oslo_utils import secretutils
from oslo_utils import units
import six
# NOTE(jokke): simplified transition to py3, behaves like py2 xrange
from six.moves import range
@ -48,7 +48,7 @@ CONF = cfg.CONF
class ImageCacheTestCase(object):
def _setup_fixture_file(self):
FIXTURE_FILE = six.BytesIO(FIXTURE_DATA)
FIXTURE_FILE = io.BytesIO(FIXTURE_DATA)
self.assertFalse(self.cache.is_cached(1))
@ -71,7 +71,7 @@ class ImageCacheTestCase(object):
"""
self._setup_fixture_file()
buff = six.BytesIO()
buff = io.BytesIO()
with self.cache.open_for_read(1) as cache_file:
for chunk in cache_file:
buff.write(chunk)
@ -85,7 +85,7 @@ class ImageCacheTestCase(object):
"""
self._setup_fixture_file()
buff = six.BytesIO()
buff = io.BytesIO()
with self.cache.open_for_read(1) as cache_file:
for chunk in cache_file:
buff.write(chunk)
@ -119,7 +119,7 @@ class ImageCacheTestCase(object):
self.assertFalse(self.cache.is_cached(image_id))
for image_id in (1, 2):
FIXTURE_FILE = six.BytesIO(FIXTURE_DATA)
FIXTURE_FILE = io.BytesIO(FIXTURE_DATA)
self.assertTrue(self.cache.cache_image_file(image_id,
FIXTURE_FILE))
@ -189,21 +189,21 @@ class ImageCacheTestCase(object):
# prune. We should see only 5 images left after pruning, and the
# images that are least recently accessed should be the ones pruned...
for x in range(10):
FIXTURE_FILE = six.BytesIO(FIXTURE_DATA)
FIXTURE_FILE = io.BytesIO(FIXTURE_DATA)
self.assertTrue(self.cache.cache_image_file(x, FIXTURE_FILE))
self.assertEqual(10 * units.Ki, self.cache.get_cache_size())
# OK, hit the images that are now cached...
for x in range(10):
buff = six.BytesIO()
buff = io.BytesIO()
with self.cache.open_for_read(x) as cache_file:
for chunk in cache_file:
buff.write(chunk)
# Add a new image to cache.
# This is specifically to test the bug: 1438564
FIXTURE_FILE = six.BytesIO(FIXTURE_DATA)
FIXTURE_FILE = io.BytesIO(FIXTURE_DATA)
self.assertTrue(self.cache.cache_image_file(99, FIXTURE_FILE))
self.cache.prune()
@ -231,13 +231,13 @@ class ImageCacheTestCase(object):
"""
self.assertEqual(0, self.cache.get_cache_size())
FIXTURE_FILE = six.BytesIO(FIXTURE_DATA)
FIXTURE_FILE = io.BytesIO(FIXTURE_DATA)
self.assertTrue(self.cache.cache_image_file('xxx', FIXTURE_FILE))
self.assertEqual(1024, self.cache.get_cache_size())
# OK, hit the image that is now cached...
buff = six.BytesIO()
buff = io.BytesIO()
with self.cache.open_for_read('xxx') as cache_file:
for chunk in cache_file:
buff.write(chunk)
@ -257,7 +257,7 @@ class ImageCacheTestCase(object):
self.assertFalse(self.cache.is_cached(1))
self.assertFalse(self.cache.is_queued(1))
FIXTURE_FILE = six.BytesIO(FIXTURE_DATA)
FIXTURE_FILE = io.BytesIO(FIXTURE_DATA)
self.assertTrue(self.cache.queue_image(1))

View File

@ -12,10 +12,11 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import io
from unittest import mock
import fixtures
from six.moves import StringIO
from glance.cmd import manage
from glance.common import exception
@ -163,7 +164,7 @@ class TestManage(TestManageBase):
def setUp(self):
super(TestManage, self).setUp()
self.db = manage.DbCommands()
self.output = StringIO()
self.output = io.StringIO()
self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.output))
def test_db_complex_password(self):

View File

@ -12,6 +12,8 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import io
from unittest import mock
import uuid
@ -19,7 +21,6 @@ from cursive import exception as cursive_exception
import glance_store
from glance_store._drivers import filesystem
from oslo_config import cfg
import six
from six.moves import http_client as http
import webob
@ -683,7 +684,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
request.headers['Content-Type'] = 'application/octet-stream'
# If we use body_file, webob assumes we want to do a chunked upload,
# ignoring the Content-Length header
request.body_file = six.StringIO('YYY')
request.body_file = io.StringIO('YYY')
output = self.deserializer.upload(request)
data = output.pop('data')
self.assertEqual('YYY', data.read())
@ -693,7 +694,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
def test_upload_chunked_with_content_length(self):
request = unit_test_utils.get_fake_request()
request.headers['Content-Type'] = 'application/octet-stream'
request.body_file = six.BytesIO(b'YYY')
request.body_file = io.BytesIO(b'YYY')
# The deserializer shouldn't care if the Content-Length is
# set when the user is attempting to send chunked data.
request.headers['Content-Length'] = 3
@ -734,7 +735,7 @@ class TestImageDataDeserializer(test_utils.BaseTestCase):
req = unit_test_utils.get_fake_request()
req.headers['Content-Type'] = 'application/octet-stream'
req.headers['Content-Length'] = 4
req.body_file = six.BytesIO(b'YYYY')
req.body_file = io.BytesIO(b'YYYY')
output = self.deserializer.stage(req)
data = output.pop('data')
self.assertEqual(b'YYYY', data.read())

View File

@ -17,6 +17,7 @@
import errno
import functools
import io
import os
import shlex
import shutil
@ -35,7 +36,6 @@ from oslo_log.fixture import logging_error as log_fixture
from oslo_log import log
from oslo_utils import timeutils
from oslo_utils import units
import six
from six.moves import BaseHTTPServer
from six.moves import http_client as http
import testtools
@ -619,7 +619,7 @@ class FakeHTTPResponse(object):
def __init__(self, status=http.OK, headers=None, data=None,
*args, **kwargs):
data = data or b'I am a teapot, short and stout\n'
self.data = six.BytesIO(data)
self.data = io.BytesIO(data)
self.read = self.data.read
self.status = status
self.headers = headers or {'content-length': len(data)}