From 7cce19d45c19e1b00912296d59a133ebbb06dd91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Walstr=C3=B6m?= Date: Mon, 8 Jan 2024 17:14:03 +0100 Subject: [PATCH] test: Add show routes (ipv4 and ipv6) to test CLI test suite --- board/netconf/rootfs/libexec/infix/cli-pretty | 1 - board/netconf/rootfs/libexec/infix/yanger | 20 +- test/case/cli/cli-output/show-routes-ipv4.txt | 13 ++ test/case/cli/cli-output/show-routes-ipv6.txt | 10 + test/case/cli/run.sh | 51 ++++- test/case/cli/sysrepo-emulator.sh | 18 +- test/case/cli/system-output/ip-4-route.json | 207 ++++++++++++++++++ test/case/cli/system-output/ip-6-route.json | 1 + test/case/cli_pretty/all.yaml | 5 - 9 files changed, 298 insertions(+), 28 deletions(-) create mode 100644 test/case/cli/cli-output/show-routes-ipv4.txt create mode 100644 test/case/cli/cli-output/show-routes-ipv6.txt create mode 100644 test/case/cli/system-output/ip-4-route.json create mode 100644 test/case/cli/system-output/ip-6-route.json diff --git a/board/netconf/rootfs/libexec/infix/cli-pretty b/board/netconf/rootfs/libexec/infix/cli-pretty index c9cc3554..970fa6d1 100755 --- a/board/netconf/rootfs/libexec/infix/cli-pretty +++ b/board/netconf/rootfs/libexec/infix/cli-pretty @@ -399,7 +399,6 @@ def ietf_routing(json, ip="ipv4"): if rib["name"] != ip: continue; routes = get_json_data(None, rib, "routes", "route") - if routes: for r in routes: route = Route(r, ip) diff --git a/board/netconf/rootfs/libexec/infix/yanger b/board/netconf/rootfs/libexec/infix/yanger index 715bf6b0..a1f0fb27 100755 --- a/board/netconf/rootfs/libexec/infix/yanger +++ b/board/netconf/rootfs/libexec/infix/yanger @@ -129,7 +129,6 @@ def get_routes(routes, proto, data): host_prefix_length="128" for d in data: new = {} - if(d['dst'] == "default"): d['dst'] = default if(d['dst'].find('/') == -1): @@ -167,13 +166,20 @@ def get_routes(routes, proto, data): out['route'].append(new) insert(routes, 'routes', out) -def add_ipv4_route(routes): - cmd = ['ip', '-4', '-s', '-d', '-j', 'route'] +def add_ipv4_route(routes, test): + if test: + cmd = ['cat', f"{test}/ip-4-route.json"] + else: + cmd = ['ip', '-4', '-s', '-d', '-j', 'route'] + data = run_json_cmd(cmd) get_routes(routes, "ipv4", data) -def add_ipv6_route(routes): - cmd = ['ip', '-6', '-s', '-d', '-j', 'route'] +def add_ipv6_route(routes, test): + if test: + cmd = ['cat', f"{test}/ip-6-route.json"] + else: + cmd = ['ip', '-6', '-s', '-d', '-j', 'route'] data = run_json_cmd(cmd) get_routes(routes, "ipv6", data) @@ -526,8 +532,8 @@ if __name__ == "__main__": ipv4routes = yang_data['ietf-routing:routing']['ribs']['rib'][0] ipv6routes = yang_data['ietf-routing:routing']['ribs']['rib'][1] - add_ipv4_route(ipv4routes) - add_ipv6_route(ipv6routes) + add_ipv4_route(ipv4routes, args.test) + add_ipv6_route(ipv6routes, args.test) elif (args.model == 'ietf-ospf'): yang_data= { "ietf-routing:routing": { diff --git a/test/case/cli/cli-output/show-routes-ipv4.txt b/test/case/cli/cli-output/show-routes-ipv4.txt new file mode 100644 index 00000000..c28fefe5 --- /dev/null +++ b/test/case/cli/cli-output/show-routes-ipv4.txt @@ -0,0 +1,13 @@ +PREFIX NEXT-HOP PREF PROTOCOL  +10.0.0.1/32 10.0.13.1 20 ospf +10.0.0.2/32 10.0.23.1 20 ospf +10.0.0.4/32 10.0.13.1 20 ospf + 10.0.23.1 +10.0.12.0/30 10.0.13.1 20 ospf + 10.0.23.1 +10.0.13.0/30 eth3 0 kernel +10.0.23.0/30 eth4 0 kernel +10.0.24.0/30 10.0.23.1 20 ospf +10.0.41.0/30 10.0.13.1 20 ospf +192.168.4.0/24 10.0.13.1 20 ospf + 10.0.23.1 diff --git a/test/case/cli/cli-output/show-routes-ipv6.txt b/test/case/cli/cli-output/show-routes-ipv6.txt new file mode 100644 index 00000000..d1bad72b --- /dev/null +++ b/test/case/cli/cli-output/show-routes-ipv6.txt @@ -0,0 +1,10 @@ +PREFIX NEXT-HOP PREF PROTOCOL  +2001:db8:3c4d:50::/64 eth4 256 kernel +2001:db8:3c4d:200::1/128 lo 256 kernel +fe80::/64 eth5 256 kernel +fe80::/64 eth3 256 kernel +fe80::/64 eth2 256 kernel +fe80::/64 eth1 256 kernel +fe80::/64 eth0 256 kernel +fe80::/64 eth4 256 kernel +::/0 2001:db8:3c4d:50::1 20 static diff --git a/test/case/cli/run.sh b/test/case/cli/run.sh index 22122120..4422af51 100755 --- a/test/case/cli/run.sh +++ b/test/case/cli/run.sh @@ -33,7 +33,7 @@ print_update_txt() { echo "# the template file." echo echo "# Here's how you update the CLI output templates:" - echo "# $SCRIPT_PATH/run.sh update" + echo "# $SCRIPT_PATH/run.sh update " echo echo "# Check the result" echo "# git diff" @@ -47,28 +47,58 @@ if [ ! -e "$CLI_PRETTY_TOOL" ]; then exit 1 fi -if [ $# -eq 1 ] && [ $1 = "update" ]; then - "$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" > "$CLI_OUTPUT_PATH/show-interfaces.txt" - for iface in "br0" "e0" "e1" "e2" "e3" "e4"; do - "$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" -n "$iface" \ - > "$CLI_OUTPUT_PATH/show-interface-${iface}.txt" - done - echo "All files updated. Check git diff and commit if they look OK" +if [ $# -eq 2 ] && [ $1 = "update" ]; then + if [ $2 = "ietf-interfaces" ]; then + "$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" > "$CLI_OUTPUT_PATH/show-interfaces.txt" + for iface in "br0" "e0" "e1" "e2" "e3" "e4"; do + "$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" -n "$iface" \ + > "$CLI_OUTPUT_PATH/show-interface-${iface}.txt" + done + elif [ $2 = "ietf-routing" ]; then + "$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-routing" -n "ipv4" > "$CLI_OUTPUT_PATH/show-routes-ipv4.txt" + "$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-routing" -n "ipv6" > "$CLI_OUTPUT_PATH/show-routes-ipv6.txt" + else + echo "Unsupported model $2" + exit 1 + fi + echo "All files updated. Check git diff and commit if they look OK" exit 0 fi -echo "1..7" +echo "1..9" +echo "# Running:" # Show interfaces -echo "# Running:" echo "# $SR_EMULATOR_TOOL | $CLI_PRETTY_TOOL ietf-interfaces" "$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" > "$CLI_OUTPUT_FILE" +# Show routes if ! diff -u "$CLI_OUTPUT_PATH/show-interfaces.txt" "$CLI_OUTPUT_FILE"; then print_update_txt fail "\"show interfaces\" output has changed" fi ok "\"show interfaces\" output looks intact" +# Show ipv4 routes +echo "# $SR_EMULATOR_TOOL | $CLI_PRETTY_TOOL ietf-routing -n ipv4" +"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-routing" -n "ipv4" > "$CLI_OUTPUT_FILE" +# Show routes +if ! diff -u "$CLI_OUTPUT_PATH/show-routes-ipv4.txt" "$CLI_OUTPUT_FILE"; then + print_update_txt + fail "\"show routes ipv4\" output has changed" +fi +ok "\"show routes ipv4\" output looks intact" + +# Show ipv6 routes +echo "# $SR_EMULATOR_TOOL | $CLI_PRETTY_TOOL ietf-routing -n ipv6" +"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-routing" -n "ipv6" > "$CLI_OUTPUT_FILE" + +# Show routes +if ! diff -u "$CLI_OUTPUT_PATH/show-routes-ipv6.txt" "$CLI_OUTPUT_FILE"; then + print_update_txt + fail "\"show routes ipv6\" output has changed" +fi +ok "\"show routes ipv6\" output looks intact" + # Show detailed interfaces for iface in "br0" "e0" "e1" "e2" "e3" "e4"; do "$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" -n "$iface" > "$CLI_OUTPUT_FILE" @@ -78,3 +108,4 @@ for iface in "br0" "e0" "e1" "e2" "e3" "e4"; do fi ok "\"show interface name $iface\" output looks intact" done + diff --git a/test/case/cli/sysrepo-emulator.sh b/test/case/cli/sysrepo-emulator.sh index 5884eb00..76f019d2 100755 --- a/test/case/cli/sysrepo-emulator.sh +++ b/test/case/cli/sysrepo-emulator.sh @@ -7,10 +7,12 @@ YANGER_TOOL="$ROOT_PATH/board/netconf/rootfs/libexec/infix/yanger" INTERFACES="br0 e0 e1 e2 e3 e4" -YANGER_OUTPUT_FILE="$(mktemp)" - +INTERFACE_OUTPUT_FILE="$(mktemp)" +INTERFACES_OUTPUT_FILE="$(mktemp)" +ROUTES_OUTPUT_FILE="$(mktemp)" cleanup() { - rm -f "$YANGER_OUTPUT_FILE" + rm -f "$INTERFACE_OUTPUT_FILE" + rm -f "$ROUTES_OUTPUT_FILE" } trap cleanup EXIT @@ -19,10 +21,11 @@ if [ ! -e "$YANGER_TOOL" ]; then exit 1 fi + for iface in $INTERFACES; do if ! "$YANGER_TOOL" "ietf-interfaces" \ -t "$SCRIPT_PATH/system-output/" \ - -p "$iface" >> "$YANGER_OUTPUT_FILE"; then + -p "$iface" >> "$INTERFACE_OUTPUT_FILE"; then echo "Error, running yanger for interface $iface" >&2 exit 1 fi @@ -31,7 +34,12 @@ done if ! jq -s 'reduce .[] as $item ({}; .["ietf-interfaces:interfaces"].interface += $item["ietf-interfaces:interfaces"].interface)' \ - "$YANGER_OUTPUT_FILE"; then + "$INTERFACE_OUTPUT_FILE" >> $INTERFACES_OUTPUT_FILE; then echo "Error, merging yanger output data" >&2 exit 1 fi + +$YANGER_TOOL "ietf-routing" -t "$SCRIPT_PATH/system-output/" > "$ROUTES_OUTPUT_FILE" + +# Merge all module files +jq -s '.[0] * .[1]' $ROUTES_OUTPUT_FILE $INTERFACES_OUTPUT_FILE diff --git a/test/case/cli/system-output/ip-4-route.json b/test/case/cli/system-output/ip-4-route.json new file mode 100644 index 00000000..1065855b --- /dev/null +++ b/test/case/cli/system-output/ip-4-route.json @@ -0,0 +1,207 @@ +[ + { + "type": "unicast", + "dst": "10.0.0.1", + "nhid": 33, + "gateway": "10.0.13.1", + "dev": "eth3", + "protocol": "ospf", + "scope": "global", + "metric": 20, + "flags": [], + "nh_info": { + "id": 33, + "gateway": "10.0.13.1", + "dev": "eth3", + "scope": "link", + "protocol": "zebra", + "flags": [] + } + }, + { + "type": "unicast", + "dst": "10.0.0.2", + "nhid": 34, + "gateway": "10.0.23.1", + "dev": "eth4", + "protocol": "ospf", + "scope": "global", + "metric": 20, + "flags": [], + "nh_info": { + "id": 34, + "gateway": "10.0.23.1", + "dev": "eth4", + "scope": "link", + "protocol": "zebra", + "flags": [] + } + }, + { + "type": "unicast", + "dst": "10.0.0.4", + "nhid": 35, + "protocol": "ospf", + "scope": "global", + "metric": 20, + "flags": [], + "nh_info": { + "id": 35, + "group": [ + { + "id": 33 + }, + { + "id": 34 + } + ], + "scope": "global", + "protocol": "zebra", + "flags": [] + }, + "nexthops": [ + { + "gateway": "10.0.13.1", + "dev": "eth3", + "weight": 1, + "flags": [] + }, + { + "gateway": "10.0.23.1", + "dev": "eth4", + "weight": 1, + "flags": [] + } + ] + }, + { + "type": "unicast", + "dst": "10.0.12.0/30", + "nhid": 35, + "protocol": "ospf", + "scope": "global", + "metric": 20, + "flags": [], + "nh_info": { + "id": 35, + "group": [ + { + "id": 33 + }, + { + "id": 34 + } + ], + "scope": "global", + "protocol": "zebra", + "flags": [] + }, + "nexthops": [ + { + "gateway": "10.0.13.1", + "dev": "eth3", + "weight": 1, + "flags": [] + }, + { + "gateway": "10.0.23.1", + "dev": "eth4", + "weight": 1, + "flags": [] + } + ] + }, + { + "type": "unicast", + "dst": "10.0.13.0/30", + "dev": "eth3", + "protocol": "kernel", + "scope": "link", + "prefsrc": "10.0.13.2", + "flags": [] + }, + { + "type": "unicast", + "dst": "10.0.23.0/30", + "dev": "eth4", + "protocol": "kernel", + "scope": "link", + "prefsrc": "10.0.23.2", + "flags": [] + }, + { + "type": "unicast", + "dst": "10.0.24.0/30", + "nhid": 34, + "gateway": "10.0.23.1", + "dev": "eth4", + "protocol": "ospf", + "scope": "global", + "metric": 20, + "flags": [], + "nh_info": { + "id": 34, + "gateway": "10.0.23.1", + "dev": "eth4", + "scope": "link", + "protocol": "zebra", + "flags": [] + } + }, + { + "type": "unicast", + "dst": "10.0.41.0/30", + "nhid": 33, + "gateway": "10.0.13.1", + "dev": "eth3", + "protocol": "ospf", + "scope": "global", + "metric": 20, + "flags": [], + "nh_info": { + "id": 33, + "gateway": "10.0.13.1", + "dev": "eth3", + "scope": "link", + "protocol": "zebra", + "flags": [] + } + }, + { + "type": "unicast", + "dst": "192.168.4.0/24", + "nhid": 35, + "protocol": "ospf", + "scope": "global", + "metric": 20, + "flags": [], + "nh_info": { + "id": 35, + "group": [ + { + "id": 33 + }, + { + "id": 34 + } + ], + "scope": "global", + "protocol": "zebra", + "flags": [] + }, + "nexthops": [ + { + "gateway": "10.0.13.1", + "dev": "eth3", + "weight": 1, + "flags": [] + }, + { + "gateway": "10.0.23.1", + "dev": "eth4", + "weight": 1, + "flags": [] + } + ] + } +] diff --git a/test/case/cli/system-output/ip-6-route.json b/test/case/cli/system-output/ip-6-route.json new file mode 100644 index 00000000..b3793834 --- /dev/null +++ b/test/case/cli/system-output/ip-6-route.json @@ -0,0 +1 @@ +[{"type":"unicast","dst":"2001:db8:3c4d:50::/64","dev":"eth4","protocol":"kernel","scope":"global","metric":256,"flags":[],"pref":"medium"},{"type":"unicast","dst":"2001:db8:3c4d:200::1","dev":"lo","protocol":"kernel","scope":"global","metric":256,"flags":[],"pref":"medium"},{"type":"unicast","dst":"fe80::/64","dev":"eth5","protocol":"kernel","scope":"global","metric":256,"flags":[],"pref":"medium"},{"type":"unicast","dst":"fe80::/64","dev":"eth3","protocol":"kernel","scope":"global","metric":256,"flags":[],"pref":"medium"},{"type":"unicast","dst":"fe80::/64","dev":"eth2","protocol":"kernel","scope":"global","metric":256,"flags":[],"pref":"medium"},{"type":"unicast","dst":"fe80::/64","dev":"eth1","protocol":"kernel","scope":"global","metric":256,"flags":[],"pref":"medium"},{"type":"unicast","dst":"fe80::/64","dev":"eth0","protocol":"kernel","scope":"global","metric":256,"flags":[],"pref":"medium"},{"type":"unicast","dst":"fe80::/64","dev":"eth4","protocol":"kernel","scope":"global","metric":256,"flags":[],"pref":"medium"},{"type":"unicast","dst":"default","nhid":23,"gateway":"2001:db8:3c4d:50::1","dev":"eth4","protocol":"static","scope":"global","metric":20,"flags":[],"pref":"medium","nh_info":{"id":23,"gateway":"2001:db8:3c4d:50::1","dev":"eth4","scope":"link","protocol":"zebra","flags":[]}}] diff --git a/test/case/cli_pretty/all.yaml b/test/case/cli_pretty/all.yaml index 06b5e815..b6198eec 100644 --- a/test/case/cli_pretty/all.yaml +++ b/test/case/cli_pretty/all.yaml @@ -37,8 +37,3 @@ - "json/bloated.json" - "ietf-interfaces" - "-n br0" -- case: run.sh - name: "routing" - opts: - - "json/bloated.json" - - "ietf-routing"