def testProcessRouter(self): agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) router = self._prepare_router_data() fake_floatingips1 = { 'floatingips': [{ 'id': _uuid(), 'floating_ip_address': '8.8.8.8', 'fixed_ip_address': '7.7.7.7', 'port_id': _uuid() }] } ri = l3_agent.RouterInfo(router['id'], self.conf.root_helper, self.conf.use_namespaces, router=router) agent.process_router(ri) # remap floating IP to a new fixed ip fake_floatingips2 = copy.deepcopy(fake_floatingips1) fake_floatingips2['floatingips'][0]['fixed_ip_address'] = '7.7.7.8' router[l3_constants.FLOATINGIP_KEY] = fake_floatingips2['floatingips'] agent.process_router(ri) # remove just the floating ips del router[l3_constants.FLOATINGIP_KEY] agent.process_router(ri) # now no ports so state is torn down del router[l3_constants.INTERFACE_KEY] del router['gw_port'] agent.process_router(ri)
def _test_routing_table_update(self, namespace): if not namespace: self.conf.set_override('use_namespaces', False) router_id = _uuid() ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces, None) agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) fake_route1 = {'destination': '135.207.0.0/16', 'nexthop': '1.2.3.4'} fake_route2 = {'destination': '135.207.111.111/32', 'nexthop': '1.2.3.4'} agent._update_routing_table(ri, 'replace', fake_route1) expected = [['ip', 'route', 'replace', 'to', '135.207.0.0/16', 'via', '1.2.3.4']] self._check_agent_method_called(agent, expected, namespace) agent._update_routing_table(ri, 'delete', fake_route1) expected = [['ip', 'route', 'delete', 'to', '135.207.0.0/16', 'via', '1.2.3.4']] self._check_agent_method_called(agent, expected, namespace) agent._update_routing_table(ri, 'replace', fake_route2) expected = [['ip', 'route', 'replace', 'to', '135.207.111.111/32', 'via', '1.2.3.4']] self._check_agent_method_called(agent, expected, namespace) agent._update_routing_table(ri, 'delete', fake_route2) expected = [['ip', 'route', 'delete', 'to', '135.207.111.111/32', 'via', '1.2.3.4']] self._check_agent_method_called(agent, expected, namespace)
def setUp(self): super(TestL3AgentEventHandler, self).setUp() cfg.CONF.register_opts(l3_agent.L3NATAgent.OPTS) cfg.CONF.set_override('interface_driver', 'quantum.agent.linux.interface.NullDriver') cfg.CONF.set_override('use_namespaces', True) agent_config.register_root_helper(cfg.CONF) self.device_exists_p = mock.patch( 'quantum.agent.linux.ip_lib.device_exists') self.device_exists = self.device_exists_p.start() self.utils_exec_p = mock.patch('quantum.agent.linux.utils.execute') self.utils_exec = self.utils_exec_p.start() self.drv_cls_p = mock.patch('quantum.agent.linux.interface.NullDriver') driver_cls = self.drv_cls_p.start() self.mock_driver = mock.MagicMock() self.mock_driver.DEV_NAME_LEN = ( interface.LinuxInterfaceDriver.DEV_NAME_LEN) driver_cls.return_value = self.mock_driver self.l3_plugin_p = mock.patch('quantum.agent.l3_agent.L3PluginApi') l3_plugin_cls = self.l3_plugin_p.start() self.plugin_api = mock.Mock() l3_plugin_cls.return_value = self.plugin_api self.external_process_p = mock.patch( 'quantum.agent.linux.external_process.ProcessManager') self.external_process = self.external_process_p.start() self.agent = l3_agent.L3NATAgent(HOSTNAME)
def _test_floating_ip_action(self, action): router_id = _uuid() ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces) agent = l3_agent.L3NATAgent(self.conf) floating_ip = '20.0.0.100' fixed_ip = '10.0.0.23' ex_gw_port = {'fixed_ips': [{'ip_address': '20.0.0.30', 'subnet_id': _uuid()}], 'subnet': {'gateway_ip': '20.0.0.1'}, 'id': _uuid(), 'mac_address': 'ca:fe:de:ad:be:ef', 'ip_cidr': '20.0.0.30/24'} interface_name = agent.get_external_device_name(ex_gw_port['id']) if action == 'add': self.device_exists.return_value = False agent.floating_ip_added(ri, ex_gw_port, floating_ip, fixed_ip) arping_cmd = ['arping', '-A', '-U', '-I', interface_name, '-c', self.conf.send_arp_for_ha, floating_ip] if self.conf.use_namespaces: self.mock_ip.netns.execute.assert_any_call( arping_cmd, check_exit_code=True) else: self.utils_exec.assert_any_call( check_exit_code=True, root_helper=self.conf.root_helper) elif action == 'remove': self.device_exists.return_value = True agent.floating_ip_removed(ri, ex_gw_port, floating_ip, fixed_ip) else: raise Exception("Invalid action %s" % action)
def _test_external_gateway_action(self, action): router_id = _uuid() ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces) agent = l3_agent.L3NATAgent(self.conf) internal_cidrs = ['100.0.1.0/24', '200.74.0.0/16'] ex_gw_port = {'fixed_ips': [{'ip_address': '20.0.0.30', 'subnet_id': _uuid()}], 'subnet': {'gateway_ip': '20.0.0.1'}, 'id': _uuid(), 'network_id': _uuid(), 'mac_address': 'ca:fe:de:ad:be:ef', 'ip_cidr': '20.0.0.30/24'} if action == 'add': self.device_exists.return_value = False agent.external_gateway_added(ri, ex_gw_port, internal_cidrs) self.assertEquals(self.mock_driver.plug.call_count, 1) self.assertEquals(self.mock_driver.init_l3.call_count, 1) self.assertEquals(self.mock_ip.netns.execute.call_count, 1) elif action == 'remove': self.device_exists.return_value = True agent.external_gateway_removed(ri, ex_gw_port, internal_cidrs) self.assertEquals(self.mock_driver.unplug.call_count, 1) else: raise Exception("Invalid action %s" % action)
def testRoutersWithAdminStateDown(self): agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) self.plugin_api.get_external_network_id.return_value = None routers = [ {'id': _uuid(), 'admin_state_up': False, 'external_gateway_info': {}}] agent._process_routers(routers) self.assertNotIn(routers[0]['id'], agent.router_info)
def _test_routes_updated(self, namespace=True): if not namespace: self.conf.set_override('use_namespaces', False) agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) router_id = _uuid() ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces, None) ri.router = {} fake_old_routes = [] fake_new_routes = [{ 'destination': "110.100.31.0/24", 'nexthop': "10.100.10.30" }, { 'destination': "110.100.30.0/24", 'nexthop': "10.100.10.30" }] ri.routes = fake_old_routes ri.router['routes'] = fake_new_routes agent.routes_updated(ri) expected = [[ 'ip', 'route', 'replace', 'to', '110.100.30.0/24', 'via', '10.100.10.30' ], [ 'ip', 'route', 'replace', 'to', '110.100.31.0/24', 'via', '10.100.10.30' ]] self._check_agent_method_called(agent, expected, namespace) fake_new_routes = [{ 'destination': "110.100.30.0/24", 'nexthop': "10.100.10.30" }] ri.router['routes'] = fake_new_routes agent.routes_updated(ri) expected = [[ 'ip', 'route', 'delete', 'to', '110.100.31.0/24', 'via', '10.100.10.30' ]] self._check_agent_method_called(agent, expected, namespace) fake_new_routes = [] ri.router['routes'] = fake_new_routes agent.routes_updated(ri) expected = [[ 'ip', 'route', 'delete', 'to', '110.100.30.0/24', 'via', '10.100.10.30' ]] self._check_agent_method_called(agent, expected, namespace)
def testDestroyNamespace(self): class FakeDev(object): def __init__(self, name): self.name = name self.mock_ip.get_namespaces.return_value = ['qrouter-foo'] self.mock_ip.get_devices.return_value = [FakeDev('qr-aaaa'), FakeDev('qgw-aaaa')] agent = l3_agent.L3NATAgent(self.conf) agent._destroy_all_router_namespaces()
def testProcessRouter(self): agent = l3_agent.L3NATAgent(self.conf) router_id = _uuid() ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces) # return data so that state is built up ex_gw_port = {'id': _uuid(), 'network_id': _uuid(), 'fixed_ips': [{'ip_address': '19.4.4.4', 'subnet_id': _uuid()}]} internal_port = {'id': _uuid(), 'network_id': _uuid(), 'admin_state_up': True, 'fixed_ips': [{'ip_address': '35.4.4.4', 'subnet_id': _uuid()}], 'mac_address': 'ca:fe:de:ad:be:ef'} def fake_list_ports1(**kwargs): if kwargs['device_owner'] == l3_db.DEVICE_OWNER_ROUTER_GW: return {'ports': [ex_gw_port]} elif kwargs['device_owner'] == l3_db.DEVICE_OWNER_ROUTER_INTF: return {'ports': [internal_port]} fake_subnet = {'subnet': {'cidr': '19.4.4.0/24', 'gateway_ip': '19.4.4.1'}} fake_floatingips1 = {'floatingips': [ {'id': _uuid(), 'floating_ip_address': '8.8.8.8', 'fixed_ip_address': '7.7.7.7', 'port_id': _uuid()}]} self.client_inst.list_ports.side_effect = fake_list_ports1 self.client_inst.show_subnet.return_value = fake_subnet self.client_inst.list_floatingips.return_value = fake_floatingips1 agent.process_router(ri) # remap floating IP to a new fixed ip fake_floatingips2 = copy.deepcopy(fake_floatingips1) fake_floatingips2['floatingips'][0]['fixed_ip_address'] = '7.7.7.8' self.client_inst.list_floatingips.return_value = fake_floatingips2 agent.process_router(ri) # remove just the floating ips self.client_inst.list_floatingips.return_value = {'floatingips': []} agent.process_router(ri) # now return no ports so state is torn down self.client_inst.list_ports.return_value = {'ports': []} agent.process_router(ri)
def testProcessRouter(self): agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) router_id = _uuid() ex_gw_port = {'id': _uuid(), 'network_id': _uuid(), 'fixed_ips': [{'ip_address': '19.4.4.4', 'subnet_id': _uuid()}], 'subnet': {'cidr': '19.4.4.0/24', 'gateway_ip': '19.4.4.1'}} internal_port = {'id': _uuid(), 'network_id': _uuid(), 'admin_state_up': True, 'fixed_ips': [{'ip_address': '35.4.4.4', 'subnet_id': _uuid()}], 'mac_address': 'ca:fe:de:ad:be:ef', 'subnet': {'cidr': '35.4.4.0/24', 'gateway_ip': '35.4.4.1'}} fake_floatingips1 = {'floatingips': [ {'id': _uuid(), 'floating_ip_address': '8.8.8.8', 'fixed_ip_address': '7.7.7.7', 'port_id': _uuid()}]} router = { 'id': router_id, l3_constants.FLOATINGIP_KEY: fake_floatingips1['floatingips'], l3_constants.INTERFACE_KEY: [internal_port], 'routes': [], 'gw_port': ex_gw_port} ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces, router=router) agent.process_router(ri) # remap floating IP to a new fixed ip fake_floatingips2 = copy.deepcopy(fake_floatingips1) fake_floatingips2['floatingips'][0]['fixed_ip_address'] = '7.7.7.8' router[l3_constants.FLOATINGIP_KEY] = fake_floatingips2['floatingips'] agent.process_router(ri) # remove just the floating ips del router[l3_constants.FLOATINGIP_KEY] agent.process_router(ri) # now no ports so state is torn down del router[l3_constants.INTERFACE_KEY] del router['gw_port'] agent.process_router(ri)
def testSingleLoopRouterRemoval(self): agent = l3_agent.L3NATAgent(self.conf) self.client_inst.list_ports.return_value = {'ports': []} self.client_inst.list_routers.return_value = {'routers': [ {'id': _uuid()}]} agent.do_single_loop() self.client_inst.list_routers.return_value = {'routers': []} agent.do_single_loop() # verify that remove is called self.assertEquals(self.mock_ip.get_devices.call_count, 1)
def testDaemonLoop(self): # just take a pass through the loop, then raise on time.sleep() time_sleep_p = mock.patch('time.sleep') time_sleep = time_sleep_p.start() class ExpectedException(Exception): pass time_sleep.side_effect = ExpectedException() agent = l3_agent.L3NATAgent(self.conf) self.assertRaises(ExpectedException, agent.daemon_loop) time_sleep_p.stop()
def testSingleLoopRouterRemoval(self): agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) self.plugin_api.get_external_network_id.return_value = None routers = [ {'id': _uuid(), 'admin_state_up': True, 'routes': [], 'external_gateway_info': {}}] agent._process_routers(routers) agent.router_deleted(None, routers[0]['id']) # verify that remove is called self.assertEqual(self.mock_ip.get_devices.call_count, 1) self.device_exists.assert_has_calls( [mock.call(self.conf.external_network_bridge)])
def testDestroyNamespace(self): class FakeDev(object): def __init__(self, name): self.name = name self.mock_ip.get_namespaces.return_value = ['qrouter-foo', 'qrouter-bar'] self.mock_ip.get_devices.return_value = [FakeDev('qr-aaaa'), FakeDev('qgw-aaaa')] agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) agent._destroy_router_namespace = mock.MagicMock() agent._destroy_router_namespaces() self.assertEqual(agent._destroy_router_namespace.call_count, 2)
def _test_external_gateway_action(self, action): router_id = _uuid() ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces, None) agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) internal_cidrs = ['100.0.1.0/24', '200.74.0.0/16'] ex_gw_port = { 'fixed_ips': [{ 'ip_address': '20.0.0.30', 'subnet_id': _uuid() }], 'subnet': { 'gateway_ip': '20.0.0.1' }, 'id': _uuid(), 'network_id': _uuid(), 'mac_address': 'ca:fe:de:ad:be:ef', 'ip_cidr': '20.0.0.30/24' } interface_name = agent.get_external_device_name(ex_gw_port['id']) if action == 'add': self.device_exists.return_value = False agent.external_gateway_added(ri, ex_gw_port, interface_name, internal_cidrs) self.assertEqual(self.mock_driver.plug.call_count, 1) self.assertEqual(self.mock_driver.init_l3.call_count, 1) arping_cmd = [ 'arping', '-A', '-U', '-I', interface_name, '-c', self.conf.send_arp_for_ha, '20.0.0.30' ] if self.conf.use_namespaces: self.mock_ip.netns.execute.assert_any_call( arping_cmd, check_exit_code=True) else: self.utils_exec.assert_any_call( check_exit_code=True, root_helper=self.conf.root_helper) elif action == 'remove': self.device_exists.return_value = True agent.external_gateway_removed(ri, ex_gw_port, interface_name, internal_cidrs) self.assertEqual(self.mock_driver.unplug.call_count, 1) else: raise Exception("Invalid action %s" % action)
def test_process_router_snat_enabled(self): agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) router = self._prepare_router_data(enable_snat=False) ri = l3_agent.RouterInfo(router['id'], self.conf.root_helper, self.conf.use_namespaces, router=router) # Process with NAT agent.process_router(ri) orig_nat_rules = ri.iptables_manager.ipv4['nat'].rules[:] # Reprocess without NAT router['enable_snat'] = True # Reassign the router object to RouterInfo ri.router = router agent.process_router(ri) nat_rules_delta = (set(ri.iptables_manager.ipv4['nat'].rules) - set(orig_nat_rules)) self.assertEqual(len(nat_rules_delta), 2) self._verify_snat_rules(nat_rules_delta, router)
def _configure_metadata_proxy(self, enableflag=True): if not enableflag: self.conf.set_override('enable_metadata_proxy', False) agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) router_id = _uuid() router = {'id': _uuid(), 'external_gateway_info': {}, 'routes': []} with mock.patch.object(agent, '_destroy_metadata_proxy') as destroy_proxy: with mock.patch.object(agent, '_spawn_metadata_proxy') as spawn_proxy: agent._router_added(router_id, router) if enableflag: spawn_proxy.assert_called_with(mock.ANY) else: self.assertFalse(spawn_proxy.call_count) agent._router_removed(router_id) if enableflag: destroy_proxy.assert_called_with(mock.ANY) else: self.assertFalse(destroy_proxy.call_count)
def _test_internal_network_action(self, action): port_id = _uuid() router_id = _uuid() network_id = _uuid() ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces, None) agent = l3_agent.L3NATAgent(HOSTNAME, self.conf) cidr = '99.0.1.9/24' mac = 'ca:fe:de:ad:be:ef' if action == 'add': self.device_exists.return_value = False agent.internal_network_added(ri, network_id, port_id, cidr, mac) self.assertEqual(self.mock_driver.plug.call_count, 1) self.assertEqual(self.mock_driver.init_l3.call_count, 1) elif action == 'remove': self.device_exists.return_value = True agent.internal_network_removed(ri, port_id, cidr) self.assertEqual(self.mock_driver.unplug.call_count, 1) else: raise Exception("Invalid action %s" % action)
def testSingleLoopRouterRemoval(self): agent = l3_agent.L3NATAgent(self.conf) self.client_inst.list_ports.return_value = {'ports': []} self.client_inst.list_networks.return_value = {'networks': []} self.client_inst.list_routers.return_value = {'routers': [ {'id': _uuid(), 'admin_state_up': True, 'external_gateway_info': {}}]} agent.do_single_loop() self.client_inst.list_routers.return_value = {'routers': []} agent.do_single_loop() # verify that remove is called self.assertEquals(self.mock_ip.get_devices.call_count, 1) self.device_exists.assert_has_calls( [mock.call(self.conf.external_network_bridge)])
def _test_floating_ip_action(self, action): router_id = _uuid() ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces) agent = l3_agent.L3NATAgent(self.conf) floating_ip = '20.0.0.100' fixed_ip = '10.0.0.23' ex_gw_port = {'fixed_ips': [{'ip_address': '20.0.0.30', 'subnet_id': _uuid()}], 'subnet': {'gateway_ip': '20.0.0.1'}, 'id': _uuid(), 'mac_address': 'ca:fe:de:ad:be:ef', 'ip_cidr': '20.0.0.30/24'} if action == 'add': self.device_exists.return_value = False agent.floating_ip_added(ri, ex_gw_port, floating_ip, fixed_ip) elif action == 'remove': self.device_exists.return_value = True agent.floating_ip_removed(ri, ex_gw_port, floating_ip, fixed_ip) else: raise Exception("Invalid action %s" % action)
def _test_internal_network_action(self, action): port_id = _uuid() router_id = _uuid() network_id = _uuid() ri = l3_agent.RouterInfo(router_id, self.conf.root_helper, self.conf.use_namespaces) agent = l3_agent.L3NATAgent(self.conf) interface_name = agent.get_internal_device_name(port_id) cidr = '99.0.1.9/24' mac = 'ca:fe:de:ad:be:ef' ex_gw_port = {'fixed_ips': [{'ip_address': '20.0.0.30'}]} if action == 'add': self.device_exists.return_value = False agent.internal_network_added(ri, ex_gw_port, network_id, port_id, cidr, mac) self.assertEquals(self.mock_driver.plug.call_count, 1) self.assertEquals(self.mock_driver.init_l3.call_count, 1) elif action == 'remove': self.device_exists.return_value = True agent.internal_network_removed(ri, ex_gw_port, port_id, cidr) self.assertEquals(self.mock_driver.unplug.call_count, 1) else: raise Exception("Invalid action %s" % action)
def testAgentCreate(self): agent = l3_agent.L3NATAgent(self.conf)
def testAgentCreate(self): l3_agent.L3NATAgent(HOSTNAME, self.conf)
def testAgentCreate(self): agent = l3_agent.L3NATAgent(self.conf) self.device_exists.assert_has_calls( [mock.call(self.conf.external_network_bridge)])