summaryrefslogtreecommitdiff
path: root/tempest/api/compute/servers/test_server_rescue.py
blob: 716ecda84228a852d8fdd70925a0a394fbc7e578 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import testtools

from tempest.api.compute import base
from tempest.common import compute
from tempest.common import utils
from tempest.common import waiters
from tempest import config
from tempest.lib.common.utils import data_utils
from tempest.lib import decorators

CONF = config.CONF


class ServerRescueTestBase(base.BaseV2ComputeTest):
    create_default_network = True

    @classmethod
    def skip_checks(cls):
        super(ServerRescueTestBase, cls).skip_checks()
        if not CONF.compute_feature_enabled.rescue:
            msg = "Server rescue not available."
            raise cls.skipException(msg)

    @classmethod
    def setup_credentials(cls):
        cls.set_network_resources(network=True, subnet=True, router=True)
        super(ServerRescueTestBase, cls).setup_credentials()

    @classmethod
    def resource_setup(cls):
        super(ServerRescueTestBase, cls).resource_setup()

        password = data_utils.rand_password()
        server = cls.create_test_server(adminPass=password,
                                        wait_until='ACTIVE')
        cls.servers_client.rescue_server(server['id'], adminPass=password)
        waiters.wait_for_server_status(cls.servers_client, server['id'],
                                       'RESCUE')
        cls.rescued_server_id = server['id']


class ServerRescueTestJSON(ServerRescueTestBase):
    """Test server rescue"""

    @decorators.idempotent_id('fd032140-714c-42e4-a8fd-adcd8df06be6')
    def test_rescue_unrescue_instance(self):
        """Test rescue/unrescue server"""
        password = data_utils.rand_password()
        server = self.create_test_server(adminPass=password,
                                         wait_until='ACTIVE')
        self.servers_client.rescue_server(server['id'], adminPass=password)
        waiters.wait_for_server_status(self.servers_client, server['id'],
                                       'RESCUE')
        self.servers_client.unrescue_server(server['id'])
        waiters.wait_for_server_status(self.servers_client, server['id'],
                                       'ACTIVE')


class ServerRescueTestJSONUnderV235(ServerRescueTestBase):
    """Test server rescue with compute microversion less than 2.36"""

    max_microversion = '2.35'

    # TODO(zhufl): After 2.35 we should switch to neutron client to create
    # floating ip, but that will need admin credential, so the testcases will
    # have to be added in somewhere in admin directory.

    @decorators.idempotent_id('4842e0cf-e87d-4d9d-b61f-f4791da3cacc')
    @testtools.skipUnless(CONF.network.public_network_id,
                          'The public_network_id option must be specified.')
    @testtools.skipUnless(CONF.network_feature_enabled.floating_ips,
                          "Floating ips are not available")
    def test_rescued_vm_associate_dissociate_floating_ip(self):
        """Test associate/dissociate floating ip for rescued server"""
        floating_ip_body = self.floating_ips_client.create_floating_ip(
            pool=CONF.network.floating_network_name)['floating_ip']
        self.addCleanup(self.floating_ips_client.delete_floating_ip,
                        floating_ip_body['id'])

        self.floating_ips_client.associate_floating_ip_to_server(
            str(floating_ip_body['ip']).strip(), self.rescued_server_id)

        # Disassociation of floating IP that was associated in this method
        self.floating_ips_client.disassociate_floating_ip_from_server(
            str(floating_ip_body['ip']).strip(), self.rescued_server_id)

    @decorators.idempotent_id('affca41f-7195-492d-8065-e09eee245404')
    def test_rescued_vm_add_remove_security_group(self):
        """Test add/remove security group to for rescued server"""
        # Add Security group
        sg = self.create_security_group()
        self.servers_client.add_security_group(self.rescued_server_id,
                                               name=sg['name'])

        # Delete Security group
        self.servers_client.remove_security_group(self.rescued_server_id,
                                                  name=sg['name'])


