def test_floating_ip_removed_dist(self, mIPRule, mIPDevice, mIPWrapper):
        router = mock.MagicMock()
        ri = self._create_router(router)

        subnet_id = _uuid()
        agent_gw_port = {
            'fixed_ips': [{
                'ip_address': '20.0.0.30',
                'prefixlen': 24,
                'subnet_id': subnet_id
            }],
            'subnets': [{
                'id': subnet_id,
                'cidr': '20.0.0.0/24',
                'gateway_ip': '20.0.0.1'
            }],
            'id':
            _uuid(),
            'network_id':
            _uuid(),
            'mac_address':
            'ca:fe:de:ad:be:ef'
        }
        fip_cidr = '11.22.33.44/24'

        ri.dist_fip_count = 2
        ri.fip_ns = mock.Mock()
        ri.fip_ns.get_name.return_value = 'fip_ns_name'
        ri.floating_ips_dict['11.22.33.44'] = FIP_PRI
        ri.fip_2_rtr = '11.22.33.42'
        ri.rtr_2_fip = '11.22.33.40'
        ri.fip_ns.agent_gateway_port = agent_gw_port
        s = lla.LinkLocalAddressPair('169.254.30.42/31')
        ri.rtr_fip_subnet = s
        ri.fip_ns.local_subnets = mock.Mock()
        ri.floating_ip_removed_dist(fip_cidr)
        mIPRule().rule.delete.assert_called_with(ip=str(
            netaddr.IPNetwork(fip_cidr).ip),
                                                 table=16,
                                                 priority=FIP_PRI)
        mIPDevice().route.delete_route.assert_called_with(fip_cidr, str(s.ip))
        ri.fip_ns.local_subnets.allocate.assert_not_called()

        ri.dist_fip_count = 1
        s1 = lla.LinkLocalAddressPair('15.1.2.3/32')
        ri.rtr_fip_subnet = None
        ri.fip_ns.local_subnets.allocate.return_value = s1
        _, fip_to_rtr = s1.get_pair()
        fip_ns = ri.fip_ns
        ri.floating_ip_removed_dist(fip_cidr)
        self.assertTrue(fip_ns.destroyed)
        mIPWrapper().del_veth.assert_called_once_with(
            fip_ns.get_int_device_name(router['id']))
        mIPDevice().route.delete_gateway.assert_called_once_with(str(
            fip_to_rtr.ip),
                                                                 table=16)
        self.assertFalse(ri.fip_ns.unsubscribe.called)
        ri.fip_ns.local_subnets.allocate.assert_called_once_with(ri.router_id)
Beispiel #2
0
    def test_create_rtr_2_fip_link(self, device_exists, IPDevice, IPWrapper):
        ri = mock.Mock()
        ri.router_id = _uuid()
        ri.rtr_fip_subnet = None
        ri.ns_name = mock.sentinel.router_ns

        rtr_2_fip_name = self.fip_ns.get_rtr_ext_device_name(ri.router_id)
        fip_2_rtr_name = self.fip_ns.get_int_device_name(ri.router_id)
        fip_ns_name = self.fip_ns.get_name()

        self.fip_ns.local_subnets = allocator = mock.Mock()
        pair = lla.LinkLocalAddressPair('169.254.31.28/31')
        allocator.allocate.return_value = pair
        device_exists.return_value = False
        ip_wrapper = IPWrapper()
        self.conf.network_device_mtu = 2000
        ip_wrapper.add_veth.return_value = (IPDevice(), IPDevice())

        self.fip_ns.create_rtr_2_fip_link(ri)

        ip_wrapper.add_veth.assert_called_with(rtr_2_fip_name,
                                               fip_2_rtr_name,
                                               fip_ns_name)

        device = IPDevice()
        device.link.set_mtu.assert_called_with(2000)
        self.assertEqual(device.link.set_mtu.call_count, 2)
        device.route.add_gateway.assert_called_once_with(
            '169.254.31.29', table=16)
    def test_floating_ip_added_dist(self, mIpRule, mIPDevice, mock_arp):
        router = mock.MagicMock()
        ri = self._create_router(router)
        ext_net_id = _uuid()
        agent_gw_port = {
            'fixed_ips': [{
                'ip_address': '20.0.0.30',
                'subnet_id': _uuid()
            }],
            'subnet': {
                'gateway_ip': '20.0.0.1'
            },
            'id': _uuid(),
            'network_id': ext_net_id,
            'mac_address': 'ca:fe:de:ad:be:ef',
            'ip_cidr': '20.0.0.30/24'
        }

        fip = {
            'id': _uuid(),
            'host': HOSTNAME,
            'floating_ip_address': '15.1.2.3',
            'fixed_ip_address': '192.168.0.1',
            'floating_network_id': ext_net_id,
            'port_id': _uuid()
        }
        ri.fip_ns = mock.Mock()
        ri.fip_ns.agent_gateway_port = agent_gw_port
        ri.fip_ns.allocate_rule_priority.return_value = FIP_PRI
        ri.rtr_fip_subnet = lla.LinkLocalAddressPair('169.254.30.42/31')
        ri.dist_fip_count = 0
        ip_cidr = common_utils.ip_to_cidr(fip['floating_ip_address'])
        ri.floating_ip_added_dist(fip, ip_cidr)
        mIpRule().add.assert_called_with('192.168.0.1', 16, FIP_PRI)
        self.assertEqual(1, ri.dist_fip_count)
