blob: 85d314bfc14f5a3c4785bf91f956ce5311adaf90 [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
26
ivan-zhuf2b00502013-10-18 10:06:52 +080027class LiveBlockMigrationTestJSON(base.BaseV2ComputeAdminTest):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +000028 _host_key = 'OS-EXT-SRV-ATTR:host'
Attila Fazekas19044d52013-02-16 07:35:06 +010029 _interface = 'json'
Mate Lakat99ee9142012-09-14 12:34:46 +010030
Sean Dague86bd8422013-12-20 09:56:44 -050031 CONF = config.CONF
Mate Lakat99ee9142012-09-14 12:34:46 +010032
33 @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 Treinishd5c96022013-10-17 21:51:23 +000060 self.config.compute_feature_enabled.
61 block_migration_for_live_migration)
Mate Lakat99ee9142012-09-14 12:34:46 +010062 return body
63
64 def _get_host_other_than(self, host):
65 for target_host in self._get_compute_hostnames():
66 if host != target_host:
67 return target_host
68
69 def _get_non_existing_host_name(self):
70 random_name = ''.join(
71 random.choice(string.ascii_uppercase) for x in range(20))
72
Jaroslav Henner3c3d1792012-11-16 10:28:47 +010073 self.assertNotIn(random_name, self._get_compute_hostnames())
Mate Lakat99ee9142012-09-14 12:34:46 +010074
75 return random_name
76
77 def _get_server_status(self, server_id):
78 return self._get_server_details(server_id)['status']
79
80 def _get_an_active_server(self):
81 for server_id in self.created_server_ids:
82 if 'ACTIVE' == self._get_server_status(server_id):
83 return server_id
84 else:
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +090085 _, server = self.create_test_server(wait_until="ACTIVE")
Mate Lakat99ee9142012-09-14 12:34:46 +010086 server_id = server['id']
87 self.password = server['adminPass']
88 self.password = 'password'
89 self.created_server_ids.append(server_id)
90 return server_id
91
Bob Ballc078be92013-04-09 14:25:00 +010092 def _volume_clean_up(self, server_id, volume_id):
93 resp, body = self.volumes_client.get_volume(volume_id)
94 if body['status'] == 'in-use':
95 self.servers_client.detach_volume(server_id, volume_id)
96 self.volumes_client.wait_for_volume_status(volume_id, 'available')
97 self.volumes_client.delete_volume(volume_id)
98
Matthew Treinishd5c96022013-10-17 21:51:23 +000099 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration,
Bob Ball0dca8972013-04-09 16:23:31 +0100100 'Live migration not available')
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400101 @attr(type='gate')
Attila Fazekas8e99b992013-02-24 09:53:23 +0100102 def test_live_block_migration(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500103 # Live block migrate an instance to another host
Mate Lakat99ee9142012-09-14 12:34:46 +0100104 if len(self._get_compute_hostnames()) < 2:
ivan-zhu1feeb382013-01-24 10:14:39 +0800105 raise self.skipTest(
Mate Lakat99ee9142012-09-14 12:34:46 +0100106 "Less than 2 compute nodes, skipping migration test.")
Mate Lakat99ee9142012-09-14 12:34:46 +0100107 server_id = self._get_an_active_server()
Mate Lakat99ee9142012-09-14 12:34:46 +0100108 actual_host = self._get_host_for_server(server_id)
Mate Lakat99ee9142012-09-14 12:34:46 +0100109 target_host = self._get_host_other_than(actual_host)
Mate Lakat99ee9142012-09-14 12:34:46 +0100110 self._migrate_server_to(server_id, target_host)
Mate Lakat99ee9142012-09-14 12:34:46 +0100111 self.servers_client.wait_for_server_status(server_id, 'ACTIVE')
Chang Bo Guofc77e932013-09-16 17:38:26 -0700112 self.assertEqual(target_host, self._get_host_for_server(server_id))
Mate Lakat99ee9142012-09-14 12:34:46 +0100113
Matthew Treinishd5c96022013-10-17 21:51:23 +0000114 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration,
Bob Ball0dca8972013-04-09 16:23:31 +0100115 'Live migration not available')
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400116 @attr(type='gate')
Attila Fazekas8e99b992013-02-24 09:53:23 +0100117 def test_invalid_host_for_migration(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500118 # Migrating to an invalid host should not change the status
Mate Lakat99ee9142012-09-14 12:34:46 +0100119 server_id = self._get_an_active_server()
Mate Lakat99ee9142012-09-14 12:34:46 +0100120 target_host = self._get_non_existing_host_name()
121
Attila Fazekasfa756cb2013-02-12 10:52:42 +0100122 self.assertRaises(exceptions.BadRequest, self._migrate_server_to,
123 server_id, target_host)
Chang Bo Guofc77e932013-09-16 17:38:26 -0700124 self.assertEqual('ACTIVE', self._get_server_status(server_id))
Mate Lakat99ee9142012-09-14 12:34:46 +0100125
Matthew Treinishd5c96022013-10-17 21:51:23 +0000126 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration or not
127 CONF.compute_feature_enabled.
128 block_migration_for_live_migration,
Bob Ballc078be92013-04-09 14:25:00 +0100129 'Block Live migration not available')
Matthew Treinishd5c96022013-10-17 21:51:23 +0000130 @testtools.skipIf(not CONF.compute_feature_enabled.
131 block_migrate_cinder_iscsi,
Bob Ballc078be92013-04-09 14:25:00 +0100132 'Block Live migration not configured for iSCSI')
Giampaolo Lauriae9c77022013-05-22 01:23:58 -0400133 @attr(type='gate')
Bob Ballc078be92013-04-09 14:25:00 +0100134 def test_iscsi_volume(self):
135 # Live block migrate an instance to another host
136 if len(self._get_compute_hostnames()) < 2:
137 raise self.skipTest(
138 "Less than 2 compute nodes, skipping migration test.")
139 server_id = self._get_an_active_server()
140 actual_host = self._get_host_for_server(server_id)
141 target_host = self._get_host_other_than(actual_host)
142
143 resp, volume = self.volumes_client.create_volume(1,
144 display_name='test')
145
146 self.volumes_client.wait_for_volume_status(volume['id'],
147 'available')
148 self.addCleanup(self._volume_clean_up, server_id, volume['id'])
149
150 # Attach the volume to the server
151 self.servers_client.attach_volume(server_id, volume['id'],
152 device='/dev/xvdb')
153 self.volumes_client.wait_for_volume_status(volume['id'], 'in-use')
154
155 self._migrate_server_to(server_id, target_host)
156 self.servers_client.wait_for_server_status(server_id, 'ACTIVE')
Chang Bo Guofc77e932013-09-16 17:38:26 -0700157 self.assertEqual(target_host, self._get_host_for_server(server_id))
Bob Ballc078be92013-04-09 14:25:00 +0100158
Mate Lakat99ee9142012-09-14 12:34:46 +0100159 @classmethod
160 def tearDownClass(cls):
161 for server_id in cls.created_server_ids:
162 cls.servers_client.delete_server(server_id)
163
ravikumar-venkatesan753262b2013-03-21 12:43:57 +0000164 super(LiveBlockMigrationTestJSON, cls).tearDownClass()
165
166
167class LiveBlockMigrationTestXML(LiveBlockMigrationTestJSON):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +0000168 _host_key = (
169 '{http://docs.openstack.org/compute/ext/extended_status/api/v1.1}host')
ravikumar-venkatesan753262b2013-03-21 12:43:57 +0000170 _interface = 'xml'