py3: port common/wsgi.py

Note that we're punting on configuring socket buffer sizes (for now)

Change-Id: I285a9b521fd0af381a227e0e824bc391817547f4
This commit is contained in:
Tim Burke 2018-02-01 15:25:53 -08:00 committed by Thiago da Silva
parent 5cb0869743
commit 624b5310b4
7 changed files with 28 additions and 18 deletions

View File

@ -119,6 +119,8 @@ class ConfigString(NamedConfigLoader):
}
self.parser = loadwsgi.NicerConfigParser("string", defaults=defaults)
self.parser.optionxform = str # Don't lower-case keys
# Defaults don't need interpolation (crazy PasteDeploy...)
self.parser.defaults = lambda: dict(self.parser._defaults, **defaults)
self.parser.readfp(self.contents)

View File

@ -608,10 +608,10 @@ class DiskFileRouter(object):
self.policy_to_manager = {}
for policy in POLICIES:
manager_cls = self.policy_type_to_manager_cls[policy.policy_type]
self.policy_to_manager[policy] = manager_cls(*args, **kwargs)
self.policy_to_manager[int(policy)] = manager_cls(*args, **kwargs)
def __getitem__(self, policy):
return self.policy_to_manager[policy]
return self.policy_to_manager[int(policy)]
class BaseDiskFileManager(object):

View File

@ -15,6 +15,7 @@
""" Object Server for Swift """
import six
import six.moves.cPickle as pickle
import json
import os
@ -170,7 +171,9 @@ class ObjectController(BaseStorageServer):
# disk_chunk_size parameter. However, it affects all created sockets
# using this class so we have chosen to tie it to the
# network_chunk_size parameter value instead.
socket._fileobject.default_bufsize = self.network_chunk_size
if six.PY2:
socket._fileobject.default_bufsize = self.network_chunk_size
# TODO: find a way to enable similar functionality in py3
# Provide further setup specific to an object server implementation.
self.setup(conf)

View File

@ -150,11 +150,11 @@ class ObjectControllerRouter(object):
def __init__(self):
self.policy_to_controller_cls = {}
for policy in POLICIES:
self.policy_to_controller_cls[policy] = \
self.policy_to_controller_cls[int(policy)] = \
self.policy_type_to_controller_map[policy.policy_type]
def __getitem__(self, policy):
return self.policy_to_controller_cls[policy]
return self.policy_to_controller_cls[int(policy)]
class BaseObjectController(Controller):

View File

@ -276,7 +276,10 @@ class Application(object):
#
# ** Because it affects the client as well, currently, we use the
# client chunk size as the govenor and not the object chunk size.
socket._fileobject.default_bufsize = self.client_chunk_size
if sys.version_info < (3,):
socket._fileobject.default_bufsize = self.client_chunk_size
# TODO: find a way to enable similar functionality in py3
self.expose_info = config_true_value(
conf.get('expose_info', 'yes'))
self.disallowed_sections = list_from_csv(

View File

@ -203,7 +203,7 @@ class TestWSGI(unittest.TestCase):
conf_file = os.path.join(tempdir, 'file.conf')
def _write_and_load_conf_file(conf):
with open(conf_file, 'wb') as fd:
with open(conf_file, 'wt') as fd:
fd.write(dedent(conf))
return wsgi.load_app_config(conf_file)
@ -659,12 +659,12 @@ class TestWSGI(unittest.TestCase):
oldenv = {}
newenv = wsgi.make_pre_authed_env(oldenv)
self.assertTrue('wsgi.input' in newenv)
self.assertEqual(newenv['wsgi.input'].read(), '')
self.assertEqual(newenv['wsgi.input'].read(), b'')
oldenv = {'wsgi.input': BytesIO(b'original wsgi.input')}
newenv = wsgi.make_pre_authed_env(oldenv)
self.assertTrue('wsgi.input' in newenv)
self.assertEqual(newenv['wsgi.input'].read(), '')
self.assertEqual(newenv['wsgi.input'].read(), b'')
oldenv = {'swift.source': 'UT'}
newenv = wsgi.make_pre_authed_env(oldenv)
@ -677,7 +677,7 @@ class TestWSGI(unittest.TestCase):
def test_pre_auth_req(self):
class FakeReq(object):
@classmethod
def fake_blank(cls, path, environ=None, body='', headers=None):
def fake_blank(cls, path, environ=None, body=b'', headers=None):
if environ is None:
environ = {}
if headers is None:
@ -687,7 +687,7 @@ class TestWSGI(unittest.TestCase):
was_blank = Request.blank
Request.blank = FakeReq.fake_blank
wsgi.make_pre_authed_request({'HTTP_X_TRANS_ID': '1234'},
'PUT', '/', body='tester', headers={})
'PUT', '/', body=b'tester', headers={})
wsgi.make_pre_authed_request({'HTTP_X_TRANS_ID': '1234'},
'PUT', '/', headers={})
Request.blank = was_blank
@ -695,7 +695,7 @@ class TestWSGI(unittest.TestCase):
def test_pre_auth_req_with_quoted_path(self):
r = wsgi.make_pre_authed_request(
{'HTTP_X_TRANS_ID': '1234'}, 'PUT', path=quote('/a space'),
body='tester', headers={})
body=b'tester', headers={})
self.assertEqual(r.path, quote('/a space'))
def test_pre_auth_req_drops_query(self):
@ -711,8 +711,8 @@ class TestWSGI(unittest.TestCase):
def test_pre_auth_req_with_body(self):
r = wsgi.make_pre_authed_request(
{'QUERY_STRING': 'original'}, 'GET', 'path', 'the body')
self.assertEqual(r.body, 'the body')
{'QUERY_STRING': 'original'}, 'GET', 'path', b'the body')
self.assertEqual(r.body, b'the body')
def test_pre_auth_creates_script_name(self):
e = wsgi.make_pre_authed_env({})
@ -730,9 +730,9 @@ class TestWSGI(unittest.TestCase):
def test_pre_auth_req_swift_source(self):
r = wsgi.make_pre_authed_request(
{'QUERY_STRING': 'original'}, 'GET', 'path', 'the body',
{'QUERY_STRING': 'original'}, 'GET', 'path', b'the body',
swift_source='UT')
self.assertEqual(r.body, 'the body')
self.assertEqual(r.body, b'the body')
self.assertEqual(r.environ['swift.source'], 'UT')
def test_run_server_global_conf_callback(self):
@ -1363,7 +1363,8 @@ class TestWSGIContext(unittest.TestCase):
self.assertEqual('aaaaa', next(iterator))
self.assertEqual('bbbbb', next(iterator))
iterable.close()
self.assertRaises(StopIteration, iterator.next)
with self.assertRaises(StopIteration):
next(iterator)
def test_update_content_length(self):
statuses = ['200 Ok']

View File

@ -40,7 +40,8 @@ commands =
test/unit/common/test_manager.py \
test/unit/common/test_splice.py \
test/unit/common/test_storage_policy.py \
test/unit/common/test_utils.py
test/unit/common/test_utils.py \
test/unit/common/test_wsgi.py
[testenv:py35]
commands = {[testenv:py34]commands}