tools: ynl: convert rt-route sample to selftest

Convert rt-route.c to use kselftest_harness.h with FIXTURE/TEST_F.
This is the last test to convert so clean up the Makefile.

Validate that the connected routes for 192.168.1.0/24 and
2001:db8::/64 appear in the dump.

Output:

  TAP version 13
  1..1
  # Starting 1 tests from 1 test cases.
  #  RUN           rt_route.dump ...
  # oif: nsim0            dst: 192.168.1.0/24
  # oif: lo               dst: ::1/128
  # oif: nsim0            dst: 2001:db8::1/128
  # oif: nsim0            dst: 2001:db8::/64
  # oif: nsim0            dst: fe80::/64
  # oif: nsim0            dst: ff00::/8
  #            OK  rt_route.dump
  ok 1 rt_route.dump
  # PASSED: 1 / 1 tests passed.
  # Totals: pass:1 fail:0 xfail:0 xpass:0 skip:0 error:0

Reviewed-by: Donald Hunter <donald.hunter@gmail.com>
Tested-by: Donald Hunter <donald.hunter@gmail.com>
Link: https://patch.msgid.link/20260307033630.1396085-11-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski
2026-03-06 19:36:30 -08:00
parent e7a39b8f5f
commit aa234faa5a
3 changed files with 72 additions and 36 deletions

View File

@@ -17,6 +17,7 @@ TEST_PROGS := \
devlink.sh \
ethtool.sh \
rt-addr.sh \
rt-route.sh \
test_ynl_cli.sh \
test_ynl_ethtool.sh \
# end of TEST_PROGS
@@ -32,11 +33,8 @@ TEST_GEN_FILES := \
devlink \
ethtool \
rt-addr \
# end of TEST_GEN_FILES
BINS := \
rt-route \
# end of BINS
# end of TEST_GEN_FILES
TEST_FILES := ynl_nsim_lib.sh
@@ -47,7 +45,7 @@ include $(wildcard *.d)
INSTALL_PATH ?= $(DESTDIR)/usr/share/kselftest
all: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
all: $(TEST_GEN_PROGS) $(TEST_GEN_FILES)
../lib/ynl.a:
@$(MAKE) -C ../lib
@@ -55,7 +53,7 @@ all: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
../generated/protos.a:
@$(MAKE) -C ../generated
$(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS): %: %.c ../lib/ynl.a ../generated/protos.a
$(TEST_GEN_PROGS) $(TEST_GEN_FILES): %: %.c ../lib/ynl.a ../generated/protos.a
@echo -e '\tCC test $@'
@$(COMPILE.c) $(CFLAGS_$@) $@.c -o $@.o
@$(LINK.c) $@.o -o $@ $(LDLIBS)
@@ -65,7 +63,7 @@ run_tests:
./$$test; \
done
install: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
install: $(TEST_GEN_PROGS) $(TEST_GEN_FILES)
@mkdir -p $(INSTALL_PATH)/ynl
@cp ../../../testing/selftests/kselftest/ktap_helpers.sh $(INSTALL_PATH)/
@for test in $(TEST_PROGS); do \
@@ -79,7 +77,7 @@ install: $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
@for file in $(TEST_FILES); do \
cp $$file $(INSTALL_PATH)/ynl/$$file; \
done
@for bin in $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS); do \
@for bin in $(TEST_GEN_PROGS) $(TEST_GEN_FILES); do \
cp $$bin $(INSTALL_PATH)/ynl/$$bin; \
done
@for test in $(TEST_PROGS) $(TEST_GEN_PROGS); do \
@@ -90,7 +88,7 @@ clean:
rm -f *.o *.d *~
distclean: clean
rm -f $(TEST_GEN_PROGS) $(TEST_GEN_FILES) $(BINS)
rm -f $(TEST_GEN_PROGS) $(TEST_GEN_FILES)
.PHONY: all install clean distclean run_tests
.DEFAULT_GOAL=all

View File

