Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 IBM |
| 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 | |
| 18 | from nose.plugins.attrib import attr |
| 19 | import unittest2 as unittest |
| 20 | |
Matthew Treinish | 481466b | 2012-12-20 17:16:01 -0500 | [diff] [blame] | 21 | from tempest import clients |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 22 | from tempest.common.utils.data_utils import rand_name |
| 23 | from tempest.common.utils.linux.remote_client import RemoteClient |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 24 | import tempest.config |
Dan Smith | 1ced842 | 2012-08-16 10:35:19 -0700 | [diff] [blame] | 25 | from tempest.tests.compute import base |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 26 | |
| 27 | |
Dan Smith | 1ced842 | 2012-08-16 10:35:19 -0700 | [diff] [blame] | 28 | class AttachVolumeTest(object): |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 29 | |
| 30 | run_ssh = tempest.config.TempestConfig().compute.run_ssh |
| 31 | |
Dan Smith | 1ced842 | 2012-08-16 10:35:19 -0700 | [diff] [blame] | 32 | @staticmethod |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 33 | def setUpClass(cls): |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 34 | cls.device = 'vdb' |
| 35 | |
| 36 | def _detach(self, server_id, volume_id): |
| 37 | self.servers_client.detach_volume(server_id, volume_id) |
| 38 | self.volumes_client.wait_for_volume_status(volume_id, 'available') |
| 39 | |
| 40 | def _delete(self, server_id, volume_id): |
| 41 | self.volumes_client.delete_volume(volume_id) |
| 42 | self.servers_client.delete_server(server_id) |
| 43 | |
| 44 | def _create_and_attach(self): |
| 45 | name = rand_name('server') |
| 46 | |
| 47 | # Start a server and wait for it to become ready |
| 48 | resp, server = self.servers_client.create_server(name, |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 49 | self.image_ref, |
| 50 | self.flavor_ref, |
| 51 | adminPass='password') |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 52 | self.servers_client.wait_for_server_status(server['id'], 'ACTIVE') |
| 53 | |
| 54 | # Record addresses so that we can ssh later |
| 55 | resp, server['addresses'] = \ |
| 56 | self.servers_client.list_addresses(server['id']) |
| 57 | |
| 58 | # Create a volume and wait for it to become ready |
| 59 | resp, volume = self.volumes_client.create_volume(1, |
| 60 | display_name='test') |
| 61 | self.volumes_client.wait_for_volume_status(volume['id'], 'available') |
| 62 | |
| 63 | # Attach the volume to the server |
| 64 | self.servers_client.attach_volume(server['id'], volume['id'], |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 65 | device='/dev/%s' % self.device) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 66 | self.volumes_client.wait_for_volume_status(volume['id'], 'in-use') |
| 67 | |
| 68 | return server, volume |
| 69 | |
| 70 | @attr(type='positive') |
| 71 | @unittest.skipIf(not run_ssh, 'SSH required for this test') |
| 72 | def test_attach_detach_volume(self): |
Sean Dague | 4dd2c0b | 2013-01-03 17:50:28 -0500 | [diff] [blame^] | 73 | # Stop and Start a server with an attached volume, ensuring that |
| 74 | # the volume remains attached. |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 75 | server, volume = self._create_and_attach() |
| 76 | |
| 77 | attached = True |
| 78 | |
| 79 | try: |
| 80 | self.servers_client.stop(server['id']) |
| 81 | self.servers_client.wait_for_server_status(server['id'], 'SHUTOFF') |
| 82 | |
| 83 | self.servers_client.start(server['id']) |
| 84 | self.servers_client.wait_for_server_status(server['id'], 'ACTIVE') |
| 85 | |
| 86 | linux_client = RemoteClient(server, |
| 87 | self.ssh_user, server['adminPass']) |
| 88 | partitions = linux_client.get_partitions() |
| 89 | self.assertTrue(self.device in partitions) |
| 90 | |
| 91 | self._detach(server['id'], volume['id']) |
| 92 | attached = False |
| 93 | |
| 94 | self.servers_client.stop(server['id']) |
| 95 | self.servers_client.wait_for_server_status(server['id'], 'SHUTOFF') |
| 96 | |
| 97 | self.servers_client.start(server['id']) |
| 98 | self.servers_client.wait_for_server_status(server['id'], 'ACTIVE') |
| 99 | |
| 100 | linux_client = RemoteClient(server, |
| 101 | self.ssh_user, server['adminPass']) |
| 102 | partitions = linux_client.get_partitions() |
| 103 | self.assertFalse(self.device in partitions) |
| 104 | finally: |
| 105 | if attached: |
| 106 | self._detach(server['id'], volume['id']) |
| 107 | self._delete(server['id'], volume['id']) |
Dan Smith | 1ced842 | 2012-08-16 10:35:19 -0700 | [diff] [blame] | 108 | |
| 109 | |
| 110 | class TestAttachVolumeJSON(base.BaseComputeTestJSON, |
| 111 | AttachVolumeTest): |
| 112 | @classmethod |
| 113 | def setUpClass(cls): |
| 114 | super(TestAttachVolumeJSON, cls).setUpClass() |
| 115 | AttachVolumeTest.setUpClass(cls) |
| 116 | |
| 117 | |
| 118 | class TestAttachVolumeXML(base.BaseComputeTestXML, |
| 119 | AttachVolumeTest): |
| 120 | @classmethod |
| 121 | def setUpClass(cls): |
| 122 | super(TestAttachVolumeXML, cls).setUpClass() |
| 123 | AttachVolumeTest.setUpClass(cls) |