blob: b0fcf94f3ed5248005a7fa408d71c6f445ea891c [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
Matt Riedemann848805f2014-06-16 13:23:51 -070016import six
17from 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
Masayuki Igawa259c1132013-10-31 17:48:44 +090021from tempest.common.utils import data_utils
Matt Riedemann848805f2014-06-16 13:23:51 -070022from tempest.openstack.common import log as logging
Haiwei Xuc367d912014-01-14 19:51:10 +090023from tempest import test
Sean Dague86bd8422013-12-20 09:56:44 -050024
Matt Riedemann848805f2014-06-16 13:23:51 -070025LOG = logging.getLogger(__name__)
26
Rohit Karajgi07599c52012-11-02 05:35:16 -070027
ivan-zhuf2b00502013-10-18 10:06:52 +080028class QuotasAdminTestJSON(base.BaseV2ComputeAdminTest):
Attila Fazekas430dae32013-10-17 15:19:32 +020029 force_tenant_isolation = True
Rohit Karajgi07599c52012-11-02 05:35:16 -070030
Matt Riedemann848805f2014-06-16 13:23:51 -070031 def setUp(self):
32 # NOTE(mriedem): Avoid conflicts with os-quota-class-sets tests.
33 self.useFixture(fixtures.LockFixture('compute_quotas'))
34 super(QuotasAdminTestJSON, self).setUp()
35
Rohit Karajgi07599c52012-11-02 05:35:16 -070036 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010037 def resource_setup(cls):
38 super(QuotasAdminTestJSON, cls).resource_setup()
Attila Fazekas4ba36582013-02-12 08:26:17 +010039 cls.adm_client = cls.os_adm.quotas_client
Attila Fazekas4ba36582013-02-12 08:26:17 +010040
Attila Fazekasf7f34f92013-08-01 17:01:44 +020041 # NOTE(afazekas): these test cases should always create and use a new
Attila Fazekas4ba36582013-02-12 08:26:17 +010042 # tenant most of them should be skipped if we can't do that
Andrea Frittoli9612e812014-03-13 10:57:26 +000043 cls.demo_tenant_id = cls.quotas_client.tenant_id
Rohit Karajgi07599c52012-11-02 05:35:16 -070044
Attila Fazekasd9aef1e2013-07-13 17:33:45 +020045 cls.default_quota_set = set(('injected_file_content_bytes',
46 'metadata_items', 'injected_files',
47 'ram', 'floating_ips',
48 'fixed_ips', 'key_pairs',
49 'injected_file_path_bytes',
50 'instances', 'security_group_rules',
51 'cores', 'security_groups'))
Rohit Karajgi07599c52012-11-02 05:35:16 -070052
Haiwei Xuc367d912014-01-14 19:51:10 +090053 @test.attr(type='smoke')
Rohit Karajgi07599c52012-11-02 05:35:16 -070054 def test_get_default_quotas(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050055 # Admin can get the default resource quota set for a tenant
Attila Fazekasd9aef1e2013-07-13 17:33:45 +020056 expected_quota_set = self.default_quota_set | set(['id'])
David Kranz3e4c28b2015-02-09 12:35:18 -050057 quota_set = self.adm_client.get_default_quota_set(
Leo Toyoda87a52b72013-04-09 10:34:40 +090058 self.demo_tenant_id)
Attila Fazekasd9aef1e2013-07-13 17:33:45 +020059 self.assertEqual(quota_set['id'], self.demo_tenant_id)
Phil Day5f615392014-09-10 12:10:16 +000060 for quota in expected_quota_set:
61 self.assertIn(quota, quota_set.keys())
Rohit Karajgi07599c52012-11-02 05:35:16 -070062
Haiwei Xuc367d912014-01-14 19:51:10 +090063 @test.attr(type='gate')
Rohit Karajgi07599c52012-11-02 05:35:16 -070064 def test_update_all_quota_resources_for_tenant(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050065 # Admin can update all the resource quota limits for a tenant
David Kranz3e4c28b2015-02-09 12:35:18 -050066 default_quota_set = self.adm_client.get_default_quota_set(
gengjh01ba9cc2013-06-15 10:14:48 +080067 self.demo_tenant_id)
68 new_quota_set = {'injected_file_content_bytes': 20480,
Rohit Karajgi07599c52012-11-02 05:35:16 -070069 'metadata_items': 256, 'injected_files': 10,
Michael Still9ac5bd02013-03-15 04:32:46 +110070 'ram': 10240, 'floating_ips': 20, 'fixed_ips': 10,
71 'key_pairs': 200, 'injected_file_path_bytes': 512,
72 'instances': 20, 'security_group_rules': 20,
73 'cores': 2, 'security_groups': 20}
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070074 # Update limits for all quota resources
David Kranz3e4c28b2015-02-09 12:35:18 -050075 quota_set = self.adm_client.update_quota_set(
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070076 self.demo_tenant_id,
gengjh01ba9cc2013-06-15 10:14:48 +080077 force=True,
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070078 **new_quota_set)
gengjh01ba9cc2013-06-15 10:14:48 +080079
80 default_quota_set.pop('id')
Phil Day5f615392014-09-10 12:10:16 +000081 # NOTE(PhilDay) The following is safe as we're not updating these
82 # two quota values yet. Once the Nova change to add these is merged
83 # and the client updated to support them this can be removed
84 if 'server_groups' in default_quota_set:
85 default_quota_set.pop('server_groups')
86 if 'server_group_members' in default_quota_set:
87 default_quota_set.pop('server_group_members')
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070088 self.addCleanup(self.adm_client.update_quota_set,
gengjh01ba9cc2013-06-15 10:14:48 +080089 self.demo_tenant_id, **default_quota_set)
Phil Day5f615392014-09-10 12:10:16 +000090 for quota in new_quota_set:
91 self.assertIn(quota, quota_set.keys())
Rohit Karajgi07599c52012-11-02 05:35:16 -070092
Attila Fazekasf7f34f92013-08-01 17:01:44 +020093 # TODO(afazekas): merge these test cases
Haiwei Xuc367d912014-01-14 19:51:10 +090094 @test.attr(type='gate')
Rohit Karajgi07599c52012-11-02 05:35:16 -070095 def test_get_updated_quotas(self):
Zhi Kun Liu27e154f2014-03-24 03:51:12 -050096 # Verify that GET shows the updated quota set of tenant
Masayuki Igawa259c1132013-10-31 17:48:44 +090097 tenant_name = data_utils.rand_name('cpu_quota_tenant_')
Attila Fazekasd9aef1e2013-07-13 17:33:45 +020098 tenant_desc = tenant_name + '-desc'
99 identity_client = self.os_adm.identity_client
David Kranzb7afa922014-12-30 10:56:26 -0500100 tenant = identity_client.create_tenant(name=tenant_name,
101 description=tenant_desc)
Attila Fazekasd9aef1e2013-07-13 17:33:45 +0200102 tenant_id = tenant['id']
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500103 self.addCleanup(identity_client.delete_tenant, tenant_id)
Rohit Karajgi07599c52012-11-02 05:35:16 -0700104
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500105 self.adm_client.update_quota_set(tenant_id, ram='5120')
David Kranz3e4c28b2015-02-09 12:35:18 -0500106 quota_set = self.adm_client.get_quota_set(tenant_id)
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500107 self.assertEqual(5120, quota_set['ram'])
108
109 # Verify that GET shows the updated quota set of user
110 user_name = data_utils.rand_name('cpu_quota_user_')
111 password = data_utils.rand_name('password-')
112 email = user_name + '@testmail.tm'
David Kranzb7afa922014-12-30 10:56:26 -0500113 user = identity_client.create_user(name=user_name,
114 password=password,
115 tenant_id=tenant_id,
116 email=email)
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500117 user_id = user['id']
118 self.addCleanup(identity_client.delete_user, user_id)
119
120 self.adm_client.update_quota_set(tenant_id,
121 user_id=user_id,
122 ram='2048')
David Kranz3e4c28b2015-02-09 12:35:18 -0500123 quota_set = self.adm_client.get_quota_set(tenant_id,
124 user_id=user_id)
Zhi Kun Liu27e154f2014-03-24 03:51:12 -0500125 self.assertEqual(2048, quota_set['ram'])
Attila Fazekasd9aef1e2013-07-13 17:33:45 +0200126
Yuiko Takada2209cf52014-02-27 12:03:49 +0000127 @test.attr(type='gate')
128 def test_delete_quota(self):
129 # Admin can delete the resource quota set for a tenant
130 tenant_name = data_utils.rand_name('ram_quota_tenant_')
131 tenant_desc = tenant_name + '-desc'
132 identity_client = self.os_adm.identity_client
David Kranzb7afa922014-12-30 10:56:26 -0500133 tenant = identity_client.create_tenant(name=tenant_name,
134 description=tenant_desc)
Yuiko Takada2209cf52014-02-27 12:03:49 +0000135 tenant_id = tenant['id']
136 self.addCleanup(identity_client.delete_tenant, tenant_id)
David Kranz3e4c28b2015-02-09 12:35:18 -0500137 quota_set_default = self.adm_client.get_quota_set(tenant_id)
Yuiko Takada2209cf52014-02-27 12:03:49 +0000138 ram_default = quota_set_default['ram']
139
David Kranz3e4c28b2015-02-09 12:35:18 -0500140 self.adm_client.update_quota_set(tenant_id, ram='5120')
Yuiko Takada2209cf52014-02-27 12:03:49 +0000141
David Kranz3e4c28b2015-02-09 12:35:18 -0500142 self.adm_client.delete_quota_set(tenant_id)
Yuiko Takada2209cf52014-02-27 12:03:49 +0000143
David Kranz3e4c28b2015-02-09 12:35:18 -0500144 quota_set_new = self.adm_client.get_quota_set(tenant_id)
Yuiko Takada2209cf52014-02-27 12:03:49 +0000145 self.assertEqual(ram_default, quota_set_new['ram'])
146
rajalakshmi-ganesan1982c3c2013-01-10 14:56:45 +0530147
Matt Riedemann848805f2014-06-16 13:23:51 -0700148class QuotaClassesAdminTestJSON(base.BaseV2ComputeAdminTest):
149 """Tests the os-quota-class-sets API to update default quotas.
150 """
151
152 def setUp(self):
153 # All test cases in this class need to externally lock on doing
154 # anything with default quota values.
155 self.useFixture(fixtures.LockFixture('compute_quotas'))
156 super(QuotaClassesAdminTestJSON, self).setUp()
157
158 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +0100159 def resource_setup(cls):
160 super(QuotaClassesAdminTestJSON, cls).resource_setup()
Matt Riedemann848805f2014-06-16 13:23:51 -0700161 cls.adm_client = cls.os_adm.quota_classes_client
162
163 def _restore_default_quotas(self, original_defaults):
164 LOG.debug("restoring quota class defaults")
David Kranz3e4c28b2015-02-09 12:35:18 -0500165 self.adm_client.update_quota_class_set(
Matt Riedemann848805f2014-06-16 13:23:51 -0700166 'default', **original_defaults)
Matt Riedemann848805f2014-06-16 13:23:51 -0700167
Sean Daguee3e9da72014-07-09 07:11:59 -0400168 # NOTE(sdague): this test is problematic as it changes
169 # global state, and possibly needs to be part of a set of
170 # tests that get run all by themselves at the end under a
171 # 'danger' flag.
Matt Riedemann848805f2014-06-16 13:23:51 -0700172 def test_update_default_quotas(self):
173 LOG.debug("get the current 'default' quota class values")
David Kranz3e4c28b2015-02-09 12:35:18 -0500174 body = self.adm_client.get_quota_class_set('default')
Matt Riedemann848805f2014-06-16 13:23:51 -0700175 self.assertIn('id', body)
176 self.assertEqual('default', body.pop('id'))
177 # restore the defaults when the test is done
178 self.addCleanup(self._restore_default_quotas, body.copy())
179 # increment all of the values for updating the default quota class
180 for quota, default in six.iteritems(body):
Sean Daguee3e9da72014-07-09 07:11:59 -0400181 # NOTE(sdague): we need to increment a lot, otherwise
182 # there is a real chance that we go from -1 (unlimitted)
183 # to a very small number which causes issues.
184 body[quota] = default + 100
Matt Riedemann848805f2014-06-16 13:23:51 -0700185 LOG.debug("update limits for the default quota class set")
David Kranz3e4c28b2015-02-09 12:35:18 -0500186 update_body = self.adm_client.update_quota_class_set('default',
187 **body)
Matt Riedemann848805f2014-06-16 13:23:51 -0700188 LOG.debug("assert that the response has all of the changed values")
189 self.assertThat(update_body.items(),
190 matchers.ContainsAll(body.items()))