mirror of
https://github.com/ilyakooo0/nixpkgs.git
synced 2024-09-20 12:08:17 +03:00
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:
parent
7574b56ef9
commit
9f7b0d8f0c
@ -138,18 +138,18 @@ in {
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
def compare_tables(expected, actual):
|
||||
assert (
|
||||
expected == actual
|
||||
), """
|
||||
Routing tables don't match!
|
||||
Expected:
|
||||
{}
|
||||
Actual:
|
||||
{}
|
||||
""".format(
|
||||
expected, actual
|
||||
)
|
||||
import json
|
||||
|
||||
def compare(raw_json, to_compare):
|
||||
data = json.loads(raw_json)
|
||||
assert len(raw_json) >= len(to_compare)
|
||||
for i, row in enumerate(to_compare):
|
||||
actual = data[i]
|
||||
assert len(row.keys()) > 0
|
||||
for key, value in row.items():
|
||||
assert value == actual[key], f"""
|
||||
In entry {i}, value {key}: got: {actual[key]}, expected {value}
|
||||
"""
|
||||
|
||||
|
||||
start_all()
|
||||
@ -178,14 +178,28 @@ in {
|
||||
# Check that networkd properly configures the main routing table
|
||||
# and the routing tables for the VRF.
|
||||
with subtest("check vrf routing tables"):
|
||||
compare_tables(
|
||||
client_ipv4_table, client.succeed("ip -4 route list | head -n2").strip()
|
||||
compare(
|
||||
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(
|
||||
vrf1_table, client.succeed("ip -4 route list table 23 | head -n4").strip()
|
||||
compare(
|
||||
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(
|
||||
vrf2_table, client.succeed("ip -4 route list table 42 | head -n4").strip()
|
||||
compare(
|
||||
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.
|
||||
|
Loading…
Reference in New Issue
Block a user