Pep8 unit test modules w/ <= 20 violations (6 of 12)

Change-Id: I7317beb97e1530cb18c62da55ccf4c64206ff362
Signed-off-by: Peter Portante <peter.portante@redhat.com>
This commit is contained in:
Peter Portante 2013-08-31 23:42:43 -04:00
parent be1cff4f1f
commit 56593a1323
7 changed files with 231 additions and 134 deletions

View File

@ -174,7 +174,8 @@ class TestAuthorize(unittest.TestCase):
def _get_account(self, identity=None): def _get_account(self, identity=None):
if not identity: if not identity:
identity = self._get_identity() identity = self._get_identity()
return self.test_auth._get_account_for_tenant(identity['HTTP_X_TENANT_ID']) return self.test_auth._get_account_for_tenant(
identity['HTTP_X_TENANT_ID'])
def _get_identity(self, tenant_id='tenant_id', tenant_name='tenant_name', def _get_identity(self, tenant_id='tenant_id', tenant_name='tenant_name',
user_id='user_id', user_name='user_name', roles=[]): user_id='user_id', user_name='user_name', roles=[]):
@ -244,7 +245,8 @@ class TestAuthorize(unittest.TestCase):
self.assertTrue(req.environ.get('swift_owner')) self.assertTrue(req.environ.get('swift_owner'))
def _check_authorize_for_tenant_owner_match(self, exception=None): def _check_authorize_for_tenant_owner_match(self, exception=None):
identity = self._get_identity(user_name='same_name', tenant_name='same_name') identity = self._get_identity(user_name='same_name',
tenant_name='same_name')
req = self._check_authenticate(identity=identity, exception=exception) req = self._check_authenticate(identity=identity, exception=exception)
expected = bool(exception is None) expected = bool(exception is None)
self.assertEqual(bool(req.environ.get('swift_owner')), expected) self.assertEqual(bool(req.environ.get('swift_owner')), expected)
@ -314,30 +316,55 @@ class TestAuthorize(unittest.TestCase):
self._check_authenticate(identity=identity, acl=acl) self._check_authenticate(identity=identity, acl=acl)
def test_cross_tenant_authorization_success(self): def test_cross_tenant_authorization_success(self):
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', self.assertEqual(
'userA', 'tenantID', 'tenantNAME', ['tenantID:userA']), 'tenantID:userA') self.test_auth._authorize_cross_tenant(
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', 'userID', 'userA', 'tenantID', 'tenantNAME',
'userA', 'tenantID', 'tenantNAME', ['tenantNAME:userA']), 'tenantNAME:userA') ['tenantID:userA']),
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', 'tenantID:userA')
'userA', 'tenantID', 'tenantNAME', ['*:userA']), '*:userA') self.assertEqual(
self.test_auth._authorize_cross_tenant(
'userID', 'userA', 'tenantID', 'tenantNAME',
['tenantNAME:userA']),
'tenantNAME:userA')
self.assertEqual(
self.test_auth._authorize_cross_tenant(
'userID', 'userA', 'tenantID', 'tenantNAME', ['*:userA']),
'*:userA')
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', self.assertEqual(
'userA', 'tenantID', 'tenantNAME', ['tenantID:userID']), 'tenantID:userID') self.test_auth._authorize_cross_tenant(
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', 'userID', 'userA', 'tenantID', 'tenantNAME',
'userA', 'tenantID', 'tenantNAME', ['tenantNAME:userID']), 'tenantNAME:userID') ['tenantID:userID']),
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', 'tenantID:userID')
'userA', 'tenantID', 'tenantNAME', ['*:userID']), '*:userID') self.assertEqual(
self.test_auth._authorize_cross_tenant(
'userID', 'userA', 'tenantID', 'tenantNAME',
['tenantNAME:userID']),
'tenantNAME:userID')
self.assertEqual(
self.test_auth._authorize_cross_tenant(
'userID', 'userA', 'tenantID', 'tenantNAME', ['*:userID']),
'*:userID')
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', self.assertEqual(
'userA', 'tenantID', 'tenantNAME', ['tenantID:*']), 'tenantID:*') self.test_auth._authorize_cross_tenant(
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', 'userID', 'userA', 'tenantID', 'tenantNAME', ['tenantID:*']),
'userA', 'tenantID', 'tenantNAME', ['tenantNAME:*']), 'tenantNAME:*') 'tenantID:*')
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', self.assertEqual(
'userA', 'tenantID', 'tenantNAME', ['*:*']), '*:*') self.test_auth._authorize_cross_tenant(
'userID', 'userA', 'tenantID', 'tenantNAME', ['tenantNAME:*']),
'tenantNAME:*')
self.assertEqual(
self.test_auth._authorize_cross_tenant(
'userID', 'userA', 'tenantID', 'tenantNAME', ['*:*']),
'*:*')
def test_cross_tenant_authorization_failure(self): def test_cross_tenant_authorization_failure(self):
self.assertEqual(self.test_auth._authorize_cross_tenant('userID', self.assertEqual(
'userA', 'tenantID', 'tenantNAME', ['tenantXYZ:userA']), None) self.test_auth._authorize_cross_tenant(
'userID', 'userA', 'tenantID', 'tenantNAME',
['tenantXYZ:userA']),
None)
def test_delete_own_account_not_allowed(self): def test_delete_own_account_not_allowed(self):
roles = self.test_auth.operator_roles.split(',') roles = self.test_auth.operator_roles.split(',')