class BaseServerStableDeviceRescueTest(base.BaseV2ComputeTest):

    @classmethod
    def skip_checks(cls):
        super(BaseServerStableDeviceRescueTest, cls).skip_checks()
        if not CONF.compute_feature_enabled.rescue:
            msg = "Server rescue not available."
            raise cls.skipException(msg)
        if not CONF.compute_feature_enabled.stable_rescue:
            msg = "Stable rescue not available."
            raise cls.skipException(msg)

    @classmethod
    def setup_credentials(cls):
        cls.set_network_resources(network=True, subnet=True, router=True,
                                  dhcp=True)
        super(BaseServerStableDeviceRescueTest, cls).setup_credentials()

    def _create_server_and_rescue_image(self, hw_rescue_device=None,
                                        hw_rescue_bus=None,
                                        block_device_mapping_v2=None,
                                        validatable=False,
                                        validation_resources=None,
                                        wait_until='ACTIVE'):
        server = self.create_test_server(
            wait_until=wait_until,
            validatable=validatable,
            validation_resources=validation_resources)
        image_id = self.create_image_from_server(
            server['id'], wait_until='ACTIVE')['id']

        if block_device_mapping_v2:
            server = self.create_test_server(
                wait_until=wait_until,
                validatable=validatable,
                validation_resources=validation_resources,
                block_device_mapping_v2=block_device_mapping_v2)

        if hw_rescue_bus:
            self.images_client.update_image(
                image_id, [dict(add='/hw_rescue_bus',
                                value=hw_rescue_bus)])
        if hw_rescue_device:
            self.images_client.update_image(
                image_id, [dict(add='/hw_rescue_device',
                                value=hw_rescue_device)])
        return server, image_id

    def _test_stable_device_rescue(
            self, server, rescue_image_id,
            validation_resources=None):
        self.servers_client.rescue_server(
            server['id'], rescue_image_ref=rescue_image_id)
        waiters.wait_for_server_status(
            self.servers_client, server['id'], 'RESCUE')
        self.servers_client.unrescue_server(server['id'])
        # NOTE(gmann) In next addCleanup, server unrescue is called before the
        # detach volume is called in cleanup (added by self.attach_volume()
        # method) so to make sure server is ready before detach operation, we
        # need to perform ssh on it, more details are in bug#1960346.
        if validation_resources and CONF.validation.run_validation:
            tenant_network = self.get_tenant_network()
            compute.wait_for_ssh_or_ping(
                server, self.os_primary, tenant_network,
                True, validation_resources, "SSHABLE", True)
        else:
            waiters.wait_for_server_status(
                self.servers_client, server['id'], 'ACTIVE')


class ServerStableDeviceRescueTestIDE(BaseServerStableDeviceRescueTest):
    """Test rescuing server using an IDE device for the rescue disk"""

    @classmethod
    def skip_checks(cls):
        super().skip_checks()
        if not CONF.compute_feature_enabled.ide_bus:
            raise cls.skipException("IDE bus not available.")

    @decorators.idempotent_id('947004c3-e8ef-47d9-9f00-97b74f9eaf96')
    @testtools.skipIf("aarch64" in CONF.scenario.img_file,
                      "Aarch64 does not support ide bus for cdrom")
    def test_stable_device_rescue_cdrom_ide(self):
        """Test rescuing server with cdrom and ide as the rescue disk"""
        server, rescue_image_id = self._create_server_and_rescue_image(
            hw_rescue_device='cdrom', hw_rescue_bus='ide')
        self._test_stable_device_rescue(server, rescue_image_id)


