diff options
author | Tim Burke <tim.burke@gmail.com> | 2019-03-06 16:17:41 -0800 |
---|---|---|
committer | Tim Burke <tim.burke@gmail.com> | 2019-03-06 16:21:31 -0800 |
commit | d3029fe4b4cc6fd47cb2032a2305d87d14f59011 (patch) | |
tree | ab5e8af7eb4140c0ad46181af954f53e15c68811 | |
parent | 5b68597400cea971aca9ed809551a69955e0cadc (diff) | |
download | glance_store-d3029fe4b4cc6fd47cb2032a2305d87d14f59011.tar.gz |
Return bytes even when get()ing a zero-byte image from swift
Drive-by: treat an unspecified slice start as 0 instead of
raising TypeErrors.
Change-Id: I5ae556771a3aa4af062772a0fa2c730a2e447972
-rw-r--r-- | glance_store/_drivers/swift/store.py | 2 | ||||
-rw-r--r-- | glance_store/backend.py | 2 | ||||
-rw-r--r-- | glance_store/tests/unit/test_swift_store.py | 30 |
3 files changed, 32 insertions, 2 deletions
diff --git a/glance_store/_drivers/swift/store.py b/glance_store/_drivers/swift/store.py index 7c970a9..0c3e044 100644 --- a/glance_store/_drivers/swift/store.py +++ b/glance_store/_drivers/swift/store.py @@ -861,7 +861,7 @@ class BaseStore(driver.Store): try: return next(self.wrapped) except StopIteration: - return '' + return b'' length = int(resp_headers.get('content-length', 0)) if allow_retry: diff --git a/glance_store/backend.py b/glance_store/backend.py index df57c90..992c49b 100644 --- a/glance_store/backend.py +++ b/glance_store/backend.py @@ -235,7 +235,7 @@ class Indexable(object): :param i: a slice-to-the-end """ - start = i.start if isinstance(i, slice) else i + start = (i.start or 0) if isinstance(i, slice) else i if start < self.cursor: return self.chunk[(start - self.cursor):] diff --git a/glance_store/tests/unit/test_swift_store.py b/glance_store/tests/unit/test_swift_store.py index 15fd5f3..287097c 100644 --- a/glance_store/tests/unit/test_swift_store.py +++ b/glance_store/tests/unit/test_swift_store.py @@ -49,6 +49,7 @@ CONF = cfg.CONF FAKE_UUID = lambda: str(uuid.uuid4()) FAKE_UUID2 = lambda: str(uuid.uuid4()) +FAKE_UUID3 = lambda: str(uuid.uuid4()) Store = swift.Store @@ -83,10 +84,15 @@ class SwiftTests(object): 'etag': 'c2e5db72bd7fd153f53ede5da5a06de3' }, 'glance/%s' % FAKE_UUID2: {'x-static-large-object': 'true', }, + 'glance/%s' % FAKE_UUID3: { + 'content-length': 0, + 'etag': "doesn't really matter", + }, } fixture_objects = { 'glance/%s' % FAKE_UUID: six.BytesIO(b"*" * FIVE_KB), 'glance/%s' % FAKE_UUID2: six.BytesIO(b"*" * FIVE_KB), + 'glance/%s' % FAKE_UUID3: six.BytesIO(), } def fake_head_container(url, token, container, **kwargs): @@ -295,6 +301,30 @@ class SwiftTests(object): data += chunk self.assertEqual(expected_data, data) + def test_get_using_slice(self): + """Test a "normal" retrieval of an image in chunks.""" + uri = "swift://%s:key@auth_address/glance/%s" % ( + self.swift_store_user, FAKE_UUID) + loc = location.get_location_from_uri(uri, conf=self.conf) + (image_swift, image_size) = self.store.get(loc) + self.assertEqual(5120, image_size) + + expected_data = b"*" * FIVE_KB + self.assertEqual(expected_data, image_swift[:]) + + expected_data = b"*" * (FIVE_KB - 100) + self.assertEqual(expected_data, image_swift[100:]) + + def test_get_empty_using_slice(self): + """Test a "normal" retrieval of a blank image.""" + uri = "swift://%s:key@auth_address/glance/%s" % ( + self.swift_store_user, FAKE_UUID3) + loc = location.get_location_from_uri(uri, conf=self.conf) + (image_swift, image_size) = self.store.get(loc) + self.assertEqual(0, image_size) + + self.assertEqual(b'', image_swift[0:]) + def test_get_with_retry(self): """ Test a retrieval where Swift does not get the full image in a single |