blob: c3f91eea22acd3bad1613cab8f8f71e48045ea5a [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
Rohan Kanade60b73092015-02-04 17:58:19 +053030 def setup_clients(cls):
31 super(LiveBlockMigrationTestJSON, cls).setup_clients()
Attila Fazekas8e99b992013-02-24 09:53:23 +010032 cls.admin_hosts_client = cls.os_adm.hosts_client
33 cls.admin_servers_client = cls.os_adm.servers_client
Mate Lakat99ee9142012-09-14 12:34:46 +010034
Rohan Kanade60b73092015-02-04 17:58:19 +053035 @classmethod
36 def resource_setup(cls):
37 super(LiveBlockMigrationTestJSON, cls).resource_setup()
38
Mate Lakat99ee9142012-09-14 12:34:46 +010039 cls.created_server_ids = []
40
41 def _get_compute_hostnames(self):
David Kranz0a735172015-01-16 10:51:18 -050042 body = self.admin_hosts_client.list_hosts()
Mate Lakat99ee9142012-09-14 12:34:46 +010043 return [
44 host_record['host_name']
45 for host_record in body
46 if host_record['service'] == 'compute'
47 ]
48
49 def _get_server_details(self, server_id):
David Kranz0fb14292015-02-11 15:55:20 -050050 body = self.admin_servers_client.get_server(server_id)
Mate Lakat99ee9142012-09-14 12:34:46 +010051 return body
52
53 def _get_host_for_server(self, server_id):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +000054 return self._get_server_details(server_id)[self._host_key]
Mate Lakat99ee9142012-09-14 12:34:46 +010055
56 def _migrate_server_to(self, server_id, dest_host):
David Kranzae99b9a2015-02-16 13:37:01 -050057 body = self.admin_servers_client.live_migrate_server(
Bob Ball0dca8972013-04-09 16:23:31 +010058 server_id, dest_host,
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000059 CONF.compute_feature_enabled.block_migration_for_live_migration)
Mate Lakat99ee9142012-09-14 12:34:46 +010060 return body
61
62 def _get_host_other_than(self, host):
63 for target_host in self._get_compute_hostnames():
64 if host != target_host:
65 return target_host
66
Mate Lakat99ee9142012-09-14 12:34:46 +010067 def _get_server_status(self, server_id):
68 return self._get_server_details(server_id)['status']
69
70 def _get_an_active_server(self):
71 for server_id in self.created_server_ids:
72 if 'ACTIVE' == self._get_server_status(server_id):
73 return server_id
74 else:
David Kranz0fb14292015-02-11 15:55:20 -050075 server = self.create_test_server(wait_until="ACTIVE")
Mate Lakat99ee9142012-09-14 12:34:46 +010076 server_id = server['id']
Mate Lakat99ee9142012-09-14 12:34:46 +010077 self.created_server_ids.append(server_id)
78 return server_id
79
Bob Ballc078be92013-04-09 14:25:00 +010080 def _volume_clean_up(self, server_id, volume_id):
Joseph Lanoux5d7b6c12015-02-09 13:57:24 +000081 body = self.volumes_client.get_volume(volume_id)
Bob Ballc078be92013-04-09 14:25:00 +010082 if body['status'] == 'in-use':
83 self.servers_client.detach_volume(server_id, volume_id)
84 self.volumes_client.wait_for_volume_status(volume_id, 'available')
85 self.volumes_client.delete_volume(volume_id)
86
Chris Hoge7579c1a2015-02-26 14:12:15 -080087 @test.idempotent_id('1dce86b8-eb04-4c03-a9d8-9c1dc3ee0c7b')
Matthew Treinishd5c96022013-10-17 21:51:23 +000088 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration,
Bob Ball0dca8972013-04-09 16:23:31 +010089 'Live migration not available')
Yuiko Takadae9999d62014-03-06 09:22:54 +000090 @test.attr(type='gate')
Attila Fazekas8e99b992013-02-24 09:53:23 +010091 def test_live_block_migration(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050092 # Live block migrate an instance to another host
Mate Lakat99ee9142012-09-14 12:34:46 +010093 if len(self._get_compute_hostnames()) < 2:
ivan-zhu1feeb382013-01-24 10:14:39 +080094 raise self.skipTest(
Mate Lakat99ee9142012-09-14 12:34:46 +010095 "Less than 2 compute nodes, skipping migration test.")
Mate Lakat99ee9142012-09-14 12:34:46 +010096 server_id = self._get_an_active_server()
Mate Lakat99ee9142012-09-14 12:34:46 +010097 actual_host = self._get_host_for_server(server_id)
Mate Lakat99ee9142012-09-14 12:34:46 +010098 target_host = self._get_host_other_than(actual_host)
Mate Lakat99ee9142012-09-14 12:34:46 +010099 self._migrate_server_to(server_id, target_host)
Mate Lakat99ee9142012-09-14 12:34:46 +0100100 self.servers_client.wait_for_server_status(server_id, 'ACTIVE')
Chang Bo Guofc77e932013-09-16 17:38:26 -0700101 self.assertEqual(target_host, self._get_host_for_server(server_id))
Mate Lakat99ee9142012-09-14 12:34:46 +0100102
Chris Hoge7579c1a2015-02-26 14:12:15 -0800103 @test.idempotent_id('e19c0cc6-6720-4ed8-be83-b6603ed5c812')
Matthew Treinishd5c96022013-10-17 21:51:23 +0000104 @testtools.skipIf(not CONF.compute_feature_enabled.live_migration or not
105 CONF.compute_feature_enabled.
106 block_migration_for_live_migration,
Bob Ballc078be92013-04-09 14:25:00 +0100107 'Block Live migration not available')
Matthew Treinishd5c96022013-10-17 21:51:23 +0000108 @testtools.skipIf(not CONF.compute_feature_enabled.
109 block_migrate_cinder_iscsi,
Bob Ballc078be92013-04-09 14:25:00 +0100110 'Block Live migration not configured for iSCSI')
Yuiko Takadae9999d62014-03-06 09:22:54 +0000111 @test.attr(type='gate')
Bob Ballc078be92013-04-09 14:25:00 +0100112 def test_iscsi_volume(self):
113 # Live block migrate an instance to another host
114 if len(self._get_compute_hostnames()) < 2:
115 raise self.skipTest(
116 "Less than 2 compute nodes, skipping migration test.")
117 server_id = self._get_an_active_server()
118 actual_host = self._get_host_for_server(server_id)
119 target_host = self._get_host_other_than(actual_host)
120
Markus Zoeller3d2a21c2015-02-27 12:04:22 +0100121 volume = self.volumes_client.create_volume(display_name='test')
Bob Ballc078be92013-04-09 14:25:00 +0100122
123 self.volumes_client.wait_for_volume_status(volume['id'],
124 'available')
125 self.addCleanup(self._volume_clean_up, server_id, volume['id'])
126
127 # Attach the volume to the server
128 self.servers_client.attach_volume(server_id, volume['id'],
129 device='/dev/xvdb')
130 self.volumes_client.wait_for_volume_status(volume['id'], 'in-use')
131
132 self._migrate_server_to(server_id, target_host)
133 self.servers_client.wait_for_server_status(server_id, 'ACTIVE')
Chang Bo Guofc77e932013-09-16 17:38:26 -0700134 self.assertEqual(target_host, self._get_host_for_server(server_id))