blob: 33b9bef0e3b7a729a4329d1c4d4bdc1e0b02ccb4 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Rohit Karajgi07599c52012-11-02 05:35:16 -07002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Doug Hellmann583ce2c2015-03-11 14:55:46 +000016from oslo_log import log as logging
Matt Riedemann848805f2014-06-16 13:23:51 -070017from testtools import matchers
18
Sean Dague1937d092013-05-17 16:36:38 -040019from tempest.api.compute import base
Matt Riedemann848805f2014-06-16 13:23:51 -070020from tempest.common import tempest_fixtures as fixtures
Fei Long Wangd39431f2015-05-14 11:30:48 +120021from tempest.common.utils import data_utils
Haiwei Xuc367d912014-01-14 19:51:10 +090022from tempest import test
Sean Dague86bd8422013-12-20 09:56:44 -050023
Matt Riedemann848805f2014-06-16 13:23:51 -070024LOG = logging.getLogger(__name__)
25
Rohit Karajgi07599c52012-11-02 05:35:16 -070026
ivan-zhuf2b00502013-10-18 10:06:52 +080027class QuotasAdminTestJSON(base.BaseV2ComputeAdminTest):
Attila Fazekas430dae32013-10-17 15:19:32 +020028 force_tenant_isolation = True
Rohit Karajgi07599c52012-11-02 05:35:16 -070029
Matt Riedemann848805f2014-06-16 13:23:51 -070030 def setUp(self):
31 # NOTE(mriedem): Avoid conflicts with os-quota-class-sets tests.
32 self.useFixture(fixtures.LockFixture('compute_quotas'))
33 super(QuotasAdminTestJSON, self).setUp()
34
Rohit Karajgi07599c52012-11-02 05:35:16 -070035 @classmethod
Rohan Kanade60b73092015-02-04 17:58:19 +053036 def setup_clients(cls):
37 super(QuotasAdminTestJSON, cls).setup_clients()
38 cls.adm_client = cls.os_adm.quotas_client
39
40 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010041 def resource_setup(cls):
42 super(QuotasAdminTestJSON, cls).resource_setup()
Attila Fazekas4ba36582013-02-12 08:26:17 +010043
Attila Fazekasf7f34f92013-08-01 17:01:44 +020044 # NOTE(afazekas): these test cases should always create and use a new
Attila Fazekas4ba36582013-02-12 08:26:17 +010045 # tenant most of them should be skipped if we can't do that
Andrea Frittoli9612e812014-03-13 10:57:26 +000046 cls.demo_tenant_id = cls.quotas_client.tenant_id
Rohit Karajgi07599c52012-11-02 05:35:16 -070047
Attila Fazekasd9aef1e2013-07-13 17:33:45 +020048 cls.default_quota_set = set(('injected_file_content_bytes',
49 'metadata_items', 'injected_files',
50 'ram', 'floating_ips',
51 'fixed_ips', 'key_pairs',
52 'injected_file_path_bytes',
53 'instances', 'security_group_rules',
54 'cores', 'security_groups'))
Rohit Karajgi07599c52012-11-02 05:35:16 -070055
Chris Hoge7579c1a2015-02-26 14:12:15 -080056 @test.idempotent_id('3b0a7c8f-cf58-46b8-a60c-715a32a8ba7d')
Rohit Karajgi07599c52012-11-02 05:35:16 -070057 def test_get_default_quotas(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050058 # Admin can get the default resource quota set for a tenant
Attila Fazekasd9aef1e2013-07-13 17:33:45 +020059 expected_quota_set = self.default_quota_set | set(['id'])
Ken'ichi Ohmichif9868fc2015-06-17 02:36:06 +000060 quota_set = self.adm_client.show_default_quota_set(
ghanshyam52657872015-08-24 16:39:10 +090061 self.demo_tenant_id)['quota_set']
Attila Fazekasd9aef1e2013-07-13 17:33:45 +020062 self.assertEqual(quota_set['id'], self.demo_tenant_id)
Phil Day5f615392014-09-10 12:10:16 +000063 for quota in expected_quota_set:
64 self.assertIn(quota, quota_set.keys())
Rohit Karajgi07599c52012-11-02 05:35:16 -070065
Chris Hoge7579c1a2015-02-26 14:12:15 -080066 @test.idempotent_id('55fbe2bf-21a9-435b-bbd2-4162b0ed799a')
Rohit Karajgi07599c52012-11-02 05:35:16 -070067 def test_update_all_quota_resources_for_tenant(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050068 # Admin can update all the resource quota limits for a tenant
Ken'ichi Ohmichif9868fc2015-06-17 02:36:06 +000069 default_quota_set = self.adm_client.show_default_quota_set(
ghanshyam52657872015-08-24 16:39:10 +090070 self.demo_tenant_id)['quota_set']
gengjh01ba9cc2013-06-15 10:14:48 +080071 new_quota_set = {'injected_file_content_bytes': 20480,
Rohit Karajgi07599c52012-11-02 05:35:16 -070072 'metadata_items': 256, 'injected_files': 10,
Michael Still9ac5bd02013-03-15 04:32:46 +110073 'ram': 10240, 'floating_ips': 20, 'fixed_ips': 10,
74 'key_pairs': 200, 'injected_file_path_bytes': 512,
75 'instances': 20, 'security_group_rules': 20,
zhufld887e0a2016-11-16 14:54:26 +080076 'cores': 2, 'security_groups': 20,
77 'server_groups': 20, 'server_group_members': 20}
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070078 # Update limits for all quota resources
David Kranz3e4c28b2015-02-09 12:35:18 -050079 quota_set = self.adm_client.update_quota_set(
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070080 self.demo_tenant_id,
gengjh01ba9cc2013-06-15 10:14:48 +080081 force=True,
ghanshyam52657872015-08-24 16:39:10 +090082 **new_quota_set)['quota_set']
gengjh01ba9cc2013-06-15 10:14:48 +080083
84 default_quota_set.pop('id')
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070085 self.addCleanup(self.adm_client.update_quota_set,
gengjh01ba9cc2013-06-15 10:14:48 +080086 self.demo_tenant_id, **default_quota_set)
Phil Day5f615392014-09-10 12:10:16 +000087 for quota in new_quota_set:
88 self.assertIn(quota, quota_set.keys())
Rohit Karajgi07599c52012-11-02 05:35:16 -070089
Attila Fazekasf7f34f92013-08-01 17:01:44 +020090 # TODO(afazekas): merge these test cases
Chris Hoge7579c1a2015-02-26 14:12:15 -080091 @test.idempotent_id('ce9e0815-8091-4abd-8345-7fe5b85faa1d')
Rohit Karajgi07599c52012-11-02 05:35:16 -070092 def test_get_updated_quotas(self):
Jamie Lennox15350172015-08-17 10:54:25 +100093 # Verify that GET shows the updated quota set of project
94 project_name = data_utils.rand_name('cpu_quota_project')
95 project_desc = project_name + '-desc'
96 project = self.identity_utils.create_project(name=project_name,
97 description=project_desc)
98 project_id = project['id']
99 self.addCleanup(self.identity_utils.delete_project, project_id)
Rohit Karajgi07599c52012-11-02 05:35:16 -0700100
Jamie Lennox15350172015-08-17 10:54:25 +1000101 self.adm_client.update_quota_set(project_id, ram='5120')
102 quota_set = self.adm_client.show_quota_set(project_id)['quota_set']
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500103 self.assertEqual(5120, quota_set['ram'])
104
105 # Verify that GET shows the updated quota set of user
Ken'ichi Ohmichi4937f562015-03-23 00:15:01 +0000106 user_name = data_utils.rand_name('cpu_quota_user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600107 password = data_utils.rand_password()
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500108 email = user_name + '@testmail.tm'
Jamie Lennox15350172015-08-17 10:54:25 +1000109 user = self.identity_utils.create_user(username=user_name,
110 password=password,
111 project=project,
112 email=email)
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500113 user_id = user['id']
Jamie Lennox15350172015-08-17 10:54:25 +1000114 self.addCleanup(self.identity_utils.delete_user, user_id)
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500115
Jamie Lennox15350172015-08-17 10:54:25 +1000116 self.adm_client.update_quota_set(project_id,
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500117 user_id=user_id,
118 ram='2048')
ghanshyam52657872015-08-24 16:39:10 +0900119 quota_set = self.adm_client.show_quota_set(
Jamie Lennox15350172015-08-17 10:54:25 +1000120 project_id, user_id=user_id)['quota_set']
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500121 self.assertEqual(2048, quota_set['ram'])
Attila Fazekasd9aef1e2013-07-13 17:33:45 +0200122
Chris Hoge7579c1a2015-02-26 14:12:15 -0800123 @test.idempotent_id('389d04f0-3a41-405f-9317-e5f86e3c44f0')
Yuiko Takada2209cf52014-02-27 12:03:49 +0000124 def test_delete_quota(self):
Jamie Lennox15350172015-08-17 10:54:25 +1000125 # Admin can delete the resource quota set for a project
126 project_name = data_utils.rand_name('ram_quota_project')
127 project_desc = project_name + '-desc'
128 project = self.identity_utils.create_project(name=project_name,
129 description=project_desc)
130 project_id = project['id']
131 self.addCleanup(self.identity_utils.delete_project, project_id)
132 quota_set_default = (self.adm_client.show_quota_set(project_id)
ghanshyam52657872015-08-24 16:39:10 +0900133 ['quota_set'])
Yuiko Takada2209cf52014-02-27 12:03:49 +0000134 ram_default = quota_set_default['ram']
135
Jamie Lennox15350172015-08-17 10:54:25 +1000136 self.adm_client.update_quota_set(project_id, ram='5120')
Yuiko Takada2209cf52014-02-27 12:03:49 +0000137
Jamie Lennox15350172015-08-17 10:54:25 +1000138 self.adm_client.delete_quota_set(project_id)
Yuiko Takada2209cf52014-02-27 12:03:49 +0000139
Jamie Lennox15350172015-08-17 10:54:25 +1000140 quota_set_new = self.adm_client.show_quota_set(project_id)['quota_set']
Yuiko Takada2209cf52014-02-27 12:03:49 +0000141 self.assertEqual(ram_default, quota_set_new['ram'])
142
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +0530143
Matt Riedemann848805f2014-06-16 13:23:51 -0700144class QuotaClassesAdminTestJSON(base.BaseV2ComputeAdminTest):
Ken'ichi Ohmichi88363cb2015-11-19 08:00:54 +0000145 """Tests the os-quota-class-sets API to update default quotas."""
Matt Riedemann848805f2014-06-16 13:23:51 -0700146
147 def setUp(self):
148 # All test cases in this class need to externally lock on doing
149 # anything with default quota values.
150 self.useFixture(fixtures.LockFixture('compute_quotas'))
151 super(QuotaClassesAdminTestJSON, self).setUp()
152
153 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +0100154 def resource_setup(cls):
155 super(QuotaClassesAdminTestJSON, cls).resource_setup()
Matt Riedemann848805f2014-06-16 13:23:51 -0700156 cls.adm_client = cls.os_adm.quota_classes_client
157
158 def _restore_default_quotas(self, original_defaults):
159 LOG.debug("restoring quota class defaults")
David Kranz3e4c28b2015-02-09 12:35:18 -0500160 self.adm_client.update_quota_class_set(
ghanshyam52657872015-08-24 16:39:10 +0900161 'default', **original_defaults)['quota_class_set']
Matt Riedemann848805f2014-06-16 13:23:51 -0700162
Sean Daguee3e9da72014-07-09 07:11:59 -0400163 # NOTE(sdague): this test is problematic as it changes
164 # global state, and possibly needs to be part of a set of
165 # tests that get run all by themselves at the end under a
166 # 'danger' flag.
Chris Hoge7579c1a2015-02-26 14:12:15 -0800167 @test.idempotent_id('7932ab0f-5136-4075-b201-c0e2338df51a')
Matt Riedemann848805f2014-06-16 13:23:51 -0700168 def test_update_default_quotas(self):
169 LOG.debug("get the current 'default' quota class values")
ghanshyam52657872015-08-24 16:39:10 +0900170 body = (self.adm_client.show_quota_class_set('default')
171 ['quota_class_set'])
Matt Riedemann848805f2014-06-16 13:23:51 -0700172 self.assertIn('id', body)
173 self.assertEqual('default', body.pop('id'))
174 # restore the defaults when the test is done
175 self.addCleanup(self._restore_default_quotas, body.copy())
176 # increment all of the values for updating the default quota class
guo yunxian7bbbec12016-08-21 20:03:10 +0800177 for quota, default in body.items():
Sean Daguee3e9da72014-07-09 07:11:59 -0400178 # NOTE(sdague): we need to increment a lot, otherwise
zhufl0892cb22016-05-06 14:46:00 +0800179 # there is a real chance that we go from -1 (unlimited)
Sean Daguee3e9da72014-07-09 07:11:59 -0400180 # to a very small number which causes issues.
181 body[quota] = default + 100
Matt Riedemann848805f2014-06-16 13:23:51 -0700182 LOG.debug("update limits for the default quota class set")
ghanshyam52657872015-08-24 16:39:10 +0900183 update_body = self.adm_client.update_quota_class_set(
184 'default', **body)['quota_class_set']
Matt Riedemann848805f2014-06-16 13:23:51 -0700185 LOG.debug("assert that the response has all of the changed values")
186 self.assertThat(update_body.items(),
187 matchers.ContainsAll(body.items()))