class ServerStableDeviceRescueTest(BaseServerStableDeviceRescueTest):
    """Test rescuing server specifying type of device for the rescue disk"""

    @decorators.idempotent_id('16865750-1417-4854-bcf7-496e6753c01e')
    def test_stable_device_rescue_disk_virtio(self):
        """Test rescuing server with disk and virtio as the rescue disk"""
        server, rescue_image_id = self._create_server_and_rescue_image(
            hw_rescue_device='disk', hw_rescue_bus='virtio')
        self._test_stable_device_rescue(server, rescue_image_id)

    @decorators.idempotent_id('12340157-6306-4745-bdda-cfa019908b48')
    def test_stable_device_rescue_disk_scsi(self):
        """Test rescuing server with disk and scsi as the rescue disk"""
        server, rescue_image_id = self._create_server_and_rescue_image(
            hw_rescue_device='disk', hw_rescue_bus='scsi')
        self._test_stable_device_rescue(server, rescue_image_id)

    @decorators.idempotent_id('647d04cf-ad35-4956-89ab-b05c5c16f30c')
    def test_stable_device_rescue_disk_usb(self):
        """Test rescuing server with disk and usb as the rescue disk"""
        server, rescue_image_id = self._create_server_and_rescue_image(
            hw_rescue_device='disk', hw_rescue_bus='usb')
        self._test_stable_device_rescue(server, rescue_image_id)

    @decorators.idempotent_id('a3772b42-00bf-4310-a90b-1cc6fd3e7eab')
    @utils.services('volume')
    def test_stable_device_rescue_disk_virtio_with_volume_attached(self):
        """Test rescuing server with volume attached

        Attach a volume to the server and then rescue the server with disk
        and virtio as the rescue disk.
        """
        # This test just check detach fail and does not
        # perfom the detach operation but in cleanup from
        # self.attach_volume() it will try to detach the server
        # after unrescue the server. Due to that we need to make
        # server SSHable before it try to detach, more details are
        # in bug#1960346
        validation_resources = self.get_class_validation_resources(
            self.os_primary)
        server, rescue_image_id = self._create_server_and_rescue_image(
            hw_rescue_device='disk', hw_rescue_bus='virtio', validatable=True,
            validation_resources=validation_resources, wait_until="SSHABLE")
        server = self.servers_client.show_server(server['id'])['server']
        volume = self.create_volume()
        self.attach_volume(server, volume)
        waiters.wait_for_volume_resource_status(self.volumes_client,
                                                volume['id'], 'in-use')
        self._test_stable_device_rescue(
            server, rescue_image_id,
            validation_resources=validation_resources)


class ServerBootFromVolumeStableRescueTest(BaseServerStableDeviceRescueTest):
    """Test rescuing server specifying type of device for the rescue disk

    Test rescuing server specifying type of device for the rescue disk with
    compute microversion greater than 2.86.
    """

    min_microversion = '2.87'

    @classmethod
    def skip_checks(cls):
        super(ServerBootFromVolumeStableRescueTest, cls).skip_checks()
        if not CONF.service_available.cinder:
            skip_msg = ("%s skipped as Cinder is not available" % cls.__name__)
            raise cls.skipException(skip_msg)

    @decorators.attr(type='slow')
    @decorators.idempotent_id('48f123cb-922a-4065-8db6-b9a9074a556b')
    def test_stable_device_rescue_bfv_blank_volume(self):
        """Test rescuing server with blank volume as block_device_mapping_v2

        Create a server with block_device_mapping_v2 with blank volume,
        then rescue the server with disk and virtio as the rescue disk.
        """
        block_device_mapping_v2 = [{
            "boot_index": "0",
            "source_type": "blank",
            "volume_size": CONF.volume.volume_size,
            "destination_type": "volume"}]
        server, rescue_image_id = self._create_server_and_rescue_image(
            hw_rescue_device='disk', hw_rescue_bus='virtio',
            block_device_mapping_v2=block_device_mapping_v2)
        self._test_stable_device_rescue(server, rescue_image_id)

    @decorators.attr(type='slow')
    @decorators.idempotent_id('e4636333-c928-40fc-98b7-70a23eef4224')
    def test_stable_device_rescue_bfv_image_volume(self):
        """Test rescuing server with blank volume as block_device_mapping_v2

        Create a server with block_device_mapping_v2 with image volume,
        then rescue the server with disk and virtio as the rescue disk.
        """
        block_device_mapping_v2 = [{
            "boot_index": "0",
            "source_type": "image",
            "volume_size": CONF.volume.volume_size,
            "uuid": CONF.compute.image_ref,
            "destination_type": "volume"}]
        server, rescue_image_id = self._create_server_and_rescue_image(
            hw_rescue_device='disk', hw_rescue_bus='virtio',
            block_device_mapping_v2=block_device_mapping_v2)
        self._test_stable_device_rescue(server, rescue_image_id)