From 0f31938dd720015444e03f0056c0cfc0e4b8e932 Mon Sep 17 00:00:00 2001 From: Lance Bragstad Date: Fri, 1 Jun 2018 21:25:19 +0000 Subject: [PATCH] Remove erroneous newline in sample generation The sample generation code for policies has a couple different cases that make sure deprecated rules have descriptions and reasoning formatted in the comment section. One of the cases that handles policies deprecated for removal was injecting an extra newline in the comment text that threw off the formatting of the sample, leaving the subsequent policy uncommented, and visually unpleasing. This commit removes the extra newline in the formatting logic for deprecated policies and adds a test case for the behavior. Change-Id: I76338d2fbaccf3b43e0da04732fd9df3c05dfbda Closes-Bug: 1771442 --- oslo_policy/generator.py | 3 +- oslo_policy/tests/test_generator.py | 39 +++++++++++++++++++ ...for-deprecated-rules-d465292e4155f483.yaml | 6 +++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/fix-rendering-for-deprecated-rules-d465292e4155f483.yaml diff --git a/oslo_policy/generator.py b/oslo_policy/generator.py index 38c9b5b7..6694559c 100644 --- a/oslo_policy/generator.py +++ b/oslo_policy/generator.py @@ -136,8 +136,7 @@ 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\n' - '"%(name)s": "%(check_str)s"' + '%(since)s.\n%(reason)s\n%(text)s' ) % {'name': default.name, 'check_str': default.check_str, 'since': default.deprecated_since, diff --git a/oslo_policy/tests/test_generator.py b/oslo_policy/tests/test_generator.py index e0aac6d3..2052572b 100644 --- a/oslo_policy/tests/test_generator.py +++ b/oslo_policy/tests/test_generator.py @@ -160,6 +160,45 @@ class GenerateSampleYAMLTestCase(base.PolicyBaseTestCase): self.assertEqual(expected, stdout.getvalue()) + def test_policies_deprecated_for_removal(self): + rule = policy.RuleDefault( + name='foo:post_bar', + check_str='role:fizz', + description='Create a bar.', + deprecated_for_removal=True, + deprecated_reason='This policy is not used anymore', + deprecated_since='N' + ) + opts = {'rules': [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" has been deprecated since N. +# This policy is not used anymore +# Create a bar. +#"foo:post_bar": "role:fizz" + +''' + 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_are_aliased_to_new_names(self): deprecated_rule = policy.DeprecatedRule( name='foo:post_bar', diff --git a/releasenotes/notes/fix-rendering-for-deprecated-rules-d465292e4155f483.yaml b/releasenotes/notes/fix-rendering-for-deprecated-rules-d465292e4155f483.yaml new file mode 100644 index 00000000..45b85b8e --- /dev/null +++ b/releasenotes/notes/fix-rendering-for-deprecated-rules-d465292e4155f483.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + [`bug 1771442 `_] + Policy rules that are deprecated for removal are now properly formatted + when rendering sample policy files for documentation.