Пример #1
0
    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'
        interface_name = agent.get_internal_device_name(port_id)

        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)
            self.send_arp.assert_called_once_with(ri, interface_name,
                                                  '99.0.1.9')
        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)
Пример #2
0
    def _test_floating_ip_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)
        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)
Пример #3
0
    def _test_floating_ip_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)
        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)
            self.send_arp.assert_called_once_with(ri, interface_name,
                                                  floating_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)
Пример #4
0
    def test_destroy_namespace_with_router_id(self):

        class FakeDev(object):
            def __init__(self, name):
                self.name = name

        self.conf.router_id = _uuid()

        namespaces = ['qrouter-foo', 'qrouter-' + self.conf.router_id]

        self.mock_ip.get_namespaces.return_value = namespaces
        self.mock_ip.get_devices.return_value = [FakeDev('qr-aaaa'),
                                                 FakeDev('qgw-aaaa')]

        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)

        pm = self.external_process.return_value
        pm.reset_mock()

        agent._destroy_router_namespace = mock.MagicMock()
        agent._destroy_router_namespaces(self.conf.router_id)

        self.assertEqual(pm.disable.call_count, 1)

        self.assertEqual(agent._destroy_router_namespace.call_count, 1)
Пример #5
0
 def test_process_router_interface_removed(self):
     agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
     router = self._prepare_router_data(num_internal_ports=2)
     ri = l3_agent.RouterInfo(router['id'],
                              self.conf.root_helper,
                              self.conf.use_namespaces,
                              router=router)
     agent.external_gateway_added = mock.Mock()
     # Process with NAT
     agent.process_router(ri)
     orig_nat_rules = ri.iptables_manager.ipv4['nat'].rules[:]
     # Add an interface and reprocess
     del router[l3_constants.INTERFACE_KEY][1]
     # Reassign the router object to RouterInfo
     ri.router = router
     agent.process_router(ri)
     # For some reason set logic does not work well with
     # IpTablesRule instances
     nat_rules_delta = [
         r for r in orig_nat_rules
         if r not in ri.iptables_manager.ipv4['nat'].rules
     ]
     self.assertEqual(len(nat_rules_delta), 1)
     self._verify_snat_rules(nat_rules_delta, router, negate=True)
     self.send_arp.assert_called_once()
Пример #6
0
    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)
Пример #7
0
    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)
Пример #8
0
 def test_process_router_interface_added(self):
     agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
     router = self._prepare_router_data()
     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[:]
     # Add an interface and reprocess
     router[l3_constants.INTERFACE_KEY].append(
         {'id': _uuid(),
          'network_id': _uuid(),
          'admin_state_up': True,
          'fixed_ips': [{'ip_address': '35.4.1.4',
                         'subnet_id': _uuid()}],
          'mac_address': 'ca:fe:de:ad:be:ef',
          'subnet': {'cidr': '35.4.1.0/24',
                     'gateway_ip': '35.4.1.1'}})
     # Reassign the router object to RouterInfo
     ri.router = router
     agent.process_router(ri)
     # For some reason set logic does not work well with
     # IpTablesRule instances
     nat_rules_delta = [r for r in ri.iptables_manager.ipv4['nat'].rules
                        if r not in orig_nat_rules]
     self.assertEqual(len(nat_rules_delta), 1)
     self._verify_snat_rules(nat_rules_delta, router)
Пример #9
0
    def test_process_router_internal_network_removed_unexpected_error(self):
        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
        router = self._prepare_router_data()
        ri = l3_agent.RouterInfo(router['id'],
                                 self.conf.root_helper,
                                 self.conf.use_namespaces,
                                 router=router)
        agent.external_gateway_added = mock.Mock()
        # add an internal port
        agent.process_router(ri)

        with mock.patch.object(
                l3_agent.L3NATAgent,
                'internal_network_removed') as internal_net_removed:
            # raise RuntimeError to simulate that an unexpected exception
            # occurrs
            internal_net_removed.side_effect = RuntimeError
            ri.internal_ports[0]['admin_state_up'] = False
            # The above port is set to down state, remove it.
            self.assertRaises(RuntimeError, agent.process_router, ri)
            self.assertIn(router[l3_constants.INTERFACE_KEY][0],
                          ri.internal_ports)

            # The unexpected exception has been fixed manually
            internal_net_removed.side_effect = None

            # _sync_routers_task finds out that _rpc_loop failed to process the
            # router last time, it will retry in the next run.
            agent.process_router(ri)
            # We were able to remove the port from ri.internal_ports
            self.assertNotIn(router[l3_constants.INTERFACE_KEY][0],
                             ri.internal_ports)
