Ejemplo n.º 1
0
def test_next_hop_str():
    nhop = next_hop.NextHop("if1", ipaddress.IPv4Address("1.2.3.4"))
    assert str(nhop) == "if1 1.2.3.4"
    nhop = next_hop.NextHop("if1", ipaddress.IPv6Address("1111:1111::"))
    assert str(nhop) == "if1 1111:1111::"
    nhop = next_hop.NextHop("if2", None)
    assert str(nhop) == "if2"
    nhop = next_hop.NextHop(None, None)
    assert str(nhop) == ""
Ejemplo n.º 2
0
def test_put_del_route_unreachable():
    kern = kernel.Kernel(log=None, log_id="", table_name="main")
    if not kern.platform_supported:
        return
    # Add unreachable prefix in the kernel routing table (no next hops)
    prefix = packet_common.make_ip_prefix("99.99.99.99/32")
    nhops = []
    rte = fib_route.FibRoute(prefix, nhops)
    assert kern.put_route(rte)
    tab_str = kern.cli_route_prefix_table(254, prefix).to_string()
    pattern = (r"[|] Table +[|] Main +[|]\n"
               r"[|] Address Family +[|] IPv4 +[|]\n"
               r"[|] Destination +[|] 99\.99\.99\.99/32 +[|]\n"
               r"[|] Type +[|] Unreachable +[|]\n"
               r"[|] Protocol +[|] RIFT +[|]\n"
               r"[|] Scope +[|] Universe +[|]\n"
               r"[|] Next-hops +[|]  +[|]\n"
               r"[|] Priority +[|] 199 +[|]\n")
    assert re.search(pattern, tab_str) is not None
    # Replace unreachable route with a route containing one next hop
    new_nhops = [next_hop.NextHop("lo", packet_common.make_ip_address("127.0.0.1"))]
    rte = fib_route.FibRoute(prefix, new_nhops)
    assert kern.put_route(rte)
    tab_str = kern.cli_route_prefix_table(254, prefix).to_string()
    pattern = (r"[|] Table +[|] Main +[|]\n"
               r"[|] Address Family +[|] IPv4 +[|]\n"
               r"[|] Destination +[|] 99\.99\.99\.99/32 +[|]\n"
               r"[|] Type +[|] Unicast +[|]\n"
               r"[|] Protocol +[|] RIFT +[|]\n"
               r"[|] Scope +[|] Universe +[|]\n"
               r"[|] Next-hops +[|] lo 127.0.0.1 +[|]\n"
               r"[|] Priority +[|] 199 +[|]\n")
    assert re.search(pattern, tab_str) is not None
    # Delete next hops
    assert kern.del_route(prefix)
Ejemplo n.º 3
0
def test_put_del_route_errors():
    kern = kernel.Kernel(log=None, log_id="", table_name="main")
    if not kern.platform_supported:
        return
    # Attempt to add route with nonsense next-hop interface
    prefix = packet_common.make_ip_prefix("99.99.99.99/32")
    nhops = [next_hop.NextHop("nonsense", None)]
    rte = fib_route.FibRoute(prefix, nhops)
    assert not kern.put_route(rte)
Ejemplo n.º 4
0
def test_put_del_route():
    kern = kernel.Kernel(log=None, log_id="", table_name="5")
    if not kern.platform_supported:
        return
    # Put route with one next-hop (with interface but no address)
    prefix = packet_common.make_ip_prefix("99.99.99.99/32")
    nhops = [next_hop.NextHop("lo", None)]
    rte = fib_route.FibRoute(prefix, nhops)
    assert kern.put_route(rte)
    # Delete route just added
    assert kern.del_route(prefix)
    # Put route with one next-hop (with interface and address)
    prefix = packet_common.make_ip_prefix("99.99.99.99/32")
    address = packet_common.make_ip_address("127.0.0.1")
    nhops = [next_hop.NextHop("lo", address)]
    rte = fib_route.FibRoute(prefix, nhops)
    assert kern.put_route(rte)
    # Delete route just added
    assert kern.del_route(prefix)
    # Put ECMP route with multiple next-hop (with interface and address)
    prefix = packet_common.make_ip_prefix("99.99.99.99/32")
    address1 = packet_common.make_ip_address("127.0.0.1")
    address2 = packet_common.make_ip_address("127.0.0.2")
    nhops = [next_hop.NextHop("lo", address1), next_hop.NextHop("lo", address2)]
    rte = fib_route.FibRoute(prefix, nhops)
    assert kern.put_route(rte)
    # Do we display the ECMP route properly?
    tab_str = kern.cli_route_prefix_table(5, prefix).to_string()
    pattern = (r"[|] Table +[|] 5 +[|]\n"
               r"[|] Address Family +[|] IPv4 +[|]\n"
               r"[|] Destination +[|] 99\.99\.99\.99/32 +[|]\n"
               r"[|] Type +[|] Unicast +[|]\n"
               r"[|] Protocol +[|] RIFT +[|]\n"
               r"[|] Scope +[|] Universe +[|]\n"
               r"[|] Next-hops +[|] lo 127\.0\.0\.1 1 +[|]\n"
               r"[|] +[|] lo 127\.0\.0\.2 1 +[|]\n")
    assert re.search(pattern, tab_str) is not None
    # Delete route just added
    assert kern.del_route(prefix)
Ejemplo n.º 5
0
def test_next_hop_ordering():
    nhop1 = next_hop.NextHop(None, None)
    nhop2 = next_hop.NextHop("if1", None)
    nhop3 = next_hop.NextHop("if1", ipaddress.IPv4Address("1.1.1.1"))
    nhop4 = next_hop.NextHop("if1", ipaddress.IPv4Address("2.2.2.2"))
    nhop5 = next_hop.NextHop("if2", ipaddress.IPv4Address("1.1.1.1"))
    nhop6 = next_hop.NextHop("if2", ipaddress.IPv6Address("1111:1111::"))
    assert nhop1 < nhop2
    assert nhop2 < nhop3
    assert nhop3 < nhop4
    assert nhop4 < nhop5
    assert nhop5 < nhop6
    # pylint:disable=unneeded-not
    assert not nhop2 < nhop1
    assert not nhop3 < nhop2
    assert not nhop4 < nhop3
    assert not nhop5 < nhop4
    assert not nhop6 < nhop5
Ejemplo n.º 6
0
def mknh(interface_str, address_str):
    if address_str is None:
        address = None
    else:
        address = packet_common.make_ip_address(address_str)
    return next_hop.NextHop(interface_str, address)