|
| 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