Пример #10
0
    def test_process_router_floating_ip_remap(self, IPDevice):
        fip = {
            'id': _uuid(), 'port_id': _uuid(),
            'floating_ip_address': '15.1.2.3',
            'fixed_ip_address': '192.168.0.2'
        }

        IPDevice.return_value = device = mock.Mock()
        device.addr.list.return_value = [{'cidr': '15.1.2.3/32'}]
        ri = mock.MagicMock()

        ri.router.get.return_value = [fip]

        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)

        agent.process_router_floating_ips(ri, {'id': _uuid()})

        self.assertFalse(device.addr.add.called)
        self.assertFalse(device.addr.delete.called)

        nat = ri.iptables_manager.ipv4['nat']
        nat.clear_rules_by_tag.assert_called_once_with('floating_ip')
        rules = agent.floating_forward_rules('15.1.2.3', '192.168.0.2')
        for chain, rule in rules:
            nat.add_rule.assert_any_call(chain, rule, tag='floating_ip')
Пример #11
0
    def setUp(self):
        super(TestL3AgentEventHandler, self).setUp()
        cfg.CONF.register_opts(l3_agent.L3NATAgent.OPTS)
        cfg.CONF.set_override('interface_driver',
                              'neutron.agent.linux.interface.NullDriver')
        cfg.CONF.set_override('use_namespaces', True)
        agent_config.register_root_helper(cfg.CONF)

        self.device_exists_p = mock.patch(
            'neutron.agent.linux.ip_lib.device_exists')
        self.device_exists = self.device_exists_p.start()

        self.utils_exec_p = mock.patch('neutron.agent.linux.utils.execute')
        self.utils_exec = self.utils_exec_p.start()

        self.drv_cls_p = mock.patch('neutron.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('neutron.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(
            'neutron.agent.linux.external_process.ProcessManager')
        self.external_process = self.external_process_p.start()

        self.agent = l3_agent.L3NATAgent(HOSTNAME)
Пример #12
0
 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)
     agent.external_gateway_added = mock.Mock()
     # Process without NAT
     agent.process_router(ri)
     orig_nat_rules = ri.iptables_manager.ipv4['nat'].rules[:]
     # Reprocess with NAT
     router['enable_snat'] = True
     # Reassign the router object to RouterInfo
     ri.router = router
     agent.process_router(ri)
     # For some reason set logic does not work well with
     # IpTablesRule instances
     nat_rules_delta = [
         r for r in ri.iptables_manager.ipv4['nat'].rules
         if r not in orig_nat_rules
     ]
     self.assertEqual(len(nat_rules_delta), 2)
     self._verify_snat_rules(nat_rules_delta, router)
     self.send_arp.assert_called_once()
