def test_processVm_disk_exception_next_retry(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(novadb, 'instance_get_by_uuid') novadb.instance_get_by_uuid(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn({'display_name': 'test_vm'}) self.mock.StubOutWithMock( self.libvirtVM.libvirtconn, 'storageVolLookupByPath') self.libvirtVM.libvirtconn.storageVolLookupByPath( mox.IgnoreArg()).AndRaise(Exception) self.mock.ReplayAll() InventoryCacheManager.delete_object_in_cache( '25f04dd3-e924-02b2-9eac-876e3c943262', Constants.Vm) libvirt_inventorymonitor.incomplete_vms = { self.libvirtVM.compute_id: {}} self.assertEquals(self.libvirtVM._processVm(libvirt.virDomain()), None) vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) self.assertEqual(len( vm.get_vmDisks()), 0, "Instance disk should not exist when there \ is an exception") self.mock.VerifyAll() self.mock.stubs.UnsetAll()
def test_delete_object_in_cache(self): self._createInvCache() vmhost = InventoryCacheManager.get_object_from_cache("vmhost1", Constants.VmHost) self.assertNotEquals(vmhost, None) InventoryCacheManager.delete_object_in_cache("vmhost1", Constants.VmHost) vmhost = InventoryCacheManager.get_object_from_cache("vmhost1", Constants.VmHost) self.assertEquals(vmhost, None) self.mox.UnsetStubs()
def testProcessUpdatesNoOp(self): self.mock.StubOutWithMock(api, 'vm_host_save') api.vm_host_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.ReplayAll() InventoryCacheManager.delete_object_in_cache('1', Constants.VmHost) self.assertEquals(self.libvirtVmHost.processUpdates(), None) self.assertEquals(self.libvirtVmHost.vmHost, None) self.mock.stubs.UnsetAll()
def test_delete_object_in_cache(self): self._createInvCache() vmhost = InventoryCacheManager.get_object_from_cache( 'vmhost1', Constants.VmHost) self.assertNotEquals(vmhost, None) InventoryCacheManager.delete_object_in_cache('vmhost1', Constants.VmHost) vmhost = InventoryCacheManager.get_object_from_cache( 'vmhost1', Constants.VmHost) self.assertEquals(vmhost, None) # self.assertEquals(len(self.inv_manager_cls._inventoryCache[Constants.VmHost]), 2) self.mox.UnsetStubs()
def test_delete_object_in_cache(self): self._createInvCache() vmhost = InventoryCacheManager.get_object_from_cache('vmhost1', Constants.VmHost) self.assertNotEquals(vmhost, None) InventoryCacheManager.delete_object_in_cache('vmhost1', Constants.VmHost) vmhost = InventoryCacheManager.get_object_from_cache('vmhost1', Constants.VmHost) self.assertEquals(vmhost, None) # self.assertEquals(len(self.inv_manager_cls._inventoryCache[Constants.VmHost]), 2) self.mox.UnsetStubs()
def testProcessUpdates_compute_stopped_none_cache(self): self.mock.StubOutWithMock( self.libvirtVmHost, '_get_compute_running_status') self.libvirtVmHost._get_compute_running_status().AndReturn(False) self.mock.ReplayAll() InventoryCacheManager.delete_object_in_cache( self.libvirtVmHost.compute_id, Constants.VmHost) self.assertEquals(self.libvirtVmHost.processUpdates(), None) vmHost = InventoryCacheManager.get_object_from_cache( self.libvirtVmHost.compute_id, Constants.VmHost) self.assertEqual( vmHost, None, "Failed \ testProcessUpdates_compute_stopped_none_cache.. \ vmHost exist in cache") self.mock.stubs.UnsetAll()
def _clean_deleted_computes(self, db_keys): keys = InventoryCacheManager.get_all_compute_inventory( ).keys() # since we're deleting deletion_list = [] for compute_id in keys: if compute_id not in db_keys: vmHostObj = InventoryCacheManager.get_all_compute_inventory()[ compute_id].get_compute_info() if vmHostObj is not None: deletion_list.append(vmHostObj.get_id()) host_deleted_list = [] if len(deletion_list) != 0: # Delete object from cache for _id in deletion_list: host_deleted = InventoryCacheManager.get_object_from_cache( _id, Constants.VmHost) if host_deleted is not None: host_deleted_list.append( InventoryCacheManager. get_object_from_cache(_id, Constants.VmHost)) else: LOG.warn(_( "VmHost object for id %s not found in cache") % _id) # Delete the VmHost from DB api.vm_host_delete_by_ids(get_admin_context(), deletion_list) # Generate the VmHost Removed Event for host_deleted in host_deleted_list: LOG.debug(_('Generating Host Removed event for the \ host id : %s') % str( host_deleted.get_id())) event_api.notify_host_update( event_metadata.EVENT_TYPE_HOST_REMOVED, host_deleted) # VmHost is deleted from compute inventory and inventory cache # after notifying the event del InventoryCacheManager.get_all_compute_inventory()[ host_deleted.get_id()] InventoryCacheManager.delete_object_in_cache( host_deleted.get_id(), Constants.VmHost) LOG.audit(_('Host with (UUID, host name) - (%s, %s) \ got removed') % ( host_deleted.get_id(), host_deleted.get_name()))
def _refresh_from_db(self, context): """Make our compute_node inventory map match the db.""" # Add/update existing compute_nodes ... computes = db.compute_node_get_all(context) existing = InventoryCacheManager.get_all_compute_inventory().keys() db_keys = [] for compute in computes: compute_id = str(compute["id"]) service = compute["service"] compute_hypervisor_type = compute["hypervisor_type"] LOG.debug(_(" Compute-id -> %s: Hypervisor Type -> %s") % (compute_id, compute_hypervisor_type)) if service is not None and compute_hypervisor_type == "QEMU": compute_alive = hnm_utils.is_service_alive(service["updated_at"], service["created_at"]) db_keys.append(compute_id) if not compute_alive: LOG.warn(_("Service %s for host %s is not active") % (service["binary"], service["host"])) continue if compute_id not in existing: rm_context = rmcontext.ComputeRMContext( rmType=compute["hypervisor_type"], rmIpAddress=service["host"], rmUserName="******", rmPassword="******", ) InventoryCacheManager.get_all_compute_inventory()[compute_id] = ComputeInventory(rm_context) LOG.audit(_("New Host with compute_id %s is obtained") % (compute_id)) InventoryCacheManager.get_all_compute_inventory()[compute_id].update_compute_Id(compute_id) else: LOG.warn(_(" No services entry found for compute id %s") % compute_id) # Cleanup compute_nodes removed from db ... keys = InventoryCacheManager.get_all_compute_inventory().keys() # since we're deleting deletion_list = [] for compute_id in keys: if compute_id not in db_keys: vmHostObj = InventoryCacheManager.get_all_compute_inventory()[compute_id].get_compute_info() if vmHostObj is not None: deletion_list.append(vmHostObj.get_id()) host_deleted_list = [] if len(deletion_list) != 0: # Delete object from cache for _id in deletion_list: host_deleted = InventoryCacheManager.get_object_from_cache(_id, Constants.VmHost) if host_deleted is not None: host_deleted_list.append(InventoryCacheManager.get_object_from_cache(_id, Constants.VmHost)) else: LOG.warn(_("VmHost object for id %s not found in cache") % _id) # Delete the VmHost from DB api.vm_host_delete_by_ids(get_admin_context(), deletion_list) # Generate the VmHost Removed Event for host_deleted in host_deleted_list: LOG.debug( _( "Generating Host Removed event \ for the host id : %s" ) % str(host_deleted.get_id()) ) event_api.notify_host_update(event_metadata.EVENT_TYPE_HOST_REMOVED, host_deleted) # VmHost is deleted from compute inventory and inventory # cache after notifying the event del InventoryCacheManager.get_all_compute_inventory()[host_deleted.get_id()] InventoryCacheManager.delete_object_in_cache(host_deleted.get_id(), Constants.VmHost) LOG.audit( _( "Host with (UUID, host name) \ - (%s, %s) got removed" ) % (host_deleted.get_id(), host_deleted.get_name()) )
def _refresh_from_db(self, context): """Make our compute_node inventory map match the db.""" # Add/update existing compute_nodes ... computes = db.compute_node_get_all(context) existing = InventoryCacheManager.get_all_compute_inventory().keys() db_keys = [] for compute in computes: compute_id = str(compute['id']) service = compute['service'] compute_hypervisor_type = compute['hypervisor_type'] LOG.debug(_(' Compute-id -> %s: Hypervisor Type -> %s') % (compute_id, compute_hypervisor_type)) if service is not None and compute_hypervisor_type == 'QEMU': compute_alive = hnm_utils.is_service_alive( service['updated_at'], service['created_at']) db_keys.append(compute_id) if not compute_alive: LOG.warn(_('Service %s for host %s is not active') % (service['binary'], service['host'])) continue if compute_id not in existing: rm_context = \ rmcontext.ComputeRMContext( rmType=compute['hypervisor_type'], rmIpAddress=service['host'], rmUserName='******', rmPassword='******') InventoryCacheManager.\ get_all_compute_inventory()[compute_id] =\ ComputeInventory(rm_context) LOG.audit(_('New Host with compute_id %s is obtained') % (compute_id)) InventoryCacheManager.get_all_compute_inventory( )[compute_id].update_compute_Id(compute_id) else: LOG.warn(_(' No services entry found for compute id %s') % compute_id) # Cleanup compute_nodes removed from db ... keys = InventoryCacheManager.get_all_compute_inventory( ).keys() # since we're deleting deletion_list = [] for compute_id in keys: if compute_id not in db_keys: vmHostObj = InventoryCacheManager.get_all_compute_inventory( )[compute_id].get_compute_info() if vmHostObj is not None: deletion_list.append(vmHostObj.get_id()) host_deleted_list = [] if len(deletion_list) != 0: # Delete object from cache for _id in deletion_list: host_deleted = InventoryCacheManager.get_object_from_cache( _id, Constants.VmHost) if host_deleted is not None: host_deleted_list.append( InventoryCacheManager.get_object_from_cache( _id, Constants.VmHost)) else: LOG.warn( _("VmHost object for id %s not found in cache") % _id) # Delete the VmHost from DB api.vm_host_delete_by_ids(get_admin_context(), deletion_list) # Generate the VmHost Removed Event for host_deleted in host_deleted_list: LOG.debug(_('Generating Host Removed event \ for the host id : %s') % str(host_deleted.get_id())) event_api.notify_host_update( event_metadata.EVENT_TYPE_HOST_REMOVED, host_deleted) # VmHost is deleted from compute inventory and inventory # cache after notifying the event del InventoryCacheManager.get_all_compute_inventory( )[host_deleted.get_id()] InventoryCacheManager.delete_object_in_cache( host_deleted.get_id(), Constants.VmHost) LOG.audit(_('Host with (UUID, host name) \ - (%s, %s) got removed') % (host_deleted.get_id(), host_deleted.get_name()))