|
| 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 mock |
| 14 | +from oslo_utils.fixture import uuidsentinel as uuids |
| 15 | + |
| 16 | +from nova.api.openstack.compute import baremetal_nodes |
| 17 | +from nova.tests.unit.api.openstack import fakes |
| 18 | +from nova.tests.unit.policies import base |
| 19 | +from nova.tests.unit.virt.ironic import utils as ironic_utils |
| 20 | + |
| 21 | + |
| 22 | +FAKE_IRONIC_CLIENT = ironic_utils.FakeClient() |
| 23 | + |
| 24 | + |
| 25 | +class BaremetalNodesPolicyTest(base.BasePolicyTest): |
| 26 | + """Test Baremetal Nodes 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(BaremetalNodesPolicyTest, self).setUp() |
| 36 | + self.controller = baremetal_nodes.BareMetalNodeController() |
| 37 | + self.req = fakes.HTTPRequest.blank('') |
| 38 | + self.stub_out('nova.api.openstack.compute.' |
| 39 | + 'baremetal_nodes._get_ironic_client', |
| 40 | + lambda *_: FAKE_IRONIC_CLIENT) |
| 41 | + # Check that admin is able to get baremetal nodes. |
| 42 | + self.admin_authorized_contexts = [ |
| 43 | + self.legacy_admin_context, self.system_admin_context, |
| 44 | + self.project_admin_context] |
| 45 | + # Check that non-admin is not able to get baremetal nodes. |
| 46 | + self.admin_unauthorized_contexts = [ |
| 47 | + self.system_member_context, self.system_reader_context, |
| 48 | + self.system_foo_context, self.project_member_context, |
| 49 | + self.other_project_member_context, |
| 50 | + self.project_foo_context, self.project_reader_context, |
| 51 | + self.other_project_reader_context |
| 52 | + ] |
| 53 | + |
| 54 | + def test_index_nodes_policy(self): |
| 55 | + rule_name = "os_compute_api:os-baremetal-nodes" |
| 56 | + self.common_policy_check(self.admin_authorized_contexts, |
| 57 | + self.admin_unauthorized_contexts, |
| 58 | + rule_name, self.controller.index, |
| 59 | + self.req) |
| 60 | + |
| 61 | + @mock.patch.object(FAKE_IRONIC_CLIENT.node, 'list_ports') |
| 62 | + @mock.patch.object(FAKE_IRONIC_CLIENT.node, 'get') |
| 63 | + def test_show_node_policy(self, mock_get, mock_port): |
| 64 | + rule_name = "os_compute_api:os-baremetal-nodes" |
| 65 | + properties = {'cpus': 1, 'memory_mb': 512, 'local_gb': 10} |
| 66 | + node = ironic_utils.get_test_node(properties=properties) |
| 67 | + mock_get.return_value = node |
| 68 | + mock_port.return_value = [] |
| 69 | + |
| 70 | + self.common_policy_check(self.admin_authorized_contexts, |
| 71 | + self.admin_unauthorized_contexts, |
| 72 | + rule_name, |
| 73 | + self.controller.show, |
| 74 | + self.req, uuids.fake_id) |
| 75 | + |
| 76 | + |
| 77 | +class BaremetalNodesScopeTypePolicyTest(BaremetalNodesPolicyTest): |
| 78 | + """Test Baremetal Nodes APIs policies with system scope enabled. |
| 79 | +
|
| 80 | + This class set the nova.conf [oslo_policy] enforce_scope to True |
| 81 | + so that we can switch on the scope checking on oslo policy side. |
| 82 | + It defines the set of context with scopped token |
| 83 | + which are allowed and not allowed to pass the policy checks. |
| 84 | + With those set of context, it will run the API operation and |
| 85 | + verify the expected behaviour. |
| 86 | + """ |
| 87 | + |
| 88 | + def setUp(self): |
| 89 | + super(BaremetalNodesScopeTypePolicyTest, self).setUp() |
| 90 | + self.flags(enforce_scope=True, group="oslo_policy") |
0 commit comments