blob: fcd055bb8968849d6573c39086ab9a0cadca2654 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Mate Lakat99ee9142012-09-14 12:34:46 +01002# 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
Mate Lakat99ee9142012-09-14 12:34:46 +010016import random
17import string
18
ivan-zhu1feeb382013-01-24 10:14:39 +080019import testtools
Matthew Treinisha83a16e2012-12-07 13:44:02 -050020
Sean Dague1937d092013-05-17 16:36:38 -040021from tempest.api.compute import base
Mate Lakat99ee9142012-09-14 12:34:46 +010022from tempest import config
23from tempest import exceptions
Chris Yeoh9465b0b2013-02-09 22:19:15 +103024from tempest.test import attr
Mate Lakat99ee9142012-09-14 12:34:46 +010025
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000026CONF = config.CONF
27
Mate Lakat99ee9142012-09-14 12:34:46 +010028
ivan-zhuf2b00502013-10-18 10:06:52 +080029class LiveBlockMigrationTestJSON(base.BaseV2ComputeAdminTest):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +000030 _host_key = 'OS-EXT-SRV-ATTR:host'
Attila Fazekas19044d52013-02-16 07:35:06 +010031 _interface = 'json'
Mate Lakat99ee9142012-09-14 12:34:46 +010032
Mate Lakat99ee9142012-09-14 12:34:46 +010033 @classmethod
34 def setUpClass(cls):
ravikumar-venkatesan753262b2013-03-21 12:43:57 +000035 super(LiveBlockMigrationTestJSON, cls).setUpClass()
Mate Lakat99ee9142012-09-14 12:34:46 +010036
Attila Fazekas8e99b992013-02-24 09:53:23 +010037 cls.admin_hosts_client = cls.os_adm.hosts_client
38 cls.admin_servers_client = cls.os_adm.servers_client
Mate Lakat99ee9142012-09-14 12:34:46 +010039
40 cls.created_server_ids = []
41
42 def _get_compute_hostnames(self):
43 _resp, body = self.admin_hosts_client.list_hosts()
44 return [
45 host_record['host_name']
46 for host_record in body
47 if host_record['service'] == 'compute'
48 ]
49
50 def _get_server_details(self, server_id):
51 _resp, body = self.admin_servers_client.get_server(server_id)
52 return body
53
54 def _get_host_for_server(self, server_id):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +000055 return self._get_server_details(server_id)[self._host_key]
Mate Lakat99ee9142012-09-14 12:34:46 +010056
57 def _migrate_server_to(self, server_id, dest_host):
58 _resp, body = self.admin_servers_client.live_migrate_server(
Bob Ball0dca8972013-04-09 16:23:31 +010059 server_id, dest_host,
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000060 CONF.compute_feature_enabled.block_migration_for_live_migration)
Mate Lakat99ee9142012-09-14 12:34:46 +010061 return body
62
63 def _get_host_other_than(self, host):
64 for target_host in self._get_compute_hostnames():
65 if host != target_host:
66 return target_host
67
68 def _get_non_existing_host_name(self):
69 random_name = ''.join(
70 random.choice(string.ascii_uppercase) for x in range(20))
71
Jaroslav Henner3c3d1792012-11-16 10:28:47 +010072 self.assertNotIn(random_name, self._get_compute_hostnames())
Mate Lakat99ee9142012-09-14 12:34:46 +010073
74 return random_name
75
76 def _get_server_status(self, server_id):
77 return self._get_server_details(server_id)['status']
78
79 def _get_an_active_server(self):
80 for server_id in self.created_server_ids:
81 if 'ACTIVE' == self._get_server_status(server_id):
82 return server_id
83 else:
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +090084 _, server = self.create_test_server(wait_until="ACTIVE")
Mate Lakat99ee9142012-09-14 12:34:46 +010085 server_id = server['id']
86 self.password = server['adminPass']
87 self.password = 'password'
88 self.created_server_ids.append(server_id)
89 return server_id
90
Bob Ballc078be92013-04-09 14:25:00 +010091 def _volume_clean_up(self, server_id, volume_id):
92 resp, body = self.volumes_client.get_volume(volume_id)
93 if body['status'] == 'in-use':
94 self.servers_client.detach_volume(server_id, volume_id)
95 self.volumes_client.wait_for_volume_status(volume_id, 'available')
96 self.volumes_client.delete_volume(volume_id)
97
Matthew Treinishd5c96022013-10-17 21:51:23 +000098 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration,
Bob Ball0dca8972013-04-09 16:23:31 +010099 'Live migration not available')
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400100 @attr(type='gate')
Attila Fazekas8e99b992013-02-24 09:53:23 +0100101 def test_live_block_migration(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500102 # Live block migrate an instance to another host
Mate Lakat99ee9142012-09-14 12:34:46 +0100103 if len(self._get_compute_hostnames()) < 2:
ivan-zhu1feeb382013-01-24 10:14:39 +0800104 raise self.skipTest(
Mate Lakat99ee9142012-09-14 12:34:46 +0100105 "Less than 2 compute nodes, skipping migration test.")
Mate Lakat99ee9142012-09-14 12:34:46 +0100106 server_id = self._get_an_active_server()
Mate Lakat99ee9142012-09-14 12:34:46 +0100107 actual_host = self._get_host_for_server(server_id)
Mate Lakat99ee9142012-09-14 12:34:46 +0100108 target_host = self._get_host_other_than(actual_host)
Mate Lakat99ee9142012-09-14 12:34:46 +0100109 self._migrate_server_to(server_id, target_host)
Mate Lakat99ee9142012-09-14 12:34:46 +0100110 self.servers_client.wait_for_server_status(server_id, 'ACTIVE')
Chang Bo Guofc77e932013-09-16 17:38:26 -0700111 self.assertEqual(target_host, self._get_host_for_server(server_id))
Mate Lakat99ee9142012-09-14 12:34:46 +0100112
Matthew Treinishd5c96022013-10-17 21:51:23 +0000113 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration,
Bob Ball0dca8972013-04-09 16:23:31 +0100114 'Live migration not available')
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400115 @attr(type='gate')
Attila Fazekas8e99b992013-02-24 09:53:23 +0100116 def test_invalid_host_for_migration(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500117 # Migrating to an invalid host should not change the status
Mate Lakat99ee9142012-09-14 12:34:46 +0100118 server_id = self._get_an_active_server()
Mate Lakat99ee9142012-09-14 12:34:46 +0100119 target_host = self._get_non_existing_host_name()
120
Attila Fazekasfa756cb2013-02-12 10:52:42 +0100121 self.assertRaises(exceptions.BadRequest, self._migrate_server_to,
122 server_id, target_host)
Chang Bo Guofc77e932013-09-16 17:38:26 -0700123 self.assertEqual('ACTIVE', self._get_server_status(server_id))
Mate Lakat99ee9142012-09-14 12:34:46 +0100124
Matthew Treinishd5c96022013-10-17 21:51:23 +0000125 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration or not
126 CONF.compute_feature_enabled.
127 block_migration_for_live_migration,
Bob Ballc078be92013-04-09 14:25:00 +0100128 'Block Live migration not available')
Matthew Treinishd5c96022013-10-17 21:51:23 +0000129 @testtools.skipIf(not CONF.compute_feature_enabled.
130 block_migrate_cinder_iscsi,
Bob Ballc078be92013-04-09 14:25:00 +0100131 'Block Live migration not configured for iSCSI')
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400132 @attr(type='gate')
Bob Ballc078be92013-04-09 14:25:00 +0100133 def test_iscsi_volume(self):
134 # Live block migrate an instance to another host
135 if len(self._get_compute_hostnames()) < 2:
136 raise self.skipTest(
137 "Less than 2 compute nodes, skipping migration test.")
138 server_id = self._get_an_active_server()
139 actual_host = self._get_host_for_server(server_id)
140 target_host = self._get_host_other_than(actual_host)
141
142 resp, volume = self.volumes_client.create_volume(1,
143 display_name='test')
144
145 self.volumes_client.wait_for_volume_status(volume['id'],
146 'available')
147 self.addCleanup(self._volume_clean_up, server_id, volume['id'])
148
149 # Attach the volume to the server
150 self.servers_client.attach_volume(server_id, volume['id'],
151 device='/dev/xvdb')
152 self.volumes_client.wait_for_volume_status(volume['id'], 'in-use')
153
154 self._migrate_server_to(server_id, target_host)
155 self.servers_client.wait_for_server_status(server_id, 'ACTIVE')
Chang Bo Guofc77e932013-09-16 17:38:26 -0700156 self.assertEqual(target_host, self._get_host_for_server(server_id))
Bob Ballc078be92013-04-09 14:25:00 +0100157
Mate Lakat99ee9142012-09-14 12:34:46 +0100158 @classmethod
159 def tearDownClass(cls):
160 for server_id in cls.created_server_ids:
161 cls.servers_client.delete_server(server_id)
162
ravikumar-venkatesan753262b2013-03-21 12:43:57 +0000163 super(LiveBlockMigrationTestJSON, cls).tearDownClass()
164
165
166class LiveBlockMigrationTestXML(LiveBlockMigrationTestJSON):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +0000167 _host_key = (
168 '{http://docs.openstack.org/compute/ext/extended_status/api/v1.1}host')
ravikumar-venkatesan753262b2013-03-21 12:43:57 +0000169 _interface = 'xml'