blob: 235b0583f6586a062907b04eac981b1757b31d82 [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 +010016
ivan-zhu1feeb382013-01-24 10:14:39 +080017import testtools
Matthew Treinisha83a16e2012-12-07 13:44:02 -050018
Sean Dague1937d092013-05-17 16:36:38 -040019from tempest.api.compute import base
Mate Lakat99ee9142012-09-14 12:34:46 +010020from tempest import config
Yuiko Takadae9999d62014-03-06 09:22:54 +000021from tempest import test
Mate Lakat99ee9142012-09-14 12:34:46 +010022
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000023CONF = config.CONF
24
Mate Lakat99ee9142012-09-14 12:34:46 +010025
ivan-zhuf2b00502013-10-18 10:06:52 +080026class LiveBlockMigrationTestJSON(base.BaseV2ComputeAdminTest):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +000027 _host_key = 'OS-EXT-SRV-ATTR:host'
Mate Lakat99ee9142012-09-14 12:34:46 +010028
Mate Lakat99ee9142012-09-14 12:34:46 +010029 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010030 def resource_setup(cls):
31 super(LiveBlockMigrationTestJSON, cls).resource_setup()
Mate Lakat99ee9142012-09-14 12:34:46 +010032
Attila Fazekas8e99b992013-02-24 09:53:23 +010033 cls.admin_hosts_client = cls.os_adm.hosts_client
34 cls.admin_servers_client = cls.os_adm.servers_client
Mate Lakat99ee9142012-09-14 12:34:46 +010035
36 cls.created_server_ids = []
37
38 def _get_compute_hostnames(self):
David Kranz0a735172015-01-16 10:51:18 -050039 body = self.admin_hosts_client.list_hosts()
Mate Lakat99ee9142012-09-14 12:34:46 +010040 return [
41 host_record['host_name']
42 for host_record in body
43 if host_record['service'] == 'compute'
44 ]
45
46 def _get_server_details(self, server_id):
David Kranz0fb14292015-02-11 15:55:20 -050047 body = self.admin_servers_client.get_server(server_id)
Mate Lakat99ee9142012-09-14 12:34:46 +010048 return body
49
50 def _get_host_for_server(self, server_id):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +000051 return self._get_server_details(server_id)[self._host_key]
Mate Lakat99ee9142012-09-14 12:34:46 +010052
53 def _migrate_server_to(self, server_id, dest_host):
David Kranzae99b9a2015-02-16 13:37:01 -050054 body = self.admin_servers_client.live_migrate_server(
Bob Ball0dca8972013-04-09 16:23:31 +010055 server_id, dest_host,
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000056 CONF.compute_feature_enabled.block_migration_for_live_migration)
Mate Lakat99ee9142012-09-14 12:34:46 +010057 return body
58
59 def _get_host_other_than(self, host):
60 for target_host in self._get_compute_hostnames():
61 if host != target_host:
62 return target_host
63
Mate Lakat99ee9142012-09-14 12:34:46 +010064 def _get_server_status(self, server_id):
65 return self._get_server_details(server_id)['status']
66
67 def _get_an_active_server(self):
68 for server_id in self.created_server_ids:
69 if 'ACTIVE' == self._get_server_status(server_id):
70 return server_id
71 else:
David Kranz0fb14292015-02-11 15:55:20 -050072 server = self.create_test_server(wait_until="ACTIVE")
Mate Lakat99ee9142012-09-14 12:34:46 +010073 server_id = server['id']
Mate Lakat99ee9142012-09-14 12:34:46 +010074 self.created_server_ids.append(server_id)
75 return server_id
76
Bob Ballc078be92013-04-09 14:25:00 +010077 def _volume_clean_up(self, server_id, volume_id):
Joseph Lanoux5d7b6c12015-02-09 13:57:24 +000078 body = self.volumes_client.get_volume(volume_id)
Bob Ballc078be92013-04-09 14:25:00 +010079 if body['status'] == 'in-use':
80 self.servers_client.detach_volume(server_id, volume_id)
81 self.volumes_client.wait_for_volume_status(volume_id, 'available')
82 self.volumes_client.delete_volume(volume_id)
83
Matthew Treinishd5c96022013-10-17 21:51:23 +000084 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration,
Bob Ball0dca8972013-04-09 16:23:31 +010085 'Live migration not available')
Yuiko Takadae9999d62014-03-06 09:22:54 +000086 @test.attr(type='gate')
Attila Fazekas8e99b992013-02-24 09:53:23 +010087 def test_live_block_migration(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050088 # Live block migrate an instance to another host
Mate Lakat99ee9142012-09-14 12:34:46 +010089 if len(self._get_compute_hostnames()) < 2:
ivan-zhu1feeb382013-01-24 10:14:39 +080090 raise self.skipTest(
Mate Lakat99ee9142012-09-14 12:34:46 +010091 "Less than 2 compute nodes, skipping migration test.")
Mate Lakat99ee9142012-09-14 12:34:46 +010092 server_id = self._get_an_active_server()
Mate Lakat99ee9142012-09-14 12:34:46 +010093 actual_host = self._get_host_for_server(server_id)
Mate Lakat99ee9142012-09-14 12:34:46 +010094 target_host = self._get_host_other_than(actual_host)
Mate Lakat99ee9142012-09-14 12:34:46 +010095 self._migrate_server_to(server_id, target_host)
Mate Lakat99ee9142012-09-14 12:34:46 +010096 self.servers_client.wait_for_server_status(server_id, 'ACTIVE')
Chang Bo Guofc77e932013-09-16 17:38:26 -070097 self.assertEqual(target_host, self._get_host_for_server(server_id))
Mate Lakat99ee9142012-09-14 12:34:46 +010098
Matthew Treinishd5c96022013-10-17 21:51:23 +000099 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration or not
100 CONF.compute_feature_enabled.
101 block_migration_for_live_migration,
Bob Ballc078be92013-04-09 14:25:00 +0100102 'Block Live migration not available')
Matthew Treinishd5c96022013-10-17 21:51:23 +0000103 @testtools.skipIf(not CONF.compute_feature_enabled.
104 block_migrate_cinder_iscsi,
Bob Ballc078be92013-04-09 14:25:00 +0100105 'Block Live migration not configured for iSCSI')
Yuiko Takadae9999d62014-03-06 09:22:54 +0000106 @test.attr(type='gate')
Bob Ballc078be92013-04-09 14:25:00 +0100107 def test_iscsi_volume(self):
108 # Live block migrate an instance to another host
109 if len(self._get_compute_hostnames()) < 2:
110 raise self.skipTest(
111 "Less than 2 compute nodes, skipping migration test.")
112 server_id = self._get_an_active_server()
113 actual_host = self._get_host_for_server(server_id)
114 target_host = self._get_host_other_than(actual_host)
115
Joseph Lanouxb8bc4342015-02-03 09:38:26 +0000116 volume = self.volumes_client.create_volume(1, display_name='test')
Bob Ballc078be92013-04-09 14:25:00 +0100117
118 self.volumes_client.wait_for_volume_status(volume['id'],
119 'available')
120 self.addCleanup(self._volume_clean_up, server_id, volume['id'])
121
122 # Attach the volume to the server
123 self.servers_client.attach_volume(server_id, volume['id'],
124 device='/dev/xvdb')
125 self.volumes_client.wait_for_volume_status(volume['id'], 'in-use')
126
127 self._migrate_server_to(server_id, target_host)
128 self.servers_client.wait_for_server_status(server_id, 'ACTIVE')
Chang Bo Guofc77e932013-09-16 17:38:26 -0700129 self.assertEqual(target_host, self._get_host_for_server(server_id))