def test_host_added_event(self): self.__mock_service_get_all_by_topic() self.mox.StubOutWithMock(InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( self.libvirtVmHost.compute_id, Constants.VmHost).AndReturn(None) self.mox.StubOutWithMock(InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVmHost.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mox.StubOutWithMock(api, 'vm_host_save') api.vm_host_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.ReplayAll() self.libvirtVmHost.processUpdates() self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_HOST_ADDED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VmHost') self.assertEquals(payload['entity_id'], self.libvirtVmHost.compute_id)
def test_host_added_event(self): self.__mock_service_get_all_by_topic() self.mox.StubOutWithMock( InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( self.libvirtVmHost.compute_id, Constants.VmHost).AndReturn(None) self.mox.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVmHost.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mox.StubOutWithMock(api, 'vm_host_save') api.vm_host_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.ReplayAll() self.libvirtVmHost.processUpdates() self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_HOST_ADDED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VmHost') self.assertEquals(payload['entity_id'], self.libvirtVmHost.compute_id)
def test_process_updates_for_updated_VM(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(api, 'vm_delete_by_ids') api.vm_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVM.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mock.ReplayAll() domainObj = libvirt.virDomain() self.assertEquals( self.libvirtVM.process_updates_for_updated_VM(domainObj), None) vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) # self.assertEquals("TestVirtMgrVM7", vm.get_name()) self.assertEquals("1048576", str(vm.get_memorySize())) self.assertEquals("hd", str(vm.get_bootOrder()).strip()) self.mock.stubs.UnsetAll()
def test_process_incomplete_vms(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(api, 'vm_delete_by_ids') api.vm_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVM.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mock.ReplayAll() libvirt_inventorymonitor.incomplete_vms = \ {self.libvirtVM.compute_id: {'25f04dd3-e924-02b2-9eac-876e3c943262': 1}} self.libvirtVM.process_incomplete_vms() vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) self.assert_(vm.get_vmDisks(), "VM disks inventory not collected") self.assert_( '25f04dd3-e924-02b2-9eac-876e3c943262' not in libvirt_inventorymonitor.incomplete_vms[ self.libvirtVM.compute_id], "VM id not removed from incomplete list") self.mock.stubs.UnsetAll()
def testProcessUpdates_compute_stopped_exception(self): vmHost = VmHost() vmHost.set_id('1') vmHost.set_connectionState(Constants.VMHOST_CONNECTED) InventoryCacheManager.update_object_in_cache('1', vmHost) self.mock.StubOutWithMock(api, 'vm_host_save') api.vm_host_save( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVmHost.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) fake_computes = [{'id': '1', 'service': {'created_at': 'created', 'updated_at':'updated'}}] self.mock.StubOutWithMock(novadb, 'compute_node_get_all') novadb.compute_node_get_all(mox.IgnoreArg()).AndReturn(fake_computes) self.mock.StubOutWithMock(hnm_utils, 'is_service_alive') hnm_utils.is_service_alive( mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(False) self.mock.StubOutWithMock(event_api, 'notify_host_update') event_api.notify_host_update( mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(Exception()) self.mock.ReplayAll() self.assertEquals(self.libvirtVmHost.processUpdates(), None) self.mock.stubs.UnsetAll()
def test_process_updates_for_updated_VM(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(api, 'vm_delete_by_ids') api.vm_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVM.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mock.ReplayAll() domainObj = libvirt.virDomain() self.assertEquals( self.libvirtVM.process_updates_for_updated_VM(domainObj), None) vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) # self.assertEquals("TestVirtMgrVM7", vm.get_name()) self.assertEquals("1048576", str(vm.get_memorySize())) self.assertEquals("hd", str(vm.get_bootOrder()).strip()) self.mock.stubs.UnsetAll()
def test_processVmForIPAddress(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVM.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mock.ReplayAll() self.assertEquals(self.libvirtVM._processVm(libvirt.virDomain()), None) self.libvirtVM.processUpdates() vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) ipProfileList = vm.get_ipAddresses() self.assertTrue(ipProfileList is not None) self.assertTrue(ipProfileList[0].get_ipAddress() == '10.1.1.19') self.assertTrue(ipProfileList[1].get_ipAddress() == '10.2.1.20')
def test_ProcessUpdatesException(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(api, 'vm_delete_by_ids') api.vm_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVM.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) mock_libvirtVm = LibvirtVM(self.connection, '1') self.mock.StubOutWithMock(mock_libvirtVm, 'processVmDeletes') mock_libvirtVm.processVmDeletes([], []).AndRaise(Exception) self.mock.ReplayAll() self.assertEquals(self.libvirtVM.processUpdates(), None) self.assertRaises(Exception, LibvirtVM) self.mock.stubs.UnsetAll()
def test_ProcessUpdatesException(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(api, 'vm_delete_by_ids') api.vm_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVM.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) mock_libvirtVm = LibvirtVM(self.connection, '1') self.mock.StubOutWithMock(mock_libvirtVm, 'processVmDeletes') mock_libvirtVm.processVmDeletes([], []).AndRaise(Exception) self.mock.ReplayAll() self.assertEquals(self.libvirtVM.processUpdates(), None) self.assertRaises(Exception, LibvirtVM) self.mock.stubs.UnsetAll()
def test_processVmForIPAddress(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVM.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mock.ReplayAll() self.assertEquals(self.libvirtVM._processVm(libvirt.virDomain()), None) self.libvirtVM.processUpdates() vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) ipProfileList = vm.get_ipAddresses() self.assertTrue(ipProfileList is not None) self.assertTrue(ipProfileList[0].get_ipAddress() == '10.1.1.19') self.assertTrue(ipProfileList[1].get_ipAddress() == '10.2.1.20')
def poll_perfmon(self, context): """ Periodically polls to refresh the performance data of VmHost and Vm in inventory """ LOG.info(_("Polling performance data periodically for Vmhosts and Vms")) def _worker(uuid, conn_driver, perfmon_type): conn_driver.update_perfdata(uuid, perfmon_type) for host_id in InventoryCacheManager.get_inventory_cache()[Constants.VmHost].keys(): conn_driver = InventoryCacheManager.get_compute_conn_driver(host_id, Constants.VmHost) if conn_driver is not None: self.perf_green_pool.spawn_n(_worker, host_id, conn_driver, Constants.VmHost) else: LOG.error(_("Error in monitoring performance data for Host %s ") % host_id) host_obj = InventoryCacheManager.get_inventory_cache()[Constants.VmHost][host_id] for vm_id in host_obj.get_virtualMachineIds(): vm_obj = InventoryCacheManager.get_object_from_cache(vm_id, Constants.Vm) if vm_obj.get_powerState() == Constants.VM_POWER_STATES[1]: conn_driver = InventoryCacheManager.get_compute_conn_driver(vm_id, Constants.Vm) if conn_driver is not None: self.perf_green_pool.spawn_n(_worker, vm_id, conn_driver, Constants.Vm) else: LOG.error( _( "Error in monitoring performance \ data for VM %s " ) % vm_id )
def test_host_disconnected_event(self): self.__mock_service_get_all_by_topic() backedUp_libvirt = connection.libvirt connection.libvirt = libvirt try: compute_id = '1' virtConnection = LibvirtConnection(False) vmHost = VmHost() vmHost.id = compute_id vmHost.set_virtualMachineIds([]) InventoryCacheManager.update_object_in_cache(compute_id, vmHost) # virtConnection.setUuid('34353438-3934-434e-3738-313630323543' # ) virtConnection._wrapped_conn = None virtConnection.compute_rmcontext = \ ComputeRMContext(rmType='KVM', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') cachedHost = VmHost() cachedHost.id = compute_id cachedHost.connectionState = Constants.VMHOST_CONNECTED self.mox.StubOutWithMock(InventoryCacheManager, 'get_object_from_cache') self.mox.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVmHost.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) InventoryCacheManager.get_object_from_cache( compute_id, Constants.VmHost).AndReturn(cachedHost) self.mox.StubOutWithMock(api, 'vm_host_save') api.vm_host_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.ReplayAll() libvirtEvents = LibvirtEvents() libvirtVmHost = LibvirtVmHost( virtConnection._wrapped_conn, compute_id, libvirtEvents) libvirtVmHost.processUpdates() self.assertEquals(libvirtVmHost.vmHost.get_connectionState(), Constants.VMHOST_DISCONNECTED) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.CRITICAL) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_HOST_DISCONNECTED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VmHost') self.assertEquals(payload['entity_id'], libvirtVmHost.compute_id) finally: connection.libvirt = backedUp_libvirt
def test_host_removed_event(self): self.__mock_service_get_all_by_topic() deleted_host = VmHost() deleted_host.set_id('compute1') deleted_host.set_name('compute1') self.mox.StubOutWithMock(api, 'vm_host_get_all') api.vm_host_get_all(mox.IgnoreArg()).AndReturn([deleted_host]) self.mox.StubOutWithMock(api, 'vm_get_all') api.vm_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'storage_volume_get_all') api.storage_volume_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'subnet_get_all') api.subnet_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(nova_db, 'compute_node_get_all') nova_db.compute_node_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'vm_host_delete_by_ids') api.vm_host_delete_by_ids( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( 'compute1', Constants.VmHost).AndReturn(fake.get_connection()) self.mox.ReplayAll() compute_service = dict(host='host1') compute = dict(id='compute1', hypervisor_type='fake', service=compute_service) rm_context = \ rmcontext.ComputeRMContext(rmType=compute['hypervisor_type'], rmIpAddress=compute_service['host'], rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory().clear() InventoryCacheManager.get_all_compute_inventory()['compute1'] = \ ComputeInventory(rm_context) InventoryCacheManager.get_compute_inventory( 'compute1').update_compute_info(rm_context, deleted_host) self.assertEquals( len(InventoryCacheManager.get_all_compute_inventory()), 1) inv_manager = InventoryManager() inv_manager._refresh_from_db(None) self.assertEquals( len(InventoryCacheManager.get_all_compute_inventory()), 0) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_HOST_REMOVED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VmHost') self.assertEquals(payload['entity_id'], deleted_host.id)
def test_host_removed_event(self): self.__mock_service_get_all_by_topic() deleted_host = VmHost() deleted_host.set_id('compute1') deleted_host.set_name('compute1') self.mox.StubOutWithMock(api, 'vm_host_get_all') api.vm_host_get_all(mox.IgnoreArg()).AndReturn([deleted_host]) self.mox.StubOutWithMock(api, 'vm_get_all') api.vm_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'storage_volume_get_all') api.storage_volume_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'subnet_get_all') api.subnet_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(nova_db, 'compute_node_get_all') nova_db.compute_node_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'vm_host_delete_by_ids') api.vm_host_delete_by_ids( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock(InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( 'compute1', Constants.VmHost).AndReturn(fake.get_connection()) self.mox.ReplayAll() compute_service = dict(host='host1') compute = dict(id='compute1', hypervisor_type='fake', service=compute_service) rm_context = \ rmcontext.ComputeRMContext(rmType=compute['hypervisor_type' ], rmIpAddress=compute_service['host'], rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory().clear() InventoryCacheManager.get_all_compute_inventory( )['compute1'] = ComputeInventory(rm_context) InventoryCacheManager.get_compute_inventory( 'compute1').update_compute_info(rm_context, deleted_host) self.assertEquals( len(InventoryCacheManager.get_all_compute_inventory()), 1) inv_manager = InventoryManager() inv_manager._refresh_from_db(None) self.assertEquals( len(InventoryCacheManager.get_all_compute_inventory()), 0) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_HOST_REMOVED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VmHost') self.assertEquals(payload['entity_id'], deleted_host.id)
def test_host_disconnected_event(self): self.__mock_service_get_all_by_topic() backedUp_libvirt = connection.libvirt connection.libvirt = libvirt try: compute_id = '1' virtConnection = LibvirtConnection(False) vmHost = VmHost() vmHost.id = compute_id vmHost.set_virtualMachineIds([]) InventoryCacheManager.update_object_in_cache(compute_id, vmHost) # virtConnection.setUuid('34353438-3934-434e-3738-313630323543' # ) virtConnection._wrapped_conn = None virtConnection.compute_rmcontext = \ ComputeRMContext(rmType='KVM', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') cachedHost = VmHost() cachedHost.id = compute_id cachedHost.connectionState = Constants.VMHOST_CONNECTED self.mox.StubOutWithMock(InventoryCacheManager, 'get_object_from_cache') self.mox.StubOutWithMock(InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVmHost.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) InventoryCacheManager.get_object_from_cache( compute_id, Constants.VmHost).AndReturn(cachedHost) self.mox.StubOutWithMock(api, 'vm_host_save') api.vm_host_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.ReplayAll() libvirtEvents = LibvirtEvents() libvirtVmHost = LibvirtVmHost(virtConnection._wrapped_conn, compute_id, libvirtEvents) libvirtVmHost.processUpdates() self.assertEquals(libvirtVmHost.vmHost.get_connectionState(), Constants.VMHOST_DISCONNECTED) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.CRITICAL) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_HOST_DISCONNECTED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VmHost') self.assertEquals(payload['entity_id'], libvirtVmHost.compute_id) finally: connection.libvirt = backedUp_libvirt
def test_host_removed_event_none_host(self): deleted_host = VmHost() deleted_host.set_id('compute1') deleted_host.set_name('compute1') self.mox.StubOutWithMock(api, 'vm_host_get_all') api.vm_host_get_all(mox.IgnoreArg()).AndReturn([deleted_host]) self.mox.StubOutWithMock(api, 'vm_get_all') api.vm_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'storage_volume_get_all') api.storage_volume_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'subnet_get_all') api.subnet_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(nova_db, 'compute_node_get_all') nova_db.compute_node_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'vm_host_delete_by_ids') api.vm_host_delete_by_ids( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( 'compute1', Constants.VmHost).AndReturn(fake.get_connection()) self.mox.ReplayAll() compute_service = dict(host='host1') compute = dict(id='compute1', hypervisor_type='fake', service=compute_service) rm_context = \ rmcontext.ComputeRMContext(rmType=compute['hypervisor_type'], rmIpAddress=compute_service['host'], rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory().clear() InventoryCacheManager.get_all_compute_inventory()['compute1'] = \ ComputeInventory(rm_context) InventoryCacheManager.get_compute_inventory( 'compute1').update_compute_info(rm_context, deleted_host) self.assertEquals( len(InventoryCacheManager.get_all_compute_inventory()), 1) InventoryCacheManager.get_inventory_cache( )[Constants.VmHost][deleted_host.get_id()] = None inv_manager = InventoryManager() inv_manager._refresh_from_db(None) self.assertEquals( len(InventoryCacheManager.get_all_compute_inventory()), 0) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1)
def test_host_removed_event_none_host(self): deleted_host = VmHost() deleted_host.set_id('compute1') deleted_host.set_name('compute1') self.mox.StubOutWithMock(api, 'vm_host_get_all') api.vm_host_get_all(mox.IgnoreArg()).AndReturn([deleted_host]) self.mox.StubOutWithMock(api, 'vm_get_all') api.vm_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'storage_volume_get_all') api.storage_volume_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'subnet_get_all') api.subnet_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(nova_db, 'compute_node_get_all') nova_db.compute_node_get_all(mox.IgnoreArg()).AndReturn([]) self.mox.StubOutWithMock(api, 'vm_host_delete_by_ids') api.vm_host_delete_by_ids( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock(InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( 'compute1', Constants.VmHost).AndReturn(fake.get_connection()) self.mox.ReplayAll() compute_service = dict(host='host1') compute = dict(id='compute1', hypervisor_type='fake', service=compute_service) rm_context = \ rmcontext.ComputeRMContext(rmType=compute['hypervisor_type' ], rmIpAddress=compute_service['host'], rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory().clear() InventoryCacheManager.get_all_compute_inventory( )['compute1'] = ComputeInventory(rm_context) InventoryCacheManager.get_compute_inventory( 'compute1').update_compute_info(rm_context, deleted_host) self.assertEquals( len(InventoryCacheManager.get_all_compute_inventory()), 1) InventoryCacheManager.get_inventory_cache()[Constants.VmHost][ deleted_host.get_id()] = None inv_manager = InventoryManager() inv_manager._refresh_from_db(None) self.assertEquals( len(InventoryCacheManager.get_all_compute_inventory()), 0) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1)
def testProcessUpdates(self): self.mock.StubOutWithMock(api, 'vm_host_save') api.vm_host_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVmHost.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mock.ReplayAll() self.assertEquals(self.libvirtVmHost.processUpdates(), None) self.assertEquals(self.libvirtVmHost.vmHost.get_connectionState(), 'Disconnected') self.mock.stubs.UnsetAll()
def get_resource_utilization(self, context, uuid, perfmon_type, window_minutes): """ Returns performance data of VMHost and VM via hypervisor connection driver """ return InventoryCacheManager.get_compute_conn_driver(uuid, perfmon_type).get_resource_utilization( uuid, perfmon_type, window_minutes )
def poll_perfmon(self, context): """ Periodically polls to refresh the performance data of VmHost and Vm in inventory """ LOG.info( _('Polling performance data periodically for Vmhosts and Vms')) def _worker(uuid, conn_driver, perfmon_type): conn_driver.update_perfdata(uuid, perfmon_type) for host_id in InventoryCacheManager.\ get_inventory_cache()[Constants.VmHost].keys(): conn_driver = InventoryCacheManager.get_compute_conn_driver( host_id, Constants.VmHost) if conn_driver is not None: self.perf_green_pool.spawn_n(_worker, host_id, conn_driver, Constants.VmHost) else: LOG.error(_('Error in monitoring performance data for Host %s ' ) % host_id) host_obj = InventoryCacheManager.get_inventory_cache( )[Constants.VmHost][host_id] for vm_id in host_obj.get_virtualMachineIds(): vm_obj = InventoryCacheManager.get_object_from_cache( vm_id, Constants.Vm) if vm_obj.get_powerState() \ == Constants.VM_POWER_STATES[1]: conn_driver = InventoryCacheManager.\ get_compute_conn_driver(vm_id, Constants.Vm) if conn_driver is not None: self.perf_green_pool.spawn_n(_worker, vm_id, conn_driver, Constants.Vm) else: LOG.error(_('Error in monitoring performance \ data for VM %s ') % vm_id)
def notify_host_update(event_type, vmHost, **kwargs): resource_utilization = InventoryCacheManager.get_compute_conn_driver( vmHost.get_id(), Constants.VmHost).get_resource_utilization(vmHost.get_id(), Constants.VmHost, 5) # update the host event payload with utilization data and notify vmHost.set_utilization(resource_utilization) LOG.audit(_('Host with (UUID, host name) - (%s, %s) got updated') % (vmHost.get_id(), vmHost.get_name())) notify(event_type, vmHost, **kwargs)
def notify_host_update(event_type, vmHost, **kwargs): resource_utilization = InventoryCacheManager.get_compute_conn_driver( vmHost.get_id(), Constants.VmHost).get_resource_utilization(vmHost.get_id(), Constants.VmHost, 5) # update the host event payload with utilization data and notify vmHost.set_utilization(resource_utilization) LOG.audit( _('Host with (UUID, host name) - (%s, %s) got updated') % (vmHost.get_id(), vmHost.get_name())) notify(event_type, vmHost, **kwargs)
def test_process_incomplete_vms_with_retry(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(api, 'vm_delete_by_ids') api.vm_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic(mox.IgnoreArg(), mox.IgnoreArg()).\ MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVM.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mock.StubOutWithMock( self.libvirtVM.libvirtconn, 'storageVolLookupByPath') self.libvirtVM.libvirtconn.storageVolLookupByPath( mox.IgnoreArg()).AndRaise(Exception) self.mock.ReplayAll() libvirt_inventorymonitor.incomplete_vms = \ {self.libvirtVM.compute_id: { '25f04dd3-e924-02b2-9eac-876e3c943262': 1}} self.libvirtVM.process_incomplete_vms() vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) self.assert_(not(vm.get_vmDisks()), "VM disks should not be collected") self.assert_( libvirt_inventorymonitor.incomplete_vms[self.libvirtVM.compute_id][ '25f04dd3-e924-02b2-9eac-876e3c943262'] == 2, "incomplete_vms retry count not incremented") self.mock.stubs.UnsetAll()
def test_process_incomplete_vms_deletedvm(self): self.mock.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(api, 'vm_delete_by_ids') api.vm_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic(mox.IgnoreArg(), mox.IgnoreArg())\ .MultipleTimes().AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver( self.libvirtVM.compute_id, Constants.VmHost).AndReturn(fake.get_connection()) self.mock.StubOutWithMock( self.libvirtVM.libvirtconn, 'listDefinedDomains') self.libvirtVM.libvirtconn.listDefinedDomains().AndReturn([]) self.mock.StubOutWithMock(self.libvirtVM.libvirtconn, 'listDomainsID') self.libvirtVM.libvirtconn.listDomainsID().AndReturn([]) self.mock.ReplayAll() libvirt_inventorymonitor.incomplete_vms = \ {self.libvirtVM.compute_id: { '25f04dd3-e924-02b2-9eac-876e3c943262': 1}} self.libvirtVM.process_incomplete_vms() self.assert_( '25f04dd3-e924-02b2-9eac-876e3c943262' not in libvirt_inventorymonitor.incomplete_vms[ self.libvirtVM.compute_id], "Deleted VM id not removed from incomplete list") self.mock.stubs.UnsetAll()
def test_processUpdates_hostupdate_event(self): defaultInstancesPath = cfg.CONF.instances_path cfg.CONF.set_override('instances_path', '/var/lib/libvirt/images') storagePool = libvirt.virStoragePool() self.mock.StubOutWithMock(api, 'storage_volume_save') api.storage_volume_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().\ AndReturn(None) cachedStorageVolume = StorageVolume() cachedStorageVolume.id = storagePool.UUIDString() cachedStorageVolume.size = 0 cachedStorageVolume.free = 0 cachedStorageVolume.connectionState = \ Constants.STORAGE_STATE_INACTIVE InventoryCacheManager.update_object_in_cache( '95f7101b-892c-c388-867a-8340e5fea27x', cachedStorageVolume) self.mock.StubOutWithMock(api, 'storage_volume_delete_by_ids') api.storage_volume_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().\ AndReturn(None) self.mock.StubOutWithMock( InventoryCacheManager, 'get_compute_conn_driver') InventoryCacheManager.get_compute_conn_driver(None, Constants.VmHost).\ AndReturn(fake. get_connection()) self.mock.ReplayAll() self.assertEquals(self.LibvirtStorageVolume.processUpdates(), None) self.assertEquals(self.LibvirtStorageVolume._createNovaPool(), None) cfg.CONF.set_override('instances_path', defaultInstancesPath) self.mock.stubs.UnsetAll()
def get_resource_utilization( self, context, uuid, perfmon_type, window_minutes, ): """ Returns performance data of VMHost and VM via hypervisor connection driver """ return InventoryCacheManager.get_compute_conn_driver( uuid, perfmon_type).get_resource_utilization(uuid, perfmon_type, window_minutes)