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_poll_perfmon(self): self._createInvCache() compute = _create_Compute(compute_id='vmhost1') service = compute['service'] rm_context = \ rmcontext.ComputeRMContext(rmType=compute['hypervisor_type'], rmIpAddress=service['host'], rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory()['vmhost1'] = \ ComputeInventory(rm_context) InventoryCacheManager.get_all_compute_inventory()[ 'vmhost1'].get_compute_conn_driver() im = self.inv_manager self.mox.ReplayAll() im.poll_perfmon(None) self.assertTrue(im.perf_green_pool is not None) self.assertTrue( InventoryCacheManager.get_all_compute_inventory() is not None) eventlet.sleep(2) self.mox.VerifyAll() self.mox.UnsetStubs()
def test_storage_deleted_event(self): self.mox.StubOutWithMock(api, 'storage_volume_delete_by_ids') api.storage_volume_delete_by_ids( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock(InventoryCacheManager, 'get_object_from_cache') deleted_storage_id = '3fbfbefb-17dd-07aa-2dac-13afbedf3be3' deleted_storage = StorageVolume() deleted_storage.id = deleted_storage_id InventoryCacheManager.get_object_from_cache( deleted_storage_id, Constants.StorageVolume).AndReturn(deleted_storage) self.mox.ReplayAll() cachedList = [ deleted_storage_id, '3fbfbefb-17dd-07aa-2dac-13afbedf1234' ] updatedList = ['3fbfbefb-17dd-07aa-2dac-13afbedf1234'] self.mox.ReplayAll() self.LibvirtStorageVolume.processStorageDeletes( cachedList, updatedList) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.INFO) eventMetaData = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_STORAGE_DELETED) event_type = eventMetaData.get_event_fully_qal_name() self.assertEquals(msg['event_type'], event_type) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'StorageVolume') self.assertEquals(payload['entity_id'], deleted_storage_id)
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_storage_added_event(self): storagePool = libvirt.virStoragePool() self.mox.StubOutWithMock(api, 'storage_volume_save') api.storage_volume_save( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock(InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( storagePool.UUIDString(), Constants.StorageVolume).AndReturn(None) self.mox.ReplayAll() self.LibvirtStorageVolume._processStorage(storagePool) 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_STORAGE_ADDED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'StorageVolume') self.assertEquals(payload['entity_id'], storagePool.UUIDString())
def setUp(self): super(StorageVolumeEventsTest, self).setUp() self.connection = LibvirtConnection(False) vmHost = VmHost() vmHost.set_storageVolumeIds([]) InventoryCacheManager.update_object_in_cache('1', vmHost) self.connection._wrapped_conn = libvirt.open('qemu:///system') self.LibvirtStorageVolume = \ LibvirtStorageVolume(self.connection._wrapped_conn, '1') self.LibvirtStorageVolume.vmHost = vmHost self.LibvirtStorageVolume.cur_total_storage_size = 0 self.LibvirtStorageVolume.curr_storage_free = 0 self.LibvirtStorageVolume.old_total_storage_size = 0 self.LibvirtStorageVolume.old_storage_free = 0 self.LibvirtStorageVolume.vmHost.set_id('1') self.connection.compute_rmcontext = \ ComputeRMContext(rmType='KVM', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') self.flags( healthnmon_notification_drivers=['nova.notifier.test_notifier']) test_notifier.NOTIFICATIONS = [] self.mox.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None)
def test_storage_no_state_change(self): storagePool = libvirt.virStoragePool() self.mox.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_ACTIVE self.mox.StubOutWithMock(InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( storagePool.UUIDString(), Constants.StorageVolume).AndReturn(cachedStorageVolume) # self.mox.StubOutWithMock(InventoryCacheManager, 'get_compute_conn_driver') # # InventoryCacheManager.get_compute_conn_driver(self.LibvirtStorageVolume.compute_id, # Constants.VmHost).AndReturn(fake.get_connection()) self.mox.ReplayAll() nova_db.service_get_all_by_topic(None, None) self.LibvirtStorageVolume._processStorage(storagePool) self.assertEquals(len(test_notifier.NOTIFICATIONS), 0)
def test_processVmDeletes(self): 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_object_from_cache' ) deleted_vm_id = '25f04dd3-e924-02b2-9eac-876e3c943123' deleted_vm = Vm() deleted_vm.id = deleted_vm_id InventoryCacheManager.get_object_from_cache(deleted_vm_id, Constants.Vm).AndReturn(deleted_vm) self.mock.ReplayAll() cachedList = ['25f04dd3-e924-02b2-9eac-876e3c943262', deleted_vm_id] updatedList = ['25f04dd3-e924-02b2-9eac-876e3c943262'] self.assertEquals(self.libvirtVM.processVmDeletes(cachedList, updatedList), None) self.assertTrue(deleted_vm_id not in InventoryCacheManager.get_inventory_cache().keys()) self.mock.stubs.UnsetAll()
def skiptestProcessUpdates_network_stopped(self): vmHost = VmHost() vmHost.set_id('1') vmHost.set_connectionState(Constants.VMHOST_CONNECTED) InventoryCacheManager.update_object_in_cache('1', vmHost) self.mock.StubOutWithMock( self.libvirtVmHost, '_get_compute_running_status') self.libvirtVmHost._get_compute_running_status().AndReturn( (True, 'host')) self.mock.StubOutWithMock(api, 'vm_host_save') api.vm_host_save( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) fake_networks = { 'id': '1', 'created_at': 'created', 'updated_at': 'updated'} self.mock.StubOutWithMock(novadb, 'service_get_by_host_and_topic') novadb.service_get_by_host_and_topic( mox.IgnoreArg(), mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(fake_networks) 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()).AndReturn(None) self.mock.ReplayAll() self.assertEquals(self.libvirtVmHost.processUpdates(), None) self.assertEquals( self.libvirtVmHost.cachedvmHost.get_connectionState(), 'Disconnected') self.mock.stubs.UnsetAll()
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'] if service is not None: 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: self._add_compute_to_inventory(compute[ 'hypervisor_type'], compute_id, service['host']) 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 ... self._clean_deleted_computes(db_keys)
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 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_vm_deleted_event(self): self.mox.StubOutWithMock(api, 'vm_delete_by_ids') api.vm_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) deleted_vm_id = '25f04dd3-e924-02b2-9eac-876e3c943123' deleted_vm = Vm() deleted_vm.id = deleted_vm_id self.mox.StubOutWithMock(InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( deleted_vm_id, Constants.Vm).AndReturn(deleted_vm) self.mox.ReplayAll() cachedList = ['25f04dd3-e924-02b2-9eac-876e3c943262', deleted_vm_id] updatedList = ['25f04dd3-e924-02b2-9eac-876e3c943262'] self.libvirtVM.processVmDeletes(cachedList, updatedList) self.assertTrue(len(test_notifier.NOTIFICATIONS) == 1) msg = test_notifier.NOTIFICATIONS[0] self.assertTrue(msg is not None) self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_VM_DELETED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'Vm') self.assertEquals(payload['entity_id'], deleted_vm_id)
def test_processVmDeletes(self): 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_object_from_cache') deleted_vm_id = '25f04dd3-e924-02b2-9eac-876e3c943123' deleted_vm = Vm() deleted_vm.id = deleted_vm_id InventoryCacheManager.get_object_from_cache( deleted_vm_id, Constants.Vm).AndReturn(deleted_vm) self.mock.ReplayAll() cachedList = ['25f04dd3-e924-02b2-9eac-876e3c943262', deleted_vm_id] updatedList = ['25f04dd3-e924-02b2-9eac-876e3c943262'] self.assertEquals(self.libvirtVM.processVmDeletes(cachedList, updatedList), None) self.assertTrue(deleted_vm_id not in InventoryCacheManager.get_inventory_cache().keys()) self.mock.stubs.UnsetAll()
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_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 testProcessnetworkDeletes(self): self.mock.StubOutWithMock(api, 'subnet_delete_by_ids') api.subnet_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.ReplayAll() # cachedSwitchList = ['52:54:00:34:14:AC', # '52:54:00:34:14:AB', # '52:54:00:34:14:AE'] # updatedSwitchList = ['52:54:00:34:14:AE'] cachedSubnetList = \ ['Subnet_52:54:00:34:14:AF', 'Subnet_52:54:00:34:14:AG', 'Subnet_52:54:00:34:14:AE'] updatedSubnetList = \ ['Subnet_52:54:00:34:14:AE'] self.assertEquals(self.LibvirtNetwork._processNetworkDeletes( cachedSubnetList, updatedSubnetList), None) subnet = InventoryCacheManager.get_object_from_cache( 'Subnet_52:54:00:34:14:AE', Constants.Network) self.assertTrue(subnet is not None) subnet = InventoryCacheManager.get_object_from_cache( 'Subnet_52:54:00:34:14:AG', Constants.Network) self.assertTrue(subnet is None) 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 test_vm_stopped_event(self): mapped_tuple = self._mapLibvirtvmToVm() domainObj = mapped_tuple[0] cachedVm = mapped_tuple[1] cachedVm.powerState = Constants.VM_POWER_STATE_ACTIVE self.mox.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock( InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( domainObj.UUIDString(), Constants.Vm).AndReturn(cachedVm) self.mox.StubOutWithMock(domainObj, 'state') domainObj.state(0).AndReturn([5]) self.mox.ReplayAll() self.libvirtVM._processVm(domainObj) self.assertTrue(len(test_notifier.NOTIFICATIONS) > 0) msg = \ self._getEventMsgForEventType(event_metadata.EVENT_TYPE_VM_STOPPED, test_notifier.NOTIFICATIONS) self.assertTrue(msg is not None) self.assertEquals(msg['priority'], notifier_api.WARN) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_VM_STOPPED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'Vm') self.assertEquals(payload['entity_id'], domainObj.UUIDString()) self.assertEquals(payload['state'], Constants.VM_POWER_STATE_STOPPED)
def test_vm_created_event(self): domainObj = libvirt.virDomain() self.mox.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock( InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( domainObj.UUIDString(), Constants.Vm).AndReturn(None) self.mox.ReplayAll() self.libvirtVM._processVm(domainObj) 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_VM_CREATED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'Vm') self.assertEquals(payload['entity_id'], domainObj.UUIDString())
def test_storage_deleted_event(self): self.mox.StubOutWithMock(api, 'storage_volume_delete_by_ids') api.storage_volume_delete_by_ids( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock( InventoryCacheManager, 'get_object_from_cache') deleted_storage_id = '3fbfbefb-17dd-07aa-2dac-13afbedf3be3' deleted_storage = StorageVolume() deleted_storage.id = deleted_storage_id InventoryCacheManager.get_object_from_cache( deleted_storage_id, Constants.StorageVolume).AndReturn(deleted_storage) self.mox.ReplayAll() cachedList = [deleted_storage_id, '3fbfbefb-17dd-07aa-2dac-13afbedf1234'] updatedList = ['3fbfbefb-17dd-07aa-2dac-13afbedf1234'] self.mox.ReplayAll() self.LibvirtStorageVolume.processStorageDeletes(cachedList, updatedList) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.INFO) eventMetaData = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_STORAGE_DELETED) event_type = eventMetaData.get_event_fully_qal_name() self.assertEquals(msg['event_type'], event_type) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'StorageVolume') self.assertEquals(payload['entity_id'], deleted_storage_id)
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 setUp(self): super(StorageVolumeEventsTest, self).setUp() self.connection = LibvirtConnection(False) vmHost = VmHost() vmHost.set_storageVolumeIds([]) InventoryCacheManager.update_object_in_cache('1', vmHost) self.connection._wrapped_conn = libvirt.open('qemu:///system') self.LibvirtStorageVolume = \ LibvirtStorageVolume(self.connection._wrapped_conn, '1') self.LibvirtStorageVolume.vmHost = vmHost self.LibvirtStorageVolume.cur_total_storage_size = 0 self.LibvirtStorageVolume.curr_storage_free = 0 self.LibvirtStorageVolume.old_total_storage_size = 0 self.LibvirtStorageVolume.old_storage_free = 0 self.LibvirtStorageVolume.vmHost.set_id('1') self.connection.compute_rmcontext = \ ComputeRMContext(rmType='KVM', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') self.flags(healthnmon_notification_drivers=[ 'nova.notifier.test_notifier']) test_notifier.NOTIFICATIONS = [] self.mox.StubOutWithMock(nova_db, 'service_get_all_by_topic') nova_db.service_get_all_by_topic( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None)
def test_storage_added_event(self): storagePool = libvirt.virStoragePool() self.mox.StubOutWithMock(api, 'storage_volume_save') api.storage_volume_save( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock( InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( storagePool.UUIDString(), Constants.StorageVolume).AndReturn(None) self.mox.ReplayAll() self.LibvirtStorageVolume._processStorage(storagePool) 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_STORAGE_ADDED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'StorageVolume') self.assertEquals(payload['entity_id'], storagePool.UUIDString())
def test_vm_reconfigured_event(self): mapped_tuple = self._mapLibvirtvmToVm() domainObj = mapped_tuple[0] cachedVm = mapped_tuple[1] cachedVm.name = 'OldName' self.mox.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock( InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( domainObj.UUIDString(), Constants.Vm).AndReturn(cachedVm) self.mox.ReplayAll() self.libvirtVM._processVm(domainObj) 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_VM_RECONFIGURED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'Vm') self.assertEquals(payload['entity_id'], domainObj.UUIDString())
def test_storage_no_state_change(self): storagePool = libvirt.virStoragePool() self.mox.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_ACTIVE self.mox.StubOutWithMock( InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache(storagePool.UUIDString(), Constants.StorageVolume).AndReturn(cachedStorageVolume) # self.mox.StubOutWithMock(InventoryCacheManager, 'get_compute_conn_driver') # # InventoryCacheManager.get_compute_conn_driver(self.LibvirtStorageVolume.compute_id, # Constants.VmHost).AndReturn(fake.get_connection()) self.mox.ReplayAll() nova_db.service_get_all_by_topic(None, None) self.LibvirtStorageVolume._processStorage(storagePool) self.assertEquals(len(test_notifier.NOTIFICATIONS), 0)
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_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_vm_suspended_event(self): mapped_tuple = self._mapLibvirtvmToVm() domainObj = mapped_tuple[0] cachedVm = mapped_tuple[1] cachedVm.powerState = Constants.VM_POWER_STATE_ACTIVE self.mox.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock(InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( domainObj.UUIDString(), Constants.Vm).AndReturn(cachedVm) self.mox.StubOutWithMock(domainObj, 'state') domainObj.state(0).AndReturn([3]) self.mox.ReplayAll() self.libvirtVM._processVm(domainObj) self.assertTrue(len(test_notifier.NOTIFICATIONS) > 0) msg = \ self._getEventMsgForEventType( event_metadata.EVENT_TYPE_VM_SUSPENDED, test_notifier.NOTIFICATIONS) self.assertTrue(msg is not None) self.assertEquals(msg['priority'], notifier_api.WARN) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_VM_SUSPENDED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'Vm') self.assertEquals(payload['entity_id'], domainObj.UUIDString()) self.assertEquals(payload['state'], Constants.VM_POWER_STATE_PAUSED)
def test_poll_perfmon(self): self._createInvCache() compute = _create_Compute(compute_id='vmhost1') service = compute['service'] rm_context = \ rmcontext.ComputeRMContext(rmType=compute['hypervisor_type' ], rmIpAddress=service['host'], rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory()['vmhost1'] = \ ComputeInventory(rm_context) InventoryCacheManager.get_all_compute_inventory( )['vmhost1'].get_compute_conn_driver() im = self.inv_manager self.mox.ReplayAll() im.poll_perfmon(None) self.assertTrue(im.perf_green_pool is not None) self.assertTrue( InventoryCacheManager.get_all_compute_inventory() is not None) eventlet.sleep(2) self.mox.VerifyAll() self.mox.UnsetStubs()
def test_vm_deleted_event(self): self.mox.StubOutWithMock(api, 'vm_delete_by_ids') api.vm_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) deleted_vm_id = '25f04dd3-e924-02b2-9eac-876e3c943123' deleted_vm = Vm() deleted_vm.id = deleted_vm_id self.mox.StubOutWithMock( InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( deleted_vm_id, Constants.Vm).AndReturn(deleted_vm) self.mox.ReplayAll() cachedList = ['25f04dd3-e924-02b2-9eac-876e3c943262', deleted_vm_id] updatedList = ['25f04dd3-e924-02b2-9eac-876e3c943262'] self.libvirtVM.processVmDeletes(cachedList, updatedList) self.assertTrue(len(test_notifier.NOTIFICATIONS) == 1) msg = test_notifier.NOTIFICATIONS[0] self.assertTrue(msg is not None) self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_VM_DELETED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'Vm') self.assertEquals(payload['entity_id'], deleted_vm_id)
def setUp(self): self.mock = mox.Mox() self.connection = LibvirtConnection(False) vmHost = VmHost() vmHost.set_virtualMachineIds([]) InventoryCacheManager.update_object_in_cache('1', vmHost) self.connection._wrapped_conn = None self.connection.compute_rmcontext = \ ComputeRMContext(rmType='fake', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory()['1'] = \ ComputeInventory(self.connection.compute_rmcontext) self.mock.StubOutWithMock(LibvirtConnection, '_connect') self.connection._connect( mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(libvirt.libvirtError) self.mock.ReplayAll() self.inventoryMonitor = LibvirtInventoryMonitor() libvirtEvents = LibvirtEvents() self.libvirtVmHost = LibvirtVmHost( self.connection._wrapped_conn, '1', libvirtEvents) cfg.CONF.set_override('healthnmon_notification_drivers', ['healthnmon.notifier.log_notifier'])
def test_vm_reconfigured_event(self): mapped_tuple = self._mapLibvirtvmToVm() domainObj = mapped_tuple[0] cachedVm = mapped_tuple[1] cachedVm.name = 'OldName' self.mox.StubOutWithMock(api, 'vm_save') api.vm_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mox.StubOutWithMock(InventoryCacheManager, 'get_object_from_cache') InventoryCacheManager.get_object_from_cache( domainObj.UUIDString(), Constants.Vm).AndReturn(cachedVm) self.mox.ReplayAll() self.libvirtVM._processVm(domainObj) 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_VM_RECONFIGURED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'Vm') self.assertEquals(payload['entity_id'], domainObj.UUIDString())
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 testProcessnetworkDeletes(self): self.mock.StubOutWithMock(api, 'subnet_delete_by_ids') api.subnet_delete_by_ids( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.ReplayAll() # cachedSwitchList = ['52:54:00:34:14:AC', # '52:54:00:34:14:AB', # '52:54:00:34:14:AE'] # updatedSwitchList = ['52:54:00:34:14:AE'] cachedSubnetList = \ ['Subnet_52:54:00:34:14:AF', 'Subnet_52:54:00:34:14:AG', 'Subnet_52:54:00:34:14:AE'] updatedSubnetList = \ ['Subnet_52:54:00:34:14:AE'] self.assertEquals(self.LibvirtNetwork._processNetworkDeletes( cachedSubnetList, updatedSubnetList), None) subnet = InventoryCacheManager.get_object_from_cache( 'Subnet_52:54:00:34:14:AE', Constants.Network) self.assertTrue(subnet is not None) subnet = InventoryCacheManager.get_object_from_cache( 'Subnet_52:54:00:34:14:AG', Constants.Network) self.assertTrue(subnet is 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.mox.UnsetStubs()
def create_compute_inventory(self): compute_inventory = ComputeInventory(self.connection.compute_rmcontext) InventoryCacheManager.get_all_compute_inventory( )[self.vmhost_id] = compute_inventory InventoryCacheManager.get_all_compute_inventory( )[self.vm_id] = compute_inventory self.mox.StubOutWithMock(ComputeInventory, 'get_compute_conn_driver') compute_inventory.get_compute_conn_driver().AndReturn(self.connection) self.mox.ReplayAll()
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 create_compute_inventory(self): compute_inventory = ComputeInventory(self.connection.compute_rmcontext) InventoryCacheManager.get_all_compute_inventory()[ self.vmhost_id] = compute_inventory InventoryCacheManager.get_all_compute_inventory()[ self.vm_id] = compute_inventory self.mox.StubOutWithMock(ComputeInventory, 'get_compute_conn_driver') compute_inventory.get_compute_conn_driver().AndReturn(self.connection) self.mox.ReplayAll()
def test_updateInventory(self): self._createInvCache() im = self.inv_manager im._updateInventory(None) self.mox.ReplayAll() eventlet.sleep(2) self.mox.VerifyAll() host = InventoryCacheManager.get_object_from_cache("vmhost1", Constants.VmHost) self.assertTrue(InventoryCacheManager.get_object_from_cache("vmhost1", Constants.VmHost) is not None) self.assertEquals("vmhost1", host.get_id()) self.mox.UnsetStubs()
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_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 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 setUp(self): self.fakeConn = libvirt.open('qemu:///system') self.libvirt_connection_cls = connection.LibvirtConnection super(Test_virt_connection, self).setUp() self.flags( healthnmon_notification_drivers=[ 'healthnmon.notifier.log_notifier'] ) vmHost = VmHost() vmHost.id = '1' vmHost.uuid = '1' InventoryCacheManager.update_object_in_cache('1', vmHost)
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 setUp(self): self.connection = LibvirtConnection(False) vmHost = VmHost() vmHost.set_storageVolumeIds([]) InventoryCacheManager.update_object_in_cache('1', vmHost) self.connection._wrapped_conn = libvirt.open('qemu:///system') self.LibvirtStorageVolume = \ LibvirtStorageVolume(self.connection._wrapped_conn, '1') self.connection.compute_rmcontext = \ ComputeRMContext(rmType='KVM', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') self.mock = mox.Mox()
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_updateInventory(self): self._createInvCache() im = self.inv_manager im._updateInventory(None) self.mox.ReplayAll() eventlet.sleep(2) self.mox.VerifyAll() host = InventoryCacheManager.get_object_from_cache( 'vmhost1', Constants.VmHost) self.assertTrue( InventoryCacheManager.get_object_from_cache( 'vmhost1', Constants.VmHost) is not None) self.assertEquals('vmhost1', host.get_id()) self.mox.UnsetStubs()
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 test_processUpdates(self): self.mock.StubOutWithMock(api, 'vm_host_save') self.mock.StubOutWithMock(api, 'subnet_delete_by_ids') self.mock.StubOutWithMock(api, 'subnet_save') api.vm_host_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) api.subnet_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) api.subnet_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.ReplayAll() self.assertEquals(self.LibvirtNetwork.processUpdates(), None) host = InventoryCacheManager.get_object_from_cache( '1', Constants.VmHost) self.assertEquals( '52:54:00:34:14:AE', host.get_virtualSwitches()[0].get_id()) self.assertEquals( 'nat', host.get_virtualSwitches()[0].get_switchType()) self.assertEquals('default', host.get_virtualSwitches()[0].get_name()) self.mock.stubs.UnsetAll()
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 _update_hostmemory_stats(self): memstats = {} ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) conn_driver = InventoryCacheManager.get_compute_inventory( self.uuid).get_compute_conn_driver() ssh.connect(conn_driver.compute_rmcontext.rmIpAddress, username=conn_driver.compute_rmcontext.rmUserName, password=conn_driver.compute_rmcontext.rmPassword) LOG.debug(_('Connected to host using paramiko')) (stdin, stdout, stderr) = ssh.exec_command('virsh nodememstats') type(stdin) error = stderr.readlines() ssh.close() if len(error) == 0: nodememstats = stdout.readlines() for memstat in nodememstats: memstatList = memstat.split() if len(memstatList) > 0: memstats[memstatList[0]] = memstatList[len(memstatList) - 2] self.temp_stats.totalMemory = int(memstats['total']) self.temp_stats.freeMemory = int(memstats['free']) + int( memstats['buffers:']) + int(memstats['cached']) LOG.debug( _('Total Memory:' + str(self.temp_stats.totalMemory) + ', Free Memory:' + str(self.temp_stats.freeMemory))) else: LOG.debug( _('Error occurred in connecting vmhost via ssh %s') % error) self.temp_stats.status = -1
def test_getObjectFromCache(self): self._createInvCache() vmhost = InventoryCacheManager.get_object_from_cache( 'vmhost1', Constants.VmHost) self.assertNotEquals(vmhost, None) self.assertEquals('vmhost1', vmhost.get_id()) self.assertTrue('vm1' in vmhost.get_virtualMachineIds()) self.mox.UnsetStubs()
def test_get_compute_list(self): self._createInvCache() compute = _create_Compute(compute_id='compute1') service = compute['service'] rm_context = \ rmcontext.ComputeRMContext(rmType=compute['hypervisor_type' ], rmIpAddress=service['host'], rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory().clear() InventoryCacheManager.get_all_compute_inventory()['compute1'] = \ ComputeInventory(rm_context) InventoryCacheManager.get_all_compute_inventory( )['compute1'].update_compute_info(rm_context, compute) compute_info = self.inv_manager.get_compute_list() self.assertEquals(compute_info[0]['hypervisor_type'], 'fake') self.mox.UnsetStubs()
def setUp(self): self.connection = LibvirtConnection(False) self.vmHost = VmHost() self.vmHost.set_virtualMachineIds([]) InventoryCacheManager.update_object_in_cache('1', self.vmHost) #self.connection.setUuid('34353438-3934-434e-3738-313630323543') self.connection._wrapped_conn = libvirt.open('qemu:///system') self.libvirtVM = LibvirtVM(self.connection._wrapped_conn, '1') self.libvirtVM.vmHost.set_id('1') self.connection.compute_rmcontext = \ ComputeRMContext(rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') self.mock = mox.Mox() cfg.CONF.set_override('healthnmon_notification_drivers', ['healthnmon.notifier.log_notifier'])