Pep8 unit test modules w/ <= 10 violations (5 of 12)

Change-Id: I8e82c14ada52d44df5a31e08982ac79cd7e5c969
Signed-off-by: Peter Portante <peter.portante@redhat.com>
This commit is contained in:
Peter Portante 2013-08-31 23:13:15 -04:00
parent c067abd21e
commit be1cff4f1f
18 changed files with 152 additions and 106 deletions

View File

@ -105,13 +105,17 @@ class TestACL(unittest.TestCase):
'http://user:pass@www.example.com:8080', ['.example.com'])) 'http://user:pass@www.example.com:8080', ['.example.com']))
self.assert_(acl.referrer_allowed('http://www.example.com', self.assert_(acl.referrer_allowed('http://www.example.com',
['.example.com'])) ['.example.com']))
self.assert_(not acl.referrer_allowed('http://thief.example.com', self.assert_(not acl.referrer_allowed(
'http://thief.example.com',
['.example.com', '-thief.example.com'])) ['.example.com', '-thief.example.com']))
self.assert_(not acl.referrer_allowed('http://thief.example.com', self.assert_(not acl.referrer_allowed(
'http://thief.example.com',
['*', '-thief.example.com'])) ['*', '-thief.example.com']))
self.assert_(acl.referrer_allowed('http://www.example.com', self.assert_(acl.referrer_allowed(
'http://www.example.com',
['.other.com', 'www.example.com'])) ['.other.com', 'www.example.com']))
self.assert_(acl.referrer_allowed('http://www.example.com', self.assert_(acl.referrer_allowed(
'http://www.example.com',
['-.example.com', 'www.example.com'])) ['-.example.com', 'www.example.com']))
# This is considered a relative uri to the request uri, a mode not # This is considered a relative uri to the request uri, a mode not
# currently supported. # currently supported.

View File

@ -18,7 +18,7 @@ from nose import SkipTest
try: try:
# this test requires the dnspython package to be installed # this test requires the dnspython package to be installed
import dns.resolver import dns.resolver # noqa
except ImportError: except ImportError:
skip = True skip = True
else: # executed if the try has no errors else: # executed if the try has no errors

View File

