Browse Source

Merge "Only alias when policy names change"

tags/2.3.2^0
Zuul 1 week ago
parent
commit
e433a3cbec
2 changed files with 56 additions and 6 deletions
  1. 13
    6
      oslo_policy/generator.py
  2. 43
    0
      oslo_policy/tests/test_generator.py

+ 13
- 6
oslo_policy/generator.py View File

@@ -193,12 +193,19 @@ def _format_rule_default_yaml(default, include_help=True):
193 193
              'check_str': default.check_str,
194 194
              'reason': default.deprecated_reason}
195 195
 
196
-        text = (
197
-            '%(text)s%(deprecated_text)s\n"%(old_name)s": "rule:%(name)s"\n'
198
-        ) % {'text': text,
199
-             'deprecated_text': _format_help_text(deprecated_text),
200
-             'old_name': default.deprecated_rule.name,
201
-             'name': default.name}
196
+        if default.name != default.deprecated_rule.name:
197
+            text = (
198
+                '%(text)s%(deprecated_text)s\n"%(old_name)s": "rule:%(name)s"'
199
+                '\n'
200
+            ) % {'text': text,
201
+                 'deprecated_text': _format_help_text(deprecated_text),
202
+                 'old_name': default.deprecated_rule.name,
203
+                 'name': default.name}
204
+        else:
205
+            text = (
206
+                '%(text)s%(deprecated_text)s\n'
207
+            ) % {'text': text,
208
+                 'deprecated_text': _format_help_text(deprecated_text)}
202 209
 
203 210
     return text
204 211
 

+ 43
- 0
oslo_policy/tests/test_generator.py View File

@@ -227,6 +227,49 @@ class GenerateSampleYAMLTestCase(base.PolicyBaseTestCase):
227 227
 # favor of "foo:create_bar":"role:fizz". foo:post_bar is being removed
228 228
 # in favor of foo:create_bar
229 229
 "foo:post_bar": "rule:foo:create_bar"
230
+'''
231
+        stdout = self._capture_stdout()
232
+        with mock.patch('stevedore.named.NamedExtensionManager',
233
+                        return_value=test_mgr) as mock_ext_mgr:
234
+            generator._generate_sample(['rules'], output_file=None)
235
+            mock_ext_mgr.assert_called_once_with(
236
+                'oslo.policy.policies', names=['rules'],
237
+                on_load_failure_callback=generator.on_load_failure_callback,
238
+                invoke_on_load=True
239
+            )
240
+        self.assertEqual(expected, stdout.getvalue())
241
+
242
+    def test_deprecated_policies_with_same_name(self):
243
+        deprecated_rule = policy.DeprecatedRule(
244
+            name='foo:create_bar',
245
+            check_str='role:old'
246
+        )
247
+        new_rule = policy.RuleDefault(
248
+            name='foo:create_bar',
249
+            check_str='role:fizz',
250
+            description='Create a bar.',
251
+            deprecated_rule=deprecated_rule,
252
+            deprecated_reason=(
253
+                'role:fizz is a more sane default for foo:create_bar'
254
+            ),
255
+            deprecated_since='N'
256
+        )
257
+        opts = {'rules': [new_rule]}
258
+
259
+        extensions = []
260
+        for name, opts in opts.items():
261
+            ext = stevedore.extension.Extension(name=name, entry_point=None,
262
+                                                plugin=None, obj=opts)
263
+            extensions.append(ext)
264
+        test_mgr = stevedore.named.NamedExtensionManager.make_test_instance(
265
+            extensions=extensions, namespace=['rules'])
266
+
267
+        expected = '''# Create a bar.
268
+#"foo:create_bar": "role:fizz"
269
+
270
+# DEPRECATED "foo:create_bar":"role:old" has been deprecated since N
271
+# in favor of "foo:create_bar":"role:fizz". role:fizz is a more sane
272
+# default for foo:create_bar
230 273
 '''
231 274
         stdout = self._capture_stdout()
232 275
         with mock.patch('stevedore.named.NamedExtensionManager',

Loading…
Cancel
Save