Merge "Handle X-Copy-From header in container_quota middleware"

This commit is contained in:
Jenkins 2013-10-10 19:17:54 +00:00 committed by Gerrit Code Review
commit 5964082b2c
2 changed files with 68 additions and 3 deletions

View File

@ -43,7 +43,7 @@ set:
""" """
from swift.common.http import is_success from swift.common.http import is_success
from swift.proxy.controllers.base import get_container_info from swift.proxy.controllers.base import get_container_info, get_object_info
from swift.common.swob import Response, HTTPBadRequest, wsgify from swift.common.swob import Response, HTTPBadRequest, wsgify
@ -84,11 +84,21 @@ class ContainerQuotaMiddleware(object):
if not container_info or not is_success(container_info['status']): if not container_info or not is_success(container_info['status']):
# this will hopefully 404 later # this will hopefully 404 later
return self.app return self.app
content_length = (req.content_length or 0)
copy_from = req.headers.get('X-Copy-From')
if obj and copy_from:
path = '/%s/%s/%s' % (version, account, copy_from.lstrip('/'))
object_info = get_object_info(req.environ, self.app, path)
if not object_info or not object_info['length']:
content_length = 0
else:
content_length = int(object_info['length'])
if 'quota-bytes' in container_info.get('meta', {}) and \ if 'quota-bytes' in container_info.get('meta', {}) and \
'bytes' in container_info and \ 'bytes' in container_info and \
container_info['meta']['quota-bytes'].isdigit(): container_info['meta']['quota-bytes'].isdigit():
new_size = int(container_info['bytes']) + (req.content_length new_size = int(container_info['bytes']) + content_length
or 0)
if int(container_info['meta']['quota-bytes']) < new_size: if int(container_info['meta']['quota-bytes']) < new_size:
return self.bad_response(req, container_info) return self.bad_response(req, container_info)
if 'quota-count' in container_info.get('meta', {}) and \ if 'quota-count' in container_info.get('meta', {}) and \

View File

@ -94,6 +94,18 @@ class TestContainerQuotas(unittest.TestCase):
res = req.get_response(app) res = req.get_response(app)
self.assertEquals(res.status_int, 413) self.assertEquals(res.status_int, 413)
def test_exceed_bytes_quota_copy_from(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '2'}})
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
'swift.object/a/c2/o2': {'length': 10},
'swift.cache': cache},
headers={'x-copy-from': '/c2/o2'})
res = req.get_response(app)
self.assertEquals(res.status_int, 413)
def test_not_exceed_bytes_quota(self): def test_not_exceed_bytes_quota(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {}) app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}}) cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}})
@ -104,6 +116,28 @@ class TestContainerQuotas(unittest.TestCase):
res = req.get_response(app) res = req.get_response(app)
self.assertEquals(res.status_int, 200) self.assertEquals(res.status_int, 200)
def test_not_exceed_bytes_quota_copy_from(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}})
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
'swift.object/a/c2/o2': {'length': 10},
'swift.cache': cache},
headers={'x-copy-from': '/c2/o2'})
res = req.get_response(app)
self.assertEquals(res.status_int, 200)
def test_bytes_quota_copy_from_no_src(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}})
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
'swift.object/a/c2/o2': {'length': 10},
'swift.cache': cache},
headers={'x-copy-from': '/c2/o3'})
res = req.get_response(app)
self.assertEquals(res.status_int, 200)
def test_exceed_counts_quota(self): def test_exceed_counts_quota(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {}) app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '1'}}) cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '1'}})
@ -114,6 +148,17 @@ class TestContainerQuotas(unittest.TestCase):
res = req.get_response(app) res = req.get_response(app)
self.assertEquals(res.status_int, 413) self.assertEquals(res.status_int, 413)
def test_exceed_counts_quota_copy_from(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '1'}})
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
'swift.object/a/c2/o2': {'length': 10},
'swift.cache': cache},
headers={'x-copy-from': '/c2/o2'})
res = req.get_response(app)
self.assertEquals(res.status_int, 413)
def test_not_exceed_counts_quota(self): def test_not_exceed_counts_quota(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {}) app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '2'}}) cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '2'}})
@ -124,6 +169,16 @@ class TestContainerQuotas(unittest.TestCase):
res = req.get_response(app) res = req.get_response(app)
self.assertEquals(res.status_int, 200) self.assertEquals(res.status_int, 200)
def test_not_exceed_counts_quota_copy_from(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'object_count': 1, 'meta': {'quota-count': '2'}})
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
'swift.cache': cache},
headers={'x-copy-from': '/c2/o2'})
res = req.get_response(app)
self.assertEquals(res.status_int, 200)
def test_invalid_quotas(self): def test_invalid_quotas(self):
req = Request.blank( req = Request.blank(
'/v1/a/c', '/v1/a/c',