blob: 7fef52fdd77db4fd7d006b62b0cc9f397642e482 [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
Sean Dague86bd8422013-12-20 09:56:44 -050020from tempest import config
Masayuki Igawa209fd502014-02-17 14:46:43 +090021from tempest import test
Dan Smithc18d8c62012-07-02 08:09:26 -070022
Sean Dague86bd8422013-12-20 09:56:44 -050023CONF = config.CONF
24
Dan Smithc18d8c62012-07-02 08:09:26 -070025
ivan-zhuf2b00502013-10-18 10:06:52 +080026class AttachVolumeTestJSON(base.BaseV2ComputeTest):
Dan Smithc18d8c62012-07-02 08:09:26 -070027
ivan-zhu2f54b282013-03-11 16:39:25 +080028 def __init__(self, *args, **kwargs):
29 super(AttachVolumeTestJSON, self).__init__(*args, **kwargs)
Ghanshyam5c2a5582014-04-14 17:16:57 +090030 self.attachment = None
ivan-zhu2f54b282013-03-11 16:39:25 +080031
Attila Fazekas19044d52013-02-16 07:35:06 +010032 @classmethod
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010033 def resource_setup(cls):
Attila Fazekas423834d2014-03-14 17:33:13 +010034 cls.prepare_instance_network()
Andrea Frittoli50bb80d2014-09-15 12:34:27 +010035 super(AttachVolumeTestJSON, cls).resource_setup()
Matthew Treinishb0a78fc2014-01-29 16:49:12 +000036 cls.device = CONF.compute.volume_device_name
37 if not CONF.service_available.cinder:
Matthew Treinish4c412922013-07-16 15:27:42 -040038 skip_msg = ("%s skipped as Cinder is not available" % cls.__name__)
39 raise cls.skipException(skip_msg)
Dan Smithc18d8c62012-07-02 08:09:26 -070040
41 def _detach(self, server_id, volume_id):
Ghanshyam5c2a5582014-04-14 17:16:57 +090042 if self.attachment:
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070043 self.servers_client.detach_volume(server_id, volume_id)
44 self.volumes_client.wait_for_volume_status(volume_id, 'available')
Dan Smithc18d8c62012-07-02 08:09:26 -070045
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070046 def _delete_volume(self):
Ghanshyam5c2a5582014-04-14 17:16:57 +090047 # Delete the created Volumes
ivan-zhu2f54b282013-03-11 16:39:25 +080048 if self.volume:
49 self.volumes_client.delete_volume(self.volume['id'])
Ghanshyam5c2a5582014-04-14 17:16:57 +090050 self.volumes_client.wait_for_resource_deletion(self.volume['id'])
ivan-zhu2f54b282013-03-11 16:39:25 +080051 self.volume = None
Dan Smithc18d8c62012-07-02 08:09:26 -070052
53 def _create_and_attach(self):
Dan Smithc18d8c62012-07-02 08:09:26 -070054 # Start a server and wait for it to become ready
Ken'ichi Ohmichicfc052e2013-10-23 11:50:04 +090055 admin_pass = self.image_ssh_password
Ghanshyam4adb7cd2014-06-03 12:32:46 +090056 _, self.server = self.create_test_server(wait_until='ACTIVE',
57 adminPass=admin_pass)
Dan Smithc18d8c62012-07-02 08:09:26 -070058
59 # Record addresses so that we can ssh later
Ghanshyam5c2a5582014-04-14 17:16:57 +090060 _, self.server['addresses'] = (
61 self.servers_client.list_addresses(self.server['id']))
Dan Smithc18d8c62012-07-02 08:09:26 -070062
63 # Create a volume and wait for it to become ready
Joseph Lanoux6809bab2014-12-18 14:57:18 +000064 self.volume = self.volumes_client.create_volume(
Ghanshyam4adb7cd2014-06-03 12:32:46 +090065 1, display_name='test')
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070066 self.addCleanup(self._delete_volume)
Ghanshyam4adb7cd2014-06-03 12:32:46 +090067 self.volumes_client.wait_for_volume_status(self.volume['id'],
68 'available')
Dan Smithc18d8c62012-07-02 08:09:26 -070069
70 # Attach the volume to the server
Ghanshyam5c2a5582014-04-14 17:16:57 +090071 _, self.attachment = self.servers_client.attach_volume(
72 self.server['id'],
73 self.volume['id'],
74 device='/dev/%s' % self.device)
Ghanshyam4adb7cd2014-06-03 12:32:46 +090075 self.volumes_client.wait_for_volume_status(self.volume['id'], 'in-use')
Dan Smithc18d8c62012-07-02 08:09:26 -070076
Ghanshyam4adb7cd2014-06-03 12:32:46 +090077 self.addCleanup(self._detach, self.server['id'], self.volume['id'])
Dan Smithc18d8c62012-07-02 08:09:26 -070078
Matt Riedemann6c668202014-03-24 09:17:10 -070079 @testtools.skipUnless(CONF.compute.run_ssh, 'SSH required for this test')
Masayuki Igawa209fd502014-02-17 14:46:43 +090080 @test.attr(type='gate')
Dan Smithc18d8c62012-07-02 08:09:26 -070081 def test_attach_detach_volume(self):
Sean Dague4dd2c0b2013-01-03 17:50:28 -050082 # Stop and Start a server with an attached volume, ensuring that
83 # the volume remains attached.
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070084 self._create_and_attach()
ivan-zhu2f54b282013-03-11 16:39:25 +080085
Ghanshyam4adb7cd2014-06-03 12:32:46 +090086 self.servers_client.stop(self.server['id'])
87 self.servers_client.wait_for_server_status(self.server['id'],
88 'SHUTOFF')
Dan Smithc18d8c62012-07-02 08:09:26 -070089
Ghanshyam4adb7cd2014-06-03 12:32:46 +090090 self.servers_client.start(self.server['id'])
91 self.servers_client.wait_for_server_status(self.server['id'], 'ACTIVE')
Dan Smithc18d8c62012-07-02 08:09:26 -070092
Ghanshyam4adb7cd2014-06-03 12:32:46 +090093 linux_client = remote_client.RemoteClient(self.server,
94 self.image_ssh_user,
95 self.server['adminPass'])
Matt Riedemannbc8dbd32013-08-02 14:02:12 -070096 partitions = linux_client.get_partitions()
97 self.assertIn(self.device, partitions)
Dan Smithc18d8c62012-07-02 08:09:26 -070098
Ghanshyam4adb7cd2014-06-03 12:32:46 +090099 self._detach(self.server['id'], self.volume['id'])
Ghanshyam5c2a5582014-04-14 17:16:57 +0900100 self.attachment = None
Ghanshyam4adb7cd2014-06-03 12:32:46 +0900101 self.servers_client.stop(self.server['id'])
102 self.servers_client.wait_for_server_status(self.server['id'],
103 'SHUTOFF')
Dan Smithc18d8c62012-07-02 08:09:26 -0700104
Ghanshyam4adb7cd2014-06-03 12:32:46 +0900105 self.servers_client.start(self.server['id'])
106 self.servers_client.wait_for_server_status(self.server['id'], 'ACTIVE')
Dan Smithc18d8c62012-07-02 08:09:26 -0700107
Ghanshyam4adb7cd2014-06-03 12:32:46 +0900108 linux_client = remote_client.RemoteClient(self.server,
109 self.image_ssh_user,
110 self.server['adminPass'])
Matt Riedemannbc8dbd32013-08-02 14:02:12 -0700111 partitions = linux_client.get_partitions()
112 self.assertNotIn(self.device, partitions)
Dan Smith1ced8422012-08-16 10:35:19 -0700113
Ghanshyam5c2a5582014-04-14 17:16:57 +0900114 @test.attr(type='gate')
115 def test_list_get_volume_attachments(self):
116 # Create Server, Volume and attach that Volume to Server
117 self._create_and_attach()
118 # List Volume attachment of the server
119 _, body = self.servers_client.list_volume_attachments(
120 self.server['id'])
121 self.assertEqual(1, len(body))
122 self.assertIn(self.attachment, body)
123
124 # Get Volume attachment of the server
125 _, body = self.servers_client.get_volume_attachment(
126 self.server['id'],
127 self.attachment['id'])
128 self.assertEqual(self.server['id'], body['serverId'])
129 self.assertEqual(self.volume['id'], body['volumeId'])
130 self.assertEqual(self.attachment['id'], body['id'])