@ -19,7 +19,9 @@ from swift.common.swob import Request
from swift.common.middleware import catch_errors from swift.common.middleware import catch_errors
from swift.common.utils import get_logger from swift.common.utils import get_logger
class FakeApp(object): class FakeApp(object):
def __init__(self, error=False, body_iter=None): def __init__(self, error=False, body_iter=None):
self.error = error self.error = error
self.body_iter = body_iter self.body_iter = body_iter
@ -34,9 +36,11 @@ class FakeApp(object):
else: else:
return self.body_iter return self.body_iter
def start_response(*args): def start_response(*args):
pass pass
class TestCatchErrors(unittest.TestCase): class TestCatchErrors(unittest.TestCase):
def setUp(self): def setUp(self):
@ -93,5 +97,6 @@ class TestCatchErrors(unittest.TestCase):
app(req.environ, start_response) app(req.environ, start_response)
self.assertTrue(self.logger.txn_id.endswith('-stuff')) self.assertTrue(self.logger.txn_id.endswith('-stuff'))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -466,7 +466,8 @@ class TestFormPost(unittest.TestCase):
'SERVER_NAME': '172.16.83.128', 'SERVER_NAME': '172.16.83.128',
'SERVER_PORT': '8080', 'SERVER_PORT': '8080',
'SERVER_PROTOCOL': 'HTTP/1.0', 'SERVER_PROTOCOL': 'HTTP/1.0',
'swift.account/AUTH_test': self._fake_cache_env('AUTH_test', [key]), 'swift.account/AUTH_test': self._fake_cache_env(
'AUTH_test', [key]),
'wsgi.errors': wsgi_errors, 'wsgi.errors': wsgi_errors,
'wsgi.input': wsgi_input, 'wsgi.input': wsgi_input,
'wsgi.multiprocess': False, 'wsgi.multiprocess': False,
@ -577,7 +578,8 @@ class TestFormPost(unittest.TestCase):
'SERVER_NAME': '172.16.83.128', 'SERVER_NAME': '172.16.83.128',
'SERVER_PORT': '8080', 'SERVER_PORT': '8080',
'SERVER_PROTOCOL': 'HTTP/1.0', 'SERVER_PROTOCOL': 'HTTP/1.0',
'swift.account/AUTH_test': self._fake_cache_env('AUTH_test', [key]), 'swift.account/AUTH_test': self._fake_cache_env(
'AUTH_test', [key]),
'wsgi.errors': wsgi_errors, 'wsgi.errors': wsgi_errors,
'wsgi.input': wsgi_input, 'wsgi.input': wsgi_input,
'wsgi.multiprocess': False, 'wsgi.multiprocess': False,
@ -691,7 +693,8 @@ class TestFormPost(unittest.TestCase):
'SERVER_NAME': '172.16.83.128', 'SERVER_NAME': '172.16.83.128',
'SERVER_PORT': '8080', 'SERVER_PORT': '8080',
'SERVER_PROTOCOL': 'HTTP/1.0', 'SERVER_PROTOCOL': 'HTTP/1.0',
'swift.account/AUTH_test': self._fake_cache_env('AUTH_test', [key]), 'swift.account/AUTH_test': self._fake_cache_env(
'AUTH_test', [key]),
'wsgi.errors': wsgi_errors, 'wsgi.errors': wsgi_errors,
'wsgi.input': wsgi_input, 'wsgi.input': wsgi_input,
'wsgi.multiprocess': False, 'wsgi.multiprocess': False,
@ -801,7 +804,8 @@ class TestFormPost(unittest.TestCase):
'SERVER_NAME': '172.16.83.128', 'SERVER_NAME': '172.16.83.128',
'SERVER_PORT': '8080', 'SERVER_PORT': '8080',
'SERVER_PROTOCOL': 'HTTP/1.0', 'SERVER_PROTOCOL': 'HTTP/1.0',
'swift.account/AUTH_test': self._fake_cache_env('AUTH_test', [key]), 'swift.account/AUTH_test': self._fake_cache_env(
'AUTH_test', [key]),
'wsgi.errors': wsgi_errors, 'wsgi.errors': wsgi_errors,
'wsgi.input': wsgi_input, 'wsgi.input': wsgi_input,
'wsgi.multiprocess': False, 'wsgi.multiprocess': False,

View File

@ -58,6 +58,7 @@ class SetConfigParser(object):
def start_response(*args): def start_response(*args):
pass pass
class TestCacheMiddleware(unittest.TestCase): class TestCacheMiddleware(unittest.TestCase):
def setUp(self): def setUp(self):

View File

@ -53,9 +53,11 @@ class TestNameCheckMiddleware(unittest.TestCase):
def test_invalid_character(self): def test_invalid_character(self):
for c in self.conf['forbidden_chars']: for c in self.conf['forbidden_chars']:
path = '/V1.0/1234' + c + '5' path = '/V1.0/1234' + c + '5'
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'} resp = Request.blank(
).get_response(self.test_check) path, environ={'REQUEST_METHOD': 'PUT'}).get_response(
self.assertEquals(resp.body, self.test_check)
self.assertEquals(
resp.body,
("Object/Container name contains forbidden chars from %s" ("Object/Container name contains forbidden chars from %s"
% self.conf['forbidden_chars'])) % self.conf['forbidden_chars']))
self.assertEquals(resp.status_int, 400) self.assertEquals(resp.status_int, 400)
@ -64,7 +66,8 @@ class TestNameCheckMiddleware(unittest.TestCase):
path = '/V1.0/' + 'c' * (MAX_LENGTH - 5) path = '/V1.0/' + 'c' * (MAX_LENGTH - 5)
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'} resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}
).get_response(self.test_check) ).get_response(self.test_check)
self.assertEquals(resp.body, self.assertEquals(
resp.body,
("Object/Container name longer than the allowed maximum %s" ("Object/Container name longer than the allowed maximum %s"
% self.conf['maximum_length'])) % self.conf['maximum_length']))
self.assertEquals(resp.status_int, 400) self.assertEquals(resp.status_int, 400)
@ -72,9 +75,11 @@ class TestNameCheckMiddleware(unittest.TestCase):
def test_invalid_regexp(self): def test_invalid_regexp(self):
for s in ['/.', '/..', '/./foo', '/../foo']: for s in ['/.', '/..', '/./foo', '/../foo']:
path = '/V1.0/' + s path = '/V1.0/' + s
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'} resp = Request.blank(
).get_response(self.test_check) path, environ={'REQUEST_METHOD': 'PUT'}).get_response(
self.assertEquals(resp.body, self.test_check)
self.assertEquals(
resp.body,
("Object/Container name contains a forbidden substring " ("Object/Container name contains a forbidden substring "
"from regular expression %s" "from regular expression %s"
% self.conf['forbidden_regexp'])) % self.conf['forbidden_regexp']))
@ -83,8 +88,9 @@ class TestNameCheckMiddleware(unittest.TestCase):
def test_valid_regexp(self): def test_valid_regexp(self):
for s in ['/...', '/.\.', '/foo']: for s in ['/...', '/.\.', '/foo']:
path = '/V1.0/' + s path = '/V1.0/' + s
resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'} resp = Request.blank(
).get_response(self.test_check) path, environ={'REQUEST_METHOD': 'PUT'}).get_response(
self.test_check)
self.assertEquals(resp.body, 'OK') self.assertEquals(resp.body, 'OK')

