blob: 4ff6b073f96b449a74b1c3a9c7ce8301d97abe27 [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')
ivan-zhu35e1f8e2013-10-18 15:51:16 +080088 def test_aggregate_create_update_metadata_get_details(self):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090089 # 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'])
ivan-zhu35e1f8e2013-10-18 15:51:16 +080099 self.assertEqual({}, body["metadata"])
100
101 # set the metadata of the aggregate
102 meta = {"key": "value"}
103 resp, body = self.client.set_metadata(aggregate['id'], meta)
104 self.assertEqual(200, resp.status)
105 self.assertEqual(meta, body["metadata"])
106
107 # verify the metadata has been set
108 resp, body = self.client.get_aggregate(aggregate['id'])
109 self.assertEqual(200, resp.status)
110 self.assertEqual(meta, body["metadata"])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900111
Zhu Zhu7b5f6292013-09-22 15:47:54 +0800112 @attr(type='gate')
113 def test_aggregate_create_update_with_az(self):
114 # Update an aggregate and ensure properties are updated correctly
115 self.useFixture(fixtures.LockFixture('availability_zone'))
116 aggregate_name = rand_name(self.aggregate_name_prefix)
117 az_name = rand_name(self.az_name_prefix)
118 resp, aggregate = self.client.create_aggregate(aggregate_name, az_name)
119 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
120
121 self.assertEqual(200, resp.status)
122 self.assertEqual(aggregate_name, aggregate['name'])
123 self.assertEqual(az_name, aggregate['availability_zone'])
124 self.assertIsNotNone(aggregate['id'])
125
126 aggregate_id = aggregate['id']
127 new_aggregate_name = aggregate_name + '_new'
128 new_az_name = az_name + '_new'
129
130 resp, resp_aggregate = self.client.update_aggregate(aggregate_id,
131 new_aggregate_name,
132 new_az_name)
133 self.assertEqual(200, resp.status)
134 self.assertEqual(new_aggregate_name, resp_aggregate['name'])
135 self.assertEqual(new_az_name, resp_aggregate['availability_zone'])
136
137 resp, aggregates = self.client.list_aggregates()
138 self.assertEqual(200, resp.status)
139 self.assertIn((aggregate_id, new_aggregate_name, new_az_name),
140 map(lambda x:
141 (x['id'], x['name'], x['availability_zone']),
142 aggregates))
143
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400144 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900145 def test_aggregate_create_as_user(self):
146 # Regular user is not allowed to create an aggregate.
147 aggregate_name = rand_name(self.aggregate_name_prefix)
148 self.assertRaises(exceptions.Unauthorized,
149 self.user_client.create_aggregate,
150 aggregate_name)
151
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400152 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900153 def test_aggregate_delete_as_user(self):
154 # Regular user is not allowed to delete an aggregate.
155 aggregate_name = rand_name(self.aggregate_name_prefix)
156 resp, aggregate = self.client.create_aggregate(aggregate_name)
157 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
158
159 self.assertRaises(exceptions.Unauthorized,
160 self.user_client.delete_aggregate,
161 aggregate['id'])
162
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400163 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900164 def test_aggregate_list_as_user(self):
165 # Regular user is not allowed to list aggregates.
166 self.assertRaises(exceptions.Unauthorized,
167 self.user_client.list_aggregates)
168
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400169 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900170 def test_aggregate_get_details_as_user(self):
171 # Regular user is not allowed to get aggregate details.
172 aggregate_name = rand_name(self.aggregate_name_prefix)
173 resp, aggregate = self.client.create_aggregate(aggregate_name)
174 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
175
176 self.assertRaises(exceptions.Unauthorized,
177 self.user_client.get_aggregate,
178 aggregate['id'])
179
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400180 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900181 def test_aggregate_delete_with_invalid_id(self):
182 # Delete an aggregate with invalid id should raise exceptions.
183 self.assertRaises(exceptions.NotFound,
184 self.client.delete_aggregate, -1)
185
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400186 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900187 def test_aggregate_get_details_with_invalid_id(self):
188 # Get aggregate details with invalid id should raise exceptions.
189 self.assertRaises(exceptions.NotFound,
190 self.client.get_aggregate, -1)
191
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200192 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900193 def test_aggregate_add_remove_host(self):
194 # Add an host to the given aggregate and remove.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000195 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900196 aggregate_name = rand_name(self.aggregate_name_prefix)
197 resp, aggregate = self.client.create_aggregate(aggregate_name)
198 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
199
200 resp, body = self.client.add_host(aggregate['id'], self.host)
Chang Bo Guofc77e932013-09-16 17:38:26 -0700201 self.assertEqual(200, resp.status)
202 self.assertEqual(aggregate_name, body['name'])
203 self.assertEqual(aggregate['availability_zone'],
204 body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900205 self.assertIn(self.host, body['hosts'])
206
207 resp, body = self.client.remove_host(aggregate['id'], self.host)
Chang Bo Guofc77e932013-09-16 17:38:26 -0700208 self.assertEqual(200, resp.status)
209 self.assertEqual(aggregate_name, body['name'])
210 self.assertEqual(aggregate['availability_zone'],
211 body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900212 self.assertNotIn(self.host, body['hosts'])
213
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200214 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900215 def test_aggregate_add_host_list(self):
216 # Add an host to the given aggregate and list.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000217 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900218 aggregate_name = rand_name(self.aggregate_name_prefix)
219 resp, aggregate = self.client.create_aggregate(aggregate_name)
220 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
221 self.client.add_host(aggregate['id'], self.host)
222 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
223
224 resp, aggregates = self.client.list_aggregates()
225 aggs = filter(lambda x: x['id'] == aggregate['id'], aggregates)
Chang Bo Guofc77e932013-09-16 17:38:26 -0700226 self.assertEqual(1, len(aggs))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900227 agg = aggs[0]
Chang Bo Guofc77e932013-09-16 17:38:26 -0700228 self.assertEqual(aggregate_name, agg['name'])
229 self.assertEqual(None, agg['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900230 self.assertIn(self.host, agg['hosts'])
231
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200232 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900233 def test_aggregate_add_host_get_details(self):
234 # Add an host to the given aggregate and get details.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000235 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900236 aggregate_name = rand_name(self.aggregate_name_prefix)
237 resp, aggregate = self.client.create_aggregate(aggregate_name)
238 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
239 self.client.add_host(aggregate['id'], self.host)
240 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
241
242 resp, body = self.client.get_aggregate(aggregate['id'])
Chang Bo Guofc77e932013-09-16 17:38:26 -0700243 self.assertEqual(aggregate_name, body['name'])
244 self.assertEqual(None, body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900245 self.assertIn(self.host, body['hosts'])
246
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200247 @attr(type='gate')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900248 def test_aggregate_add_host_create_server_with_az(self):
249 # Add an host to the given aggregate and create a server.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000250 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900251 aggregate_name = rand_name(self.aggregate_name_prefix)
252 az_name = rand_name(self.az_name_prefix)
253 resp, aggregate = self.client.create_aggregate(aggregate_name, az_name)
254 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
255 self.client.add_host(aggregate['id'], self.host)
256 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900257 server_name = rand_name('test_server_')
258 servers_client = self.servers_client
259 admin_servers_client = self.os_adm.servers_client
260 resp, server = self.create_server(name=server_name,
261 availability_zone=az_name)
262 servers_client.wait_for_server_status(server['id'], 'ACTIVE')
263 resp, body = admin_servers_client.get_server(server['id'])
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900264 self.assertEqual(self.host, body[self._host_key])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900265
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400266 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900267 def test_aggregate_add_non_exist_host(self):
268 # Adding a non-exist host to an aggregate should raise exceptions.
269 resp, hosts_all = self.os_adm.hosts_client.list_hosts()
270 hosts = map(lambda x: x['host_name'], hosts_all)
271 while True:
272 non_exist_host = rand_name('nonexist_host_')
273 if non_exist_host not in hosts:
274 break
275
276 aggregate_name = rand_name(self.aggregate_name_prefix)
277 resp, aggregate = self.client.create_aggregate(aggregate_name)
278 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
279
280 self.assertRaises(exceptions.NotFound, self.client.add_host,
281 aggregate['id'], non_exist_host)
282
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400283 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900284 def test_aggregate_add_host_as_user(self):
285 # Regular user is not allowed to add a host to an aggregate.
286 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
290 self.assertRaises(exceptions.Unauthorized,
291 self.user_client.add_host,
292 aggregate['id'], self.host)
293
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400294 @attr(type=['negative', 'gate'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900295 def test_aggregate_remove_host_as_user(self):
296 # Regular user is not allowed to remove a host from an aggregate.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000297 self.useFixture(fixtures.LockFixture('availability_zone'))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900298 aggregate_name = rand_name(self.aggregate_name_prefix)
299 resp, aggregate = self.client.create_aggregate(aggregate_name)
300 self.addCleanup(self.client.delete_aggregate, aggregate['id'])
301 self.client.add_host(aggregate['id'], self.host)
302 self.addCleanup(self.client.remove_host, aggregate['id'], self.host)
303
304 self.assertRaises(exceptions.Unauthorized,
305 self.user_client.remove_host,
306 aggregate['id'], self.host)
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +0900307
308
309class AggregatesAdminTestXML(AggregatesAdminTestJSON):
310 _host_key = (
311 '{http://docs.openstack.org/compute/ext/extended_status/api/v1.1}host')
312 _interface = 'xml'