Only alias when policy names change

Previously, oslo.policy would generate policy files with aliased names
in the event the name was changing for backwards compatibility. This
isn't needed if the name isn't changing and only the check string is
changing.

This patch adds a conditional to the generator logic that only aliases
the old name to the new name if the name is changing. Otherwise, it
only outputs comments about the deprecation.

Co-Authored-By: Ben Nemec <bnemec@redhat.com>
Change-Id: I89ff60354e4751a5096832023441d2e6166db92a
This commit is contained in:
Lance Bragstad 2019-07-25 16:28:01 +00:00 committed by Ben Nemec
parent b7da7a92ad
commit 9641e5ca0d
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',