Browse Source

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
tags/2014.1.b3
He Yongli 5 years ago
parent
commit
01903933db

+ 2
- 2
glance/tests/functional/store/__init__.py View File

@@ -13,10 +13,10 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 
16
-import StringIO
17 16
 import uuid
18 17
 
19 18
 from oslo.config import cfg
19
+import six
20 20
 
21 21
 #NOTE(bcwaldon): importing this to get the default_store option
22 22
 import glance.api.v1.images
@@ -76,7 +76,7 @@ class BaseTestCase(object):
76 76
         store = self.get_store()
77 77
 
78 78
         image_id = str(uuid.uuid4())
79
-        image_data = StringIO.StringIO('XXX')
79
+        image_data = six.StringIO('XXX')
80 80
         image_checksum = 'bc9189406be84ec297464a514221406d'
81 81
         try:
82 82
             uri, add_size, add_checksum, _ = store.add(image_id, image_data, 3)

+ 2
- 2
glance/tests/functional/store/test_rbd.py View File

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

+ 5
- 5
glance/tests/functional/store/test_swift.py View File

@@ -26,10 +26,10 @@ import os
26 26
 import os.path
27 27
 import random
28 28
 import string
29
-import StringIO
30 29
 import uuid
31 30
 
32 31
 import oslo.config.cfg
32
+import six
33 33
 import six.moves.urllib.parse as urlparse
34 34
 import testtools
35 35
 
@@ -194,7 +194,7 @@ class TestSwiftStore(store_tests.BaseTestCase, testtools.TestCase):
194 194
         store = self.get_store()
195 195
         image_id = str(uuid.uuid4())
196 196
         image_size = 5242880  # 5 MB
197
-        image_data = StringIO.StringIO('X' * image_size)
197
+        image_data = six.StringIO('X' * image_size)
198 198
         image_checksum = 'eb7f8c3716b9f059cee7617a4ba9d0d3'
199 199
         uri, add_size, add_checksum, _ = store.add(image_id,
200 200
                                                    image_data,
@@ -276,7 +276,7 @@ class TestSwiftStore(store_tests.BaseTestCase, testtools.TestCase):
276 276
                                         non_image_obj)
277 277
 
278 278
         # Simulate exceeding 'image_size_cap' setting
279
-        image_data = StringIO.StringIO('X' * image_size)
279
+        image_data = six.StringIO('X' * image_size)
280 280
         image_data = common_utils.LimitingReader(image_data, image_size - 1)
281 281
         image_id = str(uuid.uuid4())
