2190
2190
conn._check_shared_storage_test_file("file").AndReturn(False)
2192
2192
self.mox.StubOutWithMock(conn, "_assert_dest_node_has_enough_disk")
2193
conn._assert_dest_node_has_enough_disk(self.context, instance_ref,
2194
dest_check_data['disk_available_mb'],
2193
conn._assert_dest_node_has_enough_disk(
2194
self.context, instance_ref, dest_check_data['disk_available_mb'],
2197
2197
self.mox.ReplayAll()
2198
2198
conn.check_can_live_migrate_source(self.context, instance_ref,
2393
2393
self.mox.ReplayAll()
2394
2394
migrate_data = {'is_shared_storage': False,
2395
2395
'is_volume_backed': True,
2396
'block_migration': False
2396
'block_migration': False,
2397
'instance_relative_path': inst_ref['name']
2398
2399
ret = conn.pre_live_migration(c, inst_ref, vol, nw_info,
2400
2401
self.assertEqual(ret, None)
2401
2402
self.assertTrue(os.path.exists('%s/%s/' % (tmpdir,
2403
2404
db.instance_destroy(self.context, inst_ref['uuid'])
2405
2406
def test_pre_block_migration_works_correctly(self):
2503
2504
db.instance_destroy(self.context, instance_ref['uuid'])
2506
def test_get_instance_disk_info_excludes_volumes(self):
2508
instance_ref = db.instance_create(self.context, self.test_instance)
2509
dummyxml = ("<domain type='kvm'><name>instance-0000000a</name>"
2511
"<disk type='file'><driver name='qemu' type='raw'/>"
2512
"<source file='/test/disk'/>"
2513
"<target dev='vda' bus='virtio'/></disk>"
2514
"<disk type='file'><driver name='qemu' type='qcow2'/>"
2515
"<source file='/test/disk.local'/>"
2516
"<target dev='vdb' bus='virtio'/></disk>"
2517
"<disk type='file'><driver name='qemu' type='qcow2'/>"
2518
"<source file='/fake/path/to/volume1'/>"
2519
"<target dev='vdc' bus='virtio'/></disk>"
2520
"<disk type='file'><driver name='qemu' type='qcow2'/>"
2521
"<source file='/fake/path/to/volume2'/>"
2522
"<target dev='vdd' bus='virtio'/></disk>"
2523
"</devices></domain>")
2526
vdmock = self.mox.CreateMock(libvirt.virDomain)
2527
self.mox.StubOutWithMock(vdmock, "XMLDesc")
2528
vdmock.XMLDesc(0).AndReturn(dummyxml)
2530
def fake_lookup(instance_name):
2531
if instance_name == instance_ref['name']:
2533
self.create_fake_libvirt_mock(lookupByName=fake_lookup)
2535
GB = 1024 * 1024 * 1024
2536
fake_libvirt_utils.disk_sizes['/test/disk'] = 10 * GB
2537
fake_libvirt_utils.disk_sizes['/test/disk.local'] = 20 * GB
2538
fake_libvirt_utils.disk_backing_files['/test/disk.local'] = 'file'
2540
self.mox.StubOutWithMock(os.path, "getsize")
2541
os.path.getsize('/test/disk').AndReturn((10737418240))
2542
os.path.getsize('/test/disk.local').AndReturn((3328599655))
2544
ret = ("image: /test/disk\n"
2545
"file format: raw\n"
2546
"virtual size: 20G (21474836480 bytes)\n"
2548
"cluster_size: 2097152\n"
2549
"backing file: /test/dummy (actual path: /backing/file)\n")
2551
self.mox.StubOutWithMock(os.path, "exists")
2552
os.path.exists('/test/disk.local').AndReturn(True)
2554
self.mox.StubOutWithMock(utils, "execute")
2555
utils.execute('env', 'LC_ALL=C', 'LANG=C', 'qemu-img', 'info',
2556
'/test/disk.local').AndReturn((ret, ''))
2558
self.mox.ReplayAll()
2559
conn_info = {'driver_volume_type': 'fake'}
2560
info = {'block_device_mapping': [
2561
{'connection_info': conn_info, 'mount_device': '/dev/vdc'},
2562
{'connection_info': conn_info, 'mount_device': '/dev/vdd'}]}
2563
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
2564
info = conn.get_instance_disk_info(instance_ref['name'],
2565
block_device_info=info)
2566
info = jsonutils.loads(info)
2567
self.assertEquals(info[0]['type'], 'raw')
2568
self.assertEquals(info[0]['path'], '/test/disk')
2569
self.assertEquals(info[0]['disk_size'], 10737418240)
2570
self.assertEquals(info[0]['backing_file'], "")
2571
self.assertEquals(info[0]['over_committed_disk_size'], 0)
2572
self.assertEquals(info[1]['type'], 'qcow2')
2573
self.assertEquals(info[1]['path'], '/test/disk.local')
2574
self.assertEquals(info[1]['virt_disk_size'], 21474836480)
2575
self.assertEquals(info[1]['backing_file'], "file")
2576
self.assertEquals(info[1]['over_committed_disk_size'], 18146236825)
2578
db.instance_destroy(self.context, instance_ref['uuid'])
2505
2580
def test_spawn_with_network_info(self):
2506
2581
# Preparing mocks
2507
2582
def fake_none(*args, **kwargs):
3071
3146
"uuid": "875a8070-d0b9-4949-8b31-104d125c9a64"}
3072
3147
conn.destroy(instance, [])
3149
def test_destroy_timed_out(self):
3150
mock = self.mox.CreateMock(libvirt.virDomain)
3152
mock.destroy().AndRaise(libvirt.libvirtError("timed out"))
3153
self.mox.ReplayAll()
3155
def fake_lookup_by_name(instance_name):
3158
def fake_get_error_code(self):
3159
return libvirt.VIR_ERR_OPERATION_TIMEOUT
3161
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
3162
self.stubs.Set(conn, '_lookup_by_name', fake_lookup_by_name)
3163
self.stubs.Set(libvirt.libvirtError, 'get_error_code',
3164
fake_get_error_code)
3165
instance = {"name": "instancename", "id": "instanceid",
3166
"uuid": "875a8070-d0b9-4949-8b31-104d125c9a64"}
3167
self.assertRaises(exception.InstancePowerOffFailure,
3168
conn.destroy, instance, [])
3074
3170
def test_private_destroy_not_found(self):
3075
3171
mock = self.mox.CreateMock(libvirt.virDomain)
3715
3811
conn.set_cache_mode(fake_conf)
3716
3812
self.assertEqual(fake_conf.driver_cache, 'fake')
3814
def _test_shared_storage_detection(self, is_same):
3815
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
3816
self.mox.StubOutWithMock(conn, 'get_host_ip_addr')
3817
self.mox.StubOutWithMock(utils, 'execute')
3818
self.mox.StubOutWithMock(os.path, 'exists')
3819
self.mox.StubOutWithMock(os, 'unlink')
3820
conn.get_host_ip_addr().AndReturn('bar')
3821
utils.execute('ssh', 'foo', 'touch', mox.IgnoreArg())
3822
os.path.exists(mox.IgnoreArg()).AndReturn(is_same)
3824
os.unlink(mox.IgnoreArg())
3826
utils.execute('ssh', 'foo', 'rm', mox.IgnoreArg())
3827
self.mox.ReplayAll()
3828
return conn._is_storage_shared_with('foo', '/path')
3830
def test_shared_storage_detection_same_host(self):
3831
self.assertTrue(self._test_shared_storage_detection(True))
3833
def test_shared_storage_detection_different_host(self):
3834
self.assertFalse(self._test_shared_storage_detection(False))
3836
def test_shared_storage_detection_easy(self):
3837
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
3838
self.mox.StubOutWithMock(conn, 'get_host_ip_addr')
3839
self.mox.StubOutWithMock(utils, 'execute')
3840
self.mox.StubOutWithMock(os.path, 'exists')
3841
self.mox.StubOutWithMock(os, 'unlink')
3842
conn.get_host_ip_addr().AndReturn('foo')
3843
self.mox.ReplayAll()
3844
self.assertTrue(conn._is_storage_shared_with('foo', '/path'))
3719
3847
class HostStateTestCase(test.TestCase):
4408
4538
result = libvirt_utils.get_iscsi_initiator()
4409
4539
self.assertEqual(initiator, result)
4541
def test_get_missing_iscsi_initiator(self):
4542
self.mox.StubOutWithMock(utils, 'execute')
4543
file_path = '/etc/iscsi/initiatorname.iscsi'
4544
utils.execute('cat', file_path, run_as_root=True).AndRaise(
4545
exception.FileNotFound(file_path=file_path)
4548
self.mox.ReplayAll()
4549
result = libvirt_utils.get_iscsi_initiator()
4550
self.assertIsNone(result)
4411
4552
def test_create_image(self):
4412
4553
self.mox.StubOutWithMock(utils, 'execute')
4413
4554
utils.execute('qemu-img', 'create', '-f', 'raw',
4752
4895
def fake_os_path_exists(path):
4898
def fake_is_storage_shared(dest, inst_base):
4899
self.checked_shared_storage = True
4755
4902
self.stubs.Set(self.libvirtconnection, 'get_instance_disk_info',
4756
4903
fake_get_instance_disk_info)
4757
4904
self.stubs.Set(self.libvirtconnection, '_destroy', fake_destroy)
4758
4905
self.stubs.Set(self.libvirtconnection, 'get_host_ip_addr',
4759
4906
fake_get_host_ip_addr)
4907
self.stubs.Set(self.libvirtconnection, '_is_storage_shared_with',
4908
fake_is_storage_shared)
4760
4909
self.stubs.Set(utils, 'execute', fake_execute)
4761
4910
self.stubs.Set(os.path, 'exists', fake_os_path_exists)