nixos/systemd-networkd-vrf: check routing tables via ip --json

The original implementation did a simple string-comparison against the
output of `ip route`. This is problematic because

* if the details in the string-output change, the test breaks. This is
  less likely with JSON because the relevant values (i.e. destination,
  interface etc) aren't supposed to be changed.
* this is causing issues with formatters[1][2].

[1] #161703
[2] #154818
This commit is contained in:
Maximilian Bosch 2022-06-24 09:58:40 +02:00
parent 7574b56ef9
commit 9f7b0d8f0c
No known key found for this signature in database
GPG Key ID: 091DBF4D1FC46B8E

View File

@ -138,18 +138,18 @@ in {
}; };
testScript = '' testScript = ''
def compare_tables(expected, actual): import json
assert (
expected == actual def compare(raw_json, to_compare):
), """ data = json.loads(raw_json)
Routing tables don't match! assert len(raw_json) >= len(to_compare)
Expected: for i, row in enumerate(to_compare):
{} actual = data[i]
Actual: assert len(row.keys()) > 0
{} for key, value in row.items():
""".format( assert value == actual[key], f"""
expected, actual In entry {i}, value {key}: got: {actual[key]}, expected {value}
) """
start_all() start_all()
@ -178,14 +178,28 @@ in {
# Check that networkd properly configures the main routing table # Check that networkd properly configures the main routing table
# and the routing tables for the VRF. # and the routing tables for the VRF.
with subtest("check vrf routing tables"): with subtest("check vrf routing tables"):
compare_tables( compare(
client_ipv4_table, client.succeed("ip -4 route list | head -n2").strip() client.succeed("ip --json -4 route list"),
[
{"dst": "192.168.1.2", "dev": "vrf1", "metric": 100},
{"dst": "192.168.2.3", "dev": "vrf2", "metric": 100}
]
) )
compare_tables( compare(
vrf1_table, client.succeed("ip -4 route list table 23 | head -n4").strip() client.succeed("ip --json -4 route list table 23"),
[
{"dst": "192.168.1.0/24", "dev": "eth1", "prefsrc": "192.168.1.1"},
{"type": "local", "dst": "192.168.1.1", "dev": "eth1", "prefsrc": "192.168.1.1"},
{"type": "broadcast", "dev": "eth1", "prefsrc": "192.168.1.1", "dst": "192.168.1.255"}
]
) )
compare_tables( compare(
vrf2_table, client.succeed("ip -4 route list table 42 | head -n4").strip() client.succeed("ip --json -4 route list table 42"),
[
{"dst": "192.168.2.0/24", "dev": "eth2", "prefsrc": "192.168.2.1"},
{"type": "local", "dst": "192.168.2.1", "dev": "eth2", "prefsrc": "192.168.2.1"},
{"type": "broadcast", "dev": "eth2", "prefsrc": "192.168.2.1", "dst": "192.168.2.255"}
]
) )
# Ensure that other nodes are reachable via ICMP through the VRF. # Ensure that other nodes are reachable via ICMP through the VRF.