Skip to content

Commit

Permalink
Don't actually connect to libvirtd in unit tests.
Browse files Browse the repository at this point in the history
The libvirt tests shouldn't actually be connecting to libvirtd
while running unit tests. Many of the tests were calling either
getLibVersion or getCapabilites, which would start a connection.
Fake these out so the tests can run properly without libvirtd.

Fixes bug 1157992

Change-Id: Iff1a60fc3cc912fdaf641f1f289744bcc9cc9736
  • Loading branch information
vishvananda committed Mar 21, 2013
1 parent 143bd3a commit 8d5159d
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions nova/tests/test_libvirt.py
Expand Up @@ -307,6 +307,19 @@ def fake_extend(image, size):

self.stubs.Set(libvirt_driver.disk, 'extend', fake_extend)

class FakeConn():
def getCapabilities(self):
return """<capabilities>
<host><cpu><arch>x86_64</arch></cpu></host>
</capabilities>"""

def getLibVersion(self):
return (0 * 1000 * 1000) + (9 * 1000) + 11

self.conn = FakeConn()
self.stubs.Set(libvirt_driver.LibvirtDriver, '_connect',
lambda *a, **k: self.conn)

instance_type = db.instance_type_get(self.context, 5)
sys_meta = instance_types.save_instance_type_info({}, instance_type)

Expand Down Expand Up @@ -721,10 +734,10 @@ def test_get_guest_cpu_config_default_kvm(self):
self.flags(libvirt_type="kvm",
libvirt_cpu_mode=None)

def get_lib_version_stub(self):
def get_lib_version_stub():
return (0 * 1000 * 1000) + (9 * 1000) + 11

self.stubs.Set(libvirt.virConnect,
self.stubs.Set(self.conn,
"getLibVersion",
get_lib_version_stub)
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
Expand Down Expand Up @@ -769,10 +782,10 @@ def test_get_guest_cpu_config_default_lxc(self):
self.assertEquals(conf.cpu, None)

def test_get_guest_cpu_config_host_passthrough_new(self):
def get_lib_version_stub(self):
def get_lib_version_stub():
return (0 * 1000 * 1000) + (9 * 1000) + 11

self.stubs.Set(libvirt.virConnect,
self.stubs.Set(self.conn,
"getLibVersion",
get_lib_version_stub)
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
Expand All @@ -790,10 +803,10 @@ def get_lib_version_stub(self):
self.assertEquals(conf.cpu.model, None)

def test_get_guest_cpu_config_host_model_new(self):
def get_lib_version_stub(self):
def get_lib_version_stub():
return (0 * 1000 * 1000) + (9 * 1000) + 11

self.stubs.Set(libvirt.virConnect,
self.stubs.Set(self.conn,
"getLibVersion",
get_lib_version_stub)
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
Expand All @@ -811,10 +824,10 @@ def get_lib_version_stub(self):
self.assertEquals(conf.cpu.model, None)

def test_get_guest_cpu_config_custom_new(self):
def get_lib_version_stub(self):
def get_lib_version_stub():
return (0 * 1000 * 1000) + (9 * 1000) + 11

self.stubs.Set(libvirt.virConnect,
self.stubs.Set(self.conn,
"getLibVersion",
get_lib_version_stub)
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
Expand All @@ -833,10 +846,11 @@ def get_lib_version_stub(self):
self.assertEquals(conf.cpu.model, "Penryn")

def test_get_guest_cpu_config_host_passthrough_old(self):
def get_lib_version_stub(self):
def get_lib_version_stub():
return (0 * 1000 * 1000) + (9 * 1000) + 7

self.stubs.Set(libvirt.virConnect, "getLibVersion",
self.stubs.Set(self.conn,
"getLibVersion",
get_lib_version_stub)
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
instance_ref = db.instance_create(self.context, self.test_instance)
Expand All @@ -852,7 +866,7 @@ def get_lib_version_stub(self):
disk_info)

def test_get_guest_cpu_config_host_model_old(self):
def get_lib_version_stub(self):
def get_lib_version_stub():
return (0 * 1000 * 1000) + (9 * 1000) + 7

# Ensure we have a predictable host CPU
Expand All @@ -869,7 +883,7 @@ def get_host_capabilities_stub(self):
caps.host.cpu = cpu
return caps

self.stubs.Set(libvirt.virConnect,
self.stubs.Set(self.conn,
"getLibVersion",
get_lib_version_stub)
self.stubs.Set(libvirt_driver.LibvirtDriver,
Expand All @@ -894,10 +908,10 @@ def get_host_capabilities_stub(self):
self.assertEquals(conf.cpu.features[1].name, "ht")

def test_get_guest_cpu_config_custom_old(self):
def get_lib_version_stub(self):
def get_lib_version_stub():
return (0 * 1000 * 1000) + (9 * 1000) + 7

self.stubs.Set(libvirt.virConnect,
self.stubs.Set(self.conn,
"getLibVersion",
get_lib_version_stub)
conn = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
Expand Down Expand Up @@ -3574,7 +3588,7 @@ def vcpus(self):
driver = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), True)
conn = driver._conn
self.mox.StubOutWithMock(driver, 'list_instance_ids')
self.mox.StubOutWithMock(conn, 'lookupByID')
conn.lookupByID = self.mox.CreateMockAnything()

driver.list_instance_ids().AndReturn([1, 2])
conn.lookupByID(1).AndReturn(DiagFakeDomain(None))
Expand Down

0 comments on commit 8d5159d

Please sign in to comment.