Skip to content

Commit 4cb8136

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add test coverage of existing admin_actions policies"
2 parents a869f1c + fcf5163 commit 4cb8136

File tree

2 files changed

+99
-40
lines changed

2 files changed

+99
-40
lines changed

nova/tests/unit/api/openstack/compute/test_admin_actions.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
# under the License.
1414

1515
from nova.api.openstack.compute import admin_actions as admin_actions_v21
16-
from nova import exception
17-
from nova import test
1816
from nova.tests.unit.api.openstack.compute import admin_only_action_common
19-
from nova.tests.unit.api.openstack import fakes
2017

2118

2219
class AdminActionsTestV21(admin_only_action_common.CommonTests):
@@ -49,40 +46,3 @@ def test_actions_with_locked_instance(self):
4946

5047
self._test_actions_with_locked_instance(actions,
5148
method_translations=method_translations)
52-
53-
54-
class AdminActionsPolicyEnforcementV21(test.NoDBTestCase):
55-
56-
def setUp(self):
57-
super(AdminActionsPolicyEnforcementV21, self).setUp()
58-
self.controller = admin_actions_v21.AdminActionsController()
59-
self.req = fakes.HTTPRequest.blank('')
60-
self.fake_id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
61-
62-
def common_policy_check(self, rule, fun_name, *arg, **kwarg):
63-
self.policy.set_rules(rule)
64-
func = getattr(self.controller, fun_name)
65-
exc = self.assertRaises(
66-
exception.PolicyNotAuthorized, func, *arg, **kwarg)
67-
self.assertEqual(
68-
"Policy doesn't allow %s to be performed." %
69-
rule.popitem()[0], exc.format_message())
70-
71-
def test_reset_network_policy_failed(self):
72-
rule = {"os_compute_api:os-admin-actions:reset_network":
73-
"project:non_fake"}
74-
self.common_policy_check(
75-
rule, "_reset_network", self.req, self.fake_id, body={})
76-
77-
def test_inject_network_info_policy_failed(self):
78-
rule = {"os_compute_api:os-admin-actions:inject_network_info":
79-
"project:non_fake"}
80-
self.common_policy_check(
81-
rule, "_inject_network_info", self.req, self.fake_id, body={})
82-
83-
def test_reset_state_policy_failed(self):
84-
rule = {"os_compute_api:os-admin-actions:reset_state":
85-
"project:non_fake"}
86-
self.common_policy_check(
87-
rule, "_reset_state", self.req,
88-
self.fake_id, body={"os-resetState": {"state": "active"}})
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import fixtures
14+
import mock
15+
from oslo_utils.fixture import uuidsentinel as uuids
16+
from oslo_utils import timeutils
17+
18+
from nova.api.openstack.compute import admin_actions
19+
from nova.compute import vm_states
20+
from nova.tests.unit.api.openstack import fakes
21+
from nova.tests.unit import fake_instance
22+
from nova.tests.unit.policies import base
23+
24+
25+
class AdminActionsPolicyTest(base.BasePolicyTest):
26+
"""Test Admin Actions APIs policies with all possible context.
27+
28+
This class defines the set of context with different roles
29+
which are allowed and not allowed to pass the policy checks.
30+
With those set of context, it will call the API operation and
31+
verify the expected behaviour.
32+
"""
33+
34+
def setUp(self):
35+
super(AdminActionsPolicyTest, self).setUp()
36+
self.controller = admin_actions.AdminActionsController()
37+
self.req = fakes.HTTPRequest.blank('')
38+
self.mock_get = self.useFixture(
39+
fixtures.MockPatch('nova.compute.api.API.get')).mock
40+
uuid = uuids.fake_id
41+
self.instance = fake_instance.fake_instance_obj(
42+
self.project_member_context,
43+
id=1, uuid=uuid, vm_state=vm_states.ACTIVE,
44+
task_state=None, launched_at=timeutils.utcnow())
45+
self.mock_get.return_value = self.instance
46+
# Check that admin is able to change the service
47+
self.admin_authorized_contexts = [
48+
self.legacy_admin_context, self.system_admin_context,
49+
self.project_admin_context]
50+
# Check that non-admin is not able to change the service
51+
self.admin_unauthorized_contexts = [
52+
self.system_member_context, self.system_reader_context,
53+
self.system_foo_context, self.project_member_context,
54+
self.other_project_member_context,
55+
self.project_foo_context, self.project_reader_context
56+
]
57+
58+
@mock.patch('nova.objects.Instance.save')
59+
def test_reset_state_policy(self, mock_save):
60+
rule_name = "os_compute_api:os-admin-actions:reset_state"
61+
self.common_policy_check(self.admin_authorized_contexts,
62+
self.admin_unauthorized_contexts,
63+
rule_name, self.controller._reset_state,
64+
self.req, self.instance.uuid,
65+
body={'os-resetState': {'state': 'active'}})
66+
67+
def test_inject_network_info_policy(self):
68+
rule_name = "os_compute_api:os-admin-actions:inject_network_info"
69+
with mock.patch.object(self.controller.compute_api,
70+
"inject_network_info"):
71+
self.common_policy_check(self.admin_authorized_contexts,
72+
self.admin_unauthorized_contexts,
73+
rule_name,
74+
self.controller._inject_network_info,
75+
self.req, self.instance.uuid, body={})
76+
77+
def test_reset_network_policy(self):
78+
rule_name = "os_compute_api:os-admin-actions:reset_network"
79+
with mock.patch.object(self.controller.compute_api, "reset_network"):
80+
self.common_policy_check(self.admin_authorized_contexts,
81+
self.admin_unauthorized_contexts,
82+
rule_name, self.controller._reset_network,
83+
self.req, self.instance.uuid, body={})
84+
85+
86+
class AdminActionsScopeTypePolicyTest(AdminActionsPolicyTest):
87+
"""Test Admin Actions APIs policies with system scope enabled.
88+
89+
This class set the nova.conf [oslo_policy] enforce_scope to True
90+
so that we can switch on the scope checking on oslo policy side.
91+
It defines the set of context with scopped token
92+
which are allowed and not allowed to pass the policy checks.
93+
With those set of context, it will run the API operation and
94+
verify the expected behaviour.
95+
"""
96+
97+
def setUp(self):
98+
super(AdminActionsScopeTypePolicyTest, self).setUp()
99+
self.flags(enforce_scope=True, group="oslo_policy")

0 commit comments

Comments
 (0)