blob: ccd294dd64c5fe212886c287c3ed7655151e39f8 [file] [log] [blame]
Lingxian Kongbf2d5172013-10-01 22:00:24 +08001# Copyright 2013 Huawei Technologies Co.,LTD.
2# 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
Masayuki Igawad9388762015-01-20 14:56:42 +090016from tempest_lib import exceptions as lib_exc
17
Lingxian Kongbf2d5172013-10-01 22:00:24 +080018from tempest.api.compute import base
19from tempest.common import tempest_fixtures as fixtures
20from tempest.common.utils import data_utils
21from tempest import exceptions
Masayuki Igawa394d8d92014-03-04 17:21:56 +090022from tempest import test
Lingxian Kongbf2d5172013-10-01 22:00:24 +080023
24
25class AggregatesAdminNegativeTestJSON(base.BaseV2ComputeAdminTest):
26
27 """
28 Tests Aggregates API that require admin privileges
29 """
30
Lingxian Kongbf2d5172013-10-01 22:00:24 +080031 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010032 def resource_setup(cls):
33 super(AggregatesAdminNegativeTestJSON, cls).resource_setup()
Lingxian Kongbf2d5172013-10-01 22:00:24 +080034 cls.client = cls.os_adm.aggregates_client
35 cls.user_client = cls.aggregates_client
36 cls.aggregate_name_prefix = 'test_aggregate_'
37 cls.az_name_prefix = 'test_az_'
38
David Kranz0a735172015-01-16 10:51:18 -050039 hosts_all = cls.os_adm.hosts_client.list_hosts()
Lingxian Kongbf2d5172013-10-01 22:00:24 +080040 hosts = map(lambda x: x['host_name'],
41 filter(lambda y: y['service'] == 'compute', hosts_all))
42 cls.host = hosts[0]
43
Masayuki Igawa394d8d92014-03-04 17:21:56 +090044 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +080045 def test_aggregate_create_as_user(self):
46 # Regular user is not allowed to create an aggregate.
47 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
48 self.assertRaises(exceptions.Unauthorized,
49 self.user_client.create_aggregate,
Yuiko Takadaf93d2482014-01-30 16:25:08 +000050 name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +080051
Masayuki Igawa394d8d92014-03-04 17:21:56 +090052 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +080053 def test_aggregate_create_aggregate_name_length_less_than_1(self):
54 # the length of aggregate name should >= 1 and <=255
55 self.assertRaises(exceptions.BadRequest,
56 self.client.create_aggregate,
Yuiko Takadaf93d2482014-01-30 16:25:08 +000057 name='')
Lingxian Kongbf2d5172013-10-01 22:00:24 +080058
Masayuki Igawa394d8d92014-03-04 17:21:56 +090059 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +080060 def test_aggregate_create_aggregate_name_length_exceeds_255(self):
61 # the length of aggregate name should >= 1 and <=255
62 aggregate_name = 'a' * 256
63 self.assertRaises(exceptions.BadRequest,
64 self.client.create_aggregate,
Yuiko Takadaf93d2482014-01-30 16:25:08 +000065 name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +080066
Masayuki Igawa394d8d92014-03-04 17:21:56 +090067 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +080068 def test_aggregate_create_with_existent_aggregate_name(self):
69 # creating an aggregate with existent aggregate name is forbidden
70 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
David Kranz0a735172015-01-16 10:51:18 -050071 aggregate = self.client.create_aggregate(name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +080072 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
73
Masayuki Igawad9388762015-01-20 14:56:42 +090074 self.assertRaises(lib_exc.Conflict,
Lingxian Kongbf2d5172013-10-01 22:00:24 +080075 self.client.create_aggregate,
Yuiko Takadaf93d2482014-01-30 16:25:08 +000076 name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +080077
Masayuki Igawa394d8d92014-03-04 17:21:56 +090078 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +080079 def test_aggregate_delete_as_user(self):
80 # Regular user is not allowed to delete an aggregate.
81 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
David Kranz0a735172015-01-16 10:51:18 -050082 aggregate = self.client.create_aggregate(name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +080083 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
84
85 self.assertRaises(exceptions.Unauthorized,
86 self.user_client.delete_aggregate,
87 aggregate['id'])
88
Masayuki Igawa394d8d92014-03-04 17:21:56 +090089 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +080090 def test_aggregate_list_as_user(self):
91 # Regular user is not allowed to list aggregates.
92 self.assertRaises(exceptions.Unauthorized,
93 self.user_client.list_aggregates)
94
Masayuki Igawa394d8d92014-03-04 17:21:56 +090095 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +080096 def test_aggregate_get_details_as_user(self):
97 # Regular user is not allowed to get aggregate details.
98 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
David Kranz0a735172015-01-16 10:51:18 -050099 aggregate = self.client.create_aggregate(name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800100 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
101
102 self.assertRaises(exceptions.Unauthorized,
103 self.user_client.get_aggregate,
104 aggregate['id'])
105
Masayuki Igawa394d8d92014-03-04 17:21:56 +0900106 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800107 def test_aggregate_delete_with_invalid_id(self):
108 # Delete an aggregate with invalid id should raise exceptions.
109 self.assertRaises(exceptions.NotFound,
110 self.client.delete_aggregate, -1)
111
Masayuki Igawa394d8d92014-03-04 17:21:56 +0900112 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800113 def test_aggregate_get_details_with_invalid_id(self):
114 # Get aggregate details with invalid id should raise exceptions.
115 self.assertRaises(exceptions.NotFound,
116 self.client.get_aggregate, -1)
117
Masayuki Igawa394d8d92014-03-04 17:21:56 +0900118 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800119 def test_aggregate_add_non_exist_host(self):
120 # Adding a non-exist host to an aggregate should raise exceptions.
David Kranz0a735172015-01-16 10:51:18 -0500121 hosts_all = self.os_adm.hosts_client.list_hosts()
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800122 hosts = map(lambda x: x['host_name'], hosts_all)
123 while True:
124 non_exist_host = data_utils.rand_name('nonexist_host_')
125 if non_exist_host not in hosts:
126 break
127
128 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
David Kranz0a735172015-01-16 10:51:18 -0500129 aggregate = self.client.create_aggregate(name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800130 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
131
132 self.assertRaises(exceptions.NotFound, self.client.add_host,
133 aggregate['id'], non_exist_host)
134
Masayuki Igawa394d8d92014-03-04 17:21:56 +0900135 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800136 def test_aggregate_add_host_as_user(self):
137 # Regular user is not allowed to add a host to an aggregate.
138 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
David Kranz0a735172015-01-16 10:51:18 -0500139 aggregate = self.client.create_aggregate(name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800140 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
141
142 self.assertRaises(exceptions.Unauthorized,
143 self.user_client.add_host,
144 aggregate['id'], self.host)
145
Masayuki Igawa394d8d92014-03-04 17:21:56 +0900146 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800147 def test_aggregate_add_existent_host(self):
148 self.useFixture(fixtures.LockFixture('availability_zone'))
149 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
David Kranz0a735172015-01-16 10:51:18 -0500150 aggregate = self.client.create_aggregate(name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800151 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
152
David Kranz0a735172015-01-16 10:51:18 -0500153 self.client.add_host(aggregate['id'], self.host)
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800154 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
155
Masayuki Igawad9388762015-01-20 14:56:42 +0900156 self.assertRaises(lib_exc.Conflict, self.client.add_host,
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800157 aggregate['id'], self.host)
158
Masayuki Igawa394d8d92014-03-04 17:21:56 +0900159 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800160 def test_aggregate_remove_host_as_user(self):
161 # Regular user is not allowed to remove a host from an aggregate.
162 self.useFixture(fixtures.LockFixture('availability_zone'))
163 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
David Kranz0a735172015-01-16 10:51:18 -0500164 aggregate = self.client.create_aggregate(name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800165 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
David Kranz0a735172015-01-16 10:51:18 -0500166 self.client.add_host(aggregate['id'], self.host)
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800167 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
168
169 self.assertRaises(exceptions.Unauthorized,
170 self.user_client.remove_host,
171 aggregate['id'], self.host)
172
Masayuki Igawa394d8d92014-03-04 17:21:56 +0900173 @test.attr(type=['negative', 'gate'])
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800174 def test_aggregate_remove_nonexistent_host(self):
175 non_exist_host = data_utils.rand_name('nonexist_host_')
176 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
David Kranz0a735172015-01-16 10:51:18 -0500177 aggregate = self.client.create_aggregate(name=aggregate_name)
Lingxian Kongbf2d5172013-10-01 22:00:24 +0800178 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
179
180 self.assertRaises(exceptions.NotFound, self.client.remove_host,
181 aggregate['id'], non_exist_host)