def test_delete_neutron_ports(self): port1 = ovs_lib.VifPort('tap1234', 1, uuidutils.generate_uuid(), 'ca:fe:de:ad:be:ef', 'br') port2 = ovs_lib.VifPort('tap5678', 2, uuidutils.generate_uuid(), 'ca:ee:de:ad:be:ef', 'br') self.mox.StubOutWithMock(self.br, 'get_vif_ports') self.br.get_vif_ports().AndReturn([port1, port2]) self.mox.StubOutWithMock(self.br, 'delete_port') self.br.delete_port('tap1234') self.br.delete_port('tap5678') self.mox.ReplayAll() self.br.delete_ports(all_ports=False) self.mox.VerifyAll()
def test_delete_neutron_ports(self): port1 = ovs_lib.VifPort('tap1234', 1, uuidutils.generate_uuid(), 'ca:fe:de:ad:be:ef', 'br') port2 = ovs_lib.VifPort('tap5678', 2, uuidutils.generate_uuid(), 'ca:ee:de:ad:be:ef', 'br') with mock.patch.object(self.br, 'get_vif_ports', return_value=[port1, port2]) as get_ports: with mock.patch.object(self.br, 'delete_port') as delete_port: self.br.delete_ports(all_ports=False) get_ports.assert_called_once_with() delete_port.assert_has_calls( [mock.call('tap1234'), mock.call('tap5678')])
def test_collect_neutron_ports(self): port1 = ovs_lib.VifPort('tap1234', 1, uuidutils.generate_uuid(), '11:22:33:44:55:66', 'br') port2 = ovs_lib.VifPort('tap5678', 2, uuidutils.generate_uuid(), '77:88:99:aa:bb:cc', 'br') port3 = ovs_lib.VifPort('tap90ab', 3, uuidutils.generate_uuid(), '99:00:aa:bb:cc:dd', 'br') ports = [[port1, port2], [port3]] portnames = [p.port_name for p in itertools.chain(*ports)] with mock.patch('neutron.agent.linux.ovs_lib.OVSBridge') as ovs: ovs.return_value.get_vif_ports.side_effect = ports bridges = ['br-int', 'br-ex'] ret = util.collect_neutron_ports(bridges) self.assertEqual(ret, portnames)
def _setup_mock(self): 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.get_vif_ports = mock.patch.object(ovs_lib.OVSBridge, 'get_vif_ports', return_value=vif_ports).start() self.update_ports = mock.patch.object(nec_neutron_agent.NECPluginApi, 'update_ports').start() self.prepare_devices_filter = mock.patch.object( self.agent.sg_agent, 'prepare_devices_filter').start() self.remove_devices_filter = mock.patch.object( self.agent.sg_agent, 'remove_devices_filter').start()
def create_ovs_vif_port(self, iface_id=None, mac=None, iface_field='iface-id'): if iface_id is None: iface_id = base.get_rand_name() if mac is None: mac = base.get_rand_name() attrs = ('external_ids', {iface_field: iface_id, 'attached-mac': mac}) port_name, ofport = self.create_ovs_port(attrs) return ovs_lib.VifPort(port_name, ofport, iface_id, mac, self.br)
def _get_external_port(self, name): # exclude vif ports external_ids = self.db_get_map("Interface", name, "external_ids") if external_ids: return # exclude tunnel ports options = self.db_get_map("Interface", name, "options") if "remote_ip" in options: return ofport = self.get_ofport(name) return ovs_lib.VifPort(name, ofport, None, None, self)
def create_ovs_vif_port(self, iface_id=None, mac=None, iface_field='iface-id'): if iface_id is None: iface_id = base.get_rand_name() if mac is None: mac = base.get_rand_name() attrs = { 'external_ids:%s' % iface_field: iface_id, 'external_ids:attached-mac': mac } port_name, ofport = self.create_ovs_port(*attrs.items()) return ovs_lib.VifPort(port_name, ofport, iface_id, mac, self.br)
def _test_report_state(self, fail_mode): log_mocked = mock.patch.object(nec_neutron_agent, 'LOG') log_patched = log_mocked.start() def record_state(*args, **kwargs): self.record_calls.append(copy.deepcopy(args)) if fail_mode: raise Exception() self.record_calls = [] self.num_ports_hist = [] state_rpc = self.state_rpc_api.return_value state_rpc.report_state.side_effect = record_state dummy_vif = ovs_lib.VifPort('port1', '1', 'id-1', 'mac-1', None) self.state_rpc_api.assert_called_once_with('q-plugin') self.assertTrue('start_flag' in self.agent.agent_state) self._check_report_state([], 0, fail_mode, first=True) self._check_report_state([dummy_vif] * 2, 2, fail_mode) self._check_report_state([dummy_vif] * 5, 5, fail_mode) self._check_report_state([], 0, fail_mode) print 'record_state', self.record_calls print 'num_ports_hist', self.num_ports_hist # Since loopingcall start is mocked, call_count is same as # the call count of check_report_state. self.assertEqual(state_rpc.report_state.call_count, 4) self.assertEqual(len(self.record_calls), 4) for i, x in enumerate( itertools.izip(self.record_calls, self.num_ports_hist)): rec, num_ports = x expected_state = { 'binary': 'neutron-nec-agent', 'host': 'dummy-host', 'topic': 'N/A', 'configurations': { 'devices': 0 }, 'agent_type': 'NEC plugin agent' } expected_state['configurations']['devices'] = num_ports if i == 0 or fail_mode: expected_state['start_flag'] = True self.assertEqual(expected_state, rec[1]) self.assertEqual(fail_mode, log_patched.exception.called)
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_update_ports_returns_changed_vlan(self): br = ovs_lib.OVSBridge('br-int', 'sudo') mac = "ca:fe:de:ad:be:ef" port = ovs_lib.VifPort(1, 1, 1, mac, br) lvm = ovs_neutron_agent.LocalVLANMapping( 1, '1', None, 1, {port.vif_id: port}) local_vlan_map = {'1': lvm} vif_port_set = set([1, 3]) registered_ports = set([1, 2]) port_tags_dict = {1: []} expected = dict( added=set([3]), current=vif_port_set, removed=set([2]), updated=set([1]) ) with mock.patch.dict(self.agent.local_vlan_map, local_vlan_map): actual = self.mock_scan_ports( vif_port_set, registered_ports, port_tags_dict=port_tags_dict) self.assertEqual(expected, actual)
def test_port_update(self): with contextlib.nested( mock.patch.object(ovs_lib.OVSBridge, 'get_vif_port_by_id'), mock.patch.object(self.sg_agent, 'refresh_firewall') ) as (get_vif_port_by_id, 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)
from neutron.agent.linux import ovs_lib from neutron.openstack.common import log from neutron.plugins.common import constants as p_const from neutron.plugins.openvswitch.agent import ovs_neutron_agent from neutron.plugins.openvswitch.common import constants from neutron.tests import base # Useful global dummy variables. NET_UUID = '3faeebfe-5d37-11e1-a64b-000c29d5f0a7' LS_ID = 42 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} LVM = ovs_neutron_agent.LocalVLANMapping(LV_ID, 'gre', None, LS_ID, VIF_PORTS) LVM_FLAT = ovs_neutron_agent.LocalVLANMapping(LV_ID, 'flat', 'net1', LS_ID, VIF_PORTS) LVM_VLAN = ovs_neutron_agent.LocalVLANMapping(LV_ID, 'vlan', 'net1', LS_ID, VIF_PORTS) 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"
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(nec_neutron_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)