def test_get_connection_exception(self): self.mock.StubOutWithMock(libvirt_conn, "get_connection") libvirt_conn.get_connection(True).AndReturn(None) # running all the mocks self.mock.ReplayAll() try: connection.get_connection("QEMU", True) except SystemExit, e: self.assertEquals(type(e), type(SystemExit())) self.assertEquals(e.code, 1)
def test_broken_connection_cover(self): global libvirt libvirt = libvirt = __import__('libvirt') error = 38 domain = 13 conn = connection.get_connection(False) # self.mox.StubOutWithMock(libvirt,'openReadOnly') # libvirt.openReadOnly(mox.IgnoreArg()).AndReturn(self.fakeConn) # conn._wrapped_conn=self.fakeConn self.mox.StubOutWithMock(conn, '_wrapped_conn') self.mox.StubOutWithMock(conn._wrapped_conn, 'getCapabilities') self.mox.StubOutWithMock(libvirt.libvirtError, 'get_error_code') self.mox.StubOutWithMock(libvirt.libvirtError, 'get_error_domain') conn._wrapped_conn.getCapabilities().AndRaise( FakeLibvirt.libvirtError('fake failure')) libvirt.libvirtError.get_error_code().MultipleTimes(). \ AndReturn(error) libvirt.libvirtError.get_error_domain().MultipleTimes(). \ AndReturn(domain) self.mox.ReplayAll() try: self.assertFalse(conn._test_connection()) except: print 'error'
def test_update_inventory(self): # self.mox.StubOutWithMock(libvirt, 'openReadOnly') # libvirt.openReadOnly(mox.IgnoreArg()).AndReturn(self.fakeConn) # self.mox.StubOutWithMock(api, 'vm_save') # self.mox.StubOutWithMock(api, 'vm_host_save') # self.mox.StubOutWithMock(api, 'storage_volume_save') # # api.storage_volume_save( # mox.IgnoreArg(), # mox.IgnoreArg()).MultipleTimes().AndReturn(None) # # api.vm_host_save(mox.IgnoreArg(), # mox.IgnoreArg()).MultipleTimes().AndReturn(None) # # api.vm_save(mox.IgnoreArg(), # mox.IgnoreArg()).MultipleTimes().AndReturn(None) # self.mox.ReplayAll() conn = connection.get_connection(True) compute_rmcontext = ComputeRMContext( rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory()['1'] = \ ComputeInventory(compute_rmcontext) conn.init_rmcontext(compute_rmcontext) conn._wrapped_conn = self.fakeConn conn.update_inventory('1')
def test_get_inventory_monitor_None(self): conn1 = connection.get_connection(True) self.assertTrue(conn1.get_inventory_monitor() is not None) self.assertTrue(conn1.get_perf_monitor() is not None) conn1.libvirt_invmonitor = None conn1.get_inventory_monitor() self.assertTrue(conn1.get_inventory_monitor() is None)
def test_update_inventory(self): self.mox.StubOutWithMock(libvirt, 'openReadOnly') libvirt.openReadOnly(mox.IgnoreArg()).AndReturn(self.fakeConn) self.mox.StubOutWithMock(api, 'vm_save') self.mox.StubOutWithMock(api, 'vm_host_save') self.mox.StubOutWithMock(api, 'storage_volume_save') api.storage_volume_save( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) api.vm_host_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.ReplayAll() conn = connection.get_connection(True) compute_rmcontext = ComputeRMContext(rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory()['1'] = \ ComputeInventory(compute_rmcontext) conn.init_rmcontext(compute_rmcontext) conn._wrapped_conn = self.fakeConn conn.update_inventory('1')
def test_get_libvirt_error(self): conn1 = connection.get_connection(True) libvirt_error = conn1.get_libvirtError() # get_libvirtError return libvirtError class rather then an instance # Checkin for Type self.assertTrue(isinstance(libvirt_error, type), 'Not an instance of libvirtError')
def test_get_connection_exception(self): self.mock.StubOutWithMock(libvirt_conn, 'get_connection') libvirt_conn.get_connection(True).AndReturn(None) # running all the mocks self.mock.ReplayAll() try: connection.get_connection('QEMU', True) except SystemExit, e: self.assertEquals(type(e), type(SystemExit())) self.assertEquals(e.code, 1)
def test_broken_connection_remote(self): libvirtError = libvirt.libvirtError('fake failure') # libError = self.mox.CreateMockAnything() # self.stubs.Set(libvirt.libvirtError, '__init__', libError) # libError('fake failure').AndReturn(libvirtError) capability = self.mox.CreateMockAnything() self.stubs.Set(libvirt.virConnect, 'getCapabilities', capability) capability().AndRaise(libvirtError) # self.mox.StubOutWithMock(libvirt.libvirtError, "get_error_domain") # # libvirt.libvirtError.get_error_code().AndReturn(error) # libvirt.libvirtError.get_error_domain().AndReturn(domain) self.mox.ReplayAll() conn = connection.get_connection(False) conn._wrapped_conn = self.fakeConn try: self.assertFalse(conn._test_connection()) except: print 'over'
def test_broken_connection_cover(self): global libvirt libvirt = libvirt = __import__('libvirt') error = 38 domain = 13 conn = connection.get_connection(False) # self.mox.StubOutWithMock(libvirt,'openReadOnly') # libvirt.openReadOnly(mox.IgnoreArg()).AndReturn(self.fakeConn) # conn._wrapped_conn=self.fakeConn self.mox.StubOutWithMock(conn, '_wrapped_conn') self.mox.StubOutWithMock(conn._wrapped_conn, 'getCapabilities') self.mox.StubOutWithMock(libvirt.libvirtError, 'get_error_code') self.mox.StubOutWithMock(libvirt.libvirtError, 'get_error_domain') conn._wrapped_conn.getCapabilities().AndRaise( FakeLibvirt.libvirtError('fake failure' )) libvirt.libvirtError.get_error_code().MultipleTimes(). \ AndReturn(error) libvirt.libvirtError.get_error_domain().MultipleTimes(). \ AndReturn(domain) self.mox.ReplayAll() try: self.assertFalse(conn._test_connection()) except: print 'error'
def test_get_resource_utilization_None(self): libvirt_conn = connection.LibvirtConnection(True) libvirt_conn.libvirt_perfmon = None conn1 = connection.get_connection(True) self.assertTrue( libvirt_conn.get_resource_utilization('uuid', 'perfmon_type', 'window_minutes') is None) self.assertTrue(conn1.libvirt_perfmon.perfDataCache.get('old_stats') is None) self.assertTrue(conn1.libvirt_perfmon.perfDataCache.get('new_stats') is None)
def test_test_connection_exception(self): self.mox.StubOutWithMock(self.fakeConn, 'getCapabilities') self.fakeConn.getCapabilities().AndRaise(libvirt.libvirtError()) self.mox.ReplayAll() conn1 = connection.get_connection(True) conn1._wrapped_conn = self.fakeConn libvirt.VIR_ERR_SYSTEM_ERROR = 500 self.assertRaises(libvirt.libvirtError, conn1._test_connection) self.mox.UnsetStubs()
def test__get_connection_with_invalid_conn(self): conn = connection.get_connection(True) compute_rmcontext = ComputeRMContext(rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') conn.init_rmcontext(compute_rmcontext) conn._wrapped_conn = 'Invalid' self.assertRaises(Exception, conn._get_connection)
def test_init_rmcontext(self): compute_rmcontext = rmcontext.ComputeRMContext(rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') conn = connection.get_connection(True) conn.init_rmcontext(compute_rmcontext) self.assertTrue(conn.compute_rmcontext is not None) self.assertEquals(conn.compute_rmcontext.rmIpAddress, "10.10.155.165") self.assertEquals(conn.compute_rmcontext.rmUserName, "openstack") self.assertEquals(conn.compute_rmcontext.rmPassword, "password")
def test__get_connection_with_conn(self): conn = connection.get_connection(True) compute_rmcontext = ComputeRMContext(rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') conn.init_rmcontext(compute_rmcontext) conn._wrapped_conn = self.fakeConn result = conn._get_connection() assert result
def test_get_resource_utilization_None(self): libvirt_conn = connection.LibvirtConnection(True) libvirt_conn.libvirt_perfmon = None conn1 = connection.get_connection(True) self.assertTrue( libvirt_conn.get_resource_utilization('uuid', 'perfmon_type', 'window_minutes') is None) self.assertTrue( conn1.libvirt_perfmon.perfDataCache.get('old_stats') is None) self.assertTrue( conn1.libvirt_perfmon.perfDataCache.get('new_stats') is None)
def test_get_resource_utilization(self): self.mox.StubOutWithMock(libvirt_perfdata.LibvirtPerfMonitor, 'get_resource_utilization') libvirt_perfdata.LibvirtPerfMonitor.get_resource_utilization( mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn('') self.mox.ReplayAll() conn1 = connection.get_connection(True) self.assertTrue(conn1.get_resource_utilization('uuid', 'perfmon_type', 'window_minutes') is '') self.assertTrue(conn1.libvirt_perfmon.perfDataCache is not None)
def test_get_resource_utilization(self): self.mox.StubOutWithMock(libvirt_perfdata.LibvirtPerfMonitor, 'get_resource_utilization') libvirt_perfdata.LibvirtPerfMonitor.get_resource_utilization( mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn('') self.mox.ReplayAll() conn1 = connection.get_connection(True) self.assertTrue( conn1.get_resource_utilization('uuid', 'perfmon_type', 'window_minutes') is '') self.assertTrue(conn1.libvirt_perfmon.perfDataCache is not None)
def test_init_rmcontext(self): compute_rmcontext = rmcontext.ComputeRMContext( rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') conn = connection.get_connection(True) conn.init_rmcontext(compute_rmcontext) self.assertTrue(conn.compute_rmcontext is not None) self.assertEquals(conn.compute_rmcontext.rmIpAddress, "10.10.155.165") self.assertEquals(conn.compute_rmcontext.rmUserName, "openstack") self.assertEquals(conn.compute_rmcontext.rmPassword, "password")
def test_get_connection(self): conn = libvirt_conn.LibvirtConnection(True) conn1 = None self.mock.StubOutWithMock(libvirt_conn, 'get_connection') libvirt_conn.get_connection(True).AndReturn(conn) libvirt_conn.get_connection(False).AndReturn(conn1) # running all the mocks self.mock.ReplayAll() # performing assert operations self.assertTrue(connection.get_connection('QEMU', True)) self.assertTrue(connection.get_connection('fake', True)) self.assertRaises(Exception, connection.get_connection, 'xyz', True) # Unsetting all the mocks self.mock.UnsetStubs()
def test_get_connection(self): conn = libvirt_conn.LibvirtConnection(True) conn1 = None self.mock.StubOutWithMock(libvirt_conn, "get_connection") libvirt_conn.get_connection(True).AndReturn(conn) libvirt_conn.get_connection(False).AndReturn(conn1) # running all the mocks self.mock.ReplayAll() # performing assert operations self.assertTrue(connection.get_connection("QEMU", True)) self.assertTrue(connection.get_connection("fake", True)) self.assertRaises(Exception, connection.get_connection, "xyz", True) # Unsetting all the mocks self.mock.UnsetStubs()
def test__get_connection_with_invalid_conn(self): self.mox.StubOutWithMock(libvirt, 'openReadOnly') libvirt.openReadOnly(mox.IgnoreArg()).AndReturn(self.fakeConn) self.mox.ReplayAll() conn = connection.get_connection(True) compute_rmcontext = ComputeRMContext( rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') conn.init_rmcontext(compute_rmcontext) conn._wrapped_conn = 'Invalid' self.assertRaises(Exception, conn._get_connection) self.mox.UnsetStubs()
def test_get_resource_utilization(self): mock_libvirt_perfmon = \ self.mox.CreateMock(libvirt_perfdata.LibvirtPerfMonitor) mock_libvirt_perfmon.get_resource_utilization(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).\ AndReturn('') self.mox.ReplayAll() conn1 = connection.get_connection(True) self.assertTrue(conn1.get_resource_utilization('uuid', 'perfmon_type', 'window_minutes') is None) self.assertTrue(conn1.libvirt_perfmon.perfDataCache is not None)
def test_update_perfdata(self): conn = connection.get_connection(True) self.mox.StubOutWithMock(connection.LibvirtConnection, 'uri') conn.uri().AndReturn(None) self.mox.StubOutWithMock(libvirt, 'openReadOnly') libvirt.openReadOnly(mox.IgnoreArg()).AndReturn(self.fakeConn) self.mox.ReplayAll() conn.update_perfdata('uuid', 'perfmon_type') self.assertTrue(conn.libvirt_perfmon.perfDataCache is not None) conn.update_perfdata('uuid', 'perfmon_type1') self.assertTrue(conn.libvirt_perfmon.perfDataCache is not None)
def test_update_perfdata(self): self.mox.StubOutWithMock(libvirt_perfdata.LibvirtPerfMonitor, 'refresh_perfdata') libvirt_perfdata.LibvirtPerfMonitor.refresh_perfdata(mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn('') self.mox.ReplayAll() conn = connection.get_connection(True) compute_rmcontext = ComputeRMContext(rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') conn.init_rmcontext(compute_rmcontext) conn._wrapped_conn = self.fakeConn conn.update_perfdata('uuid', 'perfmon_type') self.assertTrue(conn.libvirt_perfmon.perfDataCache is not None)
def test_update_perfdata(self): self.mox.StubOutWithMock(libvirt_perfdata.LibvirtPerfMonitor, 'refresh_perfdata') libvirt_perfdata.LibvirtPerfMonitor.refresh_perfdata( mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn('') self.mox.ReplayAll() conn = connection.get_connection(True) compute_rmcontext = ComputeRMContext(rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') conn.init_rmcontext(compute_rmcontext) conn._wrapped_conn = self.fakeConn conn.update_perfdata('uuid', 'perfmon_type') self.assertTrue(conn.libvirt_perfmon.perfDataCache is not None)
def get_connection(hypervisor_type, read_only=False): """ Returns an object representing the connection to a virtualization platform. This could be :mod:`nova.virt.fake.FakeConnection` in test mode, a connection to KVM, QEMU, or UML via :mod:`libvirt_conn`, or a connection to XenServer or Xen Cloud Platform via :mod:`xenapi`. Any object returned here must conform to the interface documented by :mod:`FakeConnection`. **Related flags** :connection_type: A string literal that falls through a if/elif structure to determine what virtualization mechanism to use. Values may be * fake * libvirt """ # TODO(termie): maybe lazy load after initial check for permissions # TODO(termie): check whether we can be disconnected if hypervisor_type == 'fake': conn = fake.get_connection(read_only) elif hypervisor_type == 'QEMU': conn = libvirt_conn.get_connection(read_only) else: raise Exception('Unknown connection type "%s"' % hypervisor_type) if conn is None: LOG.error(_('Failed to open connection to the hypervisor')) sys.exit(1) return utils.check_isinstance(conn, driver.ComputeInventoryDriver)
def test_get_new_connection(self): libvirtcon = connection.get_connection(True) libvirtcon.get_new_connection("wrong:///uri", True)
def test_get_perf_monitor_None(self): conn1 = connection.get_connection(True) conn1.libvirt_perfmon = None self.assertTrue(conn1.get_perf_monitor() is None)
def test_get_perf_monitor(self): conn1 = connection.get_connection(True) libvirt_perform = conn1.get_perf_monitor() assert libvirt_perform
def test_uri_lxc(self): self.flags(libvirt_type='lxc') conn1 = connection.get_connection(True) self.assertEquals(conn1.uri, "lxc:///")
def test_uri_xen(self): self.flags(libvirt_type='xen') conn1 = connection.get_connection(True) self.assertEquals(conn1.uri, "xen:///")
def test_uri_uml(self): self.flags(libvirt_type='uml') conn1 = connection.get_connection(True) self.assertEquals(conn1.uri, "uml:///system")
def test_get_new_connection(self): libvirtcon = connection.get_connection(True) libvirtcon.get_new_connection("wrong:///uri", True) self.mox.UnsetStubs()
def test_test_connection(self): conn1 = connection.get_connection(True) conn1._wrapped_conn = self.fakeConn flag = conn1._test_connection() assert flag, True
def test_get_inventory_monitor(self): conn1 = connection.get_connection(True) libvirt_inv = conn1.get_inventory_monitor() assert libvirt_inv