~citrix-openstack/nova/xenapi

« back to all changes in this revision

Viewing changes to nova/tests/test_xenapi.py

merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from nova.virt import xenapi_conn
32
32
from nova.virt.xenapi import fake as xenapi_fake
33
33
from nova.virt.xenapi import volume_utils
 
34
from nova.virt.xenapi import vm_utils
34
35
from nova.virt.xenapi.vmops import SimpleDH
35
36
from nova.virt.xenapi.vmops import VMOps
36
37
from nova.tests.db import fakes as db_fakes
167
168
        stubs.stubout_session(self.stubs, stubs.FakeSessionForVMTests)
168
169
        stubs.stubout_get_this_vm_uuid(self.stubs)
169
170
        stubs.stubout_stream_disk(self.stubs)
 
171
        stubs.stubout_is_vdi_pv(self.stubs)
170
172
        self.stubs.Set(VMOps, 'reset_network', reset_network)
171
173
        glance_stubs.stubout_glance_client(self.stubs,
172
174
                                           glance_stubs.FakeGlance)
231
233
        vm = vms[0]
232
234
 
233
235
        # Check that m1.large above turned into the right thing.
234
 
        instance_type = instance_types.INSTANCE_TYPES['m1.large']
 
236
        instance_type = db.instance_type_get_by_name(conn, 'm1.large')
235
237
        mem_kib = long(instance_type['memory_mb']) << 10
236
238
        mem_bytes = str(mem_kib << 10)
237
239
        vcpus = instance_type['vcpus']
283
285
 
284
286
    def test_spawn_raw_glance(self):
285
287
        FLAGS.xenapi_image_service = 'glance'
286
 
        self._test_spawn(1, None, None)
 
288
        self._test_spawn(glance_stubs.FakeGlance.IMAGE_RAW, None, None)
 
289
 
 
290
    def test_spawn_vhd_glance(self):
 
291
        FLAGS.xenapi_image_service = 'glance'
 
292
        self._test_spawn(glance_stubs.FakeGlance.IMAGE_VHD, None, None)
287
293
 
288
294
    def test_spawn_glance(self):
289
295
        FLAGS.xenapi_image_service = 'glance'
290
 
        self._test_spawn(1, 2, 3)
 
296
        self._test_spawn(glance_stubs.FakeGlance.IMAGE_MACHINE,
 
297
                         glance_stubs.FakeGlance.IMAGE_KERNEL,
 
298
                         glance_stubs.FakeGlance.IMAGE_RAMDISK)
291
299
 
292
300
    def tearDown(self):
293
301
        super(XenAPIVMTestCase, self).tearDown()
336
344
 
337
345
    def tearDown(self):
338
346
        super(XenAPIDiffieHellmanTestCase, self).tearDown()
 
347
 
 
348
 
 
349
class XenAPIMigrateInstance(test.TestCase):
 
350
    """
 
351
    Unit test for verifying migration-related actions
 
352
    """
 
353
 
 
354
    def setUp(self):
 
355
        super(XenAPIMigrateInstance, self).setUp()
 
356
        self.stubs = stubout.StubOutForTesting()
 
357
        FLAGS.target_host = '127.0.0.1'
 
358
        FLAGS.xenapi_connection_url = 'test_url'
 
359
        FLAGS.xenapi_connection_password = 'test_pass'
 
360
        db_fakes.stub_out_db_instance_api(self.stubs)
 
361
        stubs.stub_out_get_target(self.stubs)
 
362
        xenapi_fake.reset()
 
363
        self.values = {'name': 1, 'id': 1,
 
364
                  'project_id': 'fake',
 
365
                  'user_id': 'fake',
 
366
                  'image_id': 1,
 
367
                  'kernel_id': 2,
 
368
                  'ramdisk_id': 3,
 
369
                  'instance_type': 'm1.large',
 
370
                  'mac_address': 'aa:bb:cc:dd:ee:ff',
 
371
                  }
 
372
        stubs.stub_out_migration_methods(self.stubs)
 
373
 
 
374
    def test_migrate_disk_and_power_off(self):
 
375
        instance = db.instance_create(self.values)
 
376
        stubs.stubout_session(self.stubs, stubs.FakeSessionForMigrationTests)
 
377
        conn = xenapi_conn.get_connection(False)
 
378
        conn.migrate_disk_and_power_off(instance, '127.0.0.1')
 
379
 
 
380
    def test_attach_disk(self):
 
381
        instance = db.instance_create(self.values)
 
382
        stubs.stubout_session(self.stubs, stubs.FakeSessionForMigrationTests)
 
383
        conn = xenapi_conn.get_connection(False)
 
384
        conn.attach_disk(instance, {'base_copy': 'hurr', 'cow': 'durr'})
 
385
 
 
386
 
 
387
class XenAPIDetermineDiskImageTestCase(test.TestCase):
 
388
    """
 
389
    Unit tests for code that detects the ImageType
 
390
    """
 
391
    def setUp(self):
 
392
        super(XenAPIDetermineDiskImageTestCase, self).setUp()
 
393
        glance_stubs.stubout_glance_client(self.stubs,
 
394
                                           glance_stubs.FakeGlance)
 
395
 
 
396
        class FakeInstance(object):
 
397
            pass
 
398
 
 
399
        self.fake_instance = FakeInstance()
 
400
        self.fake_instance.id = 42
 
401
 
 
402
    def assert_disk_type(self, disk_type):
 
403
        dt = vm_utils.VMHelper.determine_disk_image_type(
 
404
            self.fake_instance)
 
405
        self.assertEqual(disk_type, dt)
 
406
 
 
407
    def test_instance_disk(self):
 
408
        """
 
409
        If a kernel is specified then the image type is DISK (aka machine)
 
410
        """
 
411
        FLAGS.xenapi_image_service = 'objectstore'
 
412
        self.fake_instance.image_id = glance_stubs.FakeGlance.IMAGE_MACHINE
 
413
        self.fake_instance.kernel_id = glance_stubs.FakeGlance.IMAGE_KERNEL
 
414
        self.assert_disk_type(vm_utils.ImageType.DISK)
 
415
 
 
416
    def test_instance_disk_raw(self):
 
417
        """
 
418
        If the kernel isn't specified, and we're not using Glance, then
 
419
        DISK_RAW is assumed.
 
420
        """
 
421
        FLAGS.xenapi_image_service = 'objectstore'
 
422
        self.fake_instance.image_id = glance_stubs.FakeGlance.IMAGE_RAW
 
423
        self.fake_instance.kernel_id = None
 
424
        self.assert_disk_type(vm_utils.ImageType.DISK_RAW)
 
425
 
 
426
    def test_glance_disk_raw(self):
 
427
        """
 
428
        If we're using Glance, then defer to the image_type field, which in
 
429
        this case will be 'raw'.
 
430
        """
 
431
        FLAGS.xenapi_image_service = 'glance'
 
432
        self.fake_instance.image_id = glance_stubs.FakeGlance.IMAGE_RAW
 
433
        self.fake_instance.kernel_id = None
 
434
        self.assert_disk_type(vm_utils.ImageType.DISK_RAW)
 
435
 
 
436
    def test_glance_disk_vhd(self):
 
437
        """
 
438
        If we're using Glance, then defer to the image_type field, which in
 
439
        this case will be 'vhd'.
 
440
        """
 
441
        FLAGS.xenapi_image_service = 'glance'
 
442
        self.fake_instance.image_id = glance_stubs.FakeGlance.IMAGE_VHD
 
443
        self.fake_instance.kernel_id = None
 
444
        self.assert_disk_type(vm_utils.ImageType.DISK_VHD)