Skip to content

Commit 38bc8b8

Browse files
committed
Add regression test for bug 1893284
This adds a regression test for a bug where quota limit checking during server creates is not properly scoped per-user when per-user quota has been defined. As a result, users who should be able to create a server are rejected with a 403 "quota exceeded" error when they should be allowed to create a server because servers owned by other users in the project are incorrectly being counted for the current user. Related-Bug: #1893284 Change-Id: I615ada45ffcbac081474c0a0cf005afdb8eec953
1 parent f521f4d commit 38bc8b8

File tree

2 files changed

+106
-6
lines changed

2 files changed

+106
-6
lines changed

nova/tests/functional/api/client.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,16 +539,20 @@ def put_service_force_down(self, service_id, forced_down):
539539
def get_server_diagnostics(self, server_id):
540540
return self.api_get('/servers/%s/diagnostics' % server_id).body
541541

542-
def get_quota_detail(self, project_id=None):
542+
def get_quota_detail(self, project_id=None, user_id=None):
543543
if not project_id:
544544
project_id = self.project_id
545-
return self.api_get(
546-
'/os-quota-sets/%s/detail' % project_id).body['quota_set']
545+
url = '/os-quota-sets/%s/detail'
546+
if user_id:
547+
url += '?user_id=%s' % user_id
548+
return self.api_get(url % project_id).body['quota_set']
547549

548-
def update_quota(self, quotas, project_id=None):
550+
def update_quota(self, quotas, project_id=None, user_id=None):
549551
if not project_id:
550552
project_id = self.project_id
553+
url = '/os-quota-sets/%s'
554+
if user_id:
555+
url += '?user_id=%s' % user_id
551556
body = {'quota_set': {}}
552557
body['quota_set'].update(quotas)
553-
return self.api_put(
554-
'/os-quota-sets/%s' % project_id, body).body['quota_set']
558+
return self.api_put(url % project_id, body).body['quota_set']
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)