View File

@ -26,6 +26,7 @@ from swift.common.swob import Request
class FakeApp(object): class FakeApp(object):
def __init__(self, body=['FAKE APP'], response_str='200 OK'): def __init__(self, body=['FAKE APP'], response_str='200 OK'):
self.body = body self.body = body
self.response_str = response_str self.response_str = response_str
@ -40,6 +41,7 @@ class FakeApp(object):
class FakeAppNoContentLengthNoTransferEncoding(object): class FakeAppNoContentLengthNoTransferEncoding(object):
def __init__(self, body=['FAKE APP']): def __init__(self, body=['FAKE APP']):
self.body = body self.body = body
@ -51,6 +53,7 @@ class FakeAppNoContentLengthNoTransferEncoding(object):
class FileLikeExceptor(object): class FileLikeExceptor(object):
def __init__(self): def __init__(self):
pass pass
@ -62,6 +65,7 @@ class FileLikeExceptor(object):
class FakeAppReadline(object): class FakeAppReadline(object):
def __call__(self, env, start_response): def __call__(self, env, start_response):
start_response('200 OK', [('Content-Type', 'text/plain'), start_response('200 OK', [('Content-Type', 'text/plain'),
('Content-Length', '8')]) ('Content-Length', '8')])
@ -153,6 +157,7 @@ class TestProxyLogging(unittest.TestCase):
function), but only for GET requests. function), but only for GET requests.
""" """
stub_times = [] stub_times = []
def stub_time(): def stub_time():
return stub_times.pop(0) return stub_times.pop(0)
@ -201,8 +206,10 @@ class TestProxyLogging(unittest.TestCase):
iter_response = app(req.environ, lambda *_: None) iter_response = app(req.environ, lambda *_: None)
self.assertEqual('7654321', ''.join(iter_response)) self.assertEqual('7654321', ''.join(iter_response))
self.assertEqual([], app.access_logger.log_dict['timing']) self.assertEqual([], app.access_logger.log_dict['timing'])
self.assertEqual([], app.access_logger.log_dict['timing_since']) self.assertEqual([],
self.assertEqual([], app.access_logger.log_dict['update_stats']) app.access_logger.log_dict['timing_since'])
self.assertEqual([],
app.access_logger.log_dict['update_stats'])
# PUT (no first-byte timing!) # PUT (no first-byte timing!)
app = proxy_logging.ProxyLoggingMiddleware( app = proxy_logging.ProxyLoggingMiddleware(
@ -221,7 +228,8 @@ class TestProxyLogging(unittest.TestCase):
'%s.GET.314.first-byte.timing' % exp_type, app) '%s.GET.314.first-byte.timing' % exp_type, app)
self.assertNotTiming( self.assertNotTiming(
'%s.PUT.314.first-byte.timing' % exp_type, app) '%s.PUT.314.first-byte.timing' % exp_type, app)
self.assertUpdateStats('%s.PUT.314.xfer' % exp_type, 6 + 8, app) self.assertUpdateStats(
'%s.PUT.314.xfer' % exp_type, 6 + 8, app)
finally: finally:
time.time = orig_time time.time = orig_time
@ -267,7 +275,8 @@ class TestProxyLogging(unittest.TestCase):
conf_key: 'SPECIAL, GET,PUT ', # crazy spaces ok conf_key: 'SPECIAL, GET,PUT ', # crazy spaces ok
}) })
app.access_logger = FakeLogger() app.access_logger = FakeLogger()
req = Request.blank('/v1/a/c', environ={'REQUEST_METHOD': method}) req = Request.blank('/v1/a/c',
environ={'REQUEST_METHOD': method})
app.log_request(req, 911, 4, 43, 1.01) app.log_request(req, 911, 4, 43, 1.01)
self.assertTiming('container.%s.911.timing' % exp_method, app, self.assertTiming('container.%s.911.timing' % exp_method, app,
exp_timing=1.01 * 1000) exp_timing=1.01 * 1000)
@ -332,7 +341,9 @@ class TestProxyLogging(unittest.TestCase):
app = proxy_logging.ProxyLoggingMiddleware(FakeApp(), app = proxy_logging.ProxyLoggingMiddleware(FakeApp(),
{'log_headers': 'yes'}) {'log_headers': 'yes'})
app.access_logger = FakeLogger() app.access_logger = FakeLogger()
req = Request.blank('/v1/a/c/o/foo', environ={'REQUEST_METHOD': 'PUT', req = Request.blank(
'/v1/a/c/o/foo',
environ={'REQUEST_METHOD': 'PUT',
'wsgi.input': StringIO.StringIO('some stuff')}) 'wsgi.input': StringIO.StringIO('some stuff')})
resp = app(req.environ, start_response) resp = app(req.environ, start_response)
# exhaust generator # exhaust generator
@ -348,7 +359,9 @@ class TestProxyLogging(unittest.TestCase):
app = proxy_logging.ProxyLoggingMiddleware(FakeAppReadline(), app = proxy_logging.ProxyLoggingMiddleware(FakeAppReadline(),
{'log_headers': 'yes'}) {'log_headers': 'yes'})
app.access_logger = FakeLogger() app.access_logger = FakeLogger()
req = Request.blank('/v1/a/c', environ={'REQUEST_METHOD': 'POST', req = Request.blank(
'/v1/a/c',
environ={'REQUEST_METHOD': 'POST',
'wsgi.input': StringIO.StringIO( 'wsgi.input': StringIO.StringIO(
'some stuff\nsome other stuff\n')}) 'some stuff\nsome other stuff\n')})
resp = app(req.environ, start_response) resp = app(req.environ, start_response)
@ -412,7 +425,8 @@ class TestProxyLogging(unittest.TestCase):
self.assertEquals(log_parts[1], '1.2.3.4') # remote addr self.assertEquals(log_parts[1], '1.2.3.4') # remote addr
def test_facility(self): def test_facility(self):
app = proxy_logging.ProxyLoggingMiddleware(FakeApp(), app = proxy_logging.ProxyLoggingMiddleware(
FakeApp(),
{'log_headers': 'yes', {'log_headers': 'yes',
'access_log_facility': 'LOG_LOCAL7'}) 'access_log_facility': 'LOG_LOCAL7'})
handler = get_logger.handler4logger[app.access_logger.logger] handler = get_logger.handler4logger[app.access_logger.logger]
@ -642,6 +656,9 @@ class TestProxyLogging(unittest.TestCase):
log_parts = self._log_parts(app) log_parts = self._log_parts(app)
self.assertEquals(log_parts[9], '...') self.assertEquals(log_parts[9], '...')
# Avoids pyflakes error, "local variable 'resp_body' is assigned to
# but never used
self.assertTrue(resp_body is not None)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -18,7 +18,9 @@ import unittest
from swift.common.swob import Request, HTTPUnauthorized from swift.common.swob import Request, HTTPUnauthorized
from swift.common.middleware import container_quotas from swift.common.middleware import container_quotas
class FakeCache(object): class FakeCache(object):
def __init__(self, val): def __init__(self, val):
if 'status' not in val: if 'status' not in val:
val['status'] = 200 val['status'] = 200
@ -27,7 +29,9 @@ class FakeCache(object):
def get(self, *args): def get(self, *args):
return self.val return self.val
class FakeApp(object): class FakeApp(object):
def __init__(self): def __init__(self):
pass pass
@ -35,7 +39,9 @@ class FakeApp(object):
start_response('200 OK', []) start_response('200 OK', [])
return [] return []
class FakeMissingApp(object): class FakeMissingApp(object):
def __init__(self): def __init__(self):
pass pass
@ -43,16 +49,18 @@ class FakeMissingApp(object):
start_response('404 Not Found', []) start_response('404 Not Found', [])
return [] return []
def start_response(*args): def start_response(*args):
pass pass
class TestContainerQuotas(unittest.TestCase): class TestContainerQuotas(unittest.TestCase):
def test_split_path_empty_container_path_segment(self): def test_split_path_empty_container_path_segment(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {}) app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
req = Request.blank('/v1/a//something/something_else', req = Request.blank('/v1/a//something/something_else',
environ={'REQUEST_METHOD': 'PUT', environ={'REQUEST_METHOD': 'PUT',
'swift.cache': {'key':'value'}}) 'swift.cache': {'key': 'value'}})
res = req.get_response(app) res = req.get_response(app)
self.assertEquals(res.status_int, 200) self.assertEquals(res.status_int, 200)
@ -69,7 +77,8 @@ class TestContainerQuotas(unittest.TestCase):
def test_no_quotas(self): def test_no_quotas(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {}) app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
req = Request.blank('/v1/a/c/o', req = Request.blank(
'/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT', 'swift.cache': FakeCache({}), environ={'REQUEST_METHOD': 'PUT', 'swift.cache': FakeCache({}),
'CONTENT_LENGTH': '100'}) 'CONTENT_LENGTH': '100'})
res = req.get_response(app) res = req.get_response(app)
@ -78,7 +87,8 @@ class TestContainerQuotas(unittest.TestCase):
def test_exceed_bytes_quota(self): def test_exceed_bytes_quota(self):
app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {}) app = container_quotas.ContainerQuotaMiddleware(FakeApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '2'}}) cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '2'}})
req = Request.blank('/v1/a/c/o', req = Request.blank(
'/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache, environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache,
'CONTENT_LENGTH': '100'}) 'CONTENT_LENGTH': '100'})
res = req.get_response(app) res = req.get_response(app)
@ -87,7 +97,8 @@ class TestContainerQuotas(unittest.TestCase):
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'}})
req = Request.blank('/v1/a/c/o', req = Request.blank(
'/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache, environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache,
'CONTENT_LENGTH': '100'}) 'CONTENT_LENGTH': '100'})
res = req.get_response(app) res = req.get_response(app)
@ -96,7 +107,8 @@ class TestContainerQuotas(unittest.TestCase):
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'}})
req = Request.blank('/v1/a/c/o', req = Request.blank(
'/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache, environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache,
'CONTENT_LENGTH': '100'}) 'CONTENT_LENGTH': '100'})
res = req.get_response(app) res = req.get_response(app)
@ -105,21 +117,24 @@ class TestContainerQuotas(unittest.TestCase):
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'}})
req = Request.blank('/v1/a/c/o', req = Request.blank(
'/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache, environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache,
'CONTENT_LENGTH': '100'}) 'CONTENT_LENGTH': '100'})
res = req.get_response(app) res = req.get_response(app)
self.assertEquals(res.status_int, 200) self.assertEquals(res.status_int, 200)
def test_invalid_quotas(self): def test_invalid_quotas(self):
req = Request.blank('/v1/a/c', req = Request.blank(
'/v1/a/c',
environ={'REQUEST_METHOD': 'POST', environ={'REQUEST_METHOD': 'POST',
'HTTP_X_CONTAINER_META_QUOTA_BYTES': 'abc'}) 'HTTP_X_CONTAINER_META_QUOTA_BYTES': 'abc'})
res = req.get_response( res = req.get_response(
container_quotas.ContainerQuotaMiddleware(FakeApp(), {})) container_quotas.ContainerQuotaMiddleware(FakeApp(), {}))
self.assertEquals(res.status_int, 400) self.assertEquals(res.status_int, 400)
req = Request.blank('/v1/a/c', req = Request.blank(
'/v1/a/c',
environ={'REQUEST_METHOD': 'POST', environ={'REQUEST_METHOD': 'POST',
'HTTP_X_CONTAINER_META_QUOTA_COUNT': 'abc'}) 'HTTP_X_CONTAINER_META_QUOTA_COUNT': 'abc'})
res = req.get_response( res = req.get_response(
@ -127,14 +142,16 @@ class TestContainerQuotas(unittest.TestCase):
self.assertEquals(res.status_int, 400) self.assertEquals(res.status_int, 400)
def test_valid_quotas(self): def test_valid_quotas(self):
req = Request.blank('/v1/a/c', req = Request.blank(
'/v1/a/c',
environ={'REQUEST_METHOD': 'POST', environ={'REQUEST_METHOD': 'POST',
'HTTP_X_CONTAINER_META_QUOTA_BYTES': '123'}) 'HTTP_X_CONTAINER_META_QUOTA_BYTES': '123'})
res = req.get_response( res = req.get_response(
container_quotas.ContainerQuotaMiddleware(FakeApp(), {})) container_quotas.ContainerQuotaMiddleware(FakeApp(), {}))
self.assertEquals(res.status_int, 200) self.assertEquals(res.status_int, 200)
req = Request.blank('/v1/a/c', req = Request.blank(
'/v1/a/c',
environ={'REQUEST_METHOD': 'POST', environ={'REQUEST_METHOD': 'POST',
'HTTP_X_CONTAINER_META_QUOTA_COUNT': '123'}) 'HTTP_X_CONTAINER_META_QUOTA_COUNT': '123'})
res = req.get_response( res = req.get_response(
@ -142,7 +159,8 @@ class TestContainerQuotas(unittest.TestCase):
self.assertEquals(res.status_int, 200) self.assertEquals(res.status_int, 200)
def test_delete_quotas(self): def test_delete_quotas(self):
req = Request.blank('/v1/a/c', req = Request.blank(
'/v1/a/c',
environ={'REQUEST_METHOD': 'POST', environ={'REQUEST_METHOD': 'POST',
'HTTP_X_CONTAINER_META_QUOTA_BYTES': None}) 'HTTP_X_CONTAINER_META_QUOTA_BYTES': None})
res = req.get_response( res = req.get_response(
@ -152,7 +170,8 @@ class TestContainerQuotas(unittest.TestCase):
def test_missing_container(self): def test_missing_container(self):
app = container_quotas.ContainerQuotaMiddleware(FakeMissingApp(), {}) app = container_quotas.ContainerQuotaMiddleware(FakeMissingApp(), {})
cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}}) cache = FakeCache({'bytes': 0, 'meta': {'quota-bytes': '100'}})
req = Request.blank('/v1/a/c/o', req = Request.blank(
'/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache, environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache,
'CONTENT_LENGTH': '100'}) 'CONTENT_LENGTH': '100'})
res = req.get_response(app) res = req.get_response(app)
@ -162,7 +181,8 @@ class TestContainerQuotas(unittest.TestCase):
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'},
'write_acl': None}) 'write_acl': None})
req = Request.blank('/v1/a/c/o', req = Request.blank(
'/v1/a/c/o',
environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache, environ={'REQUEST_METHOD': 'PUT', 'swift.cache': cache,
'CONTENT_LENGTH': '100', 'CONTENT_LENGTH': '100',
'swift.authorize': lambda *args: HTTPUnauthorized()}) 'swift.authorize': lambda *args: HTTPUnauthorized()})

