Merge "Add validation for sorting_method values"

This commit is contained in:
Zuul 2018-10-11 02:34:31 +00:00 committed by Gerrit Code Review
commit 76a3908667
2 changed files with 37 additions and 0 deletions

View File

@ -87,6 +87,9 @@ def _label_for_policy(policy):
return '(default)'
VALID_SORTING_METHODS = ('shuffle', 'timing', 'affinity')
class ProxyOverrideOptions(object):
"""
Encapsulates proxy server options that may be overridden e.g. for
@ -100,6 +103,11 @@ class ProxyOverrideOptions(object):
return override_conf.get(key, base_conf.get(key, default))
self.sorting_method = get('sorting_method', 'shuffle').lower()
if self.sorting_method not in VALID_SORTING_METHODS:
raise ValueError(
'Invalid sorting_method value; must be one of %s, not %r' % (
', '.join(VALID_SORTING_METHODS), self.sorting_method))
self.read_affinity = get('read_affinity', '')
try:
self.read_affinity_sort_key = affinity_key_function(

View File

@ -1739,6 +1739,35 @@ class TestProxyServerConfigLoading(unittest.TestCase):
app = self._write_conf_and_load_app(conf_sections)
self._check_policy_options(app, exp_options, {})
def test_per_policy_conf_invalid_sorting_method_value(self):
def do_test(conf_sections, scope):
with self.assertRaises(ValueError) as cm:
self._write_conf_and_load_app(conf_sections)
self.assertEqual(
'Invalid sorting_method value; must be one of shuffle, '
"timing, affinity, not 'broken' for %s" % scope,
cm.exception.message)
conf_sections = """
[app:proxy-server]
use = egg:swift#proxy
sorting_method = shuffle
[proxy-server:policy:0]
sorting_method = broken
"""
do_test(conf_sections, 'policy 0 (nulo)')
conf_sections = """
[app:proxy-server]
use = egg:swift#proxy
sorting_method = broken
[proxy-server:policy:0]
sorting_method = shuffle
"""
do_test(conf_sections, '(default)')
def test_per_policy_conf_invalid_read_affinity_value(self):
def do_test(conf_sections, label):
with self.assertRaises(ValueError) as cm: