From 5d3aeb53a9f3686353e0740c1891bd0e66e90650 Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Fri, 13 Sep 2019 17:08:05 +0000 Subject: [PATCH] Suppress deprecation warnings in oslopolicy-list-redundant When running the tool against a project with a lot of deprecated policies, the deprecation warnings overwhelm the important output from the tool. Since checking for deprecations isn't the purpose of this tool, let's just suppress warnings and limit the output to the list of redundant policy rules that the user cares about. I will note that the suppress_deprecation_warnings member is marked as test only, but that was targeted at consumers of oslo.policy rather than oslo.policy itself. I could also argue that we're "testing" for redundant policies here, but that feels like semantic hair splitting. :-) Change-Id: I194af14ebd341366dbb1dd033654739a7f3d085c Partial-Bug: 1836568 --- oslo_policy/generator.py | 5 +++++ oslo_policy/tests/test_generator.py | 14 ++++++++++++-- ...dant-deprecation-warnings-f84a06133efdaedd.yaml | 6 ++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/list-redundant-deprecation-warnings-f84a06133efdaedd.yaml diff --git a/oslo_policy/generator.py b/oslo_policy/generator.py index bd753896..9e283d9e 100644 --- a/oslo_policy/generator.py +++ b/oslo_policy/generator.py @@ -315,6 +315,11 @@ def _list_redundant(namespace): in a policy file and operators should consider removing them. """ enforcer = _get_enforcer(namespace) + # NOTE(bnemec): We don't want to see policy deprecation warnings in the + # output of this tool. They tend to overwhelm the output that the user + # actually cares about, and checking for deprecations isn't the purpose of + # this tool. + enforcer.suppress_deprecation_warnings = True # Ensure that files have been parsed enforcer.load_rules() diff --git a/oslo_policy/tests/test_generator.py b/oslo_policy/tests/test_generator.py index 18c54065..a0beeaee 100644 --- a/oslo_policy/tests/test_generator.py +++ b/oslo_policy/tests/test_generator.py @@ -564,7 +564,8 @@ class ListRedundantTestCase(base.PolicyBaseTestCase): def setUp(self): super(ListRedundantTestCase, self).setUp() - def test_matched_rules(self): + @mock.patch('warnings.warn') + def test_matched_rules(self, mock_warn): extensions = [] for name, opts in OPTS.items(): ext = stevedore.extension.Extension(name=name, entry_point=None, @@ -587,7 +588,13 @@ class ListRedundantTestCase(base.PolicyBaseTestCase): enforcer.register_default( policy.RuleDefault('owner', 'project_id:%(project_id)s')) # register a new opt - enforcer.register_default(policy.RuleDefault('foo', 'role:foo')) + deprecated_rule = policy.DeprecatedRule('old_foo', 'role:bar') + enforcer.register_default( + policy.RuleDefault('foo', 'role:foo', + deprecated_rule=deprecated_rule, + deprecated_reason='reason', + deprecated_since='T') + ) # Mock out stevedore to return the configured enforcer ext = stevedore.extension.Extension(name='testing', entry_point=None, @@ -618,6 +625,9 @@ class ListRedundantTestCase(base.PolicyBaseTestCase): self.assertEqual('"owner"', opt1[0]) self.assertEqual('"project_id:%(project_id)s"', opt1[1]) + self.assertFalse(mock_warn.called, + 'Deprecation warnings not suppressed.') + class UpgradePolicyTestCase(base.PolicyBaseTestCase): def setUp(self): diff --git a/releasenotes/notes/list-redundant-deprecation-warnings-f84a06133efdaedd.yaml b/releasenotes/notes/list-redundant-deprecation-warnings-f84a06133efdaedd.yaml new file mode 100644 index 00000000..5f321b26 --- /dev/null +++ b/releasenotes/notes/list-redundant-deprecation-warnings-f84a06133efdaedd.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Deprecated policy warnings are now suppressed in the + ``oslopolicy-list-redundant`` tool so that they don't overwhelm the + relevant output.