View File

@ -98,7 +98,6 @@ class TestRingBuilder(unittest.TestCase):
r0 = rb0.get_ring() r0 = rb0.get_ring()
self.assertTrue(rb0.get_ring() is r0) self.assertTrue(rb0.get_ring() is r0)
rb0.rebalance() # NO SEED rb0.rebalance() # NO SEED
rb1.rebalance(seed=10) rb1.rebalance(seed=10)
rb2.rebalance(seed=10) rb2.rebalance(seed=10)
@ -749,7 +748,8 @@ class TestRingBuilder(unittest.TestCase):
rb.rebalance() rb.rebalance()
rb.save('some.builder') rb.save('some.builder')
mock_open.assert_called_once_with('some.builder', 'wb') mock_open.assert_called_once_with('some.builder', 'wb')
mock_pickle_dump.assert_called_once_with(rb.to_dict(), mock_fh.__enter__(), mock_pickle_dump.assert_called_once_with(rb.to_dict(),
mock_fh.__enter__(),
protocol=2) protocol=2)
def test_search_devs(self): def test_search_devs(self):

View File

@ -56,7 +56,8 @@ class TestUtils(unittest.TestCase):
self.test_devs = get_test_devs() self.test_devs = get_test_devs()
def test_tiers_for_dev(self): def test_tiers_for_dev(self):
self.assertEqual(tiers_for_dev(self.test_dev), self.assertEqual(
tiers_for_dev(self.test_dev),
((1,), ((1,),
(1, 1), (1, 1),
(1, 1, '192.168.1.1:6000'), (1, 1, '192.168.1.1:6000'),
@ -110,7 +111,8 @@ class TestUtils(unittest.TestCase):
res = parse_search_value('R127.0.0.10') res = parse_search_value('R127.0.0.10')
self.assertEqual(res, {'replication_ip': '127.0.0.10'}) self.assertEqual(res, {'replication_ip': '127.0.0.10'})
res = parse_search_value('R[127.0.0.10]:20000') res = parse_search_value('R[127.0.0.10]:20000')
self.assertEqual(res, {'replication_ip': '127.0.0.10', 'replication_port': 20000}) self.assertEqual(res, {'replication_ip': '127.0.0.10',
'replication_port': 20000})
res = parse_search_value('R:20000') res = parse_search_value('R:20000')
self.assertEqual(res, {'replication_port': 20000}) self.assertEqual(res, {'replication_port': 20000})
res = parse_search_value('/sdb1') res = parse_search_value('/sdb1')

View File

@ -33,7 +33,8 @@ class TestBufferedHTTP(unittest.TestCase):
fp.write('HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\n' fp.write('HTTP/1.1 200 OK\r\nContent-Length: 8\r\n\r\n'
'RESPONSE') 'RESPONSE')
fp.flush() fp.flush()
self.assertEquals(fp.readline(), self.assertEquals(
fp.readline(),
'PUT /dev/%s/path/..%%25/?omg&no=%%7f HTTP/1.1\r\n' % 'PUT /dev/%s/path/..%%25/?omg&no=%%7f HTTP/1.1\r\n' %
expected_par) expected_par)
headers = {} headers = {}
@ -52,10 +53,12 @@ class TestBufferedHTTP(unittest.TestCase):
event = spawn(accept, par) event = spawn(accept, par)
try: try:
with Timeout(3): with Timeout(3):
conn = bufferedhttp.http_connect('127.0.0.1', conn = bufferedhttp.http_connect(
bindsock.getsockname()[1], 'dev', par, 'PUT', '127.0.0.1', bindsock.getsockname()[1], 'dev', par,
'/path/..%/', {'content-length': 7, 'x-header': 'PUT', '/path/..%/', {
'value'}, query_string='omg&no=%7f') 'content-length': 7,
'x-header': 'value'},
query_string='omg&no=%7f')
conn.send('REQUEST\r\n') conn.send('REQUEST\r\n')
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
@ -88,10 +91,12 @@ class TestBufferedHTTP(unittest.TestCase):
origHTTPSConnection = bufferedhttp.HTTPSConnection origHTTPSConnection = bufferedhttp.HTTPSConnection
bufferedhttp.HTTPSConnection = MockHTTPSConnection bufferedhttp.HTTPSConnection = MockHTTPSConnection
try: try:
bufferedhttp.http_connect('127.0.0.1', 8080, 'sda', 1, 'GET', '/', bufferedhttp.http_connect(
'127.0.0.1', 8080, 'sda', 1, 'GET', '/',
headers={'x-one': '1', 'x-two': 2, 'x-three': 3.0, headers={'x-one': '1', 'x-two': 2, 'x-three': 3.0,
'x-four': {'crazy': 'value'}}, ssl=True) 'x-four': {'crazy': 'value'}}, ssl=True)
bufferedhttp.http_connect_raw('127.0.0.1', 8080, 'GET', '/', bufferedhttp.http_connect_raw(
'127.0.0.1', 8080, 'GET', '/',
headers={'x-one': '1', 'x-two': 2, 'x-three': 3.0, headers={'x-one': '1', 'x-two': 2, 'x-three': 3.0,
'x-four': {'crazy': 'value'}}, ssl=True) 'x-four': {'crazy': 'value'}}, ssl=True)
finally: finally:

View File

@ -1188,8 +1188,8 @@ class TestServer(unittest.TestCase):
4: conf4, 4: conf4,
} }
self.assertEquals(server.launch(once=True), expected) self.assertEquals(server.launch(once=True), expected)
self.assertEquals(mock_spawn.conf_files, [conf1, conf2, self.assertEquals(mock_spawn.conf_files, [
conf3, conf4]) conf1, conf2, conf3, conf4])
expected = { expected = {
'once': True, 'once': True,
} }

View File

@ -371,15 +371,16 @@ class TestRequest(unittest.TestCase):
def test_invalid_req_environ_property_args(self): def test_invalid_req_environ_property_args(self):
# getter only property # getter only property
try: try:
req = swift.common.swob.Request.blank('/', params={'a': 'b'}) swift.common.swob.Request.blank('/', params={'a': 'b'})
except TypeError as e: except TypeError as e:
self.assertEquals("got unexpected keyword argument 'params'", str(e)) self.assertEquals("got unexpected keyword argument 'params'",
str(e))
else: else:
self.assert_(False, "invalid req_environ_property " self.assert_(False, "invalid req_environ_property "
"didn't raise error!") "didn't raise error!")
# regular attribute # regular attribute
try: try:
req = swift.common.swob.Request.blank('/', _params_cache={'a': 'b'}) swift.common.swob.Request.blank('/', _params_cache={'a': 'b'})
except TypeError as e: except TypeError as e:
self.assertEquals("got unexpected keyword " self.assertEquals("got unexpected keyword "
"argument '_params_cache'", str(e)) "argument '_params_cache'", str(e))
@ -388,7 +389,7 @@ class TestRequest(unittest.TestCase):
"didn't raise error!") "didn't raise error!")
# non-existant attribute # non-existant attribute
try: try:
req = swift.common.swob.Request.blank('/', params_cache={'a': 'b'}) swift.common.swob.Request.blank('/', params_cache={'a': 'b'})
except TypeError as e: except TypeError as e:
self.assertEquals("got unexpected keyword " self.assertEquals("got unexpected keyword "
"argument 'params_cache'", str(e)) "argument 'params_cache'", str(e))
@ -397,7 +398,7 @@ class TestRequest(unittest.TestCase):
"didn't raise error!") "didn't raise error!")
# method # method
try: try:
req = swift.common.swob.Request.blank( swift.common.swob.Request.blank(
'/', as_referer='GET http://example.com') '/', as_referer='GET http://example.com')
except TypeError as e: except TypeError as e:
self.assertEquals("got unexpected keyword " self.assertEquals("got unexpected keyword "
@ -406,7 +407,6 @@ class TestRequest(unittest.TestCase):
self.assert_(False, "invalid req_environ_property " self.assert_(False, "invalid req_environ_property "
"didn't raise error!") "didn't raise error!")
def test_blank_path_info_precedence(self): def test_blank_path_info_precedence(self):
blank = swift.common.swob.Request.blank blank = swift.common.swob.Request.blank
req = blank('/a') req = blank('/a')
@ -725,7 +725,7 @@ class TestRequest(unittest.TestCase):
req = swift.common.swob.Request.blank( req = swift.common.swob.Request.blank(
u'/', u'/',
environ={'REQUEST_METHOD': 'PUT', 'PATH_INFO': '/'}, environ={'REQUEST_METHOD': 'PUT', 'PATH_INFO': '/'},
body='x'*42) body='x' * 42)
self.assertEquals(req.message_length(), 42) self.assertEquals(req.message_length(), 42)
req.headers['Content-Length'] = 'abc' req.headers['Content-Length'] = 'abc'
@ -741,7 +741,7 @@ class TestRequest(unittest.TestCase):
u'/', u'/',
environ={'REQUEST_METHOD': 'PUT', 'PATH_INFO': '/'}, environ={'REQUEST_METHOD': 'PUT', 'PATH_INFO': '/'},
headers={'transfer-encoding': 'chunked'}, headers={'transfer-encoding': 'chunked'},
body='x'*42) body='x' * 42)
self.assertEquals(req.message_length(), None) self.assertEquals(req.message_length(), None)
req.headers['Transfer-Encoding'] = 'gzip,chunked' req.headers['Transfer-Encoding'] = 'gzip,chunked'

