|
| 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 nova import test |
| 14 | +from nova.tests import fixtures as nova_fixtures |
| 15 | +from nova.tests.functional.api import client as api_client |
| 16 | +from nova.tests.functional import fixtures as func_fixtures |
| 17 | +from nova.tests.functional import integrated_helpers |
| 18 | +from nova.tests.unit.image import fake as fake_image |
| 19 | +from nova.tests.unit import policy_fixture |
| 20 | + |
| 21 | + |
| 22 | +class TestServersPerUserQuota(test.TestCase, |
| 23 | + integrated_helpers.InstanceHelperMixin): |
| 24 | + """This tests a regression introduced in the Pike release. |
| 25 | +
|
| 26 | + In Pike we started counting resources for quota limit checking instead of |
| 27 | + tracking usages in a separate database table. As part of that change, |
| 28 | + per-user quota functionality was broken for server creates. |
| 29 | +
|
| 30 | + When mulitple users in the same project have per-user quota, they are meant |
| 31 | + to be allowed to create resources such that may not exceed their |
| 32 | + per-user quota nor their project quota. |
| 33 | +
|
| 34 | + If a project has an 'instances' quota of 10 and user A has a quota of 1 |
| 35 | + and user B has a quota of 1, both users should each be able to create 1 |
| 36 | + server. |
| 37 | +
|
| 38 | + Because of the bug, in this scenario user A will succeed in creating a |
| 39 | + server but user B will fail to create a server with a 403 "quota exceeded" |
| 40 | + error because the 'instances' resource count isn't being correctly scoped |
| 41 | + per-user. |
| 42 | + """ |
| 43 | + def setUp(self): |
| 44 | + super(TestServersPerUserQuota, self).setUp() |
| 45 | + self.useFixture(policy_fixture.RealPolicyFixture()) |
| 46 | + self.useFixture(nova_fixtures.NeutronFixture(self)) |
| 47 | + self.useFixture(func_fixtures.PlacementFixture()) |
| 48 | + |
| 49 | + api_fixture = self.useFixture(nova_fixtures.OSAPIFixture( |
| 50 | + api_version='v2.1')) |
| 51 | + self.api = api_fixture.api |
| 52 | + self.admin_api = api_fixture.admin_api |
| 53 | + self.api.microversion = '2.37' # so we can specify networks='none' |
| 54 | + self.admin_api.microversion = '2.37' |
| 55 | + |
| 56 | + fake_image.stub_out_image_service(self) |
| 57 | + self.addCleanup(fake_image.FakeImageService_reset) |
| 58 | + |
| 59 | + self.start_service('conductor') |
| 60 | + self.start_service('scheduler') |
| 61 | + self.start_service('compute') |
| 62 | + |
| 63 | + def test_create_server_with_per_user_quota(self): |
| 64 | + # Set per-user quota for the non-admin user to allow 1 instance. |
| 65 | + # The default quota for the project is 10 instances. |
| 66 | + quotas = {'instances': 1} |
| 67 | + self.admin_api.update_quota( |
| 68 | + quotas, project_id=self.api.project_id, user_id=self.api.auth_user) |
| 69 | + # Verify that the non-admin user has a quota limit of 1 instance. |
| 70 | + quotas = self.api.get_quota_detail(user_id=self.api.auth_user) |
| 71 | + self.assertEqual(1, quotas['instances']['limit']) |
| 72 | + # Verify that the admin user has a quota limit of 10 instances. |
| 73 | + quotas = self.api.get_quota_detail(user_id=self.admin_api.auth_user) |
| 74 | + self.assertEqual(10, quotas['instances']['limit']) |
| 75 | + # Boot one instance into the default project as the admin user. |
| 76 | + # This results in usage of 1 instance for the project and 1 instance |
| 77 | + # for the admin user. |
| 78 | + self._create_server( |
| 79 | + image_uuid=fake_image.AUTO_DISK_CONFIG_ENABLED_IMAGE_UUID, |
| 80 | + networks='none', api=self.admin_api) |
| 81 | + # Now try to boot an instance as the non-admin user. |
| 82 | + # This should succeed because the non-admin user has 0 instances and |
| 83 | + # the project limit allows 10 instances. |
| 84 | + server_req = self._build_server( |
| 85 | + image_uuid=fake_image.AUTO_DISK_CONFIG_ENABLED_IMAGE_UUID, |
| 86 | + networks='none') |
| 87 | + # FIXME(melwitt): Uncomment this when the bug is fixed. Because of the |
| 88 | + # the bug, the first request by the non-admin user will fail. |
| 89 | + # server = self.api.post_server({'server': server_req}) |
| 90 | + # self._wait_for_state_change(server, 'ACTIVE') |
| 91 | + # A request to boot a second instance should fail because the |
| 92 | + # non-admin has already booted 1 allowed instance. |
| 93 | + ex = self.assertRaises( |
| 94 | + api_client.OpenStackApiException, self.api.post_server, |
| 95 | + {'server': server_req}) |
| 96 | + self.assertEqual(403, ex.response.status_code) |
0 commit comments