blob: 958102600073cede3c518375bf559ba9bcab073c [file] [log] [blame]
Dan Smithc18d8c62012-07-02 08:09:26 -07001# 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
18from nose.plugins.attrib import attr
19import unittest2 as unittest
20
Matthew Treinish481466b2012-12-20 17:16:01 -050021from tempest import clients
Dan Smithc18d8c62012-07-02 08:09:26 -070022from tempest.common.utils.data_utils import rand_name
23from tempest.common.utils.linux.remote_client import RemoteClient
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024import tempest.config
Dan Smith1ced8422012-08-16 10:35:19 -070025from tempest.tests.compute import base
Dan Smithc18d8c62012-07-02 08:09:26 -070026
27
Dan Smith1ced8422012-08-16 10:35:19 -070028class AttachVolumeTest(object):
Dan Smithc18d8c62012-07-02 08:09:26 -070029
30 run_ssh = tempest.config.TempestConfig().compute.run_ssh
31
Dan Smith1ced8422012-08-16 10:35:19 -070032 @staticmethod
Dan Smithc18d8c62012-07-02 08:09:26 -070033 def setUpClass(cls):
Dan Smithc18d8c62012-07-02 08:09:26 -070034 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 Luo79d8d362012-09-25 13:49:27 +080049 self.image_ref,
50 self.flavor_ref,
51 adminPass='password')
Dan Smithc18d8c62012-07-02 08:09:26 -070052 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 Luo79d8d362012-09-25 13:49:27 +080065 device='/dev/%s' % self.device)
Dan Smithc18d8c62012-07-02 08:09:26 -070066 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 Dague4dd2c0b2013-01-03 17:50:28 -050073 # Stop and Start a server with an attached volume, ensuring that
74 # the volume remains attached.
Dan Smithc18d8c62012-07-02 08:09:26 -070075 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 Smith1ced8422012-08-16 10:35:19 -0700108
109
110class TestAttachVolumeJSON(base.BaseComputeTestJSON,
111 AttachVolumeTest):
112 @classmethod
113 def setUpClass(cls):
114 super(TestAttachVolumeJSON, cls).setUpClass()
115 AttachVolumeTest.setUpClass(cls)
116
117
118class TestAttachVolumeXML(base.BaseComputeTestXML,
119 AttachVolumeTest):
120 @classmethod
121 def setUpClass(cls):
122 super(TestAttachVolumeXML, cls).setUpClass()
123 AttachVolumeTest.setUpClass(cls)