def setUp(self): super(NetworkEventsTest, self).setUp() self.connection = LibvirtConnection(False) self.connection._wrapped_conn = libvirt.open("qemu:///system") rm_context = ComputeRMContext(rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory()['1'] = \ ComputeInventory(rm_context) self.libvirtNetwork = LibvirtNetwork(self.connection._wrapped_conn, '1') self.flags( healthnmon_notification_drivers=['nova.notifier.test_notifier']) test_notifier.NOTIFICATIONS = []
def setUp(self): self.connection = LibvirtConnection(False) vmHost = VmHost() vSwitch = VirtualSwitch() vSwitch.set_id('52:54:00:34:14:AE') vSwitch.set_name('default') vSwitch.set_switchType('nat') vmHost.set_virtualSwitches([vSwitch]) InventoryCacheManager.update_object_in_cache('1', vmHost) #self.connection.setUuid('34353438-3934-434e-3738-313630323543') self.connection._wrapped_conn = libvirt.open('qemu:///system') self.connection.compute_rmcontext = \ ComputeRMContext(rmType='KVM', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') self.LibvirtNetwork = LibvirtNetwork(self.connection, '1') self.mock = mox.Mox() cfg.CONF.set_override('healthnmon_notification_drivers', ['healthnmon.notifier.log_notifier'])
def setUp(self): super(NetworkEventsTest, self).setUp() self.connection = LibvirtConnection(False) self.connection._wrapped_conn = libvirt.open("qemu:///system") rm_context = ComputeRMContext( rmType="QEMU", rmIpAddress="10.10.155.165", rmUserName="******", rmPassword="******" ) InventoryCacheManager.get_all_compute_inventory()["1"] = ComputeInventory(rm_context) self.libvirtNetwork = LibvirtNetwork(self.connection._wrapped_conn, "1") self.flags(healthnmon_notification_drivers=["nova.notifier.test_notifier"]) test_notifier.NOTIFICATIONS = []
def test_processUpdatesException(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) mock_libvirtNetwork = LibvirtNetwork(self.connection, '1') self.mock.StubOutWithMock( mock_libvirtNetwork, '_processNetworkDeletes') mock_libvirtNetwork._processNetworkDeletes([], [],).AndRaise(Exception) self.mock.ReplayAll() self.assertEquals(self.LibvirtNetwork.processUpdates(), None) self.assertRaises(Exception, LibvirtNetwork) self.mock.stubs.UnsetAll()
def setUp(self): super(NetworkEventsTest, self).setUp() self.connection = LibvirtConnection(False) self.connection._wrapped_conn = libvirt.open("qemu:///system") rm_context = ComputeRMContext( rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory()['1'] = \ ComputeInventory(rm_context) self.libvirtNetwork = LibvirtNetwork( self.connection._wrapped_conn, '1') self.flags( healthnmon_notification_drivers=['nova.notifier.test_notifier']) test_notifier.NOTIFICATIONS = []
class NetworkEventsTest(test.TestCase): def setUp(self): super(NetworkEventsTest, self).setUp() self.connection = LibvirtConnection(False) self.connection._wrapped_conn = libvirt.open("qemu:///system") rm_context = ComputeRMContext( rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory()['1'] = \ ComputeInventory(rm_context) self.libvirtNetwork = LibvirtNetwork( self.connection._wrapped_conn, '1') self.flags( healthnmon_notification_drivers=['nova.notifier.test_notifier']) test_notifier.NOTIFICATIONS = [] def test_no_event_generated(self): vmhost = VmHost() vmhost.id = self.libvirtNetwork.compute_id self.libvirtNetwork._processNetworkEvents(None, vmhost) self.assertEquals(len(test_notifier.NOTIFICATIONS), 0) def test_network_added_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vmhost = VmHost() vmhost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") vmhost.set_virtualSwitches([vswitch]) self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) 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_NETWORK_ADDED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VirtualSwitch') self.assertEquals(payload['entity_id'], vswitch.get_id()) def test_network_deleted_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") cachedHost.set_virtualSwitches([vswitch]) vmhost = copy.deepcopy(cachedHost) vmhost.get_virtualSwitches().pop() self.assertEquals(vmhost.get_virtualSwitches(), []) self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) 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_NETWORK_DELETED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VirtualSwitch') self.assertEquals(payload['entity_id'], vswitch.get_id()) def test_network_enabled_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") vswitch.set_connectionState("Inactive") cachedHost.set_virtualSwitches([vswitch]) vmhost = copy.deepcopy(cachedHost) vmhost.get_virtualSwitches()[0].set_connectionState("Active") self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) self.assertEquals(vmhost.get_virtualSwitches()[0]. get_connectionState(), Constants.VIRSWITCH_STATE_ACTIVE) 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_NETWORK_ENABLED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VirtualSwitch') self.assertEquals(payload['entity_id'], vswitch.get_id()) self.assertEquals(payload["state"], 'Active') def test_network_disabled_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") vswitch.set_connectionState("Active") cachedHost.set_virtualSwitches([vswitch]) vmhost = copy.deepcopy(cachedHost) vmhost.get_virtualSwitches()[0].set_connectionState("Inactive") self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) self.assertEquals(vmhost.get_virtualSwitches()[0]. get_connectionState(), Constants.VIRSWITCH_STATE_INACTIVE) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.WARN) event_type = event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_NETWORK_DISABLED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VirtualSwitch') self.assertEquals(payload['entity_id'], vswitch.get_id()) self.assertEquals(payload["state"], 'Inactive') def test_portGroup_added_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vmhost = VmHost() vmhost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") portGroup = PortGroup() portGroup.set_id("PortGroup_" + vswitch.get_id()) portGroup.set_name(vswitch.get_name()) portGroup.set_virtualSwitchId(vswitch.get_id()) vswitch.set_portGroups([portGroup]) vmhost.set_virtualSwitches([vswitch]) vmhost.set_portGroups([portGroup]) self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) self.assertEquals(len(test_notifier.NOTIFICATIONS), 2) msg = test_notifier.NOTIFICATIONS[1] self.assertEquals(msg['priority'], notifier_api.INFO) event_type = event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_PORTGROUP_ADDED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'PortGroup') self.assertEquals(payload['entity_id'], portGroup.get_id()) def test_portGroup_deleted_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") portGroup = PortGroup() portGroup.set_id("PortGroup_" + vswitch.get_id()) portGroup.set_name(vswitch.get_name()) portGroup.set_virtualSwitchId(vswitch.get_id()) vswitch.set_portGroups([portGroup]) cachedHost.set_virtualSwitches([vswitch]) cachedHost.set_portGroups([portGroup]) vmhost = copy.deepcopy(cachedHost) vmhost.get_portGroups().pop() vmhost.get_virtualSwitches().pop() self.assertEquals(vmhost.get_virtualSwitches(), []) self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) self.assertEquals(len(test_notifier.NOTIFICATIONS), 2) msg = test_notifier.NOTIFICATIONS[1] self.assertEquals(msg['priority'], notifier_api.INFO) event_type = event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_PORTGROUP_DELETED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'PortGroup') self.assertEquals(payload['entity_id'], portGroup.get_id()) def test_PortGroup_Reconfigured_Event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") portGroup = PortGroup() portGroup.set_id("PortGroup_" + vswitch.get_id()) portGroup.set_name(vswitch.get_name()) portGroup.set_virtualSwitchId(vswitch.get_id()) vswitch.set_portGroups([portGroup]) cachedHost.set_virtualSwitches([vswitch]) cachedHost.set_portGroups([portGroup]) vmhost = copy.deepcopy(cachedHost) vmhost.get_portGroups()[0].set_name("vs11") vmhost.get_virtualSwitches()[0].set_name("vs11") vmhost.get_virtualSwitches()[0].get_portGroups()[0].set_name("vs11") self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) 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_PORTGROUP_RECONFIGURED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'PortGroup') self.assertEquals(payload['entity_id'], portGroup.get_id())
class test_LibvirtNetwork(unittest.TestCase): def setUp(self): self.connection = LibvirtConnection(False) vmHost = VmHost() vSwitch = VirtualSwitch() vSwitch.set_id('52:54:00:34:14:AE') vSwitch.set_name('default') vSwitch.set_switchType('nat') vmHost.set_virtualSwitches([vSwitch]) InventoryCacheManager.update_object_in_cache('1', vmHost) #self.connection.setUuid('34353438-3934-434e-3738-313630323543') self.connection._wrapped_conn = libvirt.open('qemu:///system') self.connection.compute_rmcontext = \ ComputeRMContext(rmType='KVM', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') self.LibvirtNetwork = LibvirtNetwork(self.connection, '1') self.mock = mox.Mox() cfg.CONF.set_override('healthnmon_notification_drivers', ['healthnmon.notifier.log_notifier']) 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_processUpdatesException(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) mock_libvirtNetwork = LibvirtNetwork(self.connection, '1') self.mock.StubOutWithMock( mock_libvirtNetwork, '_processNetworkDeletes') mock_libvirtNetwork._processNetworkDeletes([], [],).AndRaise(Exception) self.mock.ReplayAll() self.assertEquals(self.LibvirtNetwork.processUpdates(), None) self.assertRaises(Exception, LibvirtNetwork) self.mock.stubs.UnsetAll() def testProcessNetworkInterface(self): self.mock.StubOutWithMock(api, 'subnet_delete_by_ids') api.subnet_delete_by_ids( mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.ReplayAll() self.assertEquals(self.LibvirtNetwork._processNetworkInterface( libvirt.virLibvirtInterfaceEth0()), None) host = InventoryCacheManager.get_object_from_cache( '1', Constants.VmHost) self.assertTrue(host.get_ipAddresses is not None) self.mock.stubs.UnsetAll() def testProcessNetwork(self): self.mock.StubOutWithMock(api, 'subnet_save') api.subnet_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.ReplayAll() self.assertEquals(self.LibvirtNetwork._processVirtualNetwork( libvirt.virLibvirtNetwork()), None) subnet = InventoryCacheManager.get_object_from_cache( 'Subnet_52:54:00:34:14:AE', Constants.Network) self.assertTrue(subnet is not None) self.assertFalse(subnet.get_isBootNetwork()) self.assertEquals('default', subnet.get_name()) 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 testGetIpType(self): self.assertEquals(self.LibvirtNetwork._getIpType('10.10.155.63' ), 'IPV4') self.assertEquals(self.LibvirtNetwork._getIpType('g3:1d:1a:63:5e:a3' ), 'IPV6') self.assertEquals(self.LibvirtNetwork._getIpType(''), 'UNSPECIFIED') self.mock.stubs.UnsetAll()
class NetworkEventsTest(test.TestCase): def setUp(self): super(NetworkEventsTest, self).setUp() self.connection = LibvirtConnection(False) self.connection._wrapped_conn = libvirt.open("qemu:///system") rm_context = ComputeRMContext(rmType='QEMU', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') InventoryCacheManager.get_all_compute_inventory()['1'] = \ ComputeInventory(rm_context) self.libvirtNetwork = LibvirtNetwork(self.connection._wrapped_conn, '1') self.flags( healthnmon_notification_drivers=['nova.notifier.test_notifier']) test_notifier.NOTIFICATIONS = [] def test_no_event_generated(self): vmhost = VmHost() vmhost.id = self.libvirtNetwork.compute_id self.libvirtNetwork._processNetworkEvents(None, vmhost) self.assertEquals(len(test_notifier.NOTIFICATIONS), 0) def test_network_added_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vmhost = VmHost() vmhost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") vmhost.set_virtualSwitches([vswitch]) self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) 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_NETWORK_ADDED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VirtualSwitch') self.assertEquals(payload['entity_id'], vswitch.get_id()) def test_network_deleted_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") cachedHost.set_virtualSwitches([vswitch]) vmhost = copy.deepcopy(cachedHost) vmhost.get_virtualSwitches().pop() self.assertEquals(vmhost.get_virtualSwitches(), []) self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) 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_NETWORK_DELETED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VirtualSwitch') self.assertEquals(payload['entity_id'], vswitch.get_id()) def test_network_enabled_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") vswitch.set_connectionState("Inactive") cachedHost.set_virtualSwitches([vswitch]) vmhost = copy.deepcopy(cachedHost) vmhost.get_virtualSwitches()[0].set_connectionState("Active") self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) self.assertEquals( vmhost.get_virtualSwitches()[0].get_connectionState(), Constants.VIRSWITCH_STATE_ACTIVE) 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_NETWORK_ENABLED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VirtualSwitch') self.assertEquals(payload['entity_id'], vswitch.get_id()) self.assertEquals(payload["state"], 'Active') def test_network_disabled_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") vswitch.set_connectionState("Active") cachedHost.set_virtualSwitches([vswitch]) vmhost = copy.deepcopy(cachedHost) vmhost.get_virtualSwitches()[0].set_connectionState("Inactive") self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) self.assertEquals( vmhost.get_virtualSwitches()[0].get_connectionState(), Constants.VIRSWITCH_STATE_INACTIVE) self.assertEquals(len(test_notifier.NOTIFICATIONS), 1) msg = test_notifier.NOTIFICATIONS[0] self.assertEquals(msg['priority'], notifier_api.WARN) event_type = event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_NETWORK_DISABLED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'VirtualSwitch') self.assertEquals(payload['entity_id'], vswitch.get_id()) self.assertEquals(payload["state"], 'Inactive') def test_portGroup_added_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vmhost = VmHost() vmhost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") portGroup = PortGroup() portGroup.set_id("PortGroup_" + vswitch.get_id()) portGroup.set_name(vswitch.get_name()) portGroup.set_virtualSwitchId(vswitch.get_id()) vswitch.set_portGroups([portGroup]) vmhost.set_virtualSwitches([vswitch]) vmhost.set_portGroups([portGroup]) self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) self.assertEquals(len(test_notifier.NOTIFICATIONS), 2) msg = test_notifier.NOTIFICATIONS[1] self.assertEquals(msg['priority'], notifier_api.INFO) event_type = event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_PORTGROUP_ADDED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'PortGroup') self.assertEquals(payload['entity_id'], portGroup.get_id()) def test_portGroup_deleted_event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") portGroup = PortGroup() portGroup.set_id("PortGroup_" + vswitch.get_id()) portGroup.set_name(vswitch.get_name()) portGroup.set_virtualSwitchId(vswitch.get_id()) vswitch.set_portGroups([portGroup]) cachedHost.set_virtualSwitches([vswitch]) cachedHost.set_portGroups([portGroup]) vmhost = copy.deepcopy(cachedHost) vmhost.get_portGroups().pop() vmhost.get_virtualSwitches().pop() self.assertEquals(vmhost.get_virtualSwitches(), []) self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) self.assertEquals(len(test_notifier.NOTIFICATIONS), 2) msg = test_notifier.NOTIFICATIONS[1] self.assertEquals(msg['priority'], notifier_api.INFO) event_type = event_metadata.get_EventMetaData( event_metadata.EVENT_TYPE_PORTGROUP_DELETED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'PortGroup') self.assertEquals(payload['entity_id'], portGroup.get_id()) def test_PortGroup_Reconfigured_Event(self): cachedHost = VmHost() cachedHost.id = self.libvirtNetwork.compute_id vswitch = VirtualSwitch() vswitch.set_id("11") vswitch.set_name("vs1") portGroup = PortGroup() portGroup.set_id("PortGroup_" + vswitch.get_id()) portGroup.set_name(vswitch.get_name()) portGroup.set_virtualSwitchId(vswitch.get_id()) vswitch.set_portGroups([portGroup]) cachedHost.set_virtualSwitches([vswitch]) cachedHost.set_portGroups([portGroup]) vmhost = copy.deepcopy(cachedHost) vmhost.get_portGroups()[0].set_name("vs11") vmhost.get_virtualSwitches()[0].set_name("vs11") vmhost.get_virtualSwitches()[0].get_portGroups()[0].set_name("vs11") self.libvirtNetwork._processNetworkEvents(cachedHost, vmhost) 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_PORTGROUP_RECONFIGURED) self.assertEquals(msg['event_type'], event_type.get_event_fully_qal_name()) payload = msg['payload'] self.assertEquals(payload['entity_type'], 'PortGroup') self.assertEquals(payload['entity_id'], portGroup.get_id())
class test_LibvirtNetwork(unittest.TestCase): def setUp(self): self.connection = LibvirtConnection(False) vmHost = VmHost() vSwitch = VirtualSwitch() vSwitch.set_id('52:54:00:34:14:AE') vSwitch.set_name('default') vSwitch.set_switchType('nat') vmHost.set_virtualSwitches([vSwitch]) InventoryCacheManager.update_object_in_cache('1', vmHost) #self.connection.setUuid('34353438-3934-434e-3738-313630323543') self.connection._wrapped_conn = libvirt.open('qemu:///system') self.connection.compute_rmcontext = \ ComputeRMContext(rmType='KVM', rmIpAddress='10.10.155.165', rmUserName='******', rmPassword='******') self.LibvirtNetwork = LibvirtNetwork(self.connection, '1') self.mock = mox.Mox() cfg.CONF.set_override('healthnmon_notification_drivers', ['healthnmon.notifier.log_notifier']) 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_processUpdatesException(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) mock_libvirtNetwork = LibvirtNetwork(self.connection, '1') self.mock.StubOutWithMock( mock_libvirtNetwork, '_processNetworkDeletes') mock_libvirtNetwork._processNetworkDeletes([], [],).AndRaise(Exception) self.mock.ReplayAll() self.assertEquals(self.LibvirtNetwork.processUpdates(), None) self.assertRaises(Exception, LibvirtNetwork) self.mock.stubs.UnsetAll() def testProcessNetworkInterface(self): self.mock.StubOutWithMock(api, 'subnet_delete_by_ids') api.subnet_delete_by_ids(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.ReplayAll() self.assertEquals(self.LibvirtNetwork._processNetworkInterface( libvirt.virLibvirtInterfaceEth0()), None) host = InventoryCacheManager.get_object_from_cache( '1', Constants.VmHost) self.assertTrue(host.get_ipAddresses is not None) self.mock.stubs.UnsetAll() def testProcessNetwork(self): self.mock.StubOutWithMock(api, 'subnet_save') api.subnet_save(mox.IgnoreArg(), mox.IgnoreArg()).MultipleTimes().AndReturn(None) self.mock.ReplayAll() self.assertEquals(self.LibvirtNetwork._processVirtualNetwork( libvirt.virLibvirtNetwork()), None) subnet = InventoryCacheManager.get_object_from_cache( 'Subnet_52:54:00:34:14:AE', Constants.Network) self.assertTrue(subnet is not None) self.assertFalse(subnet.get_isBootNetwork()) self.assertEquals('default', subnet.get_name()) 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 testGetIpType(self): self.assertEquals(self.LibvirtNetwork._getIpType('10.10.155.63' ), 'IPV4') self.assertEquals(self.LibvirtNetwork._getIpType('g3:1d:1a:63:5e:a3' ), 'IPV6') self.assertEquals(self.LibvirtNetwork._getIpType(''), 'UNSPECIFIED') self.mock.stubs.UnsetAll()