Skip to content

Commit

Permalink
waltz: add test_ip to CI
Browse files Browse the repository at this point in the history
- Early exits test_ip if capabilities are insufficient
- Checks the output of test_ip_dump routing tables
  • Loading branch information
riptl authored and ripatel-fd committed Aug 5, 2024
1 parent 00a4ef0 commit 0ccc457
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/waltz/ip/Local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ ifdef FD_HAS_LINUX
$(call add-hdrs,fd_ip.h)
$(call add-objs,fd_ip fd_netlink,fd_waltz)
$(call make-unit-test,test_netlink,test_netlink,fd_waltz fd_util)
$(call make-unit-test,test_ip,test_ip,fd_waltz fd_util)
$(call make-unit-test,test_ip_dump,test_ip_dump,fd_waltz fd_util)
$(call add-test-scripts,test_ip)
$(call run-unit-test,test_ip)
$(call make-unit-test,test_routing,test_routing,fd_waltz fd_util)
$(call make-unit-test,test_routing_load,test_routing_load,fd_waltz fd_util)
$(call make-unit-test,test_arp,test_arp,fd_waltz fd_util)
Expand Down
44 changes: 34 additions & 10 deletions src/waltz/ip/test_ip
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ CONF=tmp/test_arp.conf

########################################################################

if [ $# -ne 1 ]; then
if [[ "$(realpath $0)" =~ "/build/" ]]; then
UNIT_TEST=$(dirname $0)
elif [[ $# == 1 ]]; then
UNIT_TEST=$1/unit-test
else
echo ""
echo " build directory not specified"
echo ""
Expand All @@ -29,8 +33,6 @@ if [ $# -ne 1 ]; then
exit 1
fi

UNIT_TEST=$1/unit-test

# Disable permanent log for all the controls we are going to run in here

FD_LOG_PATH=""
Expand All @@ -46,7 +48,10 @@ ip link del dev "$IFACE1" &> /dev/null # Just in case
# (Re-)create veth virtual network devices

# create namespaces
ip netns add "$IFACE0" || exit $?
if ! ip netns add "$IFACE0"; then
echo "Failed to create netns. Insufficient capabilities?" >&2
exit 0
fi
ip netns add "$IFACE1" || exit $?

# create pair of connected interfaces
Expand All @@ -55,6 +60,9 @@ ip link add dev "$IFACE0" \
peer name "$IFACE1" \
|| exit $?

IFACE0_IDX="$(ip -json a s $IFACE0 | jq '.[0].ifindex')"
IFACE1_IDX="$(ip -json a s $IFACE1 | jq '.[0].ifindex')"

# add MAC addresses
ip link set dev "$IFACE0" address "$IFACE0_MAC" || exit $?
ip link set dev "$IFACE1" address "$IFACE1_MAC" || exit $?
Expand All @@ -72,20 +80,36 @@ ip netns exec "$IFACE0" ip link set dev "$IFACE0" up || exit $?
ip netns exec "$IFACE1" ip link set dev "$IFACE1" up || exit $?

# add routes
ip netns exec "$IFACE0" ip route add unicast 192.168.36.0/24 dev "$IFACE0"
ip netns exec "$IFACE0" ip route add unicast default via 192.168.42.11 dev "$IFACE0"
ip netns exec "$IFACE0" ip route add unicast 192.168.36.0/24 dev "$IFACE0" || exit $?
ip netns exec "$IFACE0" ip route add unicast default via 192.168.42.11 dev "$IFACE0" || exit $?

# dump routes
echo dump route:
ip netns exec "$IFACE0" ip route
ip netns exec "$IFACE0" ip route || exit $?
echo done

# ping to get ARP entry
ip netns exec "$IFACE0" ping -c1 "${IFACE1_ADDR}"
sleep "0.15s"
ip netns exec "$IFACE0" ping -c1 "${IFACE1_ADDR}" || exit $?
sleep "0.15s" || exit $?

# run test in namespace "$IFACE0"
ip netns exec "$IFACE0" $UNIT_TEST/test_ip || exit $?
ACTUAL="$(ip netns exec "$IFACE0" $UNIT_TEST/test_ip_dump)" || exit $?

set +x
read -r -d '' EXPECTED <<EOF
ARP table:
192.168. 42. 11 40:00:00:80:00:f1 $(printf '%2u' $IFACE0_IDX) 1d
Routing table:
192.168. 42. 11 0. 0. 0. 0 0. 0. 0. 0 0 0. 0. 0. 0 $(printf '%2u' $IFACE0_IDX) 51
0. 0. 0. 0 192.168. 36. 0 255.255.255. 0 24 0. 0. 0. 0 $(printf '%2u' $IFACE0_IDX) 4d
0. 0. 0. 0 192.168. 42. 0 255.255.255. 0 24 192.168. 42. 10 $(printf '%2u' $IFACE0_IDX) 6d
EOF

if [[ "$ACTUAL" != "$EXPECTED" ]]; then
echo "Unexpected output" >&2
exit 1
fi
set -x

# TODO verify output of previous command, or possibly
# change it to do verification internally
Expand Down
12 changes: 6 additions & 6 deletions src/waltz/ip/test_ip.c → src/waltz/ip/test_ip_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ main( int argc, char **argv ) {
fd_ip_arp_entry_t * arp_table = fd_ip_arp_table_get( ip );
ulong arp_table_sz = ip->cur_num_arp_entries;

FD_LOG_NOTICE(( "ARP table:" ));
puts( "ARP table:" );
for( ulong j = 0L; j < arp_table_sz; ++j ) {
fd_ip_arp_entry_t * arp_entry = arp_table + j;

Expand All @@ -43,7 +43,7 @@ main( int argc, char **argv ) {
(((IP)>>010) & 0xffU), \
(((IP)>>000) & 0xffU)

FD_LOG_NOTICE(( " " IP_FMT " %02x:%02x:%02x:%02x:%02x:%02x %2u %x",
printf( " " IP_FMT " %02x:%02x:%02x:%02x:%02x:%02x %2u %x\n",
IP_VAR(arp_table[j].dst_ip_addr),
arp_table[j].mac_addr[0],
arp_table[j].mac_addr[1],
Expand All @@ -52,7 +52,7 @@ main( int argc, char **argv ) {
arp_table[j].mac_addr[4],
arp_table[j].mac_addr[5],
arp_table[j].ifindex,
arp_table[j].flags ));
arp_table[j].flags );
}

/* fetch the route table */
Expand All @@ -61,20 +61,20 @@ main( int argc, char **argv ) {
fd_ip_route_entry_t * route_table = fd_ip_route_table_get( ip );
ulong route_table_sz = ip->cur_num_route_entries;

FD_LOG_NOTICE(( "Routing table:" ));
puts( "Routing table:" );
for( ulong j = 0L; j < route_table_sz; ++j ) {
fd_ip_route_entry_t * route_entry = route_table + j;

if( route_entry->flags == 0 ) break;

FD_LOG_NOTICE(( " " IP_FMT " " IP_FMT " " IP_FMT " %2u " IP_FMT " %2u %x",
printf( " " IP_FMT " " IP_FMT " " IP_FMT " %2u " IP_FMT " %2u %x\n",
IP_VAR(route_table[j].nh_ip_addr),
IP_VAR(route_table[j].dst_ip_addr),
IP_VAR(route_table[j].dst_netmask),
route_table[j].dst_netmask_sz,
IP_VAR(route_table[j].src_ip_addr),
route_table[j].oif,
route_table[j].flags ));
route_table[j].flags );
}

fd_ip_leave( ip );
Expand Down

0 comments on commit 0ccc457

Please sign in to comment.