Пример #13
0
    def test_process_router(self):
        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
        fake_fip_id = 'fake_fip_id'
        agent.process_router_floating_ip_addresses = mock.Mock()
        agent.process_router_floating_ip_nat_rules = mock.Mock()
        agent.process_router_floating_ip_addresses.return_value = {
            fake_fip_id: 'ACTIVE'
        }
        agent.external_gateway_added = mock.Mock()
        router = self._prepare_router_data()
        fake_floatingips1 = {
            'floatingips': [{
                'id': fake_fip_id,
                '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)
        ex_gw_port = agent._get_ex_gw_port(ri)
        agent.process_router_floating_ip_addresses.assert_called_with(
            ri, ex_gw_port)
        agent.process_router_floating_ip_addresses.reset_mock()
        agent.process_router_floating_ip_nat_rules.assert_called_with(ri)
        agent.process_router_floating_ip_nat_rules.reset_mock()

        # 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)
        ex_gw_port = agent._get_ex_gw_port(ri)
        agent.process_router_floating_ip_addresses.assert_called_with(
            ri, ex_gw_port)
        agent.process_router_floating_ip_addresses.reset_mock()
        agent.process_router_floating_ip_nat_rules.assert_called_with(ri)
        agent.process_router_floating_ip_nat_rules.reset_mock()

        # remove just the floating ips
        del router[l3_constants.FLOATINGIP_KEY]
        agent.process_router(ri)
        ex_gw_port = agent._get_ex_gw_port(ri)
        agent.process_router_floating_ip_addresses.assert_called_with(
            ri, ex_gw_port)
        agent.process_router_floating_ip_addresses.reset_mock()
        agent.process_router_floating_ip_nat_rules.assert_called_with(ri)
        agent.process_router_floating_ip_nat_rules.reset_mock()

        # now no ports so state is torn down
        del router[l3_constants.INTERFACE_KEY]
        del router['gw_port']
        agent.process_router(ri)
        self.send_arp.assert_called_once()
        self.assertFalse(agent.process_router_floating_ip_addresses.called)
        self.assertFalse(agent.process_router_floating_ip_nat_rules.called)
Пример #14
0
    def setUp(self):
        super(TestL3AgentEventHandler, self).setUp()
        cfg.CONF.register_opts(l3_agent.L3NATAgent.OPTS)
        cfg.CONF.set_override('interface_driver',
                              'neutron.agent.linux.interface.NullDriver')
        cfg.CONF.set_override('use_namespaces', True)
        agent_config.register_root_helper(cfg.CONF)

        device_exists_p = mock.patch(
            'neutron.agent.linux.ip_lib.device_exists')
        device_exists_p.start()

        utils_exec_p = mock.patch('neutron.agent.linux.utils.execute')
        utils_exec_p.start()

        drv_cls_p = mock.patch('neutron.agent.linux.interface.NullDriver')
        driver_cls = drv_cls_p.start()
        mock_driver = mock.MagicMock()
        mock_driver.DEV_NAME_LEN = (
            interface.LinuxInterfaceDriver.DEV_NAME_LEN)
        driver_cls.return_value = mock_driver

        l3_plugin_p = mock.patch('neutron.agent.l3_agent.L3PluginApi')
        l3_plugin_cls = l3_plugin_p.start()
        l3_plugin_cls.return_value = mock.Mock()

        self.external_process_p = mock.patch(
            'neutron.agent.linux.external_process.ProcessManager')
        self.external_process_p.start()
        looping_call_p = mock.patch(
            'neutron.openstack.common.loopingcall.FixedIntervalLoopingCall')
        looping_call_p.start()
        self.agent = l3_agent.L3NATAgent(HOSTNAME)
        self.addCleanup(mock.patch.stopall)
Пример #15
0
    def _cleanup_namespace_test(self, stale_namespace_list, router_list,
                                other_namespaces):
        self.conf.set_override('router_delete_namespaces', True)

        good_namespace_list = [
            l3_agent.NS_PREFIX + r['id'] for r in router_list
        ]
        self.mock_ip.get_namespaces.return_value = (stale_namespace_list +
                                                    good_namespace_list +
                                                    other_namespaces)

        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)

        self.assertTrue(agent._delete_stale_namespaces)

        pm = self.external_process.return_value
        pm.reset_mock()

        agent._destroy_router_namespace = mock.MagicMock()
        agent._cleanup_namespaces(router_list)

        self.assertEqual(pm.disable.call_count, len(stale_namespace_list))
        self.assertEqual(agent._destroy_router_namespace.call_count,
                         len(stale_namespace_list))
        expected_args = [mock.call(ns) for ns in stale_namespace_list]
        agent._destroy_router_namespace.assert_has_calls(expected_args,
                                                         any_order=True)
        self.assertFalse(agent._delete_stale_namespaces)
Пример #16
0
    def test_process_routers_with_no_bridge_no_ext_net_in_conf(self):
        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
        self.plugin_api.get_external_network_id.return_value = 'aaa'

        routers = [{
            'id': _uuid(),
            'routes': [],
            'admin_state_up': True,
            'external_gateway_info': {
                'network_id': 'aaa'
            }
        }, {
            'id': _uuid(),
            'routes': [],
            'admin_state_up': True,
            'external_gateway_info': {
                'network_id': 'bbb'
            }
        }]

        agent.router_info = {}
        self.conf.set_override('external_network_bridge', '')
        agent._process_routers(routers)
        self.assertIn(routers[0]['id'], agent.router_info)
        self.assertIn(routers[1]['id'], agent.router_info)
Пример #17
0
    def test_router_id_specified_in_conf(self):
        self.conf.set_override('use_namespaces', False)
        self.conf.set_override('router_id', '')
        self.assertRaises(SystemExit, l3_agent.L3NATAgent, HOSTNAME, self.conf)

        self.conf.set_override('router_id', '1234')
        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
        self.assertEqual(['1234'], agent._router_ids())
Пример #18
0
    def test_routers_with_admin_state_down(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)
Пример #19
0
    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)
Пример #20
0
    def test_process_router_floating_ip_nat_rules_remove(self):
        ri = mock.MagicMock()
        ri.router.get.return_value = []

        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)

        agent.process_router_floating_ip_nat_rules(ri)

        nat = ri.iptables_manager.ipv4['nat']
        nat = ri.iptables_manager.ipv4['nat`']
        nat.clear_rules_by_tag.assert_called_once_with('floating_ip')
