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()
class VmEventsTest(test.TestCase): ''' TestCase for VM Events ''' def _mapLibvirtvmToVm(self): '''Create a libvirt Domain object and map it to a healthmon Vm object for it ''' domainObj = libvirt.virDomain() self.libvirtVM.domainObj = domainObj self.libvirtVM.domainUuid = domainObj.UUIDString() self.libvirtVM.Vm = Vm() self.libvirtVM._mapVmProperties() return (domainObj, self.libvirtVM.Vm) def _getEventMsgForEventType(self, event_type, notifications): '''Get the notification message corresponding to a even_type from a list of notifications Parameters: event_type - One of the event_metadata event_type name notifications - A list of notification messages ''' metadata = event_metadata.get_EventMetaData(event_type) event_type_name = metadata.get_event_fully_qal_name() for msg in notifications: if msg['event_type'] == event_type_name: return msg return msg def setUp(self): super(VmEventsTest, self).setUp() self.connection = LibvirtConnection(False) vmHost = VmHost() vmHost.set_virtualMachineIds([]) InventoryCacheManager.update_object_in_cache('1', vmHost) self.connection._wrapped_conn = libvirt.open('qemu:///system') self.libvirtVM = LibvirtVM(self.connection._wrapped_conn, '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_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_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_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_vm_started_event(self): mapped_tuple = self._mapLibvirtvmToVm() domainObj = mapped_tuple[0] cachedVm = mapped_tuple[1] cachedVm.powerState = Constants.VM_POWER_STATE_STOPPED 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.assertTrue(len(test_notifier.NOTIFICATIONS) > 0) msg = \ self._getEventMsgForEventType(event_metadata.EVENT_TYPE_VM_STARTED, test_notifier.NOTIFICATIONS) self.assertTrue(msg is not None) self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_VM_STARTED) 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_ACTIVE) def test_vm_resumed_event(self): mapped_tuple = self._mapLibvirtvmToVm() domainObj = mapped_tuple[0] cachedVm = mapped_tuple[1] cachedVm.powerState = Constants.VM_POWER_STATE_PAUSED 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.assertTrue(len(test_notifier.NOTIFICATIONS) > 0) msg = \ self._getEventMsgForEventType(event_metadata.EVENT_TYPE_VM_RESUMED, test_notifier.NOTIFICATIONS) self.assertTrue(msg is not None) self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_VM_RESUMED) 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_ACTIVE) def test_vm_shutdown_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([4]) self.mox.ReplayAll() self.libvirtVM._processVm(domainObj) self.assertTrue(len(test_notifier.NOTIFICATIONS) > 0) msg = \ self._getEventMsgForEventType( event_metadata.EVENT_TYPE_VM_SHUTDOWN, 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_SHUTDOWN) 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_SHUTDOWN) 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_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)
class VmEventsTest(test.TestCase): ''' TestCase for VM Events ''' def _mapLibvirtvmToVm(self): '''Create a libvirt Domain object and map it to a healthmon Vm object for it ''' domainObj = libvirt.virDomain() self.libvirtVM.domainObj = domainObj self.libvirtVM.domainUuid = domainObj.UUIDString() self.libvirtVM.Vm = Vm() self.libvirtVM._mapVmProperties() return (domainObj, self.libvirtVM.Vm) def _getEventMsgForEventType(self, event_type, notifications): '''Get the notification message corresponding to a even_type from a list of notifications Parameters: event_type - One of the event_metadata event_type name notifications - A list of notification messages ''' metadata = event_metadata.get_EventMetaData(event_type) event_type_name = metadata.get_event_fully_qal_name() for msg in notifications: if msg['event_type'] == event_type_name: return msg return msg def setUp(self): super(VmEventsTest, self).setUp() self.connection = LibvirtConnection(False) vmHost = VmHost() vmHost.set_virtualMachineIds([]) InventoryCacheManager.update_object_in_cache('1', vmHost) self.connection._wrapped_conn = libvirt.open('qemu:///system') self.libvirtVM = LibvirtVM(self.connection._wrapped_conn, '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_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_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_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_vm_started_event(self): mapped_tuple = self._mapLibvirtvmToVm() domainObj = mapped_tuple[0] cachedVm = mapped_tuple[1] cachedVm.powerState = Constants.VM_POWER_STATE_STOPPED 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.assertTrue(len(test_notifier.NOTIFICATIONS) > 0) msg = \ self._getEventMsgForEventType(event_metadata.EVENT_TYPE_VM_STARTED, test_notifier.NOTIFICATIONS) self.assertTrue(msg is not None) self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_VM_STARTED) 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_ACTIVE) def test_vm_resumed_event(self): mapped_tuple = self._mapLibvirtvmToVm() domainObj = mapped_tuple[0] cachedVm = mapped_tuple[1] cachedVm.powerState = Constants.VM_POWER_STATE_PAUSED 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.assertTrue(len(test_notifier.NOTIFICATIONS) > 0) msg = \ self._getEventMsgForEventType(event_metadata.EVENT_TYPE_VM_RESUMED, test_notifier.NOTIFICATIONS) self.assertTrue(msg is not None) self.assertEquals(msg['priority'], notifier_api.INFO) event_type = \ event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_VM_RESUMED) 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_ACTIVE) def test_vm_shutdown_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([4]) self.mox.ReplayAll() self.libvirtVM._processVm(domainObj) self.assertTrue(len(test_notifier.NOTIFICATIONS) > 0) msg = \ self._getEventMsgForEventType( event_metadata.EVENT_TYPE_VM_SHUTDOWN, 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_SHUTDOWN) 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_SHUTDOWN) 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_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)
class test_LibvirtVM(unittest.TestCase): 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']) def tearDown(self): cfg.CONF.set_override('healthnmon_notification_drivers', None) self.mock.stubs.UnsetAll() def test_ProcessUpdates(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() self.assertEquals(self.libvirtVM.processUpdates(), 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_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_updates_for_updated_VM_exception(self): domainObj = libvirt.virDomain() self.libvirtVM.vmHost = None self.libvirtVM.process_updates_for_updated_VM(domainObj) self.assertRaises(Exception, LibvirtVM) self.mock.stubs.UnsetAll() self.mock.VerifyAll() 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_processVm(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.ReplayAll() self.assertEquals(self.libvirtVM._processVm(libvirt.virDomain()), None) vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) #self.assertEquals('Disconnected', vm.get_connectionState()) # self.assertEquals('TestVirtMgrVM7', str(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_processVmException(self): self.assertEquals(self.libvirtVM._processVm(libvirt.virStorageVol()), None) 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()
class test_LibvirtVM(test.TestCase): def setUp(self): super(test_LibvirtVM, self).setUp() 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']) def tearDown(self): cfg.CONF.set_override('healthnmon_notification_drivers', None) super(test_LibvirtVM, self).tearDown() def test_ProcessUpdates(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() self.assertEquals(self.libvirtVM.processUpdates(), 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 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_max_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': 5}} 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'] == 5, "incomplete_vms retry count should not be 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_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_updates_for_updated_VM_exception(self): domainObj = libvirt.virDomain() self.libvirtVM.vmHost = None self.libvirtVM.process_updates_for_updated_VM(domainObj) self.assertRaises(Exception, LibvirtVM) self.mock.stubs.UnsetAll() self.mock.VerifyAll() 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(LibvirtVM, 'processVmDeletes') mock_libvirtVm.processVmDeletes( mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(Exception) self.mock.ReplayAll() self.assertEquals(self.libvirtVM.processUpdates(), None) self.assertRaises(Exception, LibvirtVM) self.mock.stubs.UnsetAll() def test_processVm(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.ReplayAll() self.assertEquals(self.libvirtVM._processVm(libvirt.virDomain()), None) vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) # self.assertEquals('Disconnected', vm.get_connectionState()) # self.assertEquals('TestVirtMgrVM7', str(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_processVmException(self): self.assertEquals(self.libvirtVM._processVm(libvirt.virStorageVol()), None) 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_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.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_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()
class test_LibvirtVM(unittest.TestCase): 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']) def tearDown(self): cfg.CONF.set_override('healthnmon_notification_drivers', None) self.mock.stubs.UnsetAll() def test_ProcessUpdates(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() self.assertEquals(self.libvirtVM.processUpdates(), 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_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_updates_for_updated_VM_exception(self): domainObj = libvirt.virDomain() self.libvirtVM.vmHost = None self.libvirtVM.process_updates_for_updated_VM(domainObj) self.assertRaises(Exception, LibvirtVM) self.mock.stubs.UnsetAll() self.mock.VerifyAll() 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_processVm(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.ReplayAll() self.assertEquals(self.libvirtVM._processVm(libvirt.virDomain()), None) vm = InventoryCacheManager.get_object_from_cache( "25f04dd3-e924-02b2-9eac-876e3c943262", Constants.Vm) #self.assertEquals('Disconnected', vm.get_connectionState()) # self.assertEquals('TestVirtMgrVM7', str(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_processVmException(self): self.assertEquals(self.libvirtVM._processVm(libvirt.virStorageVol()), None) 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()