View File

@ -260,10 +260,12 @@ class TestDBReplicator(unittest.TestCase):
def test_rsync_file(self): def test_rsync_file(self):
replicator = TestReplicator({}) replicator = TestReplicator({})
with _mock_process(-1): with _mock_process(-1):
self.assertEquals(False, self.assertEquals(
False,
replicator._rsync_file('/some/file', 'remote:/some/file')) replicator._rsync_file('/some/file', 'remote:/some/file'))
with _mock_process(0): with _mock_process(0):
self.assertEquals(True, self.assertEquals(
True,
replicator._rsync_file('/some/file', 'remote:/some/file')) replicator._rsync_file('/some/file', 'remote:/some/file'))
def test_rsync_file_popen_args(self): def test_rsync_file_popen_args(self):
@ -383,7 +385,8 @@ class TestDBReplicator(unittest.TestCase):
with patch('os.path.getmtime', ChangingMtimesOs()): with patch('os.path.getmtime', ChangingMtimesOs()):
broker = FakeBroker() broker = FakeBroker()
replicator = MyTestReplicator(broker) replicator = MyTestReplicator(broker)
fake_device = {'ip': '127.0.0.1', 'replication_ip': '127.0.0.1', fake_device = {'ip': '127.0.0.1',
'replication_ip': '127.0.0.1',
'device': 'sda1'} 'device': 'sda1'}
replicator._rsync_db(broker, fake_device, ReplHttp(), 'abcd') replicator._rsync_db(broker, fake_device, ReplHttp(), 'abcd')
self.assertEquals(2, replicator._rsync_file_call_count) self.assertEquals(2, replicator._rsync_file_call_count)
@ -775,7 +778,8 @@ class TestReplToNode(unittest.TestCase):
self.fake_info = {'id': 'a', 'point': -1, 'max_row': 10, 'hash': 'b', self.fake_info = {'id': 'a', 'point': -1, 'max_row': 10, 'hash': 'b',
'created_at': 100, 'put_timestamp': 0, 'created_at': 100, 'put_timestamp': 0,
'delete_timestamp': 0, 'count': 0, 'delete_timestamp': 0, 'count': 0,
'metadata': {'Test': ('Value', normalize_timestamp(1))}} 'metadata': {
'Test': ('Value', normalize_timestamp(1))}}
self.replicator.logger = mock.Mock() self.replicator.logger = mock.Mock()
self.replicator._rsync_db = mock.Mock(return_value=True) self.replicator._rsync_db = mock.Mock(return_value=True)
self.replicator._usync_db = mock.Mock(return_value=True) self.replicator._usync_db = mock.Mock(return_value=True)
@ -803,7 +807,8 @@ class TestReplToNode(unittest.TestCase):
mock.call.increment('remote_merges') mock.call.increment('remote_merges')
]) ])
self.replicator._rsync_db.assert_has_calls([ self.replicator._rsync_db.assert_has_calls([
mock.call(self.broker, self.fake_node, self.http, self.fake_info['id'], mock.call(self.broker, self.fake_node, self.http,
self.fake_info['id'],
replicate_method='rsync_then_merge', replicate_method='rsync_then_merge',
replicate_timeout=(self.fake_info['count'] / 2000)) replicate_timeout=(self.fake_info['count'] / 2000))
]) ])
@ -825,7 +830,8 @@ class TestReplToNode(unittest.TestCase):
mock.call.increment('rsyncs') mock.call.increment('rsyncs')
]) ])
self.replicator._rsync_db.assert_has_calls([ self.replicator._rsync_db.assert_has_calls([
mock.call(self.broker, self.fake_node, self.http, self.fake_info['id']) mock.call(self.broker, self.fake_node, self.http,
self.fake_info['id'])
]) ])
def test_repl_to_node_drive_not_mounted(self): def test_repl_to_node_drive_not_mounted(self):

