blob: d8faa3369370a2b0e7618e910cad03c8770a934d [file] [log] [blame]
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +09001# Copyright 2013 NEC Corporation.
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
zhuflc2c2f012016-05-16 17:34:18 +080016import testtools
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
Ken'ichi Ohmichi49db4fe2016-08-12 15:26:51 -070020from tempest import config
Ken'ichi Ohmichi757833a2017-03-10 10:30:30 -080021from tempest.lib.common.utils import data_utils
Jordan Pittier9e227c52016-02-09 14:35:18 +010022from tempest.lib.common.utils import test_utils
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -080023from tempest.lib import decorators
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090024
Ken'ichi Ohmichi49db4fe2016-08-12 15:26:51 -070025CONF = config.CONF
26
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090027
ivan-zhuf2b00502013-10-18 10:06:52 +080028class AggregatesAdminTestJSON(base.BaseV2ComputeAdminTest):
Ken'ichi Ohmichi88363cb2015-11-19 08:00:54 +000029 """Tests Aggregates API that require admin privileges"""
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090030
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090031 @classmethod
Rohan Kanade60b73092015-02-04 17:58:19 +053032 def setup_clients(cls):
33 super(AggregatesAdminTestJSON, cls).setup_clients()
Jordan Pittier8160d312017-04-18 11:52:23 +020034 cls.client = cls.os_admin.aggregates_client
Rohan Kanade60b73092015-02-04 17:58:19 +053035
36 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010037 def resource_setup(cls):
38 super(AggregatesAdminTestJSON, cls).resource_setup()
Ken'ichi Ohmichi4937f562015-03-23 00:15:01 +000039 cls.aggregate_name_prefix = 'test_aggregate'
40 cls.az_name_prefix = 'test_az'
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090041
zhuflc2c2f012016-05-16 17:34:18 +080042 cls.host = None
Jordan Pittier8160d312017-04-18 11:52:23 +020043 hypers = cls.os_admin.hypervisor_client.list_hypervisors(
Jianghua Wangdbe76af2016-06-13 12:53:16 +080044 detail=True)['hypervisors']
Ken'ichi Ohmichi49db4fe2016-08-12 15:26:51 -070045
46 if CONF.compute.hypervisor_type:
47 hypers = [hyper for hyper in hypers
48 if (hyper['hypervisor_type'] ==
49 CONF.compute.hypervisor_type)]
50
zhufl34afeb22018-02-09 15:03:41 +080051 cls.hosts_available = [hyper['service']['host'] for hyper in hypers
52 if (hyper['state'] == 'up' and
53 hyper['status'] == 'enabled')]
54 if cls.hosts_available:
55 cls.host = cls.hosts_available[0]
zhuflc2c2f012016-05-16 17:34:18 +080056 else:
Ken'ichi Ohmichi49db4fe2016-08-12 15:26:51 -070057 msg = "no available compute node found"
58 if CONF.compute.hypervisor_type:
59 msg += " for hypervisor_type %s" % CONF.compute.hypervisor_type
60 raise testtools.TestCase.failureException(msg)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090061
zhuflb9266952017-05-04 15:41:57 +080062 def _create_test_aggregate(self, **kwargs):
63 if 'name' not in kwargs:
64 kwargs['name'] = data_utils.rand_name(self.aggregate_name_prefix)
65 aggregate = self.client.create_aggregate(**kwargs)['aggregate']
66 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
67 self.client.delete_aggregate, aggregate['id'])
68 self.assertEqual(kwargs['name'], aggregate['name'])
69
70 return aggregate
71
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -080072 @decorators.idempotent_id('0d148aa3-d54c-4317-aa8d-42040a475e20')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090073 def test_aggregate_create_delete(self):
74 # Create and delete an aggregate.
zhuflb9266952017-05-04 15:41:57 +080075 aggregate = self._create_test_aggregate()
llg8212e4cd3922014-02-15 12:14:21 +080076 self.assertIsNone(aggregate['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090077
David Kranz0a735172015-01-16 10:51:18 -050078 self.client.delete_aggregate(aggregate['id'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090079 self.client.wait_for_resource_deletion(aggregate['id'])
80
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -080081 @decorators.idempotent_id('5873a6f8-671a-43ff-8838-7ce430bb6d0b')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090082 def test_aggregate_create_delete_with_az(self):
83 # Create and delete an aggregate.
Masayuki Igawa259c1132013-10-31 17:48:44 +090084 az_name = data_utils.rand_name(self.az_name_prefix)
zhuflb9266952017-05-04 15:41:57 +080085 aggregate = self._create_test_aggregate(availability_zone=az_name)
Chang Bo Guofc77e932013-09-16 17:38:26 -070086 self.assertEqual(az_name, aggregate['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090087
David Kranz0a735172015-01-16 10:51:18 -050088 self.client.delete_aggregate(aggregate['id'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090089 self.client.wait_for_resource_deletion(aggregate['id'])
90
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -080091 @decorators.idempotent_id('68089c38-04b1-4758-bdf0-cf0daec4defd')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090092 def test_aggregate_create_verify_entry_in_list(self):
93 # Create an aggregate and ensure it is listed.
zhuflb9266952017-05-04 15:41:57 +080094 aggregate = self._create_test_aggregate()
ghanshyam2eb282d2015-08-04 15:05:19 +090095 aggregates = self.client.list_aggregates()['aggregates']
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +090096 self.assertIn((aggregate['id'], aggregate['availability_zone']),
97 map(lambda x: (x['id'], x['availability_zone']),
98 aggregates))
99
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -0800100 @decorators.idempotent_id('36ec92ca-7a73-43bc-b920-7531809e8540')
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800101 def test_aggregate_create_update_metadata_get_details(self):
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900102 # Create an aggregate and ensure its details are returned.
zhuflb9266952017-05-04 15:41:57 +0800103 aggregate = self._create_test_aggregate()
ghanshyam2eb282d2015-08-04 15:05:19 +0900104 body = self.client.show_aggregate(aggregate['id'])['aggregate']
Chang Bo Guofc77e932013-09-16 17:38:26 -0700105 self.assertEqual(aggregate['name'], body['name'])
106 self.assertEqual(aggregate['availability_zone'],
107 body['availability_zone'])
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800108 self.assertEqual({}, body["metadata"])
109
110 # set the metadata of the aggregate
111 meta = {"key": "value"}
Ken'ichi Ohmichiec452b82015-07-15 04:59:51 +0000112 body = self.client.set_metadata(aggregate['id'], metadata=meta)
ghanshyam2eb282d2015-08-04 15:05:19 +0900113 self.assertEqual(meta, body['aggregate']["metadata"])
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800114
115 # verify the metadata has been set
ghanshyam2eb282d2015-08-04 15:05:19 +0900116 body = self.client.show_aggregate(aggregate['id'])['aggregate']
ivan-zhu35e1f8e2013-10-18 15:51:16 +0800117 self.assertEqual(meta, body["metadata"])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900118
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -0800119 @decorators.idempotent_id('4d2b2004-40fa-40a1-aab2-66f4dab81beb')
Zhu Zhu7b5f6292013-09-22 15:47:54 +0800120 def test_aggregate_create_update_with_az(self):
121 # Update an aggregate and ensure properties are updated correctly
Masayuki Igawa259c1132013-10-31 17:48:44 +0900122 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
123 az_name = data_utils.rand_name(self.az_name_prefix)
zhuflb9266952017-05-04 15:41:57 +0800124 aggregate = self._create_test_aggregate(
125 name=aggregate_name, availability_zone=az_name)
Zhu Zhu7b5f6292013-09-22 15:47:54 +0800126
Zhu Zhu7b5f6292013-09-22 15:47:54 +0800127 self.assertEqual(az_name, aggregate['availability_zone'])
Zhu Zhu7b5f6292013-09-22 15:47:54 +0800128
129 aggregate_id = aggregate['id']
130 new_aggregate_name = aggregate_name + '_new'
131 new_az_name = az_name + '_new'
132
Ken'ichi Ohmichiec452b82015-07-15 04:59:51 +0000133 resp_aggregate = self.client.update_aggregate(
134 aggregate_id,
135 name=new_aggregate_name,
ghanshyam2eb282d2015-08-04 15:05:19 +0900136 availability_zone=new_az_name)['aggregate']
Zhu Zhu7b5f6292013-09-22 15:47:54 +0800137 self.assertEqual(new_aggregate_name, resp_aggregate['name'])
138 self.assertEqual(new_az_name, resp_aggregate['availability_zone'])
139
ghanshyam2eb282d2015-08-04 15:05:19 +0900140 aggregates = self.client.list_aggregates()['aggregates']
Zhu Zhu7b5f6292013-09-22 15:47:54 +0800141 self.assertIn((aggregate_id, new_aggregate_name, new_az_name),
142 map(lambda x:
Matthew Treinish1d14c542014-06-17 20:25:40 -0400143 (x['id'], x['name'], x['availability_zone']),
Zhu Zhu7b5f6292013-09-22 15:47:54 +0800144 aggregates))
145
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -0800146 @decorators.idempotent_id('c8e85064-e79b-4906-9931-c11c24294d02')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900147 def test_aggregate_add_remove_host(self):
Alanad42cf22016-01-04 05:34:37 -0500148 # Add a host to the given aggregate and remove.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000149 self.useFixture(fixtures.LockFixture('availability_zone'))
Masayuki Igawa259c1132013-10-31 17:48:44 +0900150 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
zhuflb9266952017-05-04 15:41:57 +0800151 aggregate = self._create_test_aggregate(name=aggregate_name)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900152
ghanshyam2eb282d2015-08-04 15:05:19 +0900153 body = (self.client.add_host(aggregate['id'], host=self.host)
154 ['aggregate'])
Chang Bo Guofc77e932013-09-16 17:38:26 -0700155 self.assertEqual(aggregate_name, body['name'])
156 self.assertEqual(aggregate['availability_zone'],
157 body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900158 self.assertIn(self.host, body['hosts'])
159
ghanshyam2eb282d2015-08-04 15:05:19 +0900160 body = (self.client.remove_host(aggregate['id'], host=self.host)
161 ['aggregate'])
Chang Bo Guofc77e932013-09-16 17:38:26 -0700162 self.assertEqual(aggregate_name, body['name'])
163 self.assertEqual(aggregate['availability_zone'],
164 body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900165 self.assertNotIn(self.host, body['hosts'])
166
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -0800167 @decorators.idempotent_id('7f6a1cc5-2446-4cdb-9baa-b6ae0a919b72')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900168 def test_aggregate_add_host_list(self):
Alanad42cf22016-01-04 05:34:37 -0500169 # Add a host to the given aggregate and list.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000170 self.useFixture(fixtures.LockFixture('availability_zone'))
Masayuki Igawa259c1132013-10-31 17:48:44 +0900171 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
zhuflb9266952017-05-04 15:41:57 +0800172 aggregate = self._create_test_aggregate(name=aggregate_name)
173
Ken'ichi Ohmichiec452b82015-07-15 04:59:51 +0000174 self.client.add_host(aggregate['id'], host=self.host)
175 self.addCleanup(self.client.remove_host, aggregate['id'],
176 host=self.host)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900177
ghanshyam2eb282d2015-08-04 15:05:19 +0900178 aggregates = self.client.list_aggregates()['aggregates']
Sirushti Murugesan935f2cc2016-07-12 19:48:24 +0530179 aggs = [agg for agg in aggregates if agg['id'] == aggregate['id']]
Chang Bo Guofc77e932013-09-16 17:38:26 -0700180 self.assertEqual(1, len(aggs))
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900181 agg = aggs[0]
Chang Bo Guofc77e932013-09-16 17:38:26 -0700182 self.assertEqual(aggregate_name, agg['name'])
llg8212e4cd3922014-02-15 12:14:21 +0800183 self.assertIsNone(agg['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900184 self.assertIn(self.host, agg['hosts'])
185
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -0800186 @decorators.idempotent_id('eeef473c-7c52-494d-9f09-2ed7fc8fc036')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900187 def test_aggregate_add_host_get_details(self):
Alanad42cf22016-01-04 05:34:37 -0500188 # Add a host to the given aggregate and get details.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000189 self.useFixture(fixtures.LockFixture('availability_zone'))
Masayuki Igawa259c1132013-10-31 17:48:44 +0900190 aggregate_name = data_utils.rand_name(self.aggregate_name_prefix)
zhuflb9266952017-05-04 15:41:57 +0800191 aggregate = self._create_test_aggregate(name=aggregate_name)
192
Ken'ichi Ohmichiec452b82015-07-15 04:59:51 +0000193 self.client.add_host(aggregate['id'], host=self.host)
194 self.addCleanup(self.client.remove_host, aggregate['id'],
195 host=self.host)
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900196
ghanshyam2eb282d2015-08-04 15:05:19 +0900197 body = self.client.show_aggregate(aggregate['id'])['aggregate']
Chang Bo Guofc77e932013-09-16 17:38:26 -0700198 self.assertEqual(aggregate_name, body['name'])
llg8212e4cd3922014-02-15 12:14:21 +0800199 self.assertIsNone(body['availability_zone'])
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900200 self.assertIn(self.host, body['hosts'])
201
Ken'ichi Ohmichiebbfd1c2017-01-27 16:37:00 -0800202 @decorators.idempotent_id('96be03c7-570d-409c-90f8-e4db3c646996')
Mitsuhiko Yamazaki74f07072013-04-02 11:52:31 +0900203 def test_aggregate_add_host_create_server_with_az(self):
Alanad42cf22016-01-04 05:34:37 -0500204 # Add a host to the given aggregate and create a server.
Matthew Treinisha03ed792013-09-23 21:38:15 +0000205 self.useFixture(fixtures.LockFixture('availability_zone'))
Masayuki Igawa259c1132013-10-31 17:48:44 +0900206 az_name = data_utils.rand_name(self.az_name_prefix)
zhuflb9266952017-05-04 15:41:57 +0800207 aggregate = self._create_test_aggregate(availability_zone=az_name)
208
zhufl34afeb22018-02-09 15:03:41 +0800209 # Find a host that has not been added to other zone,
210 # for one host can't be added to different zones.
211 aggregates = self.client.list_aggregates()['aggregates']
212 hosts_in_zone = []
213 for v in aggregates:
214 if v['availability_zone']:
215 hosts_in_zone.extend(v['hosts'])
216 hosts = [v for v in self.hosts_available if v not in hosts_in_zone]
217 if not hosts:
218 raise self.skipException("All hosts are already in other zones, "
219 "so can't add host to aggregate.")
220 host = hosts[0]
221
222 self.client.add_host(aggregate['id'], host=host)
223 self.addCleanup(self.client.remove_host, aggregate['id'], host=host)
Jordan Pittier8160d312017-04-18 11:52:23 +0200224 admin_servers_client = self.os_admin.servers_client
zhufl7ae22682016-09-18 15:22:33 +0800225 server = self.create_test_server(availability_zone=az_name,
David Kranz0fb14292015-02-11 15:55:20 -0500226 wait_until='ACTIVE')
ghanshyam0f825252015-08-25 16:02:50 +0900227 body = admin_servers_client.show_server(server['id'])['server']
zhufl34afeb22018-02-09 15:03:41 +0800228 self.assertEqual(host, body['OS-EXT-SRV-ATTR:host'])