Skip to content

Commit f99f68e

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Move rule_if_system() method to base test class"
2 parents 478c0c4 + ed1a854 commit f99f68e

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

nova/tests/unit/policies/base.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424
LOG = logging.getLogger(__name__)
2525

2626

27+
def rule_if_system(system_rule, non_system_rule, context):
28+
"""Helper function to pick a rule based on system-ness of context.
29+
30+
This can be used (with functools.partial) to choose between two
31+
rule names, based on whether or not the context has system
32+
scope. Specifically if we will fail the parent of a nested policy
33+
check based on scope_types=['project'], this can be used to choose
34+
the parent rule name for the error message check in
35+
common_policy_check().
36+
37+
"""
38+
if context.system_scope:
39+
return system_rule
40+
else:
41+
return non_system_rule
42+
43+
2744
class BasePolicyTest(test.TestCase):
2845
# NOTE(gmann): Set this flag to True if you would like to tests the
2946
# new behaviour of policy without deprecated rules.

nova/tests/unit/policies/test_servers.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,6 @@
4040
LOG = logging.getLogger(__name__)
4141

4242

43-
def rule_if_system(system_rule, non_system_rule, context):
44-
"""Helper function to pick a rule based on system-ness of context.
45-
46-
This can be used (with functools.partial) to choose between two
47-
rule names, based on whether or not the context has system
48-
scope. Specifically if we will fail the parent of a nested policy
49-
check based on scope_types=['project'], this can be used to choose
50-
the parent rule name for the error message check in
51-
common_policy_check().
52-
53-
"""
54-
if context.system_scope:
55-
return system_rule
56-
else:
57-
return non_system_rule
58-
59-
6043
class ServersPolicyTest(base.BasePolicyTest):
6144
"""Test Servers APIs policies with all possible context.
6245
This class defines the set of context with different roles
@@ -216,7 +199,8 @@ def fake_get_all(context, search_opts=None,
216199
if not CONF.oslo_policy.enforce_scope:
217200
check_rule = rule_name
218201
else:
219-
check_rule = functools.partial(rule_if_system, rule, rule_name)
202+
check_rule = functools.partial(
203+
base.rule_if_system, rule, rule_name)
220204

221205
self.common_policy_auth(self.all_projects_admin_authorized_contexts,
222206
check_rule,
@@ -267,7 +251,8 @@ def fake_get_all(context, search_opts=None,
267251
if not CONF.oslo_policy.enforce_scope:
268252
check_rule = rule_name
269253
else:
270-
check_rule = functools.partial(rule_if_system, rule, rule_name)
254+
check_rule = functools.partial(
255+
base.rule_if_system, rule, rule_name)
271256

272257
self.common_policy_auth(self.all_projects_admin_authorized_contexts,
273258
check_rule,
@@ -664,7 +649,8 @@ def test_rebuild_trusted_certs_server_policy(self, mock_rebuild):
664649
if not CONF.oslo_policy.enforce_scope:
665650
check_rule = rule_name
666651
else:
667-
check_rule = functools.partial(rule_if_system, rule, rule_name)
652+
check_rule = functools.partial(
653+
base.rule_if_system, rule, rule_name)
668654

669655
self.common_policy_auth(self.project_action_authorized_contexts,
670656
check_rule,
@@ -740,7 +726,8 @@ def test_create_image_allow_volume_backed_server_policy(self,
740726
if not CONF.oslo_policy.enforce_scope:
741727
check_rule = rule_name
742728
else:
743-
check_rule = functools.partial(rule_if_system, rule, rule_name)
729+
check_rule = functools.partial(
730+
base.rule_if_system, rule, rule_name)
744731
self.common_policy_auth(self.project_action_authorized_contexts,
745732
check_rule,
746733
self.controller._action_create_image,

0 commit comments

Comments
 (0)