282 282
         self.assertRaises(exception.ImageSizeLimitExceeded,
@@ -455,7 +455,7 @@ class TestSwiftStore(store_tests.BaseTestCase, testtools.TestCase):
455 455
         store = self.get_store(context=context)
456 456
 
457 457
         image_id = str(uuid.uuid4())
458
-        image_data = StringIO.StringIO('XXX')
458
+        image_data = six.StringIO('XXX')
459 459
         uri, _, _, _ = store.add(image_id, image_data, 3)
460 460
 
461 461
         location = glance.store.location.Location(
@@ -512,7 +512,7 @@ class TestSwiftStore(store_tests.BaseTestCase, testtools.TestCase):
512 512
         store = self.get_store(context=context)
513 513
 
514 514
         image_id = str(uuid.uuid4())
515
-        image_data = StringIO.StringIO('data')
515
+        image_data = six.StringIO('data')
516 516
         uri, _, _, _ = store.add(image_id, image_data, 4)
517 517
 
518 518
         location = glance.store.location.Location(

+ 3
- 2
glance/tests/unit/api/test_cmd.py View File

@@ -11,9 +11,10 @@
11 11
 #    under the License.
12 12
 
13 13
 import mock
14
-import StringIO
15 14
 import sys
16 15
 
16
+import six
17
+
17 18
 import glance.cmd.api
18 19
 import glance.cmd.cache_cleaner
19 20
 import glance.cmd.cache_pruner
@@ -41,7 +42,7 @@ class TestGlanceApiCmd(test_utils.BaseTestCase):
41 42
         super(TestGlanceApiCmd, self).setUp()
42 43
         self.__argv_backup = sys.argv
43 44
         sys.argv = ['glance-api']
44
-        self.stderr = StringIO.StringIO()
45
+        self.stderr = six.StringIO()
45 46
         sys.stderr = self.stderr
46 47
 
47 48
         self.stubs.Set(glance.common.config, 'load_paste_app',

+ 5
- 5
glance/tests/unit/common/test_utils.py View File

@@ -14,9 +14,9 @@
14 14
 #    under the License.
15 15
 
16 16
 import os
17
-import StringIO
18 17
 import tempfile
19 18
 
19
+import six
20 20
 import webob
21 21
 
22 22
 from glance.common import exception
@@ -77,14 +77,14 @@ class TestUtils(test_utils.BaseTestCase):
77 77
         """Ensure limiting reader class accesses all bytes of file"""
78 78
         BYTES = 1024
79 79
         bytes_read = 0
80
-        data = StringIO.StringIO("*" * BYTES)
80
+        data = six.StringIO("*" * BYTES)
81 81
         for chunk in utils.LimitingReader(data, BYTES):
82 82
             bytes_read += len(chunk)
83 83
 
84 84
         self.assertEqual(bytes_read, BYTES)
85 85
 
86 86
         bytes_read = 0
87
-        data = StringIO.StringIO("*" * BYTES)
87
+        data = six.StringIO("*" * BYTES)
88 88
         reader = utils.LimitingReader(data, BYTES)
89 89
         byte = reader.read(1)
90 90
         while len(byte) != 0:
@@ -99,7 +99,7 @@ class TestUtils(test_utils.BaseTestCase):
99 99
 
100 100
         def _consume_all_iter():
101 101
             bytes_read = 0
102
-            data = StringIO.StringIO("*" * BYTES)
102
+            data = six.StringIO("*" * BYTES)
103 103
             for chunk in utils.LimitingReader(data, BYTES - 1):
104 104
                 bytes_read += len(chunk)
105 105
 
@@ -107,7 +107,7 @@ class TestUtils(test_utils.BaseTestCase):
107 107
 
108 108
         def _consume_all_read():
109 109
             bytes_read = 0
110
-            data = StringIO.StringIO("*" * BYTES)
110
+            data = six.StringIO("*" * BYTES)
111 111
             reader = utils.LimitingReader(data, BYTES - 1)
112 112
             byte = reader.read(1)
113 113
             while len(byte) != 0:

+ 13
- 13
glance/tests/unit/test_filesystem_store.py View File

@@ -20,12 +20,12 @@ import errno
20 20
 import hashlib
21 21
 import json
22 22
 import os
23
-import StringIO
24 23
 import uuid
25 24
 
26 25
 import fixtures
27 26
 import mox
28 27
 from oslo.config import cfg
28
+import six
29 29
 
30 30
 from glance.common import exception
31 31
 from glance.openstack.common import units
@@ -157,7 +157,7 @@ class TestStore(base.IsolatedUnitTest):
157 157
         # First add an image...
158 158
         image_id = str(uuid.uuid4())
159 159
         file_contents = "chunk00000remainder"
160
-        image_file = StringIO.StringIO(file_contents)
160
+        image_file = six.StringIO(file_contents)
161 161
 
162 162
         location, size, checksum, _ = self.store.add(image_id,
163 163
                                                      image_file,
@@ -198,7 +198,7 @@ class TestStore(base.IsolatedUnitTest):
198 198
         expected_checksum = hashlib.md5(expected_file_contents).hexdigest()
199 199
         expected_location = "file://%s/%s" % (self.test_dir,
200 200
                                               expected_image_id)
201
-        image_file = StringIO.StringIO(expected_file_contents)
201
+        image_file = six.StringIO(expected_file_contents)
202 202
 
203 203
         location, size, checksum, _ = self.store.add(expected_image_id,
204 204
                                                      image_file,
@@ -239,7 +239,7 @@ class TestStore(base.IsolatedUnitTest):
239 239
         expected_checksum = hashlib.md5(expected_file_contents).hexdigest()
240 240
         expected_location = "file://%s/%s" % (store_map[1],
241 241
                                               expected_image_id)
242
-        image_file = StringIO.StringIO(expected_file_contents)
242
+        image_file = six.StringIO(expected_file_contents)
243 243
 
244 244
         location, size, checksum, _ = self.store.add(expected_image_id,
245 245
                                                      image_file,
@@ -283,7 +283,7 @@ class TestStore(base.IsolatedUnitTest):
283 283
         expected_image_id = str(uuid.uuid4())
284 284
         expected_file_size = 5 * units.Ki  # 5K
285 285
         expected_file_contents = "*" * expected_file_size
286
-        image_file = StringIO.StringIO(expected_file_contents)
286
+        image_file = six.StringIO(expected_file_contents)
287 287
 
288 288
         self.assertRaises(exception.StorageFull, self.store.add,
289 289
                           expected_image_id, image_file, expected_file_size)
@@ -299,7 +299,7 @@ class TestStore(base.IsolatedUnitTest):
299 299
             json.dump(in_metadata, fptr)
300 300
         expected_file_size = 10
301 301
         expected_file_contents = "*" * expected_file_size
302
-        image_file = StringIO.StringIO(expected_file_contents)
302
+        image_file = six.StringIO(expected_file_contents)
303 303
 
304 304
         location, size, checksum, metadata = self.store.add(expected_image_id,
305 305
                                                             image_file,
@@ -318,7 +318,7 @@ class TestStore(base.IsolatedUnitTest):
318 318
             json.dump(in_metadata, fptr)
319 319
         expected_file_size = 10
320 320
         expected_file_contents = "*" * expected_file_size
321
-        image_file = StringIO.StringIO(expected_file_contents)
321
+        image_file = six.StringIO(expected_file_contents)
322 322
 
323 323
         location, size, checksum, metadata = self.store.add(expected_image_id,
324 324
                                                             image_file,
@@ -334,7 +334,7 @@ class TestStore(base.IsolatedUnitTest):
334 334
         self.config(filesystem_store_metadata_file=jsonfilename)
335 335
         expected_file_size = 10
336 336
         expected_file_contents = "*" * expected_file_size
337
-        image_file = StringIO.StringIO(expected_file_contents)
337
+        image_file = six.StringIO(expected_file_contents)
338 338
 
339 339
         location, size, checksum, metadata = self.store.add(expected_image_id,
340 340
                                                             image_file,
@@ -351,12 +351,12 @@ class TestStore(base.IsolatedUnitTest):
351 351
         image_id = str(uuid.uuid4())
352 352
         file_size = 5 * units.Ki  # 5K
353 353
         file_contents = "*" * file_size
354
-        image_file = StringIO.StringIO(file_contents)
354
+        image_file = six.StringIO(file_contents)
355 355
 
356 356
         location, size, checksum, _ = self.store.add(image_id,
357 357
                                                      image_file,
358 358
                                                      file_size)
359
-        image_file = StringIO.StringIO("nevergonnamakeit")
359
+        image_file = six.StringIO("nevergonnamakeit")
360 360
         self.assertRaises(exception.Duplicate,
361 361
                           self.store.add,
362 362
                           image_id, image_file, 0)
@@ -367,7 +367,7 @@ class TestStore(base.IsolatedUnitTest):
367 367
         file_size = 5 * units.Ki  # 5K
368 368
         file_contents = "*" * file_size
369 369
         path = os.path.join(self.test_dir, image_id)
370
-        image_file = StringIO.StringIO(file_contents)
370
+        image_file = six.StringIO(file_contents)
371 371
 
372 372
         m = mox.Mox()
373 373
         m.StubOutWithMock(__builtin__, 'open')
@@ -424,7 +424,7 @@ class TestStore(base.IsolatedUnitTest):
424 424
         file_size = 5 * units.Ki  # 5K
425 425
         file_contents = "*" * file_size
426 426
         path = os.path.join(self.test_dir, image_id)
427
-        image_file = StringIO.StringIO(file_contents)
427
+        image_file = six.StringIO(file_contents)
428 428
 
429 429
         def fake_Error(size):
430 430
             raise AttributeError()
@@ -444,7 +444,7 @@ class TestStore(base.IsolatedUnitTest):
444 444
         image_id = str(uuid.uuid4())
445 445
         file_size = 5 * units.Ki  # 5K
446 446
         file_contents = "*" * file_size
447
-        image_file = StringIO.StringIO(file_contents)
447
+        image_file = six.StringIO(file_contents)
448 448
 
449 449
         location, size, checksum, _ = self.store.add(image_id,
450 450
                                                      image_file,

+ 4
- 4
glance/tests/unit/test_glance_replicator.py View File

@@ -14,12 +14,12 @@
14 14
 
15 15
 import copy
16 16
 import os
17
-import StringIO
18 17
 import sys
19 18
 import UserDict
20 19
 import uuid
21 20
 
22 21
 import fixtures
22
+import six
23 23
 
24 24
 from glance.cmd import replicator as glance_replicator
25 25
 from glance.openstack.common import jsonutils
@@ -106,7 +106,7 @@ class FakeHTTPConnection(object):
106 106
     def getresponse(self):
107 107
         class FakeResponse(object):
108 108
             def __init__(self, (code, body, headers)):
109
-                self.body = StringIO.StringIO(body)
109
+                self.body = six.StringIO(body)
110 110
                 self.headers = headers
111 111
                 self.status = code
112 112
 
@@ -241,7 +241,7 @@ class ImageServiceTestCase(test_utils.BaseTestCase):
241 241
 class FakeHttpResponse(object):
242 242
     def __init__(self, headers, data):
243 243
         self.headers = headers
244
-        self.data = StringIO.StringIO(data)
244
+        self.data = six.StringIO(data)
245 245
 
246 246
     def getheaders(self):
247 247
         return self.headers
@@ -339,7 +339,7 @@ class ReplicationCommandsTestCase(test_utils.BaseTestCase):
339 339
 
340 340
         stdout = sys.stdout
341 341
         orig_img_service = glance_replicator.get_image_service
342
-        sys.stdout = StringIO.StringIO()
342
+        sys.stdout = six.StringIO()
343 343
         try:
344 344
             glance_replicator.get_image_service = get_image_service
345 345
             glance_replicator.replication_size(options, args)

+ 2
- 3
glance/tests/unit/test_gridfs_store.py View File

@@ -13,8 +13,7 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 
16
-import StringIO
17
-
16
+import six
18 17
 import stubout
19 18
 
20 19
 from glance.common import exception
@@ -91,7 +90,7 @@ class TestStore(base.StoreClearingUnitTest):
91 90
         self.assertRaises(exception.ImageSizeLimitExceeded,
92 91
                           self.store.add,
93 92
                           'fake_image_id',
94
-                          utils.LimitingReader(StringIO.StringIO('xx'), 1),
93
+                          utils.LimitingReader(six.StringIO('xx'), 1),
95 94
                           2)
96 95
         self.assertEqual(self.store.fs.called_commands,
97 96
                          ['exists', 'put', 'delete'])

+ 10
- 10
glance/tests/unit/test_image_cache.py View File

@@ -17,11 +17,11 @@ from contextlib import contextmanager
17 17
 import datetime
18 18
 import hashlib
19 19
 import os
20
-import StringIO
21 20
 import tempfile
22 21
 import time
23 22
 
24 23
 import fixtures
24
+import six
25 25
 from six.moves import xrange
26 26
 import stubout
27 27
 
@@ -43,7 +43,7 @@ FIXTURE_DATA = '*' * FIXTURE_LENGTH
43 43
 class ImageCacheTestCase(object):
44 44
 
45 45
     def _setup_fixture_file(self):
46
-        FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA)
46
+        FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
47 47
 
48 48
         self.assertFalse(self.cache.is_cached(1))
49 49
 
@@ -66,7 +66,7 @@ class ImageCacheTestCase(object):
66 66
         """
67 67
         self._setup_fixture_file()
68 68
 
69
-        buff = StringIO.StringIO()
69
+        buff = six.StringIO()
70 70
         with self.cache.open_for_read(1) as cache_file:
71 71
             for chunk in cache_file:
72 72
                 buff.write(chunk)
@@ -80,7 +80,7 @@ class ImageCacheTestCase(object):
80 80
         """
81 81
         self._setup_fixture_file()
82 82
 
83
-        buff = StringIO.StringIO()
83
+        buff = six.StringIO()
84 84
         with self.cache.open_for_read(1) as cache_file:
85 85
             for chunk in cache_file:
86 86
                 buff.write(chunk)
@@ -114,7 +114,7 @@ class ImageCacheTestCase(object):
114 114
             self.assertFalse(self.cache.is_cached(image_id))
115 115
 
116 116
         for image_id in (1, 2):
117
-            FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA)
117
+            FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
118 118
             self.assertTrue(self.cache.cache_image_file(image_id,
119 119
                                                         FIXTURE_FILE))
120 120
 
@@ -183,7 +183,7 @@ class ImageCacheTestCase(object):
183 183
         # pruning, and the images that are least recently accessed
184 184
         # should be the ones pruned...
185 185
         for x in xrange(10):
186
-            FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA)
186
+            FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
187 187
             self.assertTrue(self.cache.cache_image_file(x,
188 188
                                                         FIXTURE_FILE))
189 189
 
@@ -191,7 +191,7 @@ class ImageCacheTestCase(object):
191 191
 
192 192
         # OK, hit the images that are now cached...
193 193
         for x in xrange(10):
194
-            buff = StringIO.StringIO()
194
+            buff = six.StringIO()
195 195
             with self.cache.open_for_read(x) as cache_file:
196 196
                 for chunk in cache_file:
197 197
                     buff.write(chunk)
@@ -216,13 +216,13 @@ class ImageCacheTestCase(object):
216 216
         """
217 217
         self.assertEqual(0, self.cache.get_cache_size())
218 218
 
219
-        FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA)
219
+        FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
220 220
         self.assertTrue(self.cache.cache_image_file('xxx', FIXTURE_FILE))
221 221
 
222 222
         self.assertEqual(1024, self.cache.get_cache_size())
223 223
 
224 224
         # OK, hit the image that is now cached...
225
-        buff = StringIO.StringIO()
225
+        buff = six.StringIO()
226 226
         with self.cache.open_for_read('xxx') as cache_file:
227 227
             for chunk in cache_file:
228 228
                 buff.write(chunk)
@@ -242,7 +242,7 @@ class ImageCacheTestCase(object):
242 242
         self.assertFalse(self.cache.is_cached(1))
243 243
         self.assertFalse(self.cache.is_queued(1))
244 244
 
245
-        FIXTURE_FILE = StringIO.StringIO(FIXTURE_DATA)
245
+        FIXTURE_FILE = six.StringIO(FIXTURE_DATA)
246 246
 
247 247
         self.assertTrue(self.cache.queue_image(1))
248 248
 

+ 2
- 3
glance/tests/unit/test_rbd_store.py View File

@@ -13,9 +13,8 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 
16
-import StringIO
17
-
18 16
 import mock
17
+import six
19 18
 
20 19
 from glance.common import exception
21 20
 from glance.common import utils
@@ -43,7 +42,7 @@ class TestStore(base.StoreClearingUnitTest):
43 42
         self.location = StoreLocation(self.store_specs)
44 43
         # Provide enough data to get more than one chunk iteration.
45 44
         self.data_len = 3 * units.Ki
46
-        self.data_iter = StringIO.StringIO('*' * self.data_len)
45
+        self.data_iter = six.StringIO('*' * self.data_len)
47 46
 
48 47
     def test_add_w_image_size_zero(self):
49 48
         """Assert that correct size is returned even though 0 was provided."""

+ 7
- 7
glance/tests/unit/test_s3_store.py View File

@@ -16,10 +16,10 @@
16 16
 """Tests the S3 backend store"""
17 17
 
18 18
 import hashlib
19
-import StringIO
20 19
 import uuid
21 20
 
22 21
 import boto.s3.connection
22
+import six
23 23
 import stubout
24 24
 
25 25
 from glance.common import exception
@@ -80,7 +80,7 @@ def stub_out_s3(stubs):
80 80
             return checksum_hex, None
81 81
 
82 82
         def set_contents_from_file(self, fp, replace=False, **kwargs):
83
-            self.data = StringIO.StringIO()
83
+            self.data = six.StringIO()
84 84
             for bytes in fp:
85 85
                 self.data.write(bytes)
86 86
             self.size = self.data.len
@@ -122,7 +122,7 @@ def stub_out_s3(stubs):
122 122
     fixture_buckets = {'glance': FakeBucket('glance')}
123 123
     b = fixture_buckets['glance']
124 124
     k = b.new_key(FAKE_UUID)
125
-    k.set_contents_from_file(StringIO.StringIO("*" * FIVE_KB))
125
+    k.set_contents_from_file(six.StringIO("*" * FIVE_KB))
126 126
 
127 127
     def fake_connection_constructor(self, *args, **kwargs):
128 128
         host = kwargs.get('host')
@@ -237,7 +237,7 @@ class TestStore(base.StoreClearingUnitTest):
237 237
             S3_CONF['s3_store_host'],
238 238
             S3_CONF['s3_store_bucket'],
239 239
             expected_image_id)
240
-        image_s3 = StringIO.StringIO(expected_s3_contents)
240
+        image_s3 = six.StringIO(expected_s3_contents)
241 241
 
242 242
         location, size, checksum, _ = self.store.add(expected_image_id,
243 243
                                                      image_s3,
@@ -249,7 +249,7 @@ class TestStore(base.StoreClearingUnitTest):
249 249
 
250 250
         loc = get_location_from_uri(expected_location)
251 251
         (new_image_s3, new_image_size) = self.store.get(loc)
252
-        new_image_contents = StringIO.StringIO()
252
+        new_image_contents = six.StringIO()
253 253
         for chunk in new_image_s3:
254 254
             new_image_contents.write(chunk)
255 255
         new_image_s3_size = new_image_contents.len
@@ -285,7 +285,7 @@ class TestStore(base.StoreClearingUnitTest):
285 285
                 new_conf['s3_store_host'],
286 286
                 new_conf['s3_store_bucket'],
287 287
                 expected_image_id)
288
-            image_s3 = StringIO.StringIO(expected_s3_contents)
288
+            image_s3 = six.StringIO(expected_s3_contents)
289 289
 
290 290
             self.config(**new_conf)
291 291
             self.store = Store()
@@ -310,7 +310,7 @@ class TestStore(base.StoreClearingUnitTest):
310 310
         Tests that adding an image with an existing identifier
311 311
         raises an appropriate exception
312 312
         """
313
-        image_s3 = StringIO.StringIO("nevergonnamakeit")
313
+        image_s3 = six.StringIO("nevergonnamakeit")
314 314
         self.assertRaises(exception.Duplicate,
315 315
                           self.store.add,
316 316
                           FAKE_UUID, image_s3, 0)

+ 2
- 3
glance/tests/unit/test_sheepdog_store.py View File

@@ -13,8 +13,7 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 
16
-import StringIO
17
-
16
+import six
18 17
 import stubout
19 18
 
20 19
 from glance.common import exception
@@ -55,6 +54,6 @@ class TestStore(base.StoreClearingUnitTest):
55 54
         self.assertRaises(exception.ImageSizeLimitExceeded,
56 55
                           self.store.add,
57 56
                           'fake_image_id',
58
-                          utils.LimitingReader(StringIO.StringIO('xx'), 1),
57
+                          utils.LimitingReader(six.StringIO('xx'), 1),
59 58
                           2)
60 59
         self.assertEqual(called_commands, ['list -r', 'create', 'delete'])

+ 14
- 14
glance/tests/unit/test_swift_store.py View File

@@ -18,11 +18,11 @@
18 18
 import hashlib
19 19
 import httplib
20 20
 import mock
21
-import StringIO
22 21
 import tempfile
23 22
 import uuid
24 23
 
25 24
 from oslo.config import cfg
25
+import six
26 26
 import six.moves.urllib.parse as urlparse
27 27
 import stubout
28 28
 import swiftclient
@@ -69,7 +69,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
69 69
         }
70 70
     }
71 71
     fixture_objects = {'glance/%s' % FAKE_UUID:
72
-                       StringIO.StringIO("*" * FIVE_KB)}
72
+                       six.StringIO("*" * FIVE_KB)}
73 73
 
74 74
     def fake_head_container(url, token, container, **kwargs):
75 75
         if container not in fixture_containers:
@@ -99,7 +99,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
99 99
                                                 'etag': etag}
100 100
                 return etag
101 101
             if hasattr(contents, 'read'):
102
-                fixture_object = StringIO.StringIO()
102
+                fixture_object = six.StringIO()
103 103
                 chunk = contents.read(CHUNKSIZE)
104 104
                 checksum = hashlib.md5()
105 105
                 while chunk:
@@ -108,7 +108,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
108 108
                     chunk = contents.read(CHUNKSIZE)
109 109
                 etag = checksum.hexdigest()
110 110
             else:
111
-                fixture_object = StringIO.StringIO(contents)
111
+                fixture_object = six.StringIO(contents)
112 112
                 etag = hashlib.md5(fixture_object.getvalue()).hexdigest()
113 113
             read_len = fixture_object.len
114 114
             if read_len > MAX_SWIFT_OBJECT_SIZE:
@@ -149,7 +149,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
149 149
             chunk_keys = sorted([k for k in fixture_headers.keys()
150 150
                                  if k.startswith(fixture_key) and
151 151
                                  k != fixture_key])
152
-            result = StringIO.StringIO()
152
+            result = six.StringIO()
153 153
             for key in chunk_keys:
154 154
                 result.write(fixture_objects[key].getvalue())
155 155
         else:
@@ -157,7 +157,7 @@ def stub_out_swiftclient(stubs, swift_store_auth_version):
157 157
 
158 158
         if byte_range is not None:
159 159
             start = int(byte_range.split('=')[1].strip('-'))
160
-            result = StringIO.StringIO(result.getvalue()[start:])
160
+            result = six.StringIO(result.getvalue()[start:])
161 161
             fixture_headers[fixture_key]['content-length'] = len(
162 162
                 result.getvalue())
163 163
 
@@ -321,7 +321,7 @@ class SwiftTests(object):
321 321
         loc = 'swift+https://%s:key@localhost:8080/glance/%s'
322 322
         expected_location = loc % (self.swift_store_user,
323 323
                                    expected_image_id)
324
-        image_swift = StringIO.StringIO(expected_swift_contents)
324
+        image_swift = six.StringIO(expected_swift_contents)
325 325
 
326 326
         global SWIFT_PUT_OBJECT_CALLS
327 327
         SWIFT_PUT_OBJECT_CALLS = 0
@@ -378,7 +378,7 @@ class SwiftTests(object):
378 378
             expected_checksum = \
379 379
                 hashlib.md5(expected_swift_contents).hexdigest()
380 380
 
381
-            image_swift = StringIO.StringIO(expected_swift_contents)
381
+            image_swift = six.StringIO(expected_swift_contents)
382 382
 
383 383
             global SWIFT_PUT_OBJECT_CALLS
384 384
             SWIFT_PUT_OBJECT_CALLS = 0
@@ -410,7 +410,7 @@ class SwiftTests(object):
410 410
                     swift_store_container='noexist')
411 411
         self.store = Store()
412 412
 
413
-        image_swift = StringIO.StringIO("nevergonnamakeit")
413
+        image_swift = six.StringIO("nevergonnamakeit")
414 414
 
415 415
         global SWIFT_PUT_OBJECT_CALLS
416 416
         SWIFT_PUT_OBJECT_CALLS = 0
@@ -440,7 +440,7 @@ class SwiftTests(object):
440 440
         loc = 'swift+https://%s:key@localhost:8080/noexist/%s'
441 441
         expected_location = loc % (self.swift_store_user,
442 442
                                    expected_image_id)
443
-        image_swift = StringIO.StringIO(expected_swift_contents)
443
+        image_swift = six.StringIO(expected_swift_contents)
444 444
 
445 445
         global SWIFT_PUT_OBJECT_CALLS
446 446
         SWIFT_PUT_OBJECT_CALLS = 0
@@ -479,7 +479,7 @@ class SwiftTests(object):
479 479
         loc = 'swift+https://%s:key@localhost:8080/glance/%s'
480 480
         expected_location = loc % (self.swift_store_user,
481 481
                                    expected_image_id)
482
-        image_swift = StringIO.StringIO(expected_swift_contents)
482
+        image_swift = six.StringIO(expected_swift_contents)
483 483
 
484 484
         global SWIFT_PUT_OBJECT_CALLS
485 485
         SWIFT_PUT_OBJECT_CALLS = 0
@@ -532,7 +532,7 @@ class SwiftTests(object):
532 532
         loc = 'swift+https://%s:key@localhost:8080/glance/%s'
533 533
         expected_location = loc % (self.swift_store_user,
534 534
                                    expected_image_id)
535
-        image_swift = StringIO.StringIO(expected_swift_contents)
535
+        image_swift = six.StringIO(expected_swift_contents)
536 536
 
537 537
         global SWIFT_PUT_OBJECT_CALLS
538 538
         SWIFT_PUT_OBJECT_CALLS = 0
@@ -578,7 +578,7 @@ class SwiftTests(object):
578 578
         Tests that adding an image with an existing identifier
579 579
         raises an appropriate exception
580 580
         """
581
-        image_swift = StringIO.StringIO("nevergonnamakeit")
581
+        image_swift = six.StringIO("nevergonnamakeit")
582 582
         self.assertRaises(exception.Duplicate,
583 583
                           self.store.add,
584 584
                           FAKE_UUID, image_swift, 0)
@@ -597,7 +597,7 @@ class SwiftTests(object):
597 597
             except Exception:
598 598
                 pass
599 599
 
600
-        image_swift = StringIO.StringIO(swift_contents)
600
+        image_swift = six.StringIO(swift_contents)
601 601
         connection.put_object.side_effect = exception.ClientConnectionError
602 602
         self.store._delete_stale_chunks = fake_delete_chunk
603 603
 

+ 2
- 2
glance/tests/unit/test_vmware_store.py View File

@@ -16,10 +16,10 @@
16 16
 """Tests the VMware Datastore backend store"""
17 17
 
18 18
 import hashlib
19
-import StringIO
20 19
 import uuid
21 20
 
22 21
 import mock
22
+import six
23 23
 
24 24
 from glance.common import exception
25 25
 from glance.openstack.common import units
@@ -167,7 +167,7 @@ class TestStore(base.StoreClearingUnitTest):
167 167
                 expected_image_id,
168 168
                 VMWARE_DATASTORE_CONF['vmware_datacenter_path'],
169 169
                 VMWARE_DATASTORE_CONF['vmware_datastore_name'])
170
-            image = StringIO.StringIO(expected_contents)
170
+            image = six.StringIO(expected_contents)
171 171
             with mock.patch('httplib.HTTPConnection') as HttpConn:
172 172
                 HttpConn.return_value = FakeHTTPConnection()
173 173
                 location, size, checksum, _ = self.store.add(expected_image_id,

+ 5
- 5
glance/tests/unit/v1/test_api.py View File

@@ -18,12 +18,12 @@
18 18
 import copy
19 19
 import datetime
20 20
 import hashlib
21
-import StringIO
22 21
 import uuid
23 22
 
24 23
 import mock
25 24
 from oslo.config import cfg
26 25
 import routes
26
+import six
27 27
 import webob
28 28
 
29 29
 import glance.api
@@ -532,7 +532,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
532 532
         req = webob.Request.blank("/images")
533 533
         req.method = 'POST'
534 534
 
535
-        req.body_file = StringIO.StringIO('X' * (CONF.image_size_cap + 1))
535
+        req.body_file = six.StringIO('X' * (CONF.image_size_cap + 1))
536 536
         for k, v in fixture_headers.iteritems():
537 537
             req.headers[k] = v
538 538
 
@@ -979,7 +979,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
979 979
         req = webob.Request.blank("/images")
980 980
         req.method = 'POST'
981 981
 
982
-        req.body_file = StringIO.StringIO('X' * (CONF.image_size_cap))
982
+        req.body_file = six.StringIO('X' * (CONF.image_size_cap))
983 983
         for k, v in fixture_headers.iteritems():
984 984
             req.headers[k] = v
985 985
 
@@ -1034,7 +1034,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
1034 1034
         req.headers['transfer-encoding'] = 'chunked'
1035 1035
         req.headers['x-image-disk-format'] = 'vhd'
1036 1036
         req.headers['x-image-container-format'] = 'ovf'
1037
-        req.body_file = StringIO.StringIO('X' * (CONF.image_size_cap))
1037
+        req.body_file = six.StringIO('X' * (CONF.image_size_cap))
1038 1038
         res = req.get_response(self.api)
1039 1039
         self.assertEqual(res.status_int, 403)
1040 1040
 
@@ -1655,7 +1655,7 @@ class TestGlanceAPI(base.IsolatedUnitTest):
1655 1655
         req = webob.Request.blank("/images/%s" % image_id)
1656 1656
         req.method = 'PUT'
1657 1657
 
1658
-        req.body_file = StringIO.StringIO('X' * (CONF.image_size_cap + 1))
1658
+        req.body_file = six.StringIO('X' * (CONF.image_size_cap + 1))
1659 1659
         for k, v in fixture_headers.iteritems():
1660 1660
             req.headers[k] = v
1661 1661
 

+ 3
- 3
glance/tests/unit/v2/test_image_data_resource.py View File

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

+ 2
- 2
glance/tests/utils.py View File

@@ -21,12 +21,12 @@ import os
21 21
 import shlex
22 22
 import shutil
23 23
 import socket
24
-import StringIO
25 24
 import subprocess
26 25
 import uuid
27 26
 
28 27
 import fixtures
29 28
 from oslo.config import cfg
29
+import six
30 30
 import stubout
31 31
 import testtools
32 32
 import webob
@@ -508,7 +508,7 @@ class FakeAuthMiddleware(wsgi.Middleware):
508 508
 class FakeHTTPResponse(object):
509 509
     def __init__(self, status=200, headers=None, data=None, *args, **kwargs):
510 510
         data = data or 'I am a teapot, short and stout\n'
511
-        self.data = StringIO.StringIO(data)
511
+        self.data = six.StringIO(data)
512 512
         self.read = self.data.read
513 513
         self.status = status
514 514
         self.headers = headers or {'content-length': len(data)}

Loading…
Cancel
Save