Support FakeSwift register overwritten
Current swift3.test.unit.helpers.FakeSwift.register overwrites whole things for swift response. However, sometimes it deletes necessary acl information on s3acl decorator. Change-Id: I42f398526c832f0d25e8e090c0a80dd239a91508
This commit is contained in:
@@ -19,6 +19,7 @@ from copy import deepcopy
|
||||
from hashlib import md5
|
||||
from swift.common import swob
|
||||
from swift.common.utils import split_path
|
||||
from swift.common.request_helpers import is_sys_meta
|
||||
from swift3.cfg import CONF
|
||||
|
||||
|
||||
@@ -127,4 +128,17 @@ class FakeSwift(object):
|
||||
return len(self._calls)
|
||||
|
||||
def register(self, method, path, response_class, headers, body):
|
||||
# assuming the path format like /v1/account/container/object
|
||||
resource_map = ['account', 'container', 'object']
|
||||
index = len(split_path(path, 0, 3, True)[1:]) - 1
|
||||
resource = resource_map[index]
|
||||
|
||||
if (method, path) in self._responses:
|
||||
old_headers = self._responses[(method, path)][1]
|
||||
headers = headers.copy()
|
||||
for key, value in old_headers.iteritems():
|
||||
if is_sys_meta(resource, key) and key not in headers:
|
||||
# keep old sysmeta for s3acl
|
||||
headers.update({key: value})
|
||||
|
||||
self._responses[(method, path)] = (response_class, headers, body)
|
||||
|
||||
@@ -79,6 +79,7 @@ class TestSwift3Bucket(Swift3TestCase):
|
||||
status, headers, body = self.call_swift3(req)
|
||||
self.assertEquals(status.split()[0], '404')
|
||||
|
||||
@s3acl
|
||||
def test_bucket_GET_error(self):
|
||||
code = self._test_method_error('GET', '/bucket', swob.HTTPUnauthorized)
|
||||
self.assertEquals(code, 'SignatureDoesNotMatch')
|
||||
@@ -196,6 +197,7 @@ class TestSwift3Bucket(Swift3TestCase):
|
||||
self.assertEquals(args['marker'], '\xef\xbc\xa2')
|
||||
self.assertEquals(args['prefix'], '\xef\xbc\xa3')
|
||||
|
||||
@s3acl
|
||||
def test_bucket_PUT_error(self):
|
||||
code = self._test_method_error('PUT', '/bucket', swob.HTTPCreated,
|
||||
headers={'Content-Length': 'a'})
|
||||
@@ -268,6 +270,7 @@ class TestSwift3Bucket(Swift3TestCase):
|
||||
status, headers, body = self.call_swift3(req)
|
||||
self.assertEquals(self._get_error_code(body), 'MalformedXML')
|
||||
|
||||
@s3acl
|
||||
def test_bucket_DELETE_error(self):
|
||||
code = self._test_method_error('DELETE', '/bucket',
|
||||
swob.HTTPUnauthorized)
|
||||
|
||||
69
swift3/test/unit/test_helpers.py
Normal file
69
swift3/test/unit/test_helpers.py
Normal file
@@ -0,0 +1,69 @@
|
||||
# Copyright (c) 2013 OpenStack Foundation
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# This stuff can't live in test/unit/__init__.py due to its swob dependency.
|
||||
|
||||
import unittest
|
||||
from swift3.test.unit.helpers import FakeSwift
|
||||
from swift3.utils import sysmeta_header
|
||||
from swift.common.swob import HeaderKeyDict
|
||||
from mock import MagicMock
|
||||
|
||||
|
||||
class Swift3HelperTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.method = 'HEAD'
|
||||
self.path = '/v1/AUTH_test/bucket'
|
||||
|
||||
def _check_headers(self, swift, method, path, headers):
|
||||
_, response_headers, _ = swift._responses[(method, path)]
|
||||
self.assertEquals(headers, response_headers)
|
||||
|
||||
def test_fake_swift_sysmeta(self):
|
||||
swift = FakeSwift()
|
||||
orig_headers = HeaderKeyDict()
|
||||
orig_headers.update({sysmeta_header('container', 'acl'): 'test',
|
||||
'x-container-meta-foo': 'bar'})
|
||||
|
||||
swift.register(self.method, self.path, MagicMock(), orig_headers, None)
|
||||
|
||||
self._check_headers(swift, self.method, self.path, orig_headers)
|
||||
|
||||
new_headers = orig_headers.copy()
|
||||
del new_headers[sysmeta_header('container', 'acl').title()]
|
||||
swift.register(self.method, self.path, MagicMock(), new_headers, None)
|
||||
|
||||
self._check_headers(swift, self.method, self.path, orig_headers)
|
||||
|
||||
def test_fake_swift_sysmeta_overrite(self):
|
||||
swift = FakeSwift()
|
||||
orig_headers = HeaderKeyDict()
|
||||
orig_headers.update({sysmeta_header('container', 'acl'): 'test',
|
||||
'x-container-meta-foo': 'bar'})
|
||||
swift.register(self.method, self.path, MagicMock(), orig_headers, None)
|
||||
|
||||
self._check_headers(swift, self.method, self.path, orig_headers)
|
||||
|
||||
new_headers = orig_headers.copy()
|
||||
new_headers[sysmeta_header('container', 'acl').title()] = 'bar'
|
||||
|
||||
swift.register(self.method, self.path, MagicMock(), new_headers, None)
|
||||
|
||||
self.assertFalse(orig_headers == new_headers)
|
||||
self._check_headers(swift, self.method, self.path, new_headers)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -65,6 +65,7 @@ class TestSwift3Obj(Swift3TestCase):
|
||||
if method == 'GET':
|
||||
self.assertEquals(body, self.object_body)
|
||||
|
||||
@s3acl
|
||||
def test_object_HEAD_error(self):
|
||||
# HEAD does not return the body even an error resonse in the
|
||||
# specifications of the REST API.
|
||||
@@ -100,6 +101,7 @@ class TestSwift3Obj(Swift3TestCase):
|
||||
def test_object_HEAD(self):
|
||||
self._test_object_GETorHEAD('HEAD')
|
||||
|
||||
@s3acl
|
||||
def test_object_GET_error(self):
|
||||
code = self._test_method_error('GET', '/bucket/object',
|
||||
swob.HTTPUnauthorized)
|
||||
@@ -136,6 +138,7 @@ class TestSwift3Obj(Swift3TestCase):
|
||||
self.assertTrue('content-range' in headers)
|
||||
self.assertTrue(headers['content-range'].startswith('bytes 0-3'))
|
||||
|
||||
@s3acl
|
||||
def test_object_GET_Range_error(self):
|
||||
code = self._test_method_error('GET', '/bucket/object',
|
||||
swob.HTTPRequestedRangeNotSatisfiable)
|
||||
@@ -175,6 +178,7 @@ class TestSwift3Obj(Swift3TestCase):
|
||||
self.assertTrue('content-encoding' in headers)
|
||||
self.assertEquals(headers['content-encoding'], 'gzip')
|
||||
|
||||
@s3acl
|
||||
def test_object_PUT_error(self):
|
||||
code = self._test_method_error('PUT', '/bucket/object',
|
||||
swob.HTTPUnauthorized)
|
||||
@@ -252,6 +256,7 @@ class TestSwift3Obj(Swift3TestCase):
|
||||
self.assertEquals(headers['X-Object-Meta-Something'], 'oh hai')
|
||||
self.assertEquals(headers['X-Copy-From'], '/some/source')
|
||||
|
||||
@s3acl
|
||||
def test_object_DELETE_error(self):
|
||||
code = self._test_method_error('DELETE', '/bucket/object',
|
||||
swob.HTTPUnauthorized)
|
||||
|
||||
Reference in New Issue
Block a user