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_spawn_metadata_proxy(self): router_id = _uuid() metadata_port = 8080 ip_class_path = 'quantum.agent.linux.ip_lib.IPWrapper' cfg.CONF.set_override('metadata_port', metadata_port) cfg.CONF.set_override('log_file', 'test.log') cfg.CONF.set_override('debug', True) router_info = l3_agent.RouterInfo(router_id, cfg.CONF.root_helper, cfg.CONF.use_namespaces, None) self.external_process_p.stop() try: with mock.patch(ip_class_path) as ip_mock: self.agent._spawn_metadata_proxy(router_info) ip_mock.assert_has_calls([ mock.call('sudo', 'qrouter-' + router_id), mock.call().netns.execute([ 'quantum-ns-metadata-proxy', mock.ANY, '--router_id=%s' % router_id, mock.ANY, '--metadata_port=%s' % metadata_port, '--debug', '--log-file=quantum-ns-metadata-proxy-%s.log' % router_id ]) ]) finally: self.external_process_p.start()
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 _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 _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 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 _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 _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 _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 test_router_info_create_with_router(self): 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' } } router = { 'id': _uuid(), 'enable_snat': True, 'routes': [], 'gw_port': ex_gw_port } ri = l3_agent.RouterInfo(id, self.conf.root_helper, self.conf.use_namespaces, router) self.assertTrue(ri.ns_name().endswith(id)) self.assertEqual(ri.router, router)
def testRouterInfoCreate(self): id = _uuid() ri = l3_agent.RouterInfo(id, self.conf.root_helper, self.conf.use_namespaces) self.assertTrue(ri.ns_name().endswith(id))