Skip to content

Commit

Permalink
Make remote swift image streaming functional
Browse files Browse the repository at this point in the history
Fixes lp 850425

(cherry picked from commit 4847ceb)

Change-Id: I117d45f7d5b1991296736b7915857e8764aad30d
  • Loading branch information
Brian Waldon authored and markmc committed Nov 18, 2011
1 parent a0ecd5c commit dba6b9f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion glance/store/swift.py
Expand Up @@ -260,7 +260,7 @@ def get(self, location):
# "Expected %s byte file, Swift has %s bytes" %
# (expected_size, obj_size))

return (resp_body, None)
return (resp_body, resp_headers.get('content-length'))

def _make_swift_connection(self, auth_url, user, key):
"""
Expand Down
83 changes: 83 additions & 0 deletions glance/tests/functional/test_swift.py
Expand Up @@ -385,3 +385,86 @@ def test_add_large_object_manifest_uneven_size(self):
hashlib.md5("*" * FIVE_MB).hexdigest())

self.stop_servers()

@skip_if_disabled
def test_remote_image(self):
"""
Ensure we can retrieve an image that was not stored by glance itself
"""
self.cleanup()

self.start_servers(**self.__dict__.copy())

api_port = self.api_port
registry_port = self.registry_port

# POST /images with public image named Image1
image_data = "*" * FIVE_MB
headers = {'Content-Type': 'application/octet-stream',
'X-Image-Meta-Name': 'Image1',
'X-Image-Meta-Is-Public': 'True'}
path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port)
http = httplib2.Http()
response, content = http.request(path, 'POST', headers=headers,
body=image_data)
self.assertEqual(response.status, 201, content)
data = json.loads(content)
self.assertEqual(data['image']['checksum'],
hashlib.md5(image_data).hexdigest())
self.assertEqual(data['image']['size'], FIVE_MB)
self.assertEqual(data['image']['name'], "Image1")
self.assertEqual(data['image']['is_public'], True)

# GET /images/1 and make sure data was uploaded
path = "http://%s:%d/v1/images/1" % ("0.0.0.0", self.api_port)
http = httplib2.Http()
response, content = http.request(path, 'GET')
self.assertEqual(response.status, 200)
self.assertEqual(response['content-length'], str(FIVE_MB))

self.assertEqual(content, "*" * FIVE_MB)
self.assertEqual(hashlib.md5(content).hexdigest(),
hashlib.md5("*" * FIVE_MB).hexdigest())

# use this header as the location for the next image
swift_location = response['x-image-meta-location']

# POST /images with public image named Image1 without uploading data
image_data = "*" * FIVE_MB
headers = {'Content-Type': 'application/octet-stream',
'X-Image-Meta-Name': 'Image1',
'X-Image-Meta-Is-Public': 'True',
'X-Image-Meta-Location': swift_location}
path = "http://%s:%d/v1/images" % ("0.0.0.0", self.api_port)
http = httplib2.Http()
response, content = http.request(path, 'POST', headers=headers)
self.assertEqual(response.status, 201, content)
data = json.loads(content)
self.assertEqual(data['image']['checksum'], None)
self.assertEqual(data['image']['size'], 0)
self.assertEqual(data['image']['name'], "Image1")
self.assertEqual(data['image']['is_public'], True)

# GET /images/2 ensuring the data already in swift is accessible
path = "http://%s:%d/v1/images/2" % ("0.0.0.0", self.api_port)
http = httplib2.Http()
response, content = http.request(path, 'GET')
self.assertEqual(response.status, 200)
self.assertEqual(response['content-length'], str(FIVE_MB))

self.assertEqual(content, "*" * FIVE_MB)
self.assertEqual(hashlib.md5(content).hexdigest(),
hashlib.md5("*" * FIVE_MB).hexdigest())

# DELETE /images/1 and /image/2
# Verify image and all chunks are gone...
path = "http://%s:%d/v1/images/1" % ("0.0.0.0", self.api_port)
http = httplib2.Http()
response, content = http.request(path, 'DELETE')
self.assertEqual(response.status, 200)
path = "http://%s:%d/v1/images/2" % ("0.0.0.0", self.api_port)
http = httplib2.Http()
response, content = http.request(path, 'DELETE')
self.assertEqual(response.status, 200)

self.stop_servers()
4 changes: 2 additions & 2 deletions glance/tests/unit/test_swift_store.py
Expand Up @@ -198,7 +198,7 @@ def test_get(self):
"""Test a "normal" retrieval of an image in chunks"""
loc = get_location_from_uri("swift://user:key@auth_address/glance/2")
(image_swift, image_size) = self.store.get(loc)
self.assertEqual(image_size, None)
self.assertEqual(image_size, 5120)

expected_data = "*" * FIVE_KB
data = ""
Expand All @@ -216,7 +216,7 @@ def test_get_with_http_auth(self):
loc = get_location_from_uri("swift+http://user:key@auth_address/"
"glance/2")
(image_swift, image_size) = self.store.get(loc)
self.assertEqual(image_size, None)
self.assertEqual(image_size, 5120)

expected_data = "*" * FIVE_KB
data = ""
Expand Down

0 comments on commit dba6b9f

Please sign in to comment.