blob: e438098a8e681cf5f4c381f41f8e2cba3bd2d0d0 [file] [log] [blame]
Mate Lakat99ee9142012-09-14 12:34:46 +01001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Mate Lakat99ee9142012-09-14 12:34:46 +010018import random
19import string
20
ivan-zhu1feeb382013-01-24 10:14:39 +080021import testtools
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022
Mate Lakat99ee9142012-09-14 12:34:46 +010023from tempest import config
24from tempest import exceptions
Chris Yeoh9465b0b2013-02-09 22:19:15 +103025from tempest.test import attr
Matthew Treinisha83a16e2012-12-07 13:44:02 -050026from tempest.tests.compute import base
Mate Lakat99ee9142012-09-14 12:34:46 +010027
28
29@attr(category='live-migration')
ravikumar-venkatesan753262b2013-03-21 12:43:57 +000030class LiveBlockMigrationTestJSON(base.BaseComputeAdminTest):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +000031 _host_key = 'OS-EXT-SRV-ATTR:host'
Attila Fazekas19044d52013-02-16 07:35:06 +010032 _interface = 'json'
Mate Lakat99ee9142012-09-14 12:34:46 +010033
34 live_migration_available = (
35 config.TempestConfig().compute.live_migration_available)
36 use_block_migration_for_live_migration = (
37 config.TempestConfig().compute.use_block_migration_for_live_migration)
38 run_ssh = config.TempestConfig().compute.run_ssh
39
40 @classmethod
41 def setUpClass(cls):
ravikumar-venkatesan753262b2013-03-21 12:43:57 +000042 super(LiveBlockMigrationTestJSON, cls).setUpClass()
Mate Lakat99ee9142012-09-14 12:34:46 +010043
Attila Fazekas8e99b992013-02-24 09:53:23 +010044 cls.admin_hosts_client = cls.os_adm.hosts_client
45 cls.admin_servers_client = cls.os_adm.servers_client
Mate Lakat99ee9142012-09-14 12:34:46 +010046
47 cls.created_server_ids = []
48
49 def _get_compute_hostnames(self):
50 _resp, body = self.admin_hosts_client.list_hosts()
51 return [
52 host_record['host_name']
53 for host_record in body
54 if host_record['service'] == 'compute'
55 ]
56
57 def _get_server_details(self, server_id):
58 _resp, body = self.admin_servers_client.get_server(server_id)
59 return body
60
61 def _get_host_for_server(self, server_id):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +000062 return self._get_server_details(server_id)[self._host_key]
Mate Lakat99ee9142012-09-14 12:34:46 +010063
64 def _migrate_server_to(self, server_id, dest_host):
65 _resp, body = self.admin_servers_client.live_migrate_server(
66 server_id, dest_host, self.use_block_migration_for_live_migration)
67 return body
68
69 def _get_host_other_than(self, host):
70 for target_host in self._get_compute_hostnames():
71 if host != target_host:
72 return target_host
73
74 def _get_non_existing_host_name(self):
75 random_name = ''.join(
76 random.choice(string.ascii_uppercase) for x in range(20))
77
Jaroslav Henner3c3d1792012-11-16 10:28:47 +010078 self.assertNotIn(random_name, self._get_compute_hostnames())
Mate Lakat99ee9142012-09-14 12:34:46 +010079
80 return random_name
81
82 def _get_server_status(self, server_id):
83 return self._get_server_details(server_id)['status']
84
85 def _get_an_active_server(self):
86 for server_id in self.created_server_ids:
87 if 'ACTIVE' == self._get_server_status(server_id):
88 return server_id
89 else:
Bob Balle22ecbb2013-03-01 10:52:30 +000090 _, server = self.create_server(wait_until="ACTIVE")
Mate Lakat99ee9142012-09-14 12:34:46 +010091 server_id = server['id']
92 self.password = server['adminPass']
93 self.password = 'password'
94 self.created_server_ids.append(server_id)
95 return server_id
96
97 @attr(type='positive')
ivan-zhu1feeb382013-01-24 10:14:39 +080098 @testtools.skipIf(not live_migration_available,
99 'Block Live migration not available')
Attila Fazekas8e99b992013-02-24 09:53:23 +0100100 def test_live_block_migration(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500101 # Live block migrate an instance to another host
Mate Lakat99ee9142012-09-14 12:34:46 +0100102 if len(self._get_compute_hostnames()) < 2:
ivan-zhu1feeb382013-01-24 10:14:39 +0800103 raise self.skipTest(
Mate Lakat99ee9142012-09-14 12:34:46 +0100104 "Less than 2 compute nodes, skipping migration test.")
Mate Lakat99ee9142012-09-14 12:34:46 +0100105 server_id = self._get_an_active_server()
Mate Lakat99ee9142012-09-14 12:34:46 +0100106 actual_host = self._get_host_for_server(server_id)
Mate Lakat99ee9142012-09-14 12:34:46 +0100107 target_host = self._get_host_other_than(actual_host)
Mate Lakat99ee9142012-09-14 12:34:46 +0100108 self._migrate_server_to(server_id, target_host)
Mate Lakat99ee9142012-09-14 12:34:46 +0100109 self.servers_client.wait_for_server_status(server_id, 'ACTIVE')
Jaroslav Henner3c3d1792012-11-16 10:28:47 +0100110 self.assertEquals(target_host, self._get_host_for_server(server_id))
Mate Lakat99ee9142012-09-14 12:34:46 +0100111
ivan-zhu1feeb382013-01-24 10:14:39 +0800112 @testtools.skipIf(not live_migration_available,
113 'Block Live migration not available')
Attila Fazekas8e99b992013-02-24 09:53:23 +0100114 def test_invalid_host_for_migration(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -0500115 # Migrating to an invalid host should not change the status
Mate Lakat99ee9142012-09-14 12:34:46 +0100116
117 server_id = self._get_an_active_server()
Mate Lakat99ee9142012-09-14 12:34:46 +0100118 target_host = self._get_non_existing_host_name()
119
Attila Fazekasfa756cb2013-02-12 10:52:42 +0100120 self.assertRaises(exceptions.BadRequest, self._migrate_server_to,
121 server_id, target_host)
Mate Lakat99ee9142012-09-14 12:34:46 +0100122 self.assertEquals('ACTIVE', self._get_server_status(server_id))
123
124 @classmethod
125 def tearDownClass(cls):
126 for server_id in cls.created_server_ids:
127 cls.servers_client.delete_server(server_id)
128
ravikumar-venkatesan753262b2013-03-21 12:43:57 +0000129 super(LiveBlockMigrationTestJSON, cls).tearDownClass()
130
131
132class LiveBlockMigrationTestXML(LiveBlockMigrationTestJSON):
Mate Lakat0ccf0eb2013-03-28 12:00:52 +0000133 _host_key = (
134 '{http://docs.openstack.org/compute/ext/extended_status/api/v1.1}host')
ravikumar-venkatesan753262b2013-03-21 12:43:57 +0000135 _interface = 'xml'