From 4a27df057de810dec5caf6a7dd72d334e0be94be Mon Sep 17 00:00:00 2001 From: Clay Gerrard Date: Thu, 29 Jul 2010 13:30:16 -0500 Subject: [PATCH 1/3] moved LIMIT_LISTING const to swift.common.constraints, added test.unit.MockTrue, 100% test coverage on swift.common.constraints --- .bzrignore | 1 + swift/account/server.py | 5 ++-- swift/common/__init__.py | 4 --- swift/common/constraints.py | 6 ++++ swift/container/server.py | 5 ++-- test/unit/__init__.py | 41 ++++++++++++++++++++++++++++ test/unit/common/test_constraints.py | 11 +++++++- 7 files changed, 62 insertions(+), 11 deletions(-) diff --git a/.bzrignore b/.bzrignore index e96548d5c4..69c1d7e7f7 100644 --- a/.bzrignore +++ b/.bzrignore @@ -3,3 +3,4 @@ doc/build/* dist ChangeLog +.coverage diff --git a/swift/account/server.py b/swift/account/server.py index 04e04abb19..dc741daa2a 100644 --- a/swift/account/server.py +++ b/swift/account/server.py @@ -30,13 +30,12 @@ from webob.exc import HTTPAccepted, HTTPBadRequest, HTTPConflict, \ import simplejson from xml.sax import saxutils -from swift.common import ACCOUNT_LISTING_LIMIT from swift.common.db import AccountBroker from swift.common.exceptions import MessageTimeout from swift.common.utils import get_param, split_path, storage_directory, \ hash_path -from swift.common.constraints import check_mount, check_float, \ - check_xml_encodable +from swift.common.constraints import ACCOUNT_LISTING_LIMIT, \ + check_mount, check_float, check_xml_encodable from swift.common.healthcheck import healthcheck from swift.common.db_replicator import ReplicatorRpc diff --git a/swift/common/__init__.py b/swift/common/__init__.py index dd1bc7d65f..880a66aa87 100644 --- a/swift/common/__init__.py +++ b/swift/common/__init__.py @@ -1,5 +1 @@ """ Code common to all of Swift. """ - -ACCOUNT_LISTING_LIMIT = 10000 -CONTAINER_LISTING_LIMIT = 10000 -FILE_SIZE_LIMIT = 5368709122 diff --git a/swift/common/constraints.py b/swift/common/constraints.py index 8fdacb5a93..eda4d80dff 100644 --- a/swift/common/constraints.py +++ b/swift/common/constraints.py @@ -32,6 +32,12 @@ MAX_META_COUNT = 90 MAX_META_OVERALL_SIZE = 4096 #: Max object name length MAX_OBJECT_NAME_LENGTH = 1024 +#: Max object list length of a get request for a container +CONTAINER_LISTING_LIMIT = 10000 +#: Max container list length of a get request for an account +ACCOUNT_LISTING_LIMIT = 10000 + + def check_metadata(req): diff --git a/swift/container/server.py b/swift/container/server.py index ea8d2d8b22..44fedf913a 100644 --- a/swift/container/server.py +++ b/swift/container/server.py @@ -31,12 +31,11 @@ from webob.exc import HTTPAccepted, HTTPBadRequest, HTTPConflict, \ HTTPCreated, HTTPException, HTTPInternalServerError, HTTPNoContent, \ HTTPNotFound, HTTPPreconditionFailed, HTTPMethodNotAllowed -from swift.common import CONTAINER_LISTING_LIMIT from swift.common.db import ContainerBroker from swift.common.utils import get_logger, get_param, hash_path, \ storage_directory, split_path, mkdirs -from swift.common.constraints import check_mount, check_float, \ - check_xml_encodable +from swift.common.constraints import CONTAINER_LISTING_LIMIT, \ + check_mount, check_float, check_xml_encodable from swift.common.bufferedhttp import http_connect from swift.common.healthcheck import healthcheck from swift.common.exceptions import ConnectionTimeout, MessageTimeout diff --git a/test/unit/__init__.py b/test/unit/__init__.py index 0a6e2d3b1e..88add1816c 100644 --- a/test/unit/__init__.py +++ b/test/unit/__init__.py @@ -22,3 +22,44 @@ def connect_tcp(hostport): rv = socket.socket() rv.connect(hostport) return rv + +class MockTrue(object): + """ + Instances of MockTrue evalulate like True + Any attr accessed on an instance of MockTrue will return a MockTrue instance + Any method called on an instance of MockTrue will return a MockTrue instance + + >>> thing = MockTrue() + >>> thing + True + >>> thing == True # True == True + True + >>> thing == False # True == False + False + >>> thing != True # True != True + False + >>> thing != False # True != False + True + >>> thing.attribute + True + >>> thing.method() + True + >>> thing.attribute.method() + True + >>> thing.method().attribute + True + + """ + + def __getattribute__(self, *args, **kwargs): + return self + def __call__(self, *args, **kwargs): + return self + def __repr__(*args, **kwargs): + return repr(True) + def __eq__(self, other): + self = True + return self == other + def __ne__(self, other): + self = True + return self != other diff --git a/test/unit/common/test_constraints.py b/test/unit/common/test_constraints.py index da509bcd08..d6988cd791 100644 --- a/test/unit/common/test_constraints.py +++ b/test/unit/common/test_constraints.py @@ -14,6 +14,7 @@ # limitations under the License. import unittest +from test.unit import MockTrue from webob import Request from webob.exc import HTTPBadRequest, HTTPLengthRequired, \ @@ -21,7 +22,6 @@ from webob.exc import HTTPBadRequest, HTTPLengthRequired, \ from swift.common import constraints - class TestConstraints(unittest.TestCase): def test_check_metadata_empty(self): @@ -137,6 +137,15 @@ class TestConstraints(unittest.TestCase): self.assert_(isinstance(resp, HTTPBadRequest)) self.assert_('Content-Type' in resp.body) + def test_check_mount(self): + self.assertFalse(constraints.check_mount('', '')) + constraints.os = MockTrue() # mock os module + self.assertTrue(constraints.check_mount('/srv', '1')) + reload(constraints) # put it back + + def test_check_float(self): + self.assertFalse(constraints.check_float('')) + self.assertTrue(constraints.check_float('0')) if __name__ == '__main__': unittest.main() From 7ae6ec0675db51f95630ab72e75d115ed9fccc2f Mon Sep 17 00:00:00 2001 From: Clay Gerrard Date: Thu, 29 Jul 2010 13:31:27 -0500 Subject: [PATCH 2/3] typo --- test/unit/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/__init__.py b/test/unit/__init__.py index 88add1816c..fa13512495 100644 --- a/test/unit/__init__.py +++ b/test/unit/__init__.py @@ -25,7 +25,7 @@ def connect_tcp(hostport): class MockTrue(object): """ - Instances of MockTrue evalulate like True + Instances of MockTrue evaluate like True Any attr accessed on an instance of MockTrue will return a MockTrue instance Any method called on an instance of MockTrue will return a MockTrue instance From 17a108a633cf8f020e8cba3eb9306d4ecbcb9c2a Mon Sep 17 00:00:00 2001 From: Clay Gerrard Date: Thu, 29 Jul 2010 15:06:01 -0500 Subject: [PATCH 3/3] removed extra assignment --- test/unit/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/unit/__init__.py b/test/unit/__init__.py index fa13512495..cfd6cf1ad8 100644 --- a/test/unit/__init__.py +++ b/test/unit/__init__.py @@ -58,8 +58,6 @@ class MockTrue(object): def __repr__(*args, **kwargs): return repr(True) def __eq__(self, other): - self = True - return self == other + return other == True def __ne__(self, other): - self = True - return self != other + return other != True