View File

@ -441,8 +441,8 @@ class TestUtils(unittest.TestCase):
os.path.isdir('/dev/log'): os.path.isdir('/dev/log'):
# Since socket on OSX is in /var/run/syslog, there will be # Since socket on OSX is in /var/run/syslog, there will be
# a fallback to UDP. # a fallback to UDP.
expected_args.append(((), expected_args.append(
{'facility': orig_sysloghandler.LOG_LOCAL3})) ((), {'facility': orig_sysloghandler.LOG_LOCAL3}))
self.assertEquals(expected_args, syslog_handler_args) self.assertEquals(expected_args, syslog_handler_args)
syslog_handler_args = [] syslog_handler_args = []
@ -1395,9 +1395,11 @@ log_name = %(yarr)s'''
with patch.object(utils.tpool, 'execute', lambda f: f()): with patch.object(utils.tpool, 'execute', lambda f: f()):
self.assertTrue( self.assertTrue(
utils.tpool_reraise(MagicMock(return_value='test1')), 'test1') utils.tpool_reraise(MagicMock(return_value='test1')), 'test1')
self.assertRaises(Exception, self.assertRaises(
Exception,
utils.tpool_reraise, MagicMock(side_effect=Exception('test2'))) utils.tpool_reraise, MagicMock(side_effect=Exception('test2')))
self.assertRaises(BaseException, self.assertRaises(
BaseException,
utils.tpool_reraise, utils.tpool_reraise,
MagicMock(side_effect=BaseException('test3'))) MagicMock(side_effect=BaseException('test3')))
@ -1429,8 +1431,10 @@ log_name = %(yarr)s'''
with utils.lock_file(nt.name, timeout=3, unlink=False) as f: with utils.lock_file(nt.name, timeout=3, unlink=False) as f:
try: try:
with utils.lock_file(nt.name, timeout=1, unlink=False) as f: with utils.lock_file(
self.assertTrue(False, "Expected LockTimeout exception") nt.name, timeout=1, unlink=False) as f:
self.assertTrue(
False, "Expected LockTimeout exception")
except LockTimeout: except LockTimeout:
pass pass
@ -1438,7 +1442,8 @@ log_name = %(yarr)s'''
self.assertEqual(f.read(), "test string\nanother string") self.assertEqual(f.read(), "test string\nanother string")
# we have a lock, now let's try to get a newer one # we have a lock, now let's try to get a newer one
fd = os.open(nt.name, flags) fd = os.open(nt.name, flags)
self.assertRaises(IOError, fcntl.flock, fd, fcntl.LOCK_EX | fcntl.LOCK_NB) self.assertRaises(
IOError, fcntl.flock, fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
self.assertRaises(OSError, os.remove, nt.name) self.assertRaises(OSError, os.remove, nt.name)

View File

@ -42,21 +42,24 @@ from mock import patch
def _fake_rings(tmpdir): def _fake_rings(tmpdir):
with closing(GzipFile(os.path.join(tmpdir, 'account.ring.gz'), 'wb')) as f: account_ring_path = os.path.join(tmpdir, 'account.ring.gz')
with closing(GzipFile(account_ring_path, 'wb')) as f:
pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]], pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]],
[{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1',
'port': 6012}, 'port': 6012},
{'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1',
'port': 6022}], 30), 'port': 6022}], 30),
f) f)
with closing(GzipFile(os.path.join(tmpdir, 'container.ring.gz'), 'wb')) as f: container_ring_path = os.path.join(tmpdir, 'container.ring.gz')
with closing(GzipFile(container_ring_path, 'wb')) as f:
pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]], pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]],
[{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1',
'port': 6011}, 'port': 6011},
{'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1',
'port': 6021}], 30), 'port': 6021}], 30),
f) f)
with closing(GzipFile(os.path.join(tmpdir, 'object.ring.gz'), 'wb')) as f: object_ring_path = os.path.join(tmpdir, 'object.ring.gz')
with closing(GzipFile(object_ring_path, 'wb')) as f:
pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]], pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]],
[{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1',
'port': 6010}, 'port': 6010},
@ -535,6 +538,7 @@ class TestWSGI(unittest.TestCase):
self.assertEquals(r.environ['SCRIPT_NAME'], '') self.assertEquals(r.environ['SCRIPT_NAME'], '')
self.assertEquals(r.environ['PATH_INFO'], '/override') self.assertEquals(r.environ['PATH_INFO'], '/override')
class TestWSGIContext(unittest.TestCase): class TestWSGIContext(unittest.TestCase):
def test_app_call(self): def test_app_call(self):

View File

@ -41,11 +41,12 @@ class TestContainerUpdater(unittest.TestCase):
os.mkdir(self.testdir) os.mkdir(self.testdir)
ring_file = os.path.join(self.testdir, 'account.ring.gz') ring_file = os.path.join(self.testdir, 'account.ring.gz')
with closing(GzipFile(ring_file, 'wb')) as f: with closing(GzipFile(ring_file, 'wb')) as f:
pickle.dump(RingData([[0, 1, 0, 1], [1, 0, 1, 0]], pickle.dump(
[{'id': 0, 'ip': '127.0.0.1', 'port': 12345, 'device': 'sda1', RingData([[0, 1, 0, 1], [1, 0, 1, 0]],
'zone': 0}, [{'id': 0, 'ip': '127.0.0.1', 'port': 12345,
{'id': 1, 'ip': '127.0.0.1', 'port': 12345, 'device': 'sda1', 'device': 'sda1', 'zone': 0},
'zone': 2}], 30), {'id': 1, 'ip': '127.0.0.1', 'port': 12345,
'device': 'sda1', 'zone': 2}], 30),
f) f)
self.devices_dir = os.path.join(self.testdir, 'devices') self.devices_dir = os.path.join(self.testdir, 'devices')
os.mkdir(self.devices_dir) os.mkdir(self.devices_dir)
@ -134,12 +135,14 @@ class TestContainerUpdater(unittest.TestCase):
return err return err
return None return None
bindsock = listen(('127.0.0.1', 0)) bindsock = listen(('127.0.0.1', 0))
def spawn_accepts(): def spawn_accepts():
events = [] events = []
for _junk in xrange(2): for _junk in xrange(2):
sock, addr = bindsock.accept() sock, addr = bindsock.accept()
events.append(spawn(accept, sock, addr, 201)) events.append(spawn(accept, sock, addr, 201))
return events return events
spawned = spawn(spawn_accepts) spawned = spawn(spawn_accepts)
for dev in cu.get_account_ring().devs: for dev in cu.get_account_ring().devs:
if dev is not None: if dev is not None:
@ -173,6 +176,7 @@ class TestContainerUpdater(unittest.TestCase):
cb.initialize(normalize_timestamp(1)) cb.initialize(normalize_timestamp(1))
cb.put_object('\xce\xa9', normalize_timestamp(2), 3, 'text/plain', cb.put_object('\xce\xa9', normalize_timestamp(2), 3, 'text/plain',
'68b329da9893e34099c7d8ad5cb9c940') '68b329da9893e34099c7d8ad5cb9c940')
def accept(sock, addr): def accept(sock, addr):
try: try:
with Timeout(3): with Timeout(3):
@ -186,7 +190,9 @@ class TestContainerUpdater(unittest.TestCase):
traceback.print_exc() traceback.print_exc()
return err return err
return None return None
bindsock = listen(('127.0.0.1', 0)) bindsock = listen(('127.0.0.1', 0))
def spawn_accepts(): def spawn_accepts():
events = [] events = []
for _junk in xrange(2): for _junk in xrange(2):
@ -194,6 +200,7 @@ class TestContainerUpdater(unittest.TestCase):
sock, addr = bindsock.accept() sock, addr = bindsock.accept()
events.append(spawn(accept, sock, addr)) events.append(spawn(accept, sock, addr))
return events return events
spawned = spawn(spawn_accepts) spawned = spawn(spawn_accepts)
for dev in cu.get_account_ring().devs: for dev in cu.get_account_ring().devs:
if dev is not None: if dev is not None:

View File

@ -46,10 +46,10 @@ class TestObjectUpdater(unittest.TestCase):
with closing(GzipFile(ring_file, 'wb')) as f: with closing(GzipFile(ring_file, 'wb')) as f:
pickle.dump( pickle.dump(
RingData([[0, 1, 0, 1], [1, 0, 1, 0]], RingData([[0, 1, 0, 1], [1, 0, 1, 0]],
[{'id': 0, 'ip': '127.0.0.1', 'port': 1, 'device': 'sda1', [{'id': 0, 'ip': '127.0.0.1', 'port': 1,
'zone': 0}, 'device': 'sda1', 'zone': 0},
{'id': 1, 'ip': '127.0.0.1', 'port': 1, 'device': 'sda1', {'id': 1, 'ip': '127.0.0.1', 'port': 1,
'zone': 2}], 30), 'device': 'sda1', 'zone': 2}], 30),
f) f)
self.devices_dir = os.path.join(self.testdir, 'devices') self.devices_dir = os.path.join(self.testdir, 'devices')
os.mkdir(self.devices_dir) os.mkdir(self.devices_dir)

View File

@ -124,7 +124,8 @@ class TestFuncs(unittest.TestCase):
self.assertEquals(env['swift.account/a'], info_a) self.assertEquals(env['swift.account/a'], info_a)
self.assertEquals(env['swift.container/a/c'], info_c) self.assertEquals(env['swift.container/a/c'], info_c)
# This time do a non cached call to account than non cached to container # This time do a non cached call to account than non cached to
# container
env = {} # abandon previous call to env env = {} # abandon previous call to env
with patch('swift.proxy.controllers.base.' with patch('swift.proxy.controllers.base.'
'_prepare_pre_auth_info_request', FakeRequest): '_prepare_pre_auth_info_request', FakeRequest):
@ -137,7 +138,8 @@ class TestFuncs(unittest.TestCase):
self.assertEquals(env['swift.account/a'], info_a) self.assertEquals(env['swift.account/a'], info_a)
self.assertEquals(env['swift.container/a/c'], info_c) self.assertEquals(env['swift.container/a/c'], info_c)
# This time do an env cached call to container while account is not cached # This time do an env cached call to container while account is not
# cached
del(env['swift.account/a']) del(env['swift.account/a'])
info_c = get_info(None, env, 'a', 'c') info_c = get_info(None, env, 'a', 'c')
# Check that you got proper info # Check that you got proper info
@ -217,7 +219,7 @@ class TestFuncs(unittest.TestCase):
cache_key = get_container_memcache_key("account", "cont") cache_key = get_container_memcache_key("account", "cont")
env_key = 'swift.%s' % cache_key env_key = 'swift.%s' % cache_key
req = Request.blank("/v1/account/cont", req = Request.blank("/v1/account/cont",
environ={ env_key: {'bytes': 3867}, environ={env_key: {'bytes': 3867},
'swift.cache': FakeCache({})}) 'swift.cache': FakeCache({})})
resp = get_container_info(req.environ, 'xxx') resp = get_container_info(req.environ, 'xxx')
self.assertEquals(resp['bytes'], 3867) self.assertEquals(resp['bytes'], 3867)
@ -266,7 +268,7 @@ class TestFuncs(unittest.TestCase):
cache_key = get_account_memcache_key("account") cache_key = get_account_memcache_key("account")
env_key = 'swift.%s' % cache_key env_key = 'swift.%s' % cache_key
req = Request.blank("/v1/account", req = Request.blank("/v1/account",
environ={ env_key: {'bytes': 3867}, environ={env_key: {'bytes': 3867},
'swift.cache': FakeCache({})}) 'swift.cache': FakeCache({})})
resp = get_account_info(req.environ, 'xxx') resp = get_account_info(req.environ, 'xxx')
self.assertEquals(resp['bytes'], 3867) self.assertEquals(resp['bytes'], 3867)

View File

@ -21,6 +21,7 @@ import swift
from swift.proxy import server as proxy_server from swift.proxy import server as proxy_server
from test.unit import FakeRing, FakeMemcache, fake_http_connect from test.unit import FakeRing, FakeMemcache, fake_http_connect
@contextmanager @contextmanager
def set_http_connect(*args, **kwargs): def set_http_connect(*args, **kwargs):
old_connect = swift.proxy.controllers.base.http_connect old_connect = swift.proxy.controllers.base.http_connect
@ -36,7 +37,6 @@ def set_http_connect(*args, **kwargs):
swift.proxy.controllers.container.http_connect = old_connect swift.proxy.controllers.container.http_connect = old_connect
class TestObjControllerWriteAffinity(unittest.TestCase): class TestObjControllerWriteAffinity(unittest.TestCase):
def setUp(self): def setUp(self):
self.app = proxy_server.Application( self.app = proxy_server.Application(
@ -61,7 +61,8 @@ class TestObjControllerWriteAffinity(unittest.TestCase):
def test_iter_nodes_local_first_moves_locals_first(self): def test_iter_nodes_local_first_moves_locals_first(self):
controller = proxy_server.ObjectController(self.app, 'a', 'c', 'o') controller = proxy_server.ObjectController(self.app, 'a', 'c', 'o')
self.app.write_affinity_is_local_fn = (lambda node: node['region'] == 1) self.app.write_affinity_is_local_fn = (
lambda node: node['region'] == 1)
self.app.write_affinity_node_count = lambda ring: 4 self.app.write_affinity_node_count = lambda ring: 4
all_nodes = self.app.object_ring.get_part_nodes(1) all_nodes = self.app.object_ring.get_part_nodes(1)
@ -80,8 +81,8 @@ class TestObjControllerWriteAffinity(unittest.TestCase):
controller = proxy_server.ObjectController(self.app, 'a', 'c', 'o') controller = proxy_server.ObjectController(self.app, 'a', 'c', 'o')
self.app.conn_timeout = 0.1 self.app.conn_timeout = 0.1
with set_http_connect(200, slow_connect=True): with set_http_connect(200, slow_connect=True):
nodes = [dict(ip='', port='', device=''),] nodes = [dict(ip='', port='', device='')]
res = controller._connect_put_node(nodes, '', '', {}, ('','')) res = controller._connect_put_node(nodes, '', '', {}, ('', ''))
self.assertTrue(res is None) self.assertTrue(res is None)
if __name__ == '__main__': if __name__ == '__main__':