Merge "Only alias when policy names change"

This commit is contained in:
Zuul 2019-09-10 10:45:20 +00:00 committed by Gerrit Code Review
commit e433a3cbec
2 changed files with 56 additions and 6 deletions

View File

@ -193,12 +193,19 @@ def _format_rule_default_yaml(default, include_help=True):
'check_str': default.check_str,
'reason': default.deprecated_reason}
text = (
'%(text)s%(deprecated_text)s\n"%(old_name)s": "rule:%(name)s"\n'
) % {'text': text,
'deprecated_text': _format_help_text(deprecated_text),
'old_name': default.deprecated_rule.name,
'name': default.name}
if default.name != default.deprecated_rule.name:
text = (
'%(text)s%(deprecated_text)s\n"%(old_name)s": "rule:%(name)s"'
'\n'
) % {'text': text,
'deprecated_text': _format_help_text(deprecated_text),
'old_name': default.deprecated_rule.name,
'name': default.name}
else:
text = (
'%(text)s%(deprecated_text)s\n'
) % {'text': text,
'deprecated_text': _format_help_text(deprecated_text)}
return text

View File

@ -227,6 +227,49 @@ class GenerateSampleYAMLTestCase(base.PolicyBaseTestCase):
# favor of "foo:create_bar":"role:fizz". foo:post_bar is being removed
# in favor of foo:create_bar
"foo:post_bar": "rule:foo:create_bar"
'''
stdout = self._capture_stdout()
with mock.patch('stevedore.named.NamedExtensionManager',
return_value=test_mgr) as mock_ext_mgr:
generator._generate_sample(['rules'], output_file=None)
mock_ext_mgr.assert_called_once_with(
'oslo.policy.policies', names=['rules'],
on_load_failure_callback=generator.on_load_failure_callback,
invoke_on_load=True
)
self.assertEqual(expected, stdout.getvalue())
def test_deprecated_policies_with_same_name(self):
deprecated_rule = policy.DeprecatedRule(
name='foo:create_bar',
check_str='role:old'
)
new_rule = policy.RuleDefault(
name='foo:create_bar',
check_str='role:fizz',
description='Create a bar.',
deprecated_rule=deprecated_rule,
deprecated_reason=(
'role:fizz is a more sane default for foo:create_bar'
),
deprecated_since='N'
)
opts = {'rules': [new_rule]}
extensions = []
for name, opts in opts.items():
ext = stevedore.extension.Extension(name=name, entry_point=None,
plugin=None, obj=opts)
extensions.append(ext)
test_mgr = stevedore.named.NamedExtensionManager.make_test_instance(
extensions=extensions, namespace=['rules'])
expected = '''# Create a bar.
#"foo:create_bar": "role:fizz"
# DEPRECATED "foo:create_bar":"role:old" has been deprecated since N
# in favor of "foo:create_bar":"role:fizz". role:fizz is a more sane
# default for foo:create_bar
'''
stdout = self._capture_stdout()
with mock.patch('stevedore.named.NamedExtensionManager',