|
| 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 | +from oslo_limit import fixture as limit_fixture |
| 14 | +from oslo_serialization import base64 |
| 15 | +from oslo_utils.fixture import uuidsentinel as uuids |
| 16 | + |
| 17 | +from nova import context as nova_context |
| 18 | +from nova.limit import local as local_limit |
| 19 | +from nova.objects import flavor as flavor_obj |
| 20 | +from nova.objects import instance_group as group_obj |
| 21 | +from nova.tests.functional.api import client |
| 22 | +from nova.tests.functional import integrated_helpers |
| 23 | + |
| 24 | + |
| 25 | +class UnifiedLimitsTest(integrated_helpers._IntegratedTestBase): |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + super(UnifiedLimitsTest, self).setUp() |
| 29 | + # Use different project_ids for non-admin and admin. |
| 30 | + self.api.project_id = 'fake' |
| 31 | + self.admin_api.project_id = 'admin' |
| 32 | + |
| 33 | + self.flags(driver="nova.quota.UnifiedLimitsDriver", group="quota") |
| 34 | + reglimits = {local_limit.SERVER_METADATA_ITEMS: 128, |
| 35 | + local_limit.INJECTED_FILES: 5, |
| 36 | + local_limit.INJECTED_FILES_CONTENT: 10 * 1024, |
| 37 | + local_limit.INJECTED_FILES_PATH: 255, |
| 38 | + local_limit.KEY_PAIRS: 100, |
| 39 | + local_limit.SERVER_GROUPS: 10, |
| 40 | + local_limit.SERVER_GROUP_MEMBERS: 1, |
| 41 | + 'servers': 4, |
| 42 | + 'class:VCPU': 8, |
| 43 | + 'class:MEMORY_MB': 32768, |
| 44 | + 'class:DISK_GB': 250} |
| 45 | + projlimits = {self.api.project_id: {'servers': 2, |
| 46 | + 'class:VCPU': 4, |
| 47 | + 'class:MEMORY_MB': 16384, |
| 48 | + 'class:DISK_GB': 100}} |
| 49 | + self.useFixture(limit_fixture.LimitFixture(reglimits, projlimits)) |
| 50 | + self.ctx = nova_context.get_admin_context() |
| 51 | + |
| 52 | + def _setup_services(self): |
| 53 | + # Use driver with lots of resources so we don't get NoValidHost while |
| 54 | + # testing quotas. Need to do this before services are started. |
| 55 | + self.flags(compute_driver='fake.FakeDriver') |
| 56 | + super(UnifiedLimitsTest, self)._setup_services() |
| 57 | + |
| 58 | + def test_servers(self): |
| 59 | + # First test the project limit using the non-admin project. |
| 60 | + for i in range(2): |
| 61 | + self._create_server(api=self.api) |
| 62 | + |
| 63 | + # Attempt to create a third server should fail. |
| 64 | + e = self.assertRaises( |
| 65 | + client.OpenStackApiException, self._create_server, api=self.api) |
| 66 | + self.assertEqual(403, e.response.status_code) |
| 67 | + self.assertIn('servers', e.response.text) |
| 68 | + |
| 69 | + # Then test the default limit using the admin project. |
| 70 | + for i in range(4): |
| 71 | + self._create_server(api=self.admin_api) |
| 72 | + |
| 73 | + # Attempt to create a fifth server should fail. |
| 74 | + e = self.assertRaises( |
| 75 | + client.OpenStackApiException, self._create_server, |
| 76 | + api=self.admin_api) |
| 77 | + self.assertEqual(403, e.response.status_code) |
| 78 | + self.assertIn('servers', e.response.text) |
| 79 | + |
| 80 | + def test_vcpu(self): |
| 81 | + # First test the project limit using the non-admin project. |
| 82 | + # m1.large has vcpus=4 and our project limit is 4, should succeed. |
| 83 | + flavor = flavor_obj.Flavor.get_by_name(self.ctx, 'm1.large') |
| 84 | + self._create_server(api=self.api, flavor_id=flavor.flavorid) |
| 85 | + |
| 86 | + # m1.small has vcpus=1, should fail because we are at quota. |
| 87 | + flavor = flavor_obj.Flavor.get_by_name(self.ctx, 'm1.small') |
| 88 | + e = self.assertRaises( |
| 89 | + client.OpenStackApiException, self._create_server, api=self.api, |
| 90 | + flavor_id=flavor.flavorid) |
| 91 | + self.assertEqual(403, e.response.status_code) |
| 92 | + self.assertIn('class:VCPU', e.response.text) |
| 93 | + |
| 94 | + # Then test the default limit of 8 using the admin project. |
| 95 | + flavor = flavor_obj.Flavor.get_by_name(self.ctx, 'm1.large') |
| 96 | + for i in range(2): |
| 97 | + self._create_server(api=self.admin_api, flavor_id=flavor.flavorid) |
| 98 | + |
| 99 | + # Attempt to create another server with vcpus=1 should fail because we |
| 100 | + # are at quota. |
| 101 | + flavor = flavor_obj.Flavor.get_by_name(self.ctx, 'm1.small') |
| 102 | + e = self.assertRaises( |
| 103 | + client.OpenStackApiException, self._create_server, |
| 104 | + api=self.admin_api, flavor_id=flavor.flavorid) |
| 105 | + self.assertEqual(403, e.response.status_code) |
| 106 | + self.assertIn('class:VCPU', e.response.text) |
| 107 | + |
| 108 | + def test_memory_mb(self): |
| 109 | + # First test the project limit using the non-admin project. |
| 110 | + flavor = flavor_obj.Flavor( |
| 111 | + context=self.ctx, memory_mb=16384, vcpus=1, root_gb=1, |
| 112 | + flavorid='9', name='m1.custom') |
| 113 | + flavor.create() |
| 114 | + self._create_server(api=self.api, flavor_id=flavor.flavorid) |
| 115 | + |
| 116 | + # Attempt to create another should fail as we are at quota. |
| 117 | + e = self.assertRaises( |
| 118 | + client.OpenStackApiException, self._create_server, api=self.api, |
| 119 | + flavor_id=flavor.flavorid) |
| 120 | + self.assertEqual(403, e.response.status_code) |
| 121 | + self.assertIn('class:MEMORY_MB', e.response.text) |
| 122 | + |
| 123 | + # Then test the default limit of 32768 using the admin project. |
| 124 | + for i in range(2): |
| 125 | + self._create_server(api=self.admin_api, flavor_id=flavor.flavorid) |
| 126 | + |
| 127 | + # Attempt to create another server should fail because we are at quota. |
| 128 | + e = self.assertRaises( |
| 129 | + client.OpenStackApiException, self._create_server, |
| 130 | + api=self.admin_api, flavor_id=flavor.flavorid) |
| 131 | + self.assertEqual(403, e.response.status_code) |
| 132 | + self.assertIn('class:MEMORY_MB', e.response.text) |
| 133 | + |
| 134 | + def test_disk_gb(self): |
| 135 | + # First test the project limit using the non-admin project. |
| 136 | + flavor = flavor_obj.Flavor( |
| 137 | + context=self.ctx, memory_mb=1, vcpus=1, root_gb=100, |
| 138 | + flavorid='9', name='m1.custom') |
| 139 | + flavor.create() |
| 140 | + self._create_server(api=self.api, flavor_id=flavor.flavorid) |
| 141 | + |
| 142 | + # Attempt to create another should fail as we are at quota. |
| 143 | + e = self.assertRaises( |
| 144 | + client.OpenStackApiException, self._create_server, api=self.api, |
| 145 | + flavor_id=flavor.flavorid) |
| 146 | + self.assertEqual(403, e.response.status_code) |
| 147 | + self.assertIn('class:DISK_GB', e.response.text) |
| 148 | + |
| 149 | + # Then test the default limit of 250 using the admin project. |
| 150 | + for i in range(2): |
| 151 | + self._create_server(api=self.admin_api, flavor_id=flavor.flavorid) |
| 152 | + |
| 153 | + # Attempt to create another server should fail because we are at quota. |
| 154 | + e = self.assertRaises( |
| 155 | + client.OpenStackApiException, self._create_server, |
| 156 | + api=self.admin_api, flavor_id=flavor.flavorid) |
| 157 | + self.assertEqual(403, e.response.status_code) |
| 158 | + self.assertIn('class:DISK_GB', e.response.text) |
| 159 | + |
| 160 | + def test_no_injected_files(self): |
| 161 | + self._create_server() |
| 162 | + |
| 163 | + def test_max_injected_files(self): |
| 164 | + # Quota is 5. |
| 165 | + files = [] |
| 166 | + contents = base64.encode_as_text('some content') |
| 167 | + for i in range(5): |
| 168 | + files.append(('/my/path%d' % i, contents)) |
| 169 | + server = self._build_server() |
| 170 | + personality = [ |
| 171 | + {'path': item[0], 'contents': item[1]} for item in files] |
| 172 | + server['personality'] = personality |
| 173 | + self.api.post_server({'server': server}) |
| 174 | + |
| 175 | + def test_max_injected_file_content_bytes(self): |
| 176 | + # Quota is 10 * 1024 |
| 177 | + # Hm, apparently quota is checked against the base64 encoded string |
| 178 | + # even though the api-ref claims the limit is for the decoded data. |
| 179 | + # Subtract 3072 characters to account for that. |
| 180 | + content = base64.encode_as_bytes( |
| 181 | + ''.join(['a' for i in range(10 * 1024 - 3072)])) |
| 182 | + server = self._build_server() |
| 183 | + personality = [{'path': '/test/path', 'contents': content}] |
| 184 | + server['personality'] = personality |
| 185 | + self.api.post_server({'server': server}) |
| 186 | + |
| 187 | + def test_max_injected_file_path_bytes(self): |
| 188 | + # Quota is 255. |
| 189 | + path = ''.join(['a' for i in range(255)]) |
| 190 | + contents = base64.encode_as_text('some content') |
| 191 | + server = self._build_server() |
| 192 | + personality = [{'path': path, 'contents': contents}] |
| 193 | + server['personality'] = personality |
| 194 | + self.api.post_server({'server': server}) |
| 195 | + |
| 196 | + def test_server_group_members(self): |
| 197 | + # Create a server group. |
| 198 | + instance_group = group_obj.InstanceGroup( |
| 199 | + self.ctx, policy="anti-affinity") |
| 200 | + instance_group.name = "foo" |
| 201 | + instance_group.project_id = self.ctx.project_id |
| 202 | + instance_group.user_id = self.ctx.user_id |
| 203 | + instance_group.uuid = uuids.instance_group |
| 204 | + instance_group.create() |
| 205 | + |
| 206 | + # Quota for server group members is 1. |
| 207 | + server = self._build_server() |
| 208 | + hints = {'group': uuids.instance_group} |
| 209 | + req = {'server': server, 'os:scheduler_hints': hints} |
| 210 | + self.admin_api.post_server(req) |
| 211 | + |
| 212 | + # Attempt to create another server in the group should fail because we |
| 213 | + # are at quota. |
| 214 | + e = self.assertRaises( |
| 215 | + client.OpenStackApiException, self.admin_api.post_server, req) |
| 216 | + self.assertEqual(403, e.response.status_code) |
| 217 | + self.assertIn('server_group_members', e.response.text) |
0 commit comments