@@ -7,9 +7,12 @@
#include <arpa/inet.h>
#include <net/if.h>
#include <kselftest_harness.h>
#include "rt-route-user.h"
static void rt_route_print(struct rt_route_getroute_rsp *r)
static void rt_route_print(struct __test_metadata *_metadata,
struct rt_route_getroute_rsp *r)
{
char ifname[IF_NAMESIZE];
char route_str[64];
@@ -22,8 +25,9 @@ static void rt_route_print(struct rt_route_getroute_rsp *r)
if (r->_present.oif) {
name = if_indextoname(r->oif, ifname);
EXPECT_NE(NULL, name);
if (name)
printf("oif: %-16s ", name);
ksft_print_msg("oif: %-16s ", name);
}
if (r->_len.dst) {
@@ -41,40 +45,69 @@ static void rt_route_print(struct rt_route_getroute_rsp *r)
printf("\n");
}
int main(int argc, char **argv)
FIXTURE(rt_route)
{
struct ynl_sock *ys;
};
FIXTURE_SETUP(rt_route)
{
struct ynl_error yerr;
self->ys = ynl_sock_create(&ynl_rt_route_family, &yerr);
ASSERT_NE(NULL, self->ys)
TH_LOG("failed to create rt-route socket: %s", yerr.msg);
}
FIXTURE_TEARDOWN(rt_route)
{
ynl_sock_destroy(self->ys);
}
TEST_F(rt_route, dump)
{
struct rt_route_getroute_req_dump *req;
struct rt_route_getroute_list *rsp;
struct ynl_error yerr;
struct ynl_sock *ys;
struct in6_addr v6_expected;
struct in_addr v4_expected;
bool found_v4 = false;
bool found_v6 = false;
ys = ynl_sock_create(&ynl_rt_route_family, &yerr);
if (!ys) {
fprintf(stderr, "YNL: %s\n", yerr.msg);
return 1;
}
/* The bash wrapper configures 192.168.1.1/24 and 2001:db8::1/64,
* make sure we can find the connected routes in the dump.
*/
inet_pton(AF_INET, "192.168.1.0", &v4_expected);
inet_pton(AF_INET6, "2001:db8::", &v6_expected);
req = rt_route_getroute_req_dump_alloc();
if (!req)
goto err_destroy;
ASSERT_NE(NULL, req);
rsp = rt_route_getroute_dump(ys, req);
rsp = rt_route_getroute_dump(self->ys, req);
rt_route_getroute_req_dump_free(req);
if (!rsp)
goto err_close;
ASSERT_NE(NULL, rsp) {
TH_LOG("dump failed: %s", self->ys->err.msg);
}
if (ynl_dump_empty(rsp))
fprintf(stderr, "Error: no routeesses reported\n");
ynl_dump_foreach(rsp, route)
rt_route_print(route);
ASSERT_FALSE(ynl_dump_empty(rsp)) {
rt_route_getroute_list_free(rsp);
TH_LOG("no routes reported");
}
ynl_dump_foreach(rsp, route) {
rt_route_print(_metadata, route);
if (route->_hdr.rtm_table == RT_TABLE_LOCAL)
continue;
if (route->_len.dst == 4 && route->_hdr.rtm_dst_len == 24)
found_v4 |= !memcmp(route->dst, &v4_expected, 4);
if (route->_len.dst == 16 && route->_hdr.rtm_dst_len == 64)
found_v6 |= !memcmp(route->dst, &v6_expected, 16);
}
rt_route_getroute_list_free(rsp);
ynl_sock_destroy(ys);
return 0;
err_close:
fprintf(stderr, "YNL: %s\n", ys->err.msg);
err_destroy:
ynl_sock_destroy(ys);
return 2;
EXPECT_TRUE(found_v4);
EXPECT_TRUE(found_v6);
}
TEST_HARNESS_MAIN

View File

@@ -0,0 +1,5 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
source "$(dirname "$(realpath "$0")")/ynl_nsim_lib.sh"
nsim_setup
"$(dirname "$(realpath "$0")")/rt-route"