Пример #21
0
    def test_process_router_floating_ip_addresses_remove(self, IPDevice):
        IPDevice.return_value = device = mock.Mock()
        device.addr.list.return_value = [{'cidr': '15.1.2.3/32'}]

        ri = mock.MagicMock()
        ri.router.get.return_value = []

        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)

        agent.process_router_floating_ip_addresses(ri, {'id': _uuid()})

        device.addr.delete.assert_called_once_with(4, '15.1.2.3/32')
Пример #22
0
    def test_process_routers_with_no_ext_net_in_conf_and_two_net_plugin(self):
        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)

        routers = [
            {'id': _uuid(),
             'routes': [],
             'admin_state_up': True,
             'external_gateway_info': {'network_id': 'aaa'}}]

        agent.router_info = {}
        self.plugin_api.get_external_network_id.side_effect = (
            n_exc.TooManyExternalNetworks())
        self.assertRaises(n_exc.TooManyExternalNetworks,
                          agent._process_routers,
                          routers)
        self.assertNotIn(routers[0]['id'], agent.router_info)
Пример #23
0
    def test_handle_router_snat_rules_add_back_jump(self):
        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
        ri = mock.MagicMock()
        port = {'fixed_ips': [{'ip_address': '192.168.1.4'}]}

        agent._handle_router_snat_rules(ri, port, [], "iface", "add_rules")

        nat = ri.iptables_manager.ipv4['nat']
        nat.empty_chain.assert_any_call('snat')
        nat.add_rule.assert_any_call('snat', '-j $float-snat')
        for call in nat.mock_calls:
            name, args, kwargs = call
            if name == 'add_rule':
                self.assertEqual(args, ('snat', '-j $float-snat'))
                self.assertEqual(kwargs, {})
                break
Пример #24
0
    def test_process_router_floating_ip_remove(self, IPDevice):
        IPDevice.return_value = device = mock.Mock()
        device.addr.list.return_value = [{'cidr': '15.1.2.3/32'}]

        ri = mock.MagicMock()
        ri.router.get.return_value = []

        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)

        agent.process_router_floating_ips(ri, {'id': _uuid()})

        device.addr.delete.assert_called_once_with(4, '15.1.2.3/32')

        nat = ri.iptables_manager.ipv4['nat']
        nat = ri.iptables_manager.ipv4['nat']
        nat.clear_rules_by_tag.assert_called_once_with('floating_ip')
Пример #25
0
    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
            ri.router = mock.Mock()
            ri.router.get.return_value = [{
                'floating_ip_address': '192.168.1.34'
            }]
            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)
            self.send_arp.assert_called_once_with(ri, interface_name,
                                                  '20.0.0.30')
            kwargs = {
                'preserve_ips': ['192.168.1.34/32'],
                'namespace': 'qrouter-' + router_id
            }
            self.mock_driver.init_l3.assert_called_with(
                interface_name, ['20.0.0.30/24'], **kwargs)

        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)
Пример #26
0
    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)
Пример #27
0
 def test_process_router_delete(self):
     agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
     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}
     agent._router_added(router['id'], router)
     agent.router_deleted(None, router['id'])
     agent._process_router_delete()
     self.assertFalse(list(agent.removed_routers))
Пример #28
0
    def test_process_routers_with_cached_ext_net(self):
        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
        self.plugin_api.get_external_network_id.return_value = 'aaa'
        agent.target_ex_net_id = 'aaa'

        routers = [{
            'id': _uuid(),
            'routes': [],
            'admin_state_up': True,
            'external_gateway_info': {
                'network_id': 'aaa'
            }
        }]

        agent._process_routers(routers)
        self.assertIn(routers[0]['id'], agent.router_info)
        self.assertFalse(self.plugin_api.get_external_network_id.called)
Пример #29
0
    def _test_arping(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)
        floating_ip = '20.0.0.101'
        interface_name = agent.get_external_device_name(router_id)
        agent._arping(ri, interface_name, floating_ip)

        arping_cmd = [
            'arping', '-A', '-I', interface_name, '-c',
            self.conf.send_arp_for_ha, floating_ip
        ]
        self.mock_ip.netns.execute.assert_any_call(arping_cmd,
                                                   check_exit_code=True)
Пример #30
0
    def test_process_router_with_disabled_floating_ip(self, IPDevice):
        fip_id = _uuid()
        fip = {
            'id': fip_id,
            'port_id': _uuid(),
            'floating_ip_address': '15.1.2.3',
            'fixed_ip_address': '192.168.0.2'
        }

        ri = mock.MagicMock()
        ri.floating_ips = [fip]
        ri.router.get.return_value = []

        agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)

        fip_statuses = agent.process_router_floating_ip_addresses(
            ri, {'id': _uuid()})

        self.assertIsNone(fip_statuses.get(fip_id))