tests: don't mutate and re-use Response headers

Some tests in test_account_quotas.py registered a response with
FakeSwift during setUp and then subsequently mutated the response's
headers dict. Worse, some tests then mutate the headers again and
re-use the same response.
This relies on the FakeSwift implementation not copying the original
headers when the response is registered, which may not remain the case
(as the author discovered with the Related-Change). It's also an
unnecessary shortcut.
Change-Id: I3217c17936d0c11b03de4a4a172bb2fb0a2be734
Related-Change: I84604a7ea049850ad4d0f0cea2096ab8a98dfb4a
This commit is contained in:
Alistair Coles
2024年08月07日 16:08:12 +01:00
parent 9d14c7c830
commit 6a5a681227

View File

@@ -833,9 +833,7 @@ class TestAccountQuota(unittest.TestCase):
class AccountQuotaCopyingTestCases(unittest.TestCase):
def setUp(self):
self.headers = []
self.app = FakeSwift()
self.app.register('HEAD', '/v1/a', HTTPOk, self.headers)
self.app.register('HEAD', '/v1/a/c', HTTPOk, {
'x-backend-storage-policy-index': '1'})
self.app.register('GET', '/v1/a/c2/o2', HTTPOk, {
@@ -844,8 +842,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
self.copy_filter = copy.filter_factory({})(self.aq_filter)
def test_exceed_bytes_quota_copy_from(self):
self.headers[:] = [('x-account-bytes-used', '500'),
('x-account-meta-quota-bytes', '1000')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-bytes-used', '500'),
('x-account-meta-quota-bytes', '1000')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
@@ -856,8 +855,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
self.assertEqual(res.body, b'Upload exceeds quota.')
def test_exceed_bytes_quota_copy_verb(self):
self.headers[:] = [('x-account-bytes-used', '500'),
('x-account-meta-quota-bytes', '1000')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-bytes-used', '500'),
('x-account-meta-quota-bytes', '1000')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c2/o2',
environ={'REQUEST_METHOD': 'COPY',
@@ -869,8 +869,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
def test_not_exceed_bytes_quota_copy_from(self):
self.app.register('PUT', '/v1/a/c/o', HTTPOk, {})
self.headers[:] = [('x-account-bytes-used', '0'),
('x-account-meta-quota-bytes', '1000')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-bytes-used', '0'),
('x-account-meta-quota-bytes', '1000')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
@@ -881,8 +882,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
def test_not_exceed_bytes_quota_copy_verb(self):
self.app.register('PUT', '/v1/a/c/o', HTTPOk, {})
self.headers[:] = [('x-account-bytes-used', '0'),
('x-account-meta-quota-bytes', '1000')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-bytes-used', '0'),
('x-account-meta-quota-bytes', '1000')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c2/o2',
environ={'REQUEST_METHOD': 'COPY',
@@ -892,8 +894,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
self.assertEqual(res.status_int, 200)
def test_quota_copy_from_bad_src(self):
self.headers[:] = [('x-account-bytes-used', '0'),
('x-account-meta-quota-bytes', '1000')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-bytes-used', '0'),
('x-account-meta-quota-bytes', '1000')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
@@ -902,14 +905,16 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
res = req.get_response(self.copy_filter)
self.assertEqual(res.status_int, 412)
self.headers[:] = [('x-account-bytes-used', '1000'),
('x-account-meta-quota-bytes', '0')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-bytes-used', '1000'),
('x-account-meta-quota-bytes', '0')])
res = req.get_response(self.copy_filter)
self.assertEqual(res.status_int, 412)
def test_exceed_bytes_count_quota_copy_from(self):
self.headers[:] = [('x-account-object-count', '5'),
('x-account-meta-quota-count', '5')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-object-count', '5'),
('x-account-meta-quota-count', '5')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
@@ -920,8 +925,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
self.assertEqual(res.body, b'Upload exceeds quota.')
def test_exceed_bytes_count_quota_copy_verb(self):
self.headers[:] = [('x-account-object-count', '5'),
('x-account-meta-quota-count', '5')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-object-count', '5'),
('x-account-meta-quota-count', '5')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c2/o2',
environ={'REQUEST_METHOD': 'COPY',
@@ -933,8 +939,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
def test_not_exceed_bytes_count_quota_copy_from(self):
self.app.register('PUT', '/v1/a/c/o', HTTPOk, {})
self.headers[:] = [('x-account-object-count', '5'),
('x-account-meta-quota-count', '6')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-object-count', '5'),
('x-account-meta-quota-count', '6')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
@@ -945,8 +952,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
def test_not_exceed_bytes_count_quota_copy_verb(self):
self.app.register('PUT', '/v1/a/c/o', HTTPOk, {})
self.headers[:] = [('x-account-object-count', '5'),
('x-account-meta-quota-count', '6')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-object-count', '5'),
('x-account-meta-quota-count', '6')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c2/o2',
environ={'REQUEST_METHOD': 'COPY',
@@ -956,8 +964,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
self.assertEqual(res.status_int, 200)
def test_count_quota_copy_from_bad_src(self):
self.headers[:] = [('x-account-object-count', '0'),
('x-account-meta-quota-count', '1')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-object-count', '0'),
('x-account-meta-quota-count', '1')])
cache = FakeCache(None)
req = Request.blank('/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT',
@@ -966,8 +975,9 @@ class AccountQuotaCopyingTestCases(unittest.TestCase):
res = req.get_response(self.copy_filter)
self.assertEqual(res.status_int, 412)
self.headers[:] = [('x-account-object-count', '1'),
('x-account-meta-quota-count', '0')]
self.app.register('HEAD', '/v1/a', HTTPOk,
[('x-account-object-count', '1'),
('x-account-meta-quota-count', '0')])
res = req.get_response(self.copy_filter)
self.assertEqual(res.status_int, 412)
Reference in New Issue
openstack/swift
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.

The note is not visible to the blocked user.