View File

@ -114,7 +114,8 @@ class TestDirectClient(unittest.TestCase):
was_http_connector = direct_client.http_connect was_http_connector = direct_client.http_connect
direct_client.http_connect = mock_http_connect(200, fake_headers, body) direct_client.http_connect = mock_http_connect(200, fake_headers, body)
resp_headers, resp = direct_client.direct_get_account(node, part, account) resp_headers, resp = direct_client.direct_get_account(node, part,
account)
fake_headers.update({'user-agent': 'direct-client %s' % os.getpid()}) fake_headers.update({'user-agent': 'direct-client %s' % os.getpid()})
self.assertEqual(fake_headers, resp_headers) self.assertEqual(fake_headers, resp_headers)
@ -122,7 +123,8 @@ class TestDirectClient(unittest.TestCase):
direct_client.http_connect = mock_http_connect(204, fake_headers, body) direct_client.http_connect = mock_http_connect(204, fake_headers, body)
resp_headers, resp = direct_client.direct_get_account(node, part, account) resp_headers, resp = direct_client.direct_get_account(node, part,
account)
fake_headers.update({'user-agent': 'direct-client %s' % os.getpid()}) fake_headers.update({'user-agent': 'direct-client %s' % os.getpid()})
self.assertEqual(fake_headers, resp_headers) self.assertEqual(fake_headers, resp_headers)
@ -140,7 +142,8 @@ class TestDirectClient(unittest.TestCase):
was_http_connector = direct_client.http_connect was_http_connector = direct_client.http_connect
direct_client.http_connect = mock_http_connect(200, headers) direct_client.http_connect = mock_http_connect(200, headers)
resp = direct_client.direct_head_container(node, part, account, container) resp = direct_client.direct_head_container(node, part, account,
container)
headers.update({'user-agent': 'direct-client %s' % os.getpid()}) headers.update({'user-agent': 'direct-client %s' % os.getpid()})
self.assertEqual(headers, resp) self.assertEqual(headers, resp)
@ -219,7 +222,8 @@ class TestDirectClient(unittest.TestCase):
direct_client.http_connect = mock_http_connect(200, body=contents) direct_client.http_connect = mock_http_connect(200, body=contents)
resp_header, obj_body = ( resp_header, obj_body = (
direct_client.direct_get_object(node, part, account, container, name)) direct_client.direct_get_object(node, part, account, container,
name))
self.assertEqual(obj_body, contents) self.assertEqual(obj_body, contents)
direct_client.http_connect = was_http_connector direct_client.http_connect = was_http_connector
@ -255,7 +259,8 @@ class TestDirectClient(unittest.TestCase):
was_http_connector = direct_client.http_connect was_http_connector = direct_client.http_connect
direct_client.http_connect = mock_http_connect(200) direct_client.http_connect = mock_http_connect(200)
direct_client.direct_delete_object(node, part, account, container, name) direct_client.direct_delete_object(node, part, account, container,
name)
direct_client.http_connect = was_http_connector direct_client.http_connect = was_http_connector
@ -322,7 +327,8 @@ class TestDirectClient(unittest.TestCase):
direct_client.http_connect = mock_http_connect(200, headers) direct_client.http_connect = mock_http_connect(200, headers)
attempts, resp = direct_client.retry(direct_client.direct_head_object, attempts, resp = direct_client.retry(direct_client.direct_head_object,
node, part, account, container, name) node, part, account, container,
name)
headers.update({'user-agent': 'direct-client %s' % os.getpid()}) headers.update({'user-agent': 'direct-client %s' % os.getpid()})
self.assertEqual(headers, resp) self.assertEqual(headers, resp)
self.assertEqual(attempts, 1) self.assertEqual(attempts, 1)

