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
This commit is contained in:
Lance Bragstad 2018-06-01 21:25:19 +00:00
parent ba836f2d40
commit 0f31938dd7
3 changed files with 46 additions and 2 deletions

View File

@ -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,

View File

@ -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',

View File

@ -0,0 +1,6 @@
---
fixes:
- |
[`bug 1771442 <https://bugs.launchpad.net/oslo.policy/+bug/1771442>`_]
Policy rules that are deprecated for removal are now properly formatted
when rendering sample policy files for documentation.