def test_vifport(self): """Create and stringify vif port, confirm no exceptions.""" pname = "vif1.0" ofport = 5 vif_id = uuidutils.generate_uuid() mac = "ca:fe:de:ad:be:ef" # test __init__ port = ovs_lib.VifPort(pname, ofport, vif_id, mac, self.br) self.assertEqual(port.port_name, pname) self.assertEqual(port.ofport, ofport) self.assertEqual(port.vif_id, vif_id) self.assertEqual(port.vif_mac, mac) self.assertEqual(port.switch.br_name, self.BR_NAME) # test __str__ str(port)
def test_handle_port(self): port = { "port_id": "p1", "fixed_ips": [{ "ip_address": "1.1.1.1" }], "vif_port": ovs_lib.VifPort("tap-p1", "1", "p1", "aa:aa:aa:aa:aa:aa", "br-int"), "device_owner": "compute:test" } self.ovs_dhcp.handle_port(self.context, port) self.ovs_dhcp.int_br.add_dhcp_ipv4_flow.assert_called_once_with( port['port_id'], port["vif_port"].ofport, port["vif_port"].vif_mac) self.ovs_dhcp.int_br.add_dhcp_ipv6_flow.assert_called_once_with( port['port_id'], port["vif_port"].ofport, port["vif_port"].vif_mac) self.assertIsNotNone(self.ovs_dhcp.VIF_PORT_CACHE.get(port['port_id']))
def test_port_update(self): with mock.patch.object(ovs_lib.OVSBridge, 'get_vif_port_by_id') as get_vif_port_by_id,\ mock.patch.object(self.sg_agent, 'refresh_firewall') as refresh_firewall: context = mock.Mock() vifport = ovs_lib.VifPort('port1', '1', 'id-1', 'mac-1', self.agent.int_br) # The OVS port does not exist. get_vif_port_by_id.return_value = None port = {'id': 'update-port-1'} self.callback_nvsd.port_update(context, port=port) self.assertEqual(get_vif_port_by_id.call_count, 1) self.assertFalse(refresh_firewall.call_count) # The OVS port exists but no security group is associated. get_vif_port_by_id.return_value = vifport port = {'id': 'update-port-1'} self.callback_nvsd.port_update(context, port=port) self.assertEqual(get_vif_port_by_id.call_count, 2) self.assertFalse(refresh_firewall.call_count) # The OVS port exists but a security group is associated. get_vif_port_by_id.return_value = vifport port = {'id': 'update-port-1', ext_sg.SECURITYGROUPS: ['default']} self.callback_nvsd.port_update(context, port=port) self.assertEqual(get_vif_port_by_id.call_count, 3) self.assertEqual(refresh_firewall.call_count, 1) get_vif_port_by_id.return_value = None port = {'id': 'update-port-1', ext_sg.SECURITYGROUPS: ['default']} self.callback_nvsd.port_update(context, port=port) self.assertEqual(get_vif_port_by_id.call_count, 4) self.assertEqual(refresh_firewall.call_count, 1)
def _test_delete_port(self, with_vif_port=False): vif_port = ovs_lib.VifPort("tap-p1", 1, "p1", "aa:aa:aa:aa:aa:aa", "br-int") port1 = { "port_id": "p1", "fixed_ips": [{ "ip_address": "1.1.1.1" }], "vif_port": vif_port, "device_owner": "compute:test" } self.ovs_dhcp.handle_port(self.context, port1) with mock.patch.object(self.ovs_dhcp.int_br, "get_vif_ports", return_value=[]): if with_vif_port: port2 = {"port_id": "p1", "vif_port": vif_port} else: port2 = {"port_id": "p1"} self.ovs_dhcp.delete_port(self.context, port2) self.ovs_dhcp.int_br.del_dhcp_flow.assert_called_once_with( port1["vif_port"].ofport, port1["vif_port"].vif_mac) # verify the cache self.assertNotIn("p1", self.ovs_dhcp.VIF_PORT_CACHE.keys())
def get_vif_port_by_id(self, port_id): return ovs_lib.VifPort(port_id, None, port_id, None, self)
def nonzero(f): if six.PY3: return f.__bool__() else: return f.__nonzero__() # Useful global dummy variables. NET_UUID = '3faeebfe-5d37-11e1-a64b-000c29d5f0a7' LS_ID = 420 LV_ID = 42 LV_IDS = [42, 43] VIF_ID = '404deaec-5d37-11e1-a64b-000c29d5f0a8' VIF_MAC = '3c:09:24:1e:78:23' OFPORT_NUM = 1 VIF_PORT = ovs_lib.VifPort('port', OFPORT_NUM, VIF_ID, VIF_MAC, 'switch') VIF_PORTS = {VIF_ID: VIF_PORT} FIXED_IPS = [{'subnet_id': 'my-subnet-uuid', 'ip_address': '1.1.1.1'}] VM_DEVICE_OWNER = "compute:None" TUN_OFPORTS = {p_const.TYPE_GRE: {'ip1': '11', 'ip2': '12'}} BCAST_MAC = "01:00:00:00:00:00/01:00:00:00:00:00" UCAST_MAC = "00:00:00:00:00:00/01:00:00:00:00:00" class DummyPort(object): def __init__(self, interface_id): self.interface_id = interface_id
from neutron.tests.unit.plugins.ml2.drivers.openvswitch.agent \ import ovs_test_base from neutron.tests.unit.plugins.ml2.drivers.openvswitch.agent \ import test_vlanmanager Switch = collections.namedtuple('Switch', ['br_name']) # Useful global dummy variables. NET_UUID = '3faeebfe-5d37-11e1-a64b-000c29d5f0a7' LS_ID = 420 LV_ID = 42 LV_IDS = [42, 43] VIF_ID = '404deaec-5d37-11e1-a64b-000c29d5f0a8' VIF_MAC = '3c:09:24:1e:78:23' OFPORT_NUM = 1 VIF_PORT = ovs_lib.VifPort('port', OFPORT_NUM, VIF_ID, VIF_MAC, Switch(br_name='br_name')) VIF_PORTS = {VIF_ID: VIF_PORT} FIXED_IPS = [{'subnet_id': 'my-subnet-uuid', 'ip_address': '1.1.1.1'}] VM_DEVICE_OWNER = n_const.DEVICE_OWNER_COMPUTE_PREFIX + 'fake' TUN_OFPORTS = {n_const.TYPE_GRE: {'ip1': '11', 'ip2': '12'}} BCAST_MAC = "01:00:00:00:00:00/01:00:00:00:00:00" UCAST_MAC = "00:00:00:00:00:00/01:00:00:00:00:00" class DummyPort(object): def __init__(self, interface_id): self.interface_id = interface_id
def test_daemon_loop(self): def state_check(index): self.assertEqual(len(self.vif_ports_scenario[index]), len(self.agent.cur_ports)) # Fake time.sleep to stop the infinite loop in daemon_loop() self.sleep_count = 0 def sleep_mock(*args, **kwargs): state_check(self.sleep_count) self.sleep_count += 1 if self.sleep_count >= DAEMON_LOOP_COUNT: raise RuntimeError() vif_ports = [ ovs_lib.VifPort('port1', '1', 'id-1', 'mac-1', self.agent.int_br), ovs_lib.VifPort('port2', '2', 'id-2', 'mac-2', self.agent.int_br) ] self.vif_ports_scenario = [[], [], vif_ports[0:1], vif_ports[0:2], vif_ports[1:2], []] # Ensure vif_ports_scenario is longer than DAEMON_LOOP_COUNT if len(self.vif_ports_scenario) < DAEMON_LOOP_COUNT: self.vif_ports_scenario.extend( [] for _i in moves.xrange(DAEMON_LOOP_COUNT - len(self.vif_ports_scenario))) with contextlib.nested( mock.patch.object(time, 'sleep', side_effect=sleep_mock), mock.patch.object(ovs_lib.OVSBridge, 'get_vif_ports'), mock.patch.object(l2_agent.NECPluginApi, 'update_ports'), mock.patch.object(self.agent.sg_agent, 'prepare_devices_filter'), mock.patch.object( self.agent.sg_agent, 'remove_devices_filter')) as (sleep, get_vif_potrs, update_ports, prepare_devices_filter, remove_devices_filter): get_vif_potrs.side_effect = self.vif_ports_scenario with testtools.ExpectedException(RuntimeError): self.agent.daemon_loop() self.assertEqual(update_ports.call_count, 4) self.assertEqual(sleep.call_count, DAEMON_LOOP_COUNT) agent_id = 'nec-q-agent.dummy-host' expected = [ mock.call(mock.ANY, agent_id, OVS_DPID_0X, [{ 'id': 'id-1', 'mac': 'mac-1', 'port_no': '1' }], []), mock.call(mock.ANY, agent_id, OVS_DPID_0X, [{ 'id': 'id-2', 'mac': 'mac-2', 'port_no': '2' }], []), mock.call(mock.ANY, agent_id, OVS_DPID_0X, [], ['id-1']), mock.call(mock.ANY, agent_id, OVS_DPID_0X, [], ['id-2']) ] update_ports.assert_has_calls(expected) expected = [mock.call(['id-1']), mock.call(['id-2'])] self.assertEqual(prepare_devices_filter.call_count, 2) prepare_devices_filter.assert_has_calls(expected) self.assertEqual(remove_devices_filter.call_count, 2) remove_devices_filter.assert_has_calls(expected) sleep.assert_called_with(self.agent.polling_interval)