Skip to content

Commit d9954df

Browse files
author
Ghanshyam Mann
committed
Add test coverage of multinic policies
Current tests do not have good test coverage of existing policies. Either tests for policies do not exist or if they exist then they do not cover the actual negative and positive testing. To adopt the keystone's scope_type and new defaults in deprecated API policies, we need to first write test coverage for the same to know the complete effect of policies changes. Partial implement blueprint policy-defaults-refresh-deprecated-apis Change-Id: I78283bcf7a0d188cadaf2eb9501af782deb33f88
1 parent 904e4db commit d9954df

File tree

2 files changed

+94
-35
lines changed

2 files changed

+94
-35
lines changed

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -152,41 +152,6 @@ def test_remove_fixed_ip_not_found(self, _remove_fixed_ip):
152152
UUID, body=body)
153153

154154

155-
class MultinicPolicyEnforcementV21(test.NoDBTestCase):
156-
157-
def setUp(self):
158-
super(MultinicPolicyEnforcementV21, self).setUp()
159-
self.controller = multinic_v21.MultinicController()
160-
self.req = fakes.HTTPRequest.blank('')
161-
self.mock_get = self.useFixture(
162-
fixtures.MockPatch('nova.api.openstack.common.get_instance')).mock
163-
self.mock_get.return_value = fake_instance.fake_instance_obj(
164-
self.req.environ['nova.context'],
165-
project_id=self.req.environ['nova.context'].project_id)
166-
167-
def test_add_fixed_ip_policy_failed(self):
168-
rule_name = "os_compute_api:os-multinic"
169-
self.policy.set_rules({rule_name: "project:non_fake"})
170-
exc = self.assertRaises(
171-
exception.PolicyNotAuthorized,
172-
self.controller._add_fixed_ip, self.req, fakes.FAKE_UUID,
173-
body={'addFixedIp': {'networkId': fakes.FAKE_UUID}})
174-
self.assertEqual(
175-
"Policy doesn't allow %s to be performed." % rule_name,
176-
exc.format_message())
177-
178-
def test_remove_fixed_ip_policy_failed(self):
179-
rule_name = "os_compute_api:os-multinic"
180-
self.policy.set_rules({rule_name: "project:non_fake"})
181-
exc = self.assertRaises(
182-
exception.PolicyNotAuthorized,
183-
self.controller._remove_fixed_ip, self.req, fakes.FAKE_UUID,
184-
body={'removeFixedIp': {'address': "10.0.0.1"}})
185-
self.assertEqual(
186-
"Policy doesn't allow %s to be performed." % rule_name,
187-
exc.format_message())
188-
189-
190155
class MultinicAPIDeprecationTest(test.NoDBTestCase):
191156

192157
def setUp(self):
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 multinic
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 MultinicPolicyTest(base.BasePolicyTest):
26+
"""Test Multinic 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(MultinicPolicyTest, self).setUp()
36+
self.controller = multinic.MultinicController()
37+
self.req = fakes.HTTPRequest.blank('')
38+
self.mock_get = self.useFixture(
39+
fixtures.MockPatch('nova.api.openstack.common.get_instance')).mock
40+
uuid = uuids.fake_id
41+
self.instance = fake_instance.fake_instance_obj(
42+
self.project_member_context, project_id=self.project_id,
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 or owner is able to add/remove fixed ip.
47+
self.admin_or_owner_authorized_contexts = [
48+
self.legacy_admin_context, self.system_admin_context,
49+
self.project_admin_context, self.project_member_context,
50+
self.project_reader_context, self.project_foo_context
51+
]
52+
# Check that non-admin and non-owner is not able to add/remove
53+
# fixed ip.
54+
self.admin_or_owner_unauthorized_contexts = [
55+
self.system_member_context, self.system_reader_context,
56+
self.system_foo_context,
57+
self.other_project_member_context
58+
]
59+
60+
@mock.patch('nova.compute.api.API.add_fixed_ip')
61+
def test_add_fixed_ip_policy(self, mock_add):
62+
rule_name = "os_compute_api:os-multinic"
63+
body = dict(addFixedIp=dict(networkId='test_net'))
64+
self.common_policy_check(self.admin_or_owner_authorized_contexts,
65+
self.admin_or_owner_unauthorized_contexts,
66+
rule_name, self.controller._add_fixed_ip,
67+
self.req, self.instance.uuid,
68+
body=body)
69+
70+
@mock.patch('nova.compute.api.API.remove_fixed_ip')
71+
def test_remove_fixed_ip_policy(self, mock_remove):
72+
rule_name = "os_compute_api:os-multinic"
73+
body = dict(removeFixedIp=dict(address='1.2.3.4'))
74+
self.common_policy_check(self.admin_or_owner_authorized_contexts,
75+
self.admin_or_owner_unauthorized_contexts,
76+
rule_name, self.controller._remove_fixed_ip,
77+
self.req, self.instance.uuid,
78+
body=body)
79+
80+
81+
class MultinicScopeTypePolicyTest(MultinicPolicyTest):
82+
"""Test Multinic APIs policies with system scope enabled.
83+
84+
This class set the nova.conf [oslo_policy] enforce_scope to True
85+
so that we can switch on the scope checking on oslo policy side.
86+
It defines the set of context with scoped token
87+
which are allowed and not allowed to pass the policy checks.
88+
With those set of context, it will run the API operation and
89+
verify the expected behaviour.
90+
"""
91+
92+
def setUp(self):
93+
super(MultinicScopeTypePolicyTest, self).setUp()
94+
self.flags(enforce_scope=True, group="oslo_policy")

0 commit comments

Comments
 (0)