diff options
author | Zuul <zuul@review.opendev.org> | 2019-09-10 10:45:20 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2019-09-10 10:45:20 +0000 |
commit | e433a3cbec5da0241bb1716b8bfda9fa927069ce (patch) | |
tree | c6fce63696a3022b579af7af7c4c5fc0d12e69ee | |
parent | 65ca33adb99622e48604e01edc3f1925443681ef (diff) | |
parent | 9641e5ca0d758376fb7f7f5156056d5206d71046 (diff) | |
download | oslo-policy-e433a3cbec5da0241bb1716b8bfda9fa927069ce.tar.gz |
Merge "Only alias when policy names change"2.3.2
-rw-r--r-- | oslo_policy/generator.py | 19 | ||||
-rw-r--r-- | oslo_policy/tests/test_generator.py | 43 |
2 files changed, 56 insertions, 6 deletions
diff --git a/oslo_policy/generator.py b/oslo_policy/generator.py index 1ea768e..bd75389 100644 --- a/oslo_policy/generator.py +++ b/oslo_policy/generator.py @@ -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 diff --git a/oslo_policy/tests/test_generator.py b/oslo_policy/tests/test_generator.py index ab0940a..18c5406 100644 --- a/oslo_policy/tests/test_generator.py +++ b/oslo_policy/tests/test_generator.py @@ -239,6 +239,49 @@ class GenerateSampleYAMLTestCase(base.PolicyBaseTestCase): ) 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', + 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_formatting(self, description, expected): rule = [policy.RuleDefault('admin', 'is_admin:True', description=description)] |