summaryrefslogtreecommitdiffstats
path: root/nova/tests/test_virt_drivers.py
blob: adf8f8eb817b66d8d147302d6a232ff439f81e50 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
#    Copyright 2010 OpenStack LLC
#
#    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 base64
import netaddr
import sys
import traceback

from nova import exception
from nova import flags
from nova import image
from nova import log as logging
from nova import test
from nova.tests import utils as test_utils

libvirt = None
FLAGS = flags.FLAGS

LOG = logging.getLogger('nova.tests.test_virt_drivers')


def catch_notimplementederror(f):
    """Decorator to simplify catching drivers raising NotImplementedError

    If a particular call makes a driver raise NotImplementedError, we
    log it so that we can extract this information afterwards to
    automatically generate a hypervisor/feature support matrix."""
    def wrapped_func(self, *args, **kwargs):
        try:
            return f(self, *args, **kwargs)
        except NotImplementedError:
            frame = traceback.extract_tb(sys.exc_info()[2])[-1]
            LOG.error('%(driver)s does not implement %(method)s' % {
                                               'driver': type(self.connection),
                                               'method': frame[2]})

    wrapped_func.__name__ = f.__name__
    wrapped_func.__doc__ = f.__doc__
    return wrapped_func


class _VirtDriverTestCase(test.TestCase):
    def setUp(self):
        super(_VirtDriverTestCase, self).setUp()
        self.connection = self.driver_module.get_connection('')
        self.ctxt = test_utils.get_test_admin_context()
        self.image_service = image.get_default_image_service()

    def _get_running_instance(self):
        instance_ref = test_utils.get_test_instance()
        network_info = test_utils.get_test_network_info()
        image_info = test_utils.get_test_image_info(None, instance_ref)
        self.connection.spawn(self.ctxt, instance=instance_ref,
                              image_meta=image_info,
                              network_info=network_info)
        return instance_ref, network_info

    @catch_notimplementederror
    def test_init_host(self):
        self.connection.init_host('myhostname')

    @catch_notimplementederror
    def test_list_instances(self):
        self.connection.list_instances()

    @catch_notimplementederror
    def test_list_instances_detail(self):
        self.connection.list_instances_detail()

    @catch_notimplementederror
    def test_spawn(self):
        instance_ref, network_info = self._get_running_instance()
        domains = self.connection.list_instances()
        self.assertIn(instance_ref['name'], domains)

        domains_details = self.connection.list_instances_detail()
        self.assertIn(instance_ref['name'], [i.name for i in domains_details])

    @catch_notimplementederror
    def test_snapshot_not_running(self):
        instance_ref = test_utils.get_test_instance()
        img_ref = self.image_service.create(self.ctxt, {'name': 'snap-1'})
        self.assertRaises(exception.InstanceNotRunning,
                          self.connection.snapshot,
                          self.ctxt, instance_ref, img_ref['id'])

    @catch_notimplementederror
    def test_snapshot_running(self):
        img_ref = self.image_service.create(self.ctxt, {'name': 'snap-1'})
        instance_ref, network_info = self._get_running_instance()
        self.connection.snapshot(self.ctxt, instance_ref, img_ref['id'])

    @catch_notimplementederror
    def test_reboot(self):
        reboot_type = "SOFT"
        instance_ref, network_info = self._get_running_instance()
        self.connection.reboot(instance_ref, network_info, reboot_type)

    @catch_notimplementederror
    def test_get_host_ip_addr(self):
        host_ip = self.connection.get_host_ip_addr()

        # Will raise an exception if it's not a valid IP at all
        ip = netaddr.IPAddress(host_ip)

        # For now, assume IPv4.
        self.assertEquals(ip.version, 4)

    @catch_notimplementederror
    def test_resize_running(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.resize(instance_ref, 7)

    @catch_notimplementederror
    def test_set_admin_password(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.set_admin_password(instance_ref, 'p4ssw0rd')

    @catch_notimplementederror
    def test_inject_file(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.inject_file(instance_ref,
                                    base64.b64encode('/testfile'),
                                    base64.b64encode('testcontents'))

    @catch_notimplementederror
    def test_agent_update(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.agent_update(instance_ref, 'http://www.openstack.org/',
                                     'd41d8cd98f00b204e9800998ecf8427e')

    @catch_notimplementederror
    def test_rescue(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.rescue(self.ctxt, instance_ref, network_info, None)

    @catch_notimplementederror
    def test_unrescue_unrescued_instance(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.unrescue(instance_ref, network_info)

    @catch_notimplementederror
    def test_unrescue_rescued_instance(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.rescue(self.ctxt, instance_ref, network_info, None)
        self.connection.unrescue(instance_ref, network_info)

    @catch_notimplementederror
    def test_poll_rebooting_instances(self):
        self.connection.poll_rebooting_instances(10)

    @catch_notimplementederror
    def test_poll_rescued_instances(self):
        self.connection.poll_rescued_instances(10)

    @catch_notimplementederror
    def test_poll_unconfirmed_resizes(self):
        self.connection.poll_unconfirmed_resizes(10)

    @catch_notimplementederror
    def test_migrate_disk_and_power_off(self):
        instance_ref, network_info = self._get_running_instance()
        instance_type_ref = test_utils.get_test_instance_type()
        self.connection.migrate_disk_and_power_off(
            self.ctxt, instance_ref, 'dest_host', instance_type_ref)

    @catch_notimplementederror
    def test_pause(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.pause(instance_ref)

    @catch_notimplementederror
    def test_unpause_unpaused_instance(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.unpause(instance_ref)

    @catch_notimplementederror
    def test_unpause_paused_instance(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.pause(instance_ref)
        self.connection.unpause(instance_ref)

    @catch_notimplementederror
    def test_suspend(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.suspend(instance_ref)

    @catch_notimplementederror
    def test_resume_unsuspended_instance(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.resume(instance_ref)

    @catch_notimplementederror
    def test_resume_suspended_instance(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.suspend(instance_ref)
        self.connection.resume(instance_ref)

    @catch_notimplementederror
    def test_destroy_instance_nonexistant(self):
        fake_instance = {'id': 42, 'name': 'I just made this up!'}
        network_info = test_utils.get_test_network_info()
        self.connection.destroy(fake_instance, network_info)

    @catch_notimplementederror
    def test_destroy_instance(self):
        instance_ref, network_info = self._get_running_instance()
        self.assertIn(instance_ref['name'],
                      self.connection.list_instances())
        self.connection.destroy(instance_ref, network_info)
        self.assertNotIn(instance_ref['name'],
                         self.connection.list_instances())

    @catch_notimplementederror
    def test_attach_detach_volume(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.attach_volume({'driver_volume_type': 'fake'},
                                      instance_ref['name'],
                                      '/mnt/nova/something')
        self.connection.detach_volume({'driver_volume_type': 'fake'},
                                      instance_ref['name'],
                                      '/mnt/nova/something')

    @catch_notimplementederror
    def test_get_info(self):
        instance_ref, network_info = self._get_running_instance()
        info = self.connection.get_info(instance_ref['name'])
        self.assertIn('state', info)
        self.assertIn('max_mem', info)
        self.assertIn('mem', info)
        self.assertIn('num_cpu', info)
        self.assertIn('cpu_time', info)

    @catch_notimplementederror
    def test_get_info_for_unknown_instance(self):
        self.assertRaises(exception.NotFound,
                          self.connection.get_info, 'I just made this name up')

    @catch_notimplementederror
    def test_get_diagnostics(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.get_diagnostics(instance_ref['name'])

    @catch_notimplementederror
    def test_list_disks(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.list_disks(instance_ref['name'])

    @catch_notimplementederror
    def test_list_interfaces(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.list_interfaces(instance_ref['name'])

    @catch_notimplementederror
    def test_block_stats(self):
        instance_ref, network_info = self._get_running_instance()
        stats = self.connection.block_stats(instance_ref['name'], 'someid')
        self.assertEquals(len(stats), 5)

    @catch_notimplementederror
    def test_interface_stats(self):
        instance_ref, network_info = self._get_running_instance()
        stats = self.connection.interface_stats(instance_ref['name'], 'someid')
        self.assertEquals(len(stats), 8)

    @catch_notimplementederror
    def test_get_console_output(self):
        instance_ref, network_info = self._get_running_instance()
        console_output = self.connection.get_console_output(instance_ref)
        self.assertTrue(isinstance(console_output, basestring))

    @catch_notimplementederror
    def test_get_ajax_console(self):
        instance_ref, network_info = self._get_running_instance()
        ajax_console = self.connection.get_ajax_console(instance_ref)
        self.assertIn('token', ajax_console)
        self.assertIn('host', ajax_console)
        self.assertIn('port', ajax_console)

    @catch_notimplementederror
    def test_get_vnc_console(self):
        instance_ref, network_info = self._get_running_instance()
        vnc_console = self.connection.get_vnc_console(instance_ref)
        self.assertIn('internal_access_path', vnc_console)
        self.assertIn('host', vnc_console)
        self.assertIn('port', vnc_console)

    @catch_notimplementederror
    def test_get_console_pool_info(self):
        instance_ref, network_info = self._get_running_instance()
        console_pool = self.connection.get_console_pool_info(instance_ref)
        self.assertIn('address', console_pool)
        self.assertIn('username', console_pool)
        self.assertIn('password', console_pool)

    @catch_notimplementederror
    def test_refresh_security_group_rules(self):
        # FIXME: Create security group and add the instance to it
        instance_ref, network_info = self._get_running_instance()
        self.connection.refresh_security_group_rules(1)

    @catch_notimplementederror
    def test_refresh_security_group_members(self):
        # FIXME: Create security group and add the instance to it
        instance_ref, network_info = self._get_running_instance()
        self.connection.refresh_security_group_members(1)

    @catch_notimplementederror
    def test_refresh_provider_fw_rules(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.refresh_provider_fw_rules()

    @catch_notimplementederror
    def test_compare_cpu(self):
        cpu_info = '''{ "topology": {
                               "sockets": 1,
                               "cores": 2,
                               "threads": 1 },
                        "features": [
                            "xtpr",
                            "tm2",
                            "est",
                            "vmx",
                            "ds_cpl",
                            "monitor",
                            "pbe",
                            "tm",
                            "ht",
                            "ss",
                            "acpi",
                            "ds",
                            "vme"],
                        "arch": "x86_64",
                        "model": "Penryn",
                        "vendor": "Intel" }'''

        self.connection.compare_cpu(cpu_info)

    @catch_notimplementederror
    def test_ensure_filtering_for_instance(self):
        instance_ref = test_utils.get_test_instance()
        network_info = test_utils.get_test_network_info()
        self.connection.ensure_filtering_rules_for_instance(instance_ref,
                                                            network_info)

    @catch_notimplementederror
    def test_unfilter_instance(self):
        instance_ref = test_utils.get_test_instance()
        network_info = test_utils.get_test_network_info()
        self.connection.unfilter_instance(instance_ref, network_info)

    @catch_notimplementederror
    def test_live_migration(self):
        instance_ref, network_info = self._get_running_instance()
        self.connection.live_migration(self.ctxt, instance_ref, 'otherhost',
                                       None, None)

    @catch_notimplementederror
    def _check_host_status_fields(self, host_status):
        self.assertIn('disk_total', host_status)
        self.assertIn('disk_used', host_status)
        self.assertIn('host_memory_total', host_status)
        self.assertIn('host_memory_free', host_status)

    @catch_notimplementederror
    def test_update_host_status(self):
        host_status = self.connection.update_host_status()
        self._check_host_status_fields(host_status)

    @catch_notimplementederror
    def test_get_host_stats(self):
        host_status = self.connection.get_host_stats()
        self._check_host_status_fields(host_status)

    @catch_notimplementederror
    def test_set_host_enabled(self):
        self.connection.set_host_enabled('a useless argument?', True)

    @catch_notimplementederror
    def test_host_power_action_reboot(self):
        self.connection.host_power_action('a useless argument?', 'reboot')

    @catch_notimplementederror
    def test_host_power_action_shutdown(self):
        self.connection.host_power_action('a useless argument?', 'shutdown')

    @catch_notimplementederror
    def test_host_power_action_startup(self):
        self.connection.host_power_action('a useless argument?', 'startup')


class AbstractDriverTestCase(_VirtDriverTestCase):
    def setUp(self):
        import nova.virt.driver

        self.driver_module = nova.virt.driver

        def get_driver_connection(_):
            return nova.virt.driver.ComputeDriver()

        self.driver_module.get_connection = get_driver_connection
        super(AbstractDriverTestCase, self).setUp()


class FakeConnectionTestCase(_VirtDriverTestCase):
    def setUp(self):
        import nova.virt.fake
        self.driver_module = nova.virt.fake
        super(FakeConnectionTestCase, self).setUp()


class LibvirtConnTestCase(_VirtDriverTestCase):
    def setUp(self):
        # Put fakelibvirt in place
        if 'libvirt' in sys.modules:
            self.saved_libvirt = sys.modules['libvirt']
        else:
            self.saved_libvirt = None

        import fakelibvirt
        import fake_libvirt_utils

        sys.modules['libvirt'] = fakelibvirt

        import nova.virt.libvirt.connection
        import nova.virt.libvirt.firewall

        nova.virt.libvirt.connection.libvirt = fakelibvirt
        nova.virt.libvirt.connection.libvirt_utils = fake_libvirt_utils
        nova.virt.libvirt.firewall.libvirt = fakelibvirt

        # Point _VirtDriverTestCase at the right module
        self.driver_module = nova.virt.libvirt.connection
        super(LibvirtConnTestCase, self).setUp()
        FLAGS.rescue_image_id = "2"
        FLAGS.rescue_kernel_id = "3"
        FLAGS.rescue_ramdisk_id = None

    def tearDown(self):
        super(LibvirtConnTestCase, self).setUp()

        # Restore libvirt
        import nova.virt.libvirt.connection
        import nova.virt.libvirt.firewall
        if self.saved_libvirt:
            sys.modules['libvirt'] = self.saved_libvirt
            nova.virt.libvirt.connection.libvirt = self.saved_libvirt
            nova.virt.libvirt.connection.libvirt_utils = self.saved_libvirt
            nova.virt.libvirt.firewall.libvirt = self.saved_libvirt