blob: e8acec59c6129cb20b889219222255aea3f033d2 [file] [log] [blame]
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +09001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2013 NEC Corporation.
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Sean Dague1937d092013-05-17 16:36:38 -040018from tempest.api.compute import base
Matthew Treinisha03ed792013-09-23 21:38:15 +000019from tempest.common import tempest_fixtures as fixtures
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090020from tempest.common.utils.data_utils import rand_name
21from tempest import exceptions
22from tempest.test import attr
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090023
24
ivan-zhuf2b00502013-10-18 10:06:52 +080025class AggregatesAdminTestJSON(base.BaseV2ComputeAdminTest):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090026
27 """
28 Tests Aggregates API that require admin privileges
29 """
30
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090031 _host_key = 'OS-EXT-SRV-ATTR:host'
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090032 _interface = 'json'
33
34 @classmethod
35 def setUpClass(cls):
36 super(AggregatesAdminTestJSON, cls).setUpClass()
37 cls.client = cls.os_adm.aggregates_client
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090038 cls.user_client = cls.aggregates_client
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090039 cls.aggregate_name_prefix = 'test_aggregate_'
40 cls.az_name_prefix = 'test_az_'
41
42 resp, hosts_all = cls.os_adm.hosts_client.list_hosts()
43 hosts = map(lambda x: x['host_name'],
44 filter(lambda y: y['service'] == 'compute', hosts_all))
45 cls.host = hosts[0]
46
Giulio Fidenteba3985a2013-05-29 01:46:36 +020047 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090048 def test_aggregate_create_delete(self):
49 # Create and delete an aggregate.
50 aggregate_name = rand_name(self.aggregate_name_prefix)
51 resp, aggregate = self.client.create_aggregate(aggregate_name)
Chang Bo Guofc77e932013-09-16 17:38:26 -070052 self.assertEqual(200, resp.status)
53 self.assertEqual(aggregate_name, aggregate['name'])
54 self.assertEqual(None, aggregate['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090055
56 resp, _ = self.client.delete_aggregate(aggregate['id'])
Chang Bo Guofc77e932013-09-16 17:38:26 -070057 self.assertEqual(200, resp.status)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090058 self.client.wait_for_resource_deletion(aggregate['id'])
59
Giulio Fidenteba3985a2013-05-29 01:46:36 +020060 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090061 def test_aggregate_create_delete_with_az(self):
62 # Create and delete an aggregate.
63 aggregate_name = rand_name(self.aggregate_name_prefix)
64 az_name = rand_name(self.az_name_prefix)
65 resp, aggregate = self.client.create_aggregate(aggregate_name, az_name)
Chang Bo Guofc77e932013-09-16 17:38:26 -070066 self.assertEqual(200, resp.status)
67 self.assertEqual(aggregate_name, aggregate['name'])
68 self.assertEqual(az_name, aggregate['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090069
70 resp, _ = self.client.delete_aggregate(aggregate['id'])
Chang Bo Guofc77e932013-09-16 17:38:26 -070071 self.assertEqual(200, resp.status)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090072 self.client.wait_for_resource_deletion(aggregate['id'])
73
Giulio Fidenteba3985a2013-05-29 01:46:36 +020074 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090075 def test_aggregate_create_verify_entry_in_list(self):
76 # Create an aggregate and ensure it is listed.
77 aggregate_name = rand_name(self.aggregate_name_prefix)
78 resp, aggregate = self.client.create_aggregate(aggregate_name)
79 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
80
81 resp, aggregates = self.client.list_aggregates()
Chang Bo Guofc77e932013-09-16 17:38:26 -070082 self.assertEqual(200, resp.status)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090083 self.assertIn((aggregate['id'], aggregate['availability_zone']),
84 map(lambda x: (x['id'], x['availability_zone']),
85 aggregates))
86
Giulio Fidenteba3985a2013-05-29 01:46:36 +020087 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090088 def test_aggregate_create_get_details(self):
89 # Create an aggregate and ensure its details are returned.
90 aggregate_name = rand_name(self.aggregate_name_prefix)
91 resp, aggregate = self.client.create_aggregate(aggregate_name)
92 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
93
94 resp, body = self.client.get_aggregate(aggregate['id'])
Chang Bo Guofc77e932013-09-16 17:38:26 -070095 self.assertEqual(200, resp.status)
96 self.assertEqual(aggregate['name'], body['name'])
97 self.assertEqual(aggregate['availability_zone'],
98 body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090099
Zhu Zhu7b5f6292013-09-22 15:47:54 +0800100 @attr(type='gate')
101 def test_aggregate_create_update_with_az(self):
102 # Update an aggregate and ensure properties are updated correctly
103 self.useFixture(fixtures.LockFixture('availability_zone'))
104 aggregate_name = rand_name(self.aggregate_name_prefix)
105 az_name = rand_name(self.az_name_prefix)
106 resp, aggregate = self.client.create_aggregate(aggregate_name, az_name)
107 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
108
109 self.assertEqual(200, resp.status)
110 self.assertEqual(aggregate_name, aggregate['name'])
111 self.assertEqual(az_name, aggregate['availability_zone'])
112 self.assertIsNotNone(aggregate['id'])
113
114 aggregate_id = aggregate['id']
115 new_aggregate_name = aggregate_name + '_new'
116 new_az_name = az_name + '_new'
117
118 resp, resp_aggregate = self.client.update_aggregate(aggregate_id,
119 new_aggregate_name,
120 new_az_name)
121 self.assertEqual(200, resp.status)
122 self.assertEqual(new_aggregate_name, resp_aggregate['name'])
123 self.assertEqual(new_az_name, resp_aggregate['availability_zone'])
124
125 resp, aggregates = self.client.list_aggregates()
126 self.assertEqual(200, resp.status)
127 self.assertIn((aggregate_id, new_aggregate_name, new_az_name),
128 map(lambda x:
129 (x['id'], x['name'], x['availability_zone']),
130 aggregates))
131
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400132 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900133 def test_aggregate_create_as_user(self):
134 # Regular user is not allowed to create an aggregate.
135 aggregate_name = rand_name(self.aggregate_name_prefix)
136 self.assertRaises(exceptions.Unauthorized,
137 self.user_client.create_aggregate,
138 aggregate_name)
139
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400140 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900141 def test_aggregate_delete_as_user(self):
142 # Regular user is not allowed to delete an aggregate.
143 aggregate_name = rand_name(self.aggregate_name_prefix)
144 resp, aggregate = self.client.create_aggregate(aggregate_name)
145 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
146
147 self.assertRaises(exceptions.Unauthorized,
148 self.user_client.delete_aggregate,
149 aggregate['id'])
150
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400151 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900152 def test_aggregate_list_as_user(self):
153 # Regular user is not allowed to list aggregates.
154 self.assertRaises(exceptions.Unauthorized,
155 self.user_client.list_aggregates)
156
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400157 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900158 def test_aggregate_get_details_as_user(self):
159 # Regular user is not allowed to get aggregate details.
160 aggregate_name = rand_name(self.aggregate_name_prefix)
161 resp, aggregate = self.client.create_aggregate(aggregate_name)
162 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
163
164 self.assertRaises(exceptions.Unauthorized,
165 self.user_client.get_aggregate,
166 aggregate['id'])
167
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400168 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900169 def test_aggregate_delete_with_invalid_id(self):
170 # Delete an aggregate with invalid id should raise exceptions.
171 self.assertRaises(exceptions.NotFound,
172 self.client.delete_aggregate, -1)
173
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400174 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900175 def test_aggregate_get_details_with_invalid_id(self):
176 # Get aggregate details with invalid id should raise exceptions.
177 self.assertRaises(exceptions.NotFound,
178 self.client.get_aggregate, -1)
179
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200180 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900181 def test_aggregate_add_remove_host(self):
182 # Add an host to the given aggregate and remove.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000183 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900184 aggregate_name = rand_name(self.aggregate_name_prefix)
185 resp, aggregate = self.client.create_aggregate(aggregate_name)
186 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
187
188 resp, body = self.client.add_host(aggregate['id'], self.host)
Chang Bo Guofc77e932013-09-16 17:38:26 -0700189 self.assertEqual(200, resp.status)
190 self.assertEqual(aggregate_name, body['name'])
191 self.assertEqual(aggregate['availability_zone'],
192 body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900193 self.assertIn(self.host, body['hosts'])
194
195 resp, body = self.client.remove_host(aggregate['id'], self.host)
Chang Bo Guofc77e932013-09-16 17:38:26 -0700196 self.assertEqual(200, resp.status)
197 self.assertEqual(aggregate_name, body['name'])
198 self.assertEqual(aggregate['availability_zone'],
199 body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900200 self.assertNotIn(self.host, body['hosts'])
201
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200202 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900203 def test_aggregate_add_host_list(self):
204 # Add an host to the given aggregate and list.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000205 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900206 aggregate_name = rand_name(self.aggregate_name_prefix)
207 resp, aggregate = self.client.create_aggregate(aggregate_name)
208 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
209 self.client.add_host(aggregate['id'], self.host)
210 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
211
212 resp, aggregates = self.client.list_aggregates()
213 aggs = filter(lambda x: x['id'] == aggregate['id'], aggregates)
Chang Bo Guofc77e932013-09-16 17:38:26 -0700214 self.assertEqual(1, len(aggs))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900215 agg = aggs[0]
Chang Bo Guofc77e932013-09-16 17:38:26 -0700216 self.assertEqual(aggregate_name, agg['name'])
217 self.assertEqual(None, agg['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900218 self.assertIn(self.host, agg['hosts'])
219
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200220 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900221 def test_aggregate_add_host_get_details(self):
222 # Add an host to the given aggregate and get details.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000223 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900224 aggregate_name = rand_name(self.aggregate_name_prefix)
225 resp, aggregate = self.client.create_aggregate(aggregate_name)
226 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
227 self.client.add_host(aggregate['id'], self.host)
228 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
229
230 resp, body = self.client.get_aggregate(aggregate['id'])
Chang Bo Guofc77e932013-09-16 17:38:26 -0700231 self.assertEqual(aggregate_name, body['name'])
232 self.assertEqual(None, body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900233 self.assertIn(self.host, body['hosts'])
234
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200235 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900236 def test_aggregate_add_host_create_server_with_az(self):
237 # Add an host to the given aggregate and create a server.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000238 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900239 aggregate_name = rand_name(self.aggregate_name_prefix)
240 az_name = rand_name(self.az_name_prefix)
241 resp, aggregate = self.client.create_aggregate(aggregate_name, az_name)
242 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
243 self.client.add_host(aggregate['id'], self.host)
244 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900245 server_name = rand_name('test_server_')
246 servers_client = self.servers_client
247 admin_servers_client = self.os_adm.servers_client
248 resp, server = self.create_server(name=server_name,
249 availability_zone=az_name)
250 servers_client.wait_for_server_status(server['id'], 'ACTIVE')
251 resp, body = admin_servers_client.get_server(server['id'])
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900252 self.assertEqual(self.host, body[self._host_key])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900253
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400254 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900255 def test_aggregate_add_non_exist_host(self):
256 # Adding a non-exist host to an aggregate should raise exceptions.
257 resp, hosts_all = self.os_adm.hosts_client.list_hosts()
258 hosts = map(lambda x: x['host_name'], hosts_all)
259 while True:
260 non_exist_host = rand_name('nonexist_host_')
261 if non_exist_host not in hosts:
262 break
263
264 aggregate_name = rand_name(self.aggregate_name_prefix)
265 resp, aggregate = self.client.create_aggregate(aggregate_name)
266 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
267
268 self.assertRaises(exceptions.NotFound, self.client.add_host,
269 aggregate['id'], non_exist_host)
270
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400271 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900272 def test_aggregate_add_host_as_user(self):
273 # Regular user is not allowed to add a host to an aggregate.
274 aggregate_name = rand_name(self.aggregate_name_prefix)
275 resp, aggregate = self.client.create_aggregate(aggregate_name)
276 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
277
278 self.assertRaises(exceptions.Unauthorized,
279 self.user_client.add_host,
280 aggregate['id'], self.host)
281
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400282 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900283 def test_aggregate_remove_host_as_user(self):
284 # Regular user is not allowed to remove a host from an aggregate.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000285 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900286 aggregate_name = rand_name(self.aggregate_name_prefix)
287 resp, aggregate = self.client.create_aggregate(aggregate_name)
288 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
289 self.client.add_host(aggregate['id'], self.host)
290 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
291
292 self.assertRaises(exceptions.Unauthorized,
293 self.user_client.remove_host,
294 aggregate['id'], self.host)
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900295
296
297class AggregatesAdminTestXML(AggregatesAdminTestJSON):
298 _host_key = (
299 '{http://docs.openstack.org/compute/ext/extended_status/api/v1.1}host')
300 _interface = 'xml'