Beispiel #4
0
 def test_floating_ip_removed_dist(self, mIPDevice, mIPWrapper):
     router = mock.MagicMock()
     ri = self._create_router(router)
     ri.ex_gw_port = ri.router['gw_port']
     subnet_id = _uuid()
     fixed_ip = '20.0.0.30'
     agent_gw_port = {'fixed_ips': [{'ip_address': fixed_ip,
                                     'prefixlen': 24,
                                     'subnet_id': subnet_id}],
                      'subnets': [{'id': subnet_id,
                                   'cidr': '20.0.0.0/24',
                                   'gateway_ip': '20.0.0.1'}],
                      'id': _uuid(),
                      'network_id': _uuid(),
                      'mac_address': 'ca:fe:de:ad:be:ef'}
     fip_cidr = '11.22.33.44/24'
     ri.fip_ns = mock.Mock()
     ri.fip_ns.get_name.return_value = 'fip_ns_name'
     ri.floating_ips_dict['11.22.33.44'] = (fixed_ip, FIP_PRI)
     ri.fip_2_rtr = '11.22.33.42'
     ri.rtr_2_fip = '11.22.33.40'
     ri.fip_ns.agent_gateway_port = agent_gw_port
     s = lla.LinkLocalAddressPair('169.254.30.42/31')
     ri.rtr_fip_subnet = s
     ri.fip_ns.local_subnets = mock.Mock()
     ri.floating_ip_removed_dist(fip_cidr)
     self.mock_delete_ip_rule.assert_called_with(
         ri.router_namespace.name, ip=fixed_ip, table=16, priority=FIP_PRI)
     mIPDevice().route.delete_route.assert_called_with(fip_cidr,
                                                       via=str(s.ip))
     ri.fip_ns.local_subnets.allocate.assert_not_called()
    def test_floating_ip_added_dist(self, mIPRule, mIPDevice, mock_adv_notif):
        router = mock.MagicMock()
        ri = self._create_router(router)
        ri.ex_gw_port = ri.router['gw_port']
        ext_net_id = _uuid()
        subnet_id = _uuid()
        agent_gw_port = {
            'fixed_ips': [{
                'ip_address': '20.0.0.30',
                'prefixlen': 24,
                'subnet_id': subnet_id
            }],
            'subnets': [{
                'id': subnet_id,
                'cidr': '20.0.0.0/24',
                'gateway_ip': '20.0.0.1'
            }],
            'id':
            _uuid(),
            'network_id':
            ext_net_id,
            'mac_address':
            'ca:fe:de:ad:be:ef'
        }

        fip = {
            'id': _uuid(),
            'host': HOSTNAME,
            'floating_ip_address': '15.1.2.3',
            'fixed_ip_address': '192.168.0.1',
            'floating_network_id': ext_net_id,
            'port_id': _uuid()
        }
        ri.fip_ns = mock.Mock()
        ri.fip_ns.agent_gateway_port = agent_gw_port
        ri.create_dvr_external_gateway_on_agent(ri.ex_gw_port)
        ri._check_rtr_2_fip_connect = mock.Mock()
        ri.connect_rtr_2_fip()
        self.assertTrue(ri.rtr_fip_connect)
        ri.fip_ns.allocate_rule_priority.return_value = FIP_PRI
        subnet = lla.LinkLocalAddressPair('169.254.30.42/31')
        ri.rtr_fip_subnet = subnet
        ri.fip_ns.local_subnets = mock.Mock()
        ri.fip_ns.local_subnets.allocate.return_value = subnet
        ip_cidr = common_utils.ip_to_cidr(fip['floating_ip_address'])
        ri.floating_ip_added_dist(fip, ip_cidr)
        mIPRule().rule.add.assert_called_with(ip='192.168.0.1',
                                              table=16,
                                              priority=FIP_PRI)
        ri.fip_ns.local_subnets.allocate.assert_not_called()

        # Validate that fip_ns.local_subnets is called when
        # ri.rtr_fip_subnet is None
        ri.rtr_fip_subnet = None
        ri.floating_ip_added_dist(fip, ip_cidr)
        mIPRule().rule.add.assert_called_with(ip='192.168.0.1',
                                              table=16,
                                              priority=FIP_PRI)
        ri.fip_ns.local_subnets.allocate.assert_called_once_with(ri.router_id)
