blob: ab4ddf7c421411b0f45cb520e4279737197af5e1 [file] [log] [blame]
Matt Riedemannbc8dbd32013-08-02 14:02:12 -07001# Copyright 2013 IBM Corp.
Dan Smithc18d8c62012-07-02 08:09:26 -07002# 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
ivan-zhu1feeb382013-01-24 10:14:39 +080016import testtools
Dan Smithc18d8c62012-07-02 08:09:26 -070017
Sean Dague1937d092013-05-17 16:36:38 -040018from tempest.api.compute import base
Masayuki Igawa209fd502014-02-17 14:46:43 +090019from tempest.common.utils.linux import remote_client
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +000020from tempest.common import waiters
Sean Dague86bd8422013-12-20 09:56:44 -050021from tempest import config
Masayuki Igawa209fd502014-02-17 14:46:43 +090022from tempest import test
Dan Smithc18d8c62012-07-02 08:09:26 -070023
Sean Dague86bd8422013-12-20 09:56:44 -050024CONF = config.CONF
25
Dan Smithc18d8c62012-07-02 08:09:26 -070026
ivan-zhuf2b00502013-10-18 10:06:52 +080027class AttachVolumeTestJSON(base.BaseV2ComputeTest):
Dan Smithc18d8c62012-07-02 08:09:26 -070028
ivan-zhu2f54b282013-03-11 16:39:25 +080029 def __init__(self, *args, **kwargs):
30 super(AttachVolumeTestJSON, self).__init__(*args, **kwargs)
Ghanshyam5c2a5582014-04-14 17:16:57 +090031 self.attachment = None
ivan-zhu2f54b282013-03-11 16:39:25 +080032
Attila Fazekas19044d52013-02-16 07:35:06 +010033 @classmethod
Emily Hugenbruch8284a342014-12-11 22:04:55 +000034 def skip_checks(cls):
35 super(AttachVolumeTestJSON, cls).skip_checks()
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000036 if not CONF.service_available.cinder:
Matthew Treinish4c412922013-07-16 15:27:42 -040037 skip_msg = ("%s skipped as Cinder is not available" % cls.__name__)
38 raise cls.skipException(skip_msg)
Dan Smithc18d8c62012-07-02 08:09:26 -070039
Emily Hugenbruch8284a342014-12-11 22:04:55 +000040 @classmethod
41 def setup_credentials(cls):
42 cls.prepare_instance_network()
43 super(AttachVolumeTestJSON, cls).setup_credentials()
44
45 @classmethod
46 def resource_setup(cls):
Joseph Lanouxffe09dd2015-03-18 16:45:33 +000047 cls.set_validation_resources()
48
Emily Hugenbruch8284a342014-12-11 22:04:55 +000049 super(AttachVolumeTestJSON, cls).resource_setup()
50 cls.device = CONF.compute.volume_device_name
51
Dan Smithc18d8c62012-07-02 08:09:26 -070052 def _detach(self, server_id, volume_id):
Ghanshyam5c2a5582014-04-14 17:16:57 +090053 if self.attachment:
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070054 self.servers_client.detach_volume(server_id, volume_id)
55 self.volumes_client.wait_for_volume_status(volume_id, 'available')
Dan Smithc18d8c62012-07-02 08:09:26 -070056
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070057 def _delete_volume(self):
Ghanshyam5c2a5582014-04-14 17:16:57 +090058 # Delete the created Volumes
ivan-zhu2f54b282013-03-11 16:39:25 +080059 if self.volume:
60 self.volumes_client.delete_volume(self.volume['id'])
Ghanshyam5c2a5582014-04-14 17:16:57 +090061 self.volumes_client.wait_for_resource_deletion(self.volume['id'])
ivan-zhu2f54b282013-03-11 16:39:25 +080062 self.volume = None
Dan Smithc18d8c62012-07-02 08:09:26 -070063
64 def _create_and_attach(self):
Dan Smithc18d8c62012-07-02 08:09:26 -070065 # Start a server and wait for it to become ready
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +090066 admin_pass = self.image_ssh_password
Joseph Lanouxffe09dd2015-03-18 16:45:33 +000067 self.server = self.create_test_server(
68 validatable=True,
69 wait_until='ACTIVE',
70 adminPass=admin_pass)
Dan Smithc18d8c62012-07-02 08:09:26 -070071
72 # Record addresses so that we can ssh later
ghanshyam0f825252015-08-25 16:02:50 +090073 self.server['addresses'] = self.servers_client.list_addresses(
74 self.server['id'])['addresses']
Dan Smithc18d8c62012-07-02 08:09:26 -070075
76 # Create a volume and wait for it to become ready
Joseph Lanoux6809bab2014-12-18 14:57:18 +000077 self.volume = self.volumes_client.create_volume(
John Warren6177c9e2015-08-19 20:00:17 +000078 CONF.volume.volume_size, display_name='test')['volume']
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070079 self.addCleanup(self._delete_volume)
Ghanshyam4adb7cd2014-06-03 12:32:46 +090080 self.volumes_client.wait_for_volume_status(self.volume['id'],
81 'available')
Dan Smithc18d8c62012-07-02 08:09:26 -070082
83 # Attach the volume to the server
David Kranz3ebc7212015-02-10 12:19:19 -050084 self.attachment = self.servers_client.attach_volume(
Ghanshyam5c2a5582014-04-14 17:16:57 +090085 self.server['id'],
Ken'ichi Ohmichidfc88de2015-08-13 05:12:20 +000086 volumeId=self.volume['id'],
ghanshyam0f825252015-08-25 16:02:50 +090087 device='/dev/%s' % self.device)['volumeAttachment']
Ghanshyam4adb7cd2014-06-03 12:32:46 +090088 self.volumes_client.wait_for_volume_status(self.volume['id'], 'in-use')
Dan Smithc18d8c62012-07-02 08:09:26 -070089
Ghanshyam4adb7cd2014-06-03 12:32:46 +090090 self.addCleanup(self._detach, self.server['id'], self.volume['id'])
Dan Smithc18d8c62012-07-02 08:09:26 -070091
Chris Hoge7579c1a2015-02-26 14:12:15 -080092 @test.idempotent_id('52e9045a-e90d-4c0d-9087-79d657faffff')
Matthew Treinishe5cca002015-05-11 15:36:50 -040093 @testtools.skipUnless(CONF.validation.run_validation,
94 'SSH required for this test')
Dan Smithc18d8c62012-07-02 08:09:26 -070095 def test_attach_detach_volume(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050096 # Stop and Start a server with an attached volume, ensuring that
97 # the volume remains attached.
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070098 self._create_and_attach()
ivan-zhu2f54b282013-03-11 16:39:25 +080099
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000100 self.servers_client.stop_server(self.server['id'])
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000101 waiters.wait_for_server_status(self.servers_client, self.server['id'],
102 'SHUTOFF')
Dan Smithc18d8c62012-07-02 08:09:26 -0700103
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000104 self.servers_client.start_server(self.server['id'])
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000105 waiters.wait_for_server_status(self.servers_client, self.server['id'],
106 'ACTIVE')
Dan Smithc18d8c62012-07-02 08:09:26 -0700107
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000108 linux_client = remote_client.RemoteClient(
109 self.get_server_ip(self.server),
110 self.image_ssh_user,
111 self.server['adminPass'],
112 self.validation_resources['keypair']['private_key'])
113
Matt Riedemannbc8dbd32013-08-02 14:02:12 -0700114 partitions = linux_client.get_partitions()
115 self.assertIn(self.device, partitions)
Dan Smithc18d8c62012-07-02 08:09:26 -0700116
Ghanshyam4adb7cd2014-06-03 12:32:46 +0900117 self._detach(self.server['id'], self.volume['id'])
Ghanshyam5c2a5582014-04-14 17:16:57 +0900118 self.attachment = None
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000119 self.servers_client.stop_server(self.server['id'])
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000120 waiters.wait_for_server_status(self.servers_client, self.server['id'],
121 'SHUTOFF')
Dan Smithc18d8c62012-07-02 08:09:26 -0700122
Ken'ichi Ohmichib2631082015-08-27 01:31:00 +0000123 self.servers_client.start_server(self.server['id'])
Ken'ichi Ohmichi0eb153c2015-07-13 02:18:25 +0000124 waiters.wait_for_server_status(self.servers_client, self.server['id'],
125 'ACTIVE')
Dan Smithc18d8c62012-07-02 08:09:26 -0700126
Joseph Lanouxffe09dd2015-03-18 16:45:33 +0000127 linux_client = remote_client.RemoteClient(
128 self.get_server_ip(self.server),
129 self.image_ssh_user,
130 self.server['adminPass'],
131 self.validation_resources['keypair']['private_key'])
132
Matt Riedemannbc8dbd32013-08-02 14:02:12 -0700133 partitions = linux_client.get_partitions()
134 self.assertNotIn(self.device, partitions)
Dan Smith1ced8422012-08-16 10:35:19 -0700135
Chris Hoge7579c1a2015-02-26 14:12:15 -0800136 @test.idempotent_id('7fa563fe-f0f7-43eb-9e22-a1ece036b513')
Ghanshyam5c2a5582014-04-14 17:16:57 +0900137 def test_list_get_volume_attachments(self):
138 # Create Server, Volume and attach that Volume to Server
139 self._create_and_attach()
140 # List Volume attachment of the server
David Kranz3ebc7212015-02-10 12:19:19 -0500141 body = self.servers_client.list_volume_attachments(
ghanshyam0f825252015-08-25 16:02:50 +0900142 self.server['id'])['volumeAttachments']
Ghanshyam5c2a5582014-04-14 17:16:57 +0900143 self.assertEqual(1, len(body))
144 self.assertIn(self.attachment, body)
145
146 # Get Volume attachment of the server
David Kranz3ebc7212015-02-10 12:19:19 -0500147 body = self.servers_client.get_volume_attachment(
Ghanshyam5c2a5582014-04-14 17:16:57 +0900148 self.server['id'],
ghanshyam0f825252015-08-25 16:02:50 +0900149 self.attachment['id'])['volumeAttachment']
Ghanshyam5c2a5582014-04-14 17:16:57 +0900150 self.assertEqual(self.server['id'], body['serverId'])
151 self.assertEqual(self.volume['id'], body['volumeId'])
152 self.assertEqual(self.attachment['id'], body['id'])