diff --git a/oslo_policy/generator.py b/oslo_policy/generator.py index 77dbf7c3..fa5f4ee2 100644 --- a/oslo_policy/generator.py +++ b/oslo_policy/generator.py @@ -136,24 +136,31 @@ def _format_rule_default_yaml(default, include_help=True): if default.deprecated_for_removal: text = ( '# DEPRECATED\n# "%(name)s" has been deprecated since ' - '%(since)s.\n%(reason)s\n%(text)s' + '%(since)s.\n%(reason)s\n%(text)s\n' + '"%(name)s": "%(check_str)s"' ) % {'name': default.name, + 'check_str': default.check_str, 'since': default.deprecated_since, 'reason': _format_help_text(default.deprecated_reason), 'text': text} elif default.deprecated_rule: - text = ( - '# DEPRECATED\n# "%(old_name)s":"%(old_check_str)s" has been ' + # This issues a deprecation warning but aliases the old policy name + # with the new policy name for compatibility. + deprecated_text = ( + 'DEPRECATED\n"%(old_name)s":"%(old_check_str)s" has been ' 'deprecated since %(since)s in favor of ' - '"%(name)s":"%(check_str)s".\n' - '%(reason)s\n%(text)s' + '"%(name)s":"%(check_str)s".' ) % {'old_name': default.deprecated_rule.name, 'old_check_str': default.deprecated_rule.check_str, 'since': default.deprecated_since, 'name': default.name, - 'check_str': default.check_str, - 'reason': _format_help_text(default.deprecated_reason), - 'text': text} + 'check_str': default.check_str} + + text = ( + '%(deprecated_text)s\n"%(old_name)s": "rule:%(name)s"\n' + ) % {'deprecated_text': _format_help_text(deprecated_text), + 'old_name': default.deprecated_rule.name, + 'name': default.name} return text diff --git a/oslo_policy/tests/test_generator.py b/oslo_policy/tests/test_generator.py index 97a4f594..3e6b4255 100644 --- a/oslo_policy/tests/test_generator.py +++ b/oslo_policy/tests/test_generator.py @@ -160,6 +160,47 @@ class GenerateSampleYAMLTestCase(base.PolicyBaseTestCase): self.assertEqual(expected, stdout.getvalue()) + def test_deprecated_policies_are_aliased_to_new_names(self): + deprecated_rule = policy.DeprecatedRule( + name='foo:post_bar', + check_str='role:fizz' + ) + new_rule = policy.RuleDefault( + name='foo:create_bar', + check_str='role:fizz', + description='Create a bar.', + deprecated_rule=deprecated_rule, + deprecated_reason=( + 'foo:post_bar is being removed in favor of 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 = '''# DEPRECATED +# "foo:post_bar":"role:fizz" has been deprecated since N in favor of +# "foo:create_bar":"role:fizz". +"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_empty_line_formatting(self): rule = [policy.RuleDefault('admin', 'is_admin:True', description='Check Summary \n'