def setUp(self): super(L3AgentNDPProxyTestFramework, self).setUp() self.conf.set_override('extensions', ['ndp_proxy'], 'agent') self.agent = neutron_l3_agent.L3NATAgentWithStateReport(HOSTNAME, self.conf) self.np_ext = np.NDPProxyAgentExtension() port_id1 = uuidutils.generate_uuid() port1_binding = ports_obj.PortBinding(port_id=port_id1, host=self.agent.host) port1_obj = ports_obj.Port(id=port_id1, bindings=[port1_binding]) port_id2 = uuidutils.generate_uuid() port2_binding = ports_obj.PortBinding(port_id=port_id1, host='fake_host') port2_obj = ports_obj.Port(id=port_id2, bindings=[port2_binding]) self.ports = [port1_obj, port2_obj] self.port_binding_map = {port_id1: port1_binding, port_id2: port2_binding} self.ndpproxy1 = np_obj.NDPProxy( context=None, id=uuidutils.generate_uuid(), router_id=uuidutils.generate_uuid(), port_id=port_id1, ip_address='2002::1:3') self.ndpproxy2 = np_obj.NDPProxy( context=None, id=uuidutils.generate_uuid(), router_id=uuidutils.generate_uuid(), port_id=port_id2, ip_address='2002::1:4') self.ndp_proxies = [self.ndpproxy1, self.ndpproxy2] agent_configurations = { 'agent_mode': constants.L3_AGENT_MODE_DVR_NO_EXTERNAL} self.agent_obj = agent_obj.Agent( id=uuidutils.generate_uuid(), host=self.agent.host, agent_type=constants.AGENT_TYPE_L3, configurations=agent_configurations) self._set_pull_mock()
def setUp(self): super(RouterNDPProxyMappingTestCase, self).setUp() self.mapping = np.RouterNDPProxyMapping() self.router1 = _uuid() self.router2 = _uuid() self.ndpproxy1 = np_obj.NDPProxy(context=None, id=_uuid(), router_id=self.router1, port_id=_uuid(), ip_address='2002::1:3') self.ndpproxy2 = np_obj.NDPProxy(context=None, id=_uuid(), router_id=self.router2, port_id=_uuid(), ip_address='2002::1:4') self.ndpproxies = [self.ndpproxy1, self.ndpproxy2]
def _test__handle_notification(self): self.np_ext.add_router(self.context, self.router) self.add_chain.reset_mock() self.add_rule.reset_mock() self.ip_wrapper.reset_mock() ndpproxy = np_obj.NDPProxy(context=None, id=_uuid(), router_id=self.fake_router_id, port_id=self.port_id, ip_address='2002::1:5') self.np_ext._handle_notification(mock.MagicMock(), mock.MagicMock(), [ndpproxy], events.CREATED) self.add_chain.assert_not_called() self.remove_chain.assert_not_called() accept_rule = '-i %s --destination %s -j ACCEPT' % ( self.ext_device_name, '2002::1:5') expected_calls = [ mock.call(np.DEFAULT_NDP_PROXY_CHAIN, accept_rule, top=True) ] self.assertEqual(expected_calls, self.add_rule.mock_calls) self.remove_rule.assert_not_called() proxy_cmd = [ 'ip', '-6', 'neigh', 'add', 'proxy', '2002::1:5', 'dev', self.ext_device_name ] ndsend_cmd = ['ndsend', '2002::1:5', self.ext_device_name] expected_calls = [ mock.call(namespace=self.namespace), mock.call().netns.execute(proxy_cmd, privsep_exec=True), mock.call().netns.execute(ndsend_cmd, check_exit_code=False, log_fail_as_error=True, privsep_exec=True) ] self.assertEqual(expected_calls, self.ip_wrapper.mock_calls) self.add_chain.reset_mock() self.remove_chain.reset_mock() self.add_rule.reset_mock() self.remove_rule.reset_mock() self.ip_wrapper.reset_mock() self.np_ext._handle_notification(mock.MagicMock(), mock.MagicMock(), [ndpproxy], events.DELETED) self.add_chain.assert_not_called() self.remove_chain.assert_not_called() self.add_rule.assert_not_called() accept_rule = ('-i %s --destination %s -j ACCEPT') % ( self.ext_device_name, '2002::1:5') expected_calls = [ mock.call(np.DEFAULT_NDP_PROXY_CHAIN, accept_rule, top=True) ] self.assertEqual(expected_calls, self.remove_rule.mock_calls) proxy_cmd = [ 'ip', '-6', 'neigh', 'del', 'proxy', '2002::1:5', 'dev', self.ext_device_name ] expected_calls = [ mock.call(namespace=self.namespace), mock.call().netns.execute(proxy_cmd, privsep_exec=True) ] self.assertEqual(expected_calls, self.ip_wrapper.mock_calls)
def test_add_ndp_proxy_update_router(self): self.np_ext.add_router(self.context, self.router) self.add_route.reset_mock() self.ip_wrapper.reset_mock() ndpproxy = np_obj.NDPProxy(context=None, id=_uuid(), router_id=self.fake_router_id, port_id=self.port_id, ip_address='2002::1:6') self.ndp_proxies.append(ndpproxy) self.np_ext.update_router(self.context, self.router) expected_calls = [mock.call('2002::1:6', via=self.lladdr)] self.assertEqual(expected_calls, self.add_route.mock_calls) sysctl_cmd = [ 'sysctl', '-w', 'net.ipv6.conf.%s.proxy_ndp=1' % self.agent_ext_dvice ] proxy_cmd = [ 'ip', '-6', 'neigh', 'add', 'proxy', '2002::1:6', 'dev', self.agent_ext_dvice ] ndsend_cmd = ['ndsend', '2002::1:6', self.agent_ext_dvice] expected_calls = [ mock.call(namespace=self.namespace), mock.call().netns.execute(sysctl_cmd, privsep_exec=True), mock.call(namespace=self.namespace), mock.call().netns.execute(proxy_cmd, privsep_exec=True), mock.call().netns.execute(ndsend_cmd, check_exit_code=False, log_fail_as_error=True, privsep_exec=True), mock.call(namespace=self.namespace) ] self.assertEqual(expected_calls, self.ip_wrapper.mock_calls)
def create_ndp_proxy(self, context, ndp_proxy): ndp_proxy = ndp_proxy.get(np_apidef.RESOURCE_NAME) router_id = ndp_proxy['router_id'] port_id = ndp_proxy['port_id'] port_dict = self.core_plugin.get_port(context, port_id) router_ports = self.core_plugin.get_ports( context, filters={ 'device_id': [router_id], 'network_id': [port_dict['network_id']] }) if not router_ports: raise exc.PortUnreachableRouter(router_id=router_id, port_id=port_id) router_dict = self.l3_plugin.get_router(context, router_id) if not router_dict.get('enable_ndp_proxy', None): raise exc.RouterNDPProxyNotEnable(router_id=router_dict['id']) extrnal_gw_info = router_dict[l3_apidef.EXTERNAL_GW_INFO] gw_network_dict = self.core_plugin.get_network( context, extrnal_gw_info['network_id']) ext_address_scope = gw_network_dict.get('ipv6_address_scope', None) internal_address_scope = self._check_port(context, port_dict, ndp_proxy, router_ports) # If the external network and internal network not belong to same # adddress scope, the packets can't be forwarded by route. So, in # this case we should forbid to create ndp proxy entry. if ext_address_scope != internal_address_scope: raise exc.AddressScopeConflict( ext_address_scope=ext_address_scope, internal_address_scope=internal_address_scope) tenant_id = ndp_proxy.pop('tenant_id', None) if not ndp_proxy.get('project_id', None): ndp_proxy['project_id'] = tenant_id with db_api.CONTEXT_WRITER.using(context): np_obj = np.NDPProxy(context, **ndp_proxy) np_obj.create() LOG.debug( "Notify l3-agent to create ndp proxy rules for " "ndp proxy: %s", np_obj.to_dict()) self.push_api.push(context, [np_obj], rpc_events.CREATED) return np_obj
def test__handle_notification(self): self.np_ext.add_router(self.context, self.router) self.add_route.reset_mock() self.ip_wrapper.reset_mock() ndpproxy = np_obj.NDPProxy(context=None, id=_uuid(), router_id=self.fake_router_id, port_id=self.port_id, ip_address='2002::1:5') self.np_ext._handle_notification(mock.MagicMock(), mock.MagicMock(), [ndpproxy], events.CREATED) expected_calls = [mock.call('2002::1:5', via=self.lladdr)] self.assertEqual(expected_calls, self.add_route.mock_calls) proxy_cmd = [ 'ip', '-6', 'neigh', 'add', 'proxy', '2002::1:5', 'dev', self.agent_ext_dvice ] ndsend_cmd = ['ndsend', '2002::1:5', self.agent_ext_dvice] expected_calls = [ mock.call(namespace=self.namespace), mock.call().netns.execute(proxy_cmd, privsep_exec=True), mock.call().netns.execute(ndsend_cmd, check_exit_code=False, log_fail_as_error=True, privsep_exec=True) ] self.assertEqual(expected_calls, self.ip_wrapper.mock_calls) self.add_route.reset_mock() self.ip_wrapper.reset_mock() self.np_ext._handle_notification(mock.MagicMock(), mock.MagicMock(), [ndpproxy], events.DELETED) self.add_route.assert_not_called() expected_calls = [mock.call('2002::1:5', via=self.lladdr)] self.assertEqual(expected_calls, self.delete_route.mock_calls) proxy_cmd = [ 'ip', '-6', 'neigh', 'del', 'proxy', '2002::1:5', 'dev', self.agent_ext_dvice ] expceted_calls = [ mock.call(namespace=self.namespace), mock.call().netns.execute(proxy_cmd, privsep_exec=True) ] self.assertEqual(expceted_calls, self.ip_wrapper.mock_calls)
def _test_add_ndp_proxy_update_router(self): self.np_ext.add_router(self.context, self.router) self.add_chain.reset_mock() self.add_rule.reset_mock() self.ip_wrapper.reset_mock() self.ndp_proxies.append( np_obj.NDPProxy(context=None, id=_uuid(), router_id=self.fake_router_id, port_id=self.port_id, ip_address='2002::1:4')) self.np_ext.update_router(self.context, self.router) self.add_chain.assert_not_called() accept_rule = '-i %s --destination %s -j ACCEPT' % ( self.ext_device_name, '2002::1:4') expected_calls = [ mock.call(np.DEFAULT_NDP_PROXY_CHAIN, accept_rule, top=True) ] self.assertEqual(expected_calls, self.add_rule.mock_calls) sysctl_cmd = [ 'sysctl', '-w', 'net.ipv6.conf.%s.proxy_ndp=1' % self.ext_device_name ] proxy_cmd = [ 'ip', '-6', 'neigh', 'add', 'proxy', '2002::1:4', 'dev', self.ext_device_name ] ndsend_cmd = ['ndsend', '2002::1:4', self.ext_device_name] expected_calls = [ mock.call(namespace=self.namespace), mock.call().netns.execute(sysctl_cmd, privsep_exec=True), mock.call(namespace=self.namespace), mock.call().netns.execute(proxy_cmd, privsep_exec=True), mock.call().netns.execute(ndsend_cmd, check_exit_code=False, log_fail_as_error=True, privsep_exec=True), mock.call(namespace=self.namespace) ] self.assertEqual(expected_calls, self.ip_wrapper.mock_calls)
def setUp(self): super(NDPProxyExtensionTestCaseBase, self).setUp() self.context = context.get_admin_context() self.connection = mock.Mock() self.ext_port_id = _uuid() self.ex_net_id = _uuid() self.ex_gw_port = { 'id': self.ext_port_id, 'network_id': self.ex_net_id, 'gw_port_host': HOSTNAME } self.fake_router_id = _uuid() self.port_id = _uuid() self.agent_api = l3_ext_api.L3AgentExtensionAPI(None, None) self.np_ext = np.NDPProxyAgentExtension() self.np_ext.consume_api(self.agent_api) self.np_ext.initialize(self.connection, lib_const.L3_AGENT_MODE) self.ndpproxy = np_obj.NDPProxy(context=None, id=_uuid(), router_id=self.fake_router_id, port_id=self.port_id, ip_address='2002::1:3') port_binding = ports_obj.PortBinding(port_id=self.port_id, host=HOSTNAME) port_obj = ports_obj.Port(id=self.port_id, bindings=[port_binding]) self.ndp_proxies = [self.ndpproxy] self.ports = [port_obj] agent_configurations = { 'agent_mode': lib_const.L3_AGENT_MODE_DVR_NO_EXTERNAL } self.agent_obj = agent_obj.Agent(id=_uuid(), host=HOSTNAME, agent_type=lib_const.AGENT_TYPE_L3, configurations=agent_configurations) self.ip_wrapper = mock.patch('neutron.agent.linux.' 'ip_lib.IPWrapper').start() self._set_pull_mock()