View File

@ -116,7 +116,9 @@ class TestObjectExpirer(TestCase):
self.assertRaises(ValueError, expirer.ObjectExpirer, conf) self.assertRaises(ValueError, expirer.ObjectExpirer, conf)
def test_process_based_concurrency(self): def test_process_based_concurrency(self):
class ObjectExpirer(expirer.ObjectExpirer): class ObjectExpirer(expirer.ObjectExpirer):
def __init__(self, conf): def __init__(self, conf):
super(ObjectExpirer, self).__init__(conf) super(ObjectExpirer, self).__init__(conf)
self.processes = 3 self.processes = 3
@ -128,6 +130,7 @@ class TestObjectExpirer(TestCase):
self.deleted_objects[container].add(obj) self.deleted_objects[container].add(obj)
class InternalClient(object): class InternalClient(object):
def __init__(self, containers): def __init__(self, containers):
self.containers = containers self.containers = containers
@ -286,7 +289,8 @@ class TestObjectExpirer(TestCase):
x.logger = FakeLogger() x.logger = FakeLogger()
x.swift = InternalClient([{'name': str(int(time() - 86400))}]) x.swift = InternalClient([{'name': str(int(time() - 86400))}])
x.run_once() x.run_once()
self.assertEqual(x.logger.log_dict['exception'], self.assertEqual(
x.logger.log_dict['exception'],
[(('Unhandled exception',), {}, [(('Unhandled exception',), {},
str(Exception('This should not have been called')))]) str(Exception('This should not have been called')))])
@ -313,13 +317,15 @@ class TestObjectExpirer(TestCase):
x = expirer.ObjectExpirer({}) x = expirer.ObjectExpirer({})
x.logger = FakeLogger() x.logger = FakeLogger()
x.swift = InternalClient([{'name': str(int(time() - 86400))}], x.swift = InternalClient(
[{'name': str(int(time() - 86400))}],
[{'name': '%d-actual-obj' % int(time() + 86400)}]) [{'name': '%d-actual-obj' % int(time() + 86400)}])
x.run_once() x.run_once()
for exccall in x.logger.log_dict['exception']: for exccall in x.logger.log_dict['exception']:
self.assertTrue( self.assertTrue(
'This should not have been called' not in exccall[0][0]) 'This should not have been called' not in exccall[0][0])
self.assertEqual(x.logger.log_dict['info'], self.assertEqual(
x.logger.log_dict['info'],
[(('Pass beginning; 1 possible containers; ' [(('Pass beginning; 1 possible containers; '
'2 possible objects',), {}), '2 possible objects',), {}),
(('Pass completed in 0s; 0 objects expired',), {})]) (('Pass completed in 0s; 0 objects expired',), {})])
@ -328,7 +334,8 @@ class TestObjectExpirer(TestCase):
x = expirer.ObjectExpirer({}) x = expirer.ObjectExpirer({})
x.logger = FakeLogger() x.logger = FakeLogger()
ts = int(time() - 86400) ts = int(time() - 86400)
x.swift = InternalClient([{'name': str(int(time() - 86400))}], x.swift = InternalClient(
[{'name': str(int(time() - 86400))}],
[{'name': '%d-actual-obj' % ts}]) [{'name': '%d-actual-obj' % ts}])
x.delete_actual_object = should_not_be_called x.delete_actual_object = should_not_be_called
x.run_once() x.run_once()
@ -336,7 +343,8 @@ class TestObjectExpirer(TestCase):
for exccall in x.logger.log_dict['exception']: for exccall in x.logger.log_dict['exception']:
if exccall[0][0].startswith('Exception while deleting '): if exccall[0][0].startswith('Exception while deleting '):
excswhiledeleting.append(exccall[0][0]) excswhiledeleting.append(exccall[0][0])
self.assertEqual(excswhiledeleting, self.assertEqual(
excswhiledeleting,
['Exception while deleting object %d %d-actual-obj ' ['Exception while deleting object %d %d-actual-obj '
'This should not have been called' % (ts, ts)]) 'This should not have been called' % (ts, ts)])
@ -372,17 +380,20 @@ class TestObjectExpirer(TestCase):
x.iter_containers = lambda: [str(int(time() - 86400))] x.iter_containers = lambda: [str(int(time() - 86400))]
ts = int(time() - 86400) ts = int(time() - 86400)
x.delete_actual_object = deliberately_blow_up x.delete_actual_object = deliberately_blow_up
x.swift = InternalClient([{'name': str(int(time() - 86400))}], x.swift = InternalClient(
[{'name': str(int(time() - 86400))}],
[{'name': '%d-actual-obj' % ts}]) [{'name': '%d-actual-obj' % ts}])
x.run_once() x.run_once()
excswhiledeleting = [] excswhiledeleting = []
for exccall in x.logger.log_dict['exception']: for exccall in x.logger.log_dict['exception']:
if exccall[0][0].startswith('Exception while deleting '): if exccall[0][0].startswith('Exception while deleting '):
excswhiledeleting.append(exccall[0][0]) excswhiledeleting.append(exccall[0][0])
self.assertEqual(excswhiledeleting, self.assertEqual(
excswhiledeleting,
['Exception while deleting object %d %d-actual-obj ' ['Exception while deleting object %d %d-actual-obj '
'failed to delete actual object' % (ts, ts)]) 'failed to delete actual object' % (ts, ts)])
self.assertEqual(x.logger.log_dict['info'], self.assertEqual(
x.logger.log_dict['info'],
[(('Pass beginning; 1 possible containers; ' [(('Pass beginning; 1 possible containers; '
'2 possible objects',), {}), '2 possible objects',), {}),
(('Pass completed in 0s; 0 objects expired',), {})]) (('Pass completed in 0s; 0 objects expired',), {})])
@ -392,14 +403,16 @@ class TestObjectExpirer(TestCase):
x.logger = FakeLogger() x.logger = FakeLogger()
ts = int(time() - 86400) ts = int(time() - 86400)
x.delete_actual_object = lambda o, t: None x.delete_actual_object = lambda o, t: None
x.swift = InternalClient([{'name': str(int(time() - 86400))}], x.swift = InternalClient(
[{'name': str(int(time() - 86400))}],
[{'name': '%d-actual-obj' % ts}]) [{'name': '%d-actual-obj' % ts}])
x.run_once() x.run_once()
excswhiledeleting = [] excswhiledeleting = []
for exccall in x.logger.log_dict['exception']: for exccall in x.logger.log_dict['exception']:
if exccall[0][0].startswith('Exception while deleting '): if exccall[0][0].startswith('Exception while deleting '):
excswhiledeleting.append(exccall[0][0]) excswhiledeleting.append(exccall[0][0])
self.assertEqual(excswhiledeleting, self.assertEqual(
excswhiledeleting,
['Exception while deleting object %d %d-actual-obj This should ' ['Exception while deleting object %d %d-actual-obj This should '
'not have been called' % (ts, ts)]) 'not have been called' % (ts, ts)])
@ -428,11 +441,13 @@ class TestObjectExpirer(TestCase):
x.logger = FakeLogger() x.logger = FakeLogger()
x.delete_actual_object = lambda o, t: None x.delete_actual_object = lambda o, t: None
self.assertEqual(x.report_objects, 0) self.assertEqual(x.report_objects, 0)
x.swift = InternalClient([{'name': str(int(time() - 86400))}], x.swift = InternalClient(
[{'name': str(int(time() - 86400))}],
[{'name': '%d-actual-obj' % int(time() - 86400)}]) [{'name': '%d-actual-obj' % int(time() - 86400)}])
x.run_once() x.run_once()
self.assertEqual(x.report_objects, 1) self.assertEqual(x.report_objects, 1)
self.assertEqual(x.logger.log_dict['info'], self.assertEqual(
x.logger.log_dict['info'],
[(('Pass beginning; 1 possible containers; ' [(('Pass beginning; 1 possible containers; '
'2 possible objects',), {}), '2 possible objects',), {}),
(('Pass completed in 0s; 1 objects expired',), {})]) (('Pass completed in 0s; 1 objects expired',), {})])
@ -468,11 +483,13 @@ class TestObjectExpirer(TestCase):
x.logger = FakeLogger() x.logger = FakeLogger()
x.delete_actual_object = delete_actual_object_test_for_unicode x.delete_actual_object = delete_actual_object_test_for_unicode
self.assertEqual(x.report_objects, 0) self.assertEqual(x.report_objects, 0)
x.swift = InternalClient([{'name': str(int(time() - 86400))}], x.swift = InternalClient(
[{'name': str(int(time() - 86400))}],
[{'name': u'%d-actual-obj' % int(time() - 86400)}]) [{'name': u'%d-actual-obj' % int(time() - 86400)}])
x.run_once() x.run_once()
self.assertEqual(x.report_objects, 1) self.assertEqual(x.report_objects, 1)
self.assertEqual(x.logger.log_dict['info'], self.assertEqual(
x.logger.log_dict['info'],
[(('Pass beginning; 1 possible containers; ' [(('Pass beginning; 1 possible containers; '
'2 possible objects',), {}), '2 possible objects',), {}),
(('Pass completed in 0s; 1 objects expired',), {})]) (('Pass completed in 0s; 1 objects expired',), {})])
@ -538,7 +555,8 @@ class TestObjectExpirer(TestCase):
'container' % (cts,), 'container' % (cts,),
'Exception while deleting container %d failed to delete ' 'Exception while deleting container %d failed to delete '
'container' % (cts + 1,)])) 'container' % (cts + 1,)]))
self.assertEqual(x.logger.log_dict['info'], self.assertEqual(
x.logger.log_dict['info'],
[(('Pass beginning; 1 possible containers; ' [(('Pass beginning; 1 possible containers; '
'2 possible objects',), {}), '2 possible objects',), {}),
(('Pass completed in 0s; 0 objects expired',), {})]) (('Pass completed in 0s; 0 objects expired',), {})])
@ -651,7 +669,8 @@ class TestObjectExpirer(TestCase):
def test_delete_actual_object_does_not_handle_odd_stuff(self): def test_delete_actual_object_does_not_handle_odd_stuff(self):
def fake_app(env, start_response): def fake_app(env, start_response):
start_response('503 Internal Server Error', start_response(
'503 Internal Server Error',
[('Content-Length', '0')]) [('Content-Length', '0')])
return [] return []

