fix(routes): read route types and multipath next hops in ip route
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m16s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / test (3.13) (pull_request) Successful in 1m5s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 36s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Successful in 2m57s
driver / publish (pull_request) Skipped

get_route_to took the first word of every "ip route show" line as the
destination. For unreachable, blackhole, prohibit and throw routes that
word is the route type, so they came back as "unreachable/32" and the
like; a multipath route's continuation lines became "nexthop/32"
routes, and the route itself had no next hop.

The type word is now read as the type and reported in
protocol_attributes, without a next hop or outgoing interface for the
types that forward nothing (IPv6 names lo for them). nexthop lines
attach to the route before them, one entry per next hop. The test
fixture is ip's output (iproute2 6.1.0) recorded in a network
namespace set up with these routes.

Closes #57
This commit is contained in:
Christian Manivong committed 2026-11-02 08:41:56 +01:00
1 parent d3134c83b8
commit e4c65945b9
4 files changed
+205 -20

No files matched your search

+134
View File
@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patch
import pytest
from napalm_linux._routes import IpRoute, parse_ip_route
from napalm_linux.linux import (
_YUM_SECURITY_COMMAND,
LinuxDriver,
@@ -2341,3 +2342,136 @@ class TestRouteFamilies:
)
assert "2001:db8:2::7/128" in routes
#: ``ip -4 route show`` / ``ip -6 route show``, iproute2 6.1.0 on Linux 7.0 (Linux Mint
#: 22.3), recorded in a network namespace set up for it: a multipath default per
#: family, and unreachable, prohibit, throw and blackhole routes (#57). Documentation
#: addresses only. The trailing blanks and the tabs are ip's.
_IP4_ROUTE_TYPES = (
"default proto static metric 100 \n"
"\tnexthop via 192.0.2.1 dev eth0 weight 1 \n"
"\tnexthop via 198.51.100.1 dev eth1 weight 1 \n"
"unreachable 10.0.0.0/8 metric 1024 \n"
"prohibit 172.16.0.0/12 \n"
"192.0.2.0/24 dev eth0 proto kernel scope link src 192.0.2.10 \n"
"throw 192.168.0.0/16 \n"
"198.51.100.0/24 dev eth1 proto kernel scope link src 198.51.100.10 \n"
"blackhole 203.0.113.0/24 proto static \n"
"203.0.113.7 via 192.0.2.1 dev eth0 \n"
)
_IP6_ROUTE_TYPES = (
"2001:db8:1::/64 dev eth0 proto kernel metric 256 pref medium\n"
"2001:db8:2::/64 dev eth1 proto kernel metric 256 pref medium\n"
"blackhole 2001:db8:beef::/48 dev lo metric 1024 pref medium\n"
"unreachable 2001:db8:dead::/48 dev lo metric 1024 pref medium\n"
"fe80::/64 dev eth0 proto kernel metric 256 pref medium\n"
"fe80::/64 dev eth1 proto kernel metric 256 pref medium\n"
"default proto static metric 1024 pref medium\n"
"\tnexthop via 2001:db8:1::1 dev eth0 weight 1 \n"
"\tnexthop via 2001:db8:2::1 dev eth1 weight 1 \n"
)
class TestRouteTypesAndMultipath:
"""A route type in front of the destination is the type, and a multipath
route's ``nexthop`` lines are its next hops (#57)."""
@pytest.fixture()
def routes(self, driver):
def send(command, **_kw):
if "ip -4 route" in command:
return _IP4_ROUTE_TYPES
if "ip -6 route" in command:
return _IP6_ROUTE_TYPES
return ""
with patch.object(driver, "_send", side_effect=send):
return driver.get_route_to()
def test_every_destination_is_an_address(self, routes):
assert sorted(routes) == sorted(
[
"0.0.0.0/0",
"10.0.0.0/8",
"172.16.0.0/12",
"192.0.2.0/24",
"192.168.0.0/16",
"198.51.100.0/24",
"203.0.113.0/24",
"203.0.113.7/32",
"2001:db8:1::/64",
"2001:db8:2::/64",
"2001:db8:beef::/48",
"2001:db8:dead::/48",
"fe80::/64",
"::/0",
]
)
@pytest.mark.parametrize(
("network", "route_type", "metric"),
[
("10.0.0.0/8", "unreachable", 1024),
("172.16.0.0/12", "prohibit", 0),
("192.168.0.0/16", "throw", 0),
("203.0.113.0/24", "blackhole", 0),
("2001:db8:beef::/48", "blackhole", 1024),
("2001:db8:dead::/48", "unreachable", 1024),
],
)
def test_a_route_that_forwards_nothing_says_its_type(self, routes, network, route_type, metric):
(entry,) = routes[network]
assert entry["protocol_attributes"] == {"type": route_type}
assert entry["preference"] == metric
# Nothing leaves through an interface; IPv6 names lo for it.
assert (entry["next_hop"], entry["outgoing_interface"]) == ("", "")
def test_the_rest_of_a_typed_line_is_read_as_before(self, routes):
assert routes["203.0.113.0/24"][0]["protocol"] == "static"
def test_a_unicast_route_has_no_type(self, routes):
assert routes["192.0.2.0/24"][0]["protocol_attributes"] == {}
@pytest.mark.parametrize(
("network", "hops"),
[
("0.0.0.0/0", [("192.0.2.1", "eth0"), ("198.51.100.1", "eth1")]),
("::/0", [("2001:db8:1::1", "eth0"), ("2001:db8:2::1", "eth1")]),
],
)
def test_a_multipath_route_has_one_entry_per_next_hop(self, routes, network, hops):
entries = routes[network]
assert [(e["next_hop"], e["outgoing_interface"]) for e in entries] == hops
assert {(e["protocol"], e["preference"]) for e in entries} == {
("static", 100 if network == "0.0.0.0/0" else 1024)
}
def test_no_next_hop_line_is_a_route_of_its_own(self, routes):
assert not any(network.startswith("nexthop") for network in routes)
class TestParseIpRoute:
"""``parse_ip_route`` line by line (#57)."""
def test_a_typed_route_that_forwards_keeps_its_interface(self):
# ``ip route show table local``: a local route names the interface it is on.
line = "local 192.0.2.10 dev eth0 proto kernel scope host src 192.0.2.10\n"
assert list(parse_ip_route(line, "ipv4")) == [
IpRoute("192.0.2.10/32", "kernel", "", "eth0", 0, "local")
]
def test_a_next_hop_line_without_a_route_is_dropped(self):
out = "\tnexthop via 192.0.2.1 dev eth0 weight 1\n192.0.2.0/24 dev eth0 proto kernel\n"
assert [r.network for r in parse_ip_route(out, "ipv4")] == ["192.0.2.0/24"]
def test_a_route_without_next_hop_lines_is_one_route(self):
out = "default via 192.0.2.1 dev eth0 proto dhcp metric 100\n"
assert list(parse_ip_route(out, "ipv4")) == [
IpRoute("0.0.0.0/0", "dhcp", "192.0.2.1", "eth0", 100)
]