Beispiel #6
0
    def _test_create_rtr_2_fip_link(self, dev_exists, addr_exists, IPDevice,
                                    IPWrapper):
        ri = mock.Mock()
        ri.router_id = _uuid()
        ri.rtr_fip_subnet = None
        ri.ns_name = mock.sentinel.router_ns
        ri.get_ex_gw_port.return_value = {'mtu': 2000}

        fip_ns_name = self.fip_ns.get_name()

        self.fip_ns.local_subnets = allocator = mock.Mock()
        pair = lla.LinkLocalAddressPair('169.254.31.28/31')
        allocator.allocate.return_value = pair
        addr_pair = pair.get_pair()
        ip_wrapper = IPWrapper()
        ip_wrapper.add_veth.return_value = (IPDevice(), IPDevice())
        device = IPDevice()
        device.exists.return_value = dev_exists
        device.addr.list.return_value = addr_exists
        ri._get_snat_idx = mock.Mock()
        self.fip_ns._add_rtr_ext_route_rule_to_route_table = mock.Mock()
        self.fip_ns.create_rtr_2_fip_link(ri)

        if not dev_exists:
            ip_wrapper.add_veth.assert_called_with(device.name, device.name,
                                                   fip_ns_name)

            self.assertEqual(2, device.link.set_up.call_count)

        device.link.set_mtu.assert_called_with(2000)
        self.assertEqual(2, device.link.set_mtu.call_count)

        if not addr_exists:
            expected = [
                mock.call(str(addr_pair[0]), add_broadcast=False),
                mock.call(str(addr_pair[1]), add_broadcast=False)
            ]
            device.addr.add.assert_has_calls(expected)
            self.assertEqual(2, device.addr.add.call_count)

        expected = [
            mock.call(n_utils.cidr_to_ip(addr_pair[1]), mock.ANY),
            mock.call(n_utils.cidr_to_ip(addr_pair[0]), mock.ANY)
        ]
        device.neigh.add.assert_has_calls(expected)
        self.assertEqual(2, device.neigh.add.call_count)

        expected_calls = [
            mock.call('169.254.31.29', table=16),
            mock.call(self.lladdr)
        ]
        self.assertEqual(expected_calls, device.route.add_gateway.mock_calls)
        self.assertTrue(
            self.fip_ns._add_rtr_ext_route_rule_to_route_table.called)
Beispiel #7
0
    def test_create_rtr_2_fip_link_already_exists(self, device_exists,
                                                  IPDevice, IPWrapper):
        ri = mock.Mock()
        ri.router_id = _uuid()
        ri.rtr_fip_subnet = None
        device_exists.return_value = True

        self.fip_ns.local_subnets = allocator = mock.Mock()
        pair = lla.LinkLocalAddressPair('169.254.31.28/31')
        allocator.allocate.return_value = pair
        self.fip_ns.create_rtr_2_fip_link(ri)

        ip_wrapper = IPWrapper()
        self.assertFalse(ip_wrapper.add_veth.called)
Beispiel #8
0
    def _test_create_rtr_2_fip_link(self, dev_exists, addr_exists, IPDevice,
                                    IPWrapper):
        ri = mock.Mock()
        ri.router_id = _uuid()
        ri.rtr_fip_subnet = None
        ri.ns_name = mock.sentinel.router_ns

        rtr_2_fip_name = self.fip_ns.get_rtr_ext_device_name(ri.router_id)
        fip_2_rtr_name = self.fip_ns.get_int_device_name(ri.router_id)
        fip_ns_name = self.fip_ns.get_name()

        self.fip_ns.local_subnets = allocator = mock.Mock()
        pair = lla.LinkLocalAddressPair('169.254.31.28/31')
        allocator.allocate.return_value = pair
        addr_pair = pair.get_pair()
        ip_wrapper = IPWrapper()
        self.conf.network_device_mtu = 2000
        ip_wrapper.add_veth.return_value = (IPDevice(), IPDevice())
        device = IPDevice()
        device.exists.return_value = dev_exists
        device.addr.list.return_value = addr_exists

        self.fip_ns.create_rtr_2_fip_link(ri)

        if not dev_exists:
            ip_wrapper.add_veth.assert_called_with(rtr_2_fip_name,
                                                   fip_2_rtr_name, fip_ns_name)

            device.link.set_mtu.assert_called_with(2000)
            self.assertEqual(2, device.link.set_mtu.call_count)
            self.assertEqual(2, device.link.set_up.call_count)

        if not addr_exists:
            expected = [
                mock.call(str(addr_pair[0]), add_broadcast=False),
                mock.call(str(addr_pair[1]), add_broadcast=False)
            ]
            device.addr.add.assert_has_calls(expected)
            self.assertEqual(2, device.addr.add.call_count)

        device.route.add_gateway.assert_called_once_with('169.254.31.29',
                                                         table=16)