View File

@ -126,10 +126,12 @@ def _create_test_ring(path):
intended_part_shift = 30 intended_part_shift = 30
intended_reload_time = 15 intended_reload_time = 15
with closing(GzipFile(testgz, 'wb')) as f: with closing(GzipFile(testgz, 'wb')) as f:
pickle.dump(ring.RingData(intended_replica2part2dev_id, pickle.dump(
ring.RingData(intended_replica2part2dev_id,
intended_devs, intended_part_shift), intended_devs, intended_part_shift),
f) f)
return ring.Ring(path, ring_name='object', reload_time=intended_reload_time) return ring.Ring(path, ring_name='object',
reload_time=intended_reload_time)
class TestObjectReplicator(unittest.TestCase): class TestObjectReplicator(unittest.TestCase):
@ -321,8 +323,8 @@ class TestObjectReplicator(unittest.TestCase):
# Write some files into '1' and run replicate- they should be moved # Write some files into '1' and run replicate- they should be moved
# to the other partitoins and then node should get deleted. # to the other partitoins and then node should get deleted.
cur_part = '1' cur_part = '1'
df = diskfile.DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o', df = diskfile.DiskFile(
FakeLogger()) self.devices, 'sda', cur_part, 'a', 'c', 'o', FakeLogger())
mkdirs(df.datadir) mkdirs(df.datadir)
f = open(os.path.join(df.datadir, f = open(os.path.join(df.datadir,
normalize_timestamp(time.time()) + '.data'), normalize_timestamp(time.time()) + '.data'),
@ -385,8 +387,8 @@ class TestObjectReplicator(unittest.TestCase):
# Write some files into '1' and run replicate- they should be moved # Write some files into '1' and run replicate- they should be moved
# to the other partitions and then node should get deleted. # to the other partitions and then node should get deleted.
cur_part = '1' cur_part = '1'
df = diskfile.DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o', df = diskfile.DiskFile(
FakeLogger()) self.devices, 'sda', cur_part, 'a', 'c', 'o', FakeLogger())
mkdirs(df.datadir) mkdirs(df.datadir)
f = open(os.path.join(df.datadir, f = open(os.path.join(df.datadir,
normalize_timestamp(time.time()) + '.data'), normalize_timestamp(time.time()) + '.data'),