Rewrite the test suite for better compat and stability
[tinc] / test / ns-ping.test
index 5a5415c..5931c75 100755 (executable)
@@ -1,72 +1,79 @@
 #!/bin/sh
 
+# shellcheck source=testlib.sh
 . "${0%/*}/testlib.sh"
 
-# Skip this test if we aren't root or if "ip netns" does not exist
+echo "[STEP] Skip this test if we aren't root or if 'ip netns' does not exist"
 
-test "`id -u`" = "0" || exit 77
-ip netns list || exit 77
+test "$(id -u)" = "0" || exit $EXIT_SKIP_TEST
+test -e /dev/net/tun || exit $EXIT_SKIP_TEST
+ip netns list || exit $EXIT_SKIP_TEST
 
-# Initialize two nodes
+ip_foo=192.168.1.1
+ip_bar=192.168.1.2
+mask=24
 
-$tinc $c1 <<EOF
+echo [STEP] Initialize two nodes
+
+tinc foo <<EOF
 init foo
-set Subnet 192.168.1.1
+set Subnet $ip_foo
 set Interface ping.test1
-set Port 32577
+set Port 30090
 set Address localhost
 set AutoConnect no
 EOF
 
-cat >$d1/tinc-up <<EOF
-#!/bin/sh
-ip netns add ping.test1
-ip link set dev \$INTERFACE netns ping.test1
-ip netns exec ping.test1 ip addr add 192.168.1.1/24 dev \$INTERFACE
-ip netns exec ping.test1 ip link set \$INTERFACE up
-EOF
+# shellcheck disable=SC2016
+create_script foo tinc-up "
+  ip netns add ping.test1
+  ip link set dev \$INTERFACE netns ping.test1
+  ip netns exec ping.test1 ip addr add $ip_foo/$mask dev \$INTERFACE
+  ip netns exec ping.test1 ip link set \$INTERFACE up
+"
 
-$tinc $c2 <<EOF
+tinc bar <<EOF
 init bar
-set Subnet 192.168.1.2
+set Subnet $ip_bar
 set Interface ping.test2
-set Port 32574
+set Port 30091
 set AutoConnect no
 EOF
 
-cat >$d2/tinc-up <<EOF
-#!/bin/sh
-ip netns add ping.test2
-ip link set dev \$INTERFACE netns ping.test2
-ip netns exec ping.test2 ip addr add 192.168.1.2/24 dev \$INTERFACE
-ip netns exec ping.test2 ip link set \$INTERFACE up
-EOF
+# shellcheck disable=SC2016
+create_script bar tinc-up "
+  ip netns add ping.test2
+  ip link set dev \$INTERFACE netns ping.test2
+  ip netns exec ping.test2 ip addr add $ip_bar/$mask dev \$INTERFACE
+  ip netns exec ping.test2 ip link set \$INTERFACE up
+"
+
+echo [STEP] Exchange configuration files
 
-# Exchange configuration files
+tinc foo export | tinc bar exchange | tinc foo import
 
-$tinc $c1 export | $tinc $c2 exchange | $tinc $c1 import
+echo [STEP] Start tinc
 
-# Start tinc
+start_tinc foo
+start_tinc bar
 
-$tinc $c1 start $r1
-$tinc $c2 start $r2
+wait_script foo tinc-up
+wait_script bar tinc-up
 
-sleep 1
+echo [STEP] The nodes should not be able to ping each other if there is no connection
 
-# The nodes should not be able to ping each other if there is no connection
+must_fail ip netns exec ping.test1 ping -W1 -c3 $ip_bar
 
-ip netns exec ping.test1 ping -W1 -c3 192.168.1.2 && exit 1
+echo [STEP] After connecting they should be
 
-# After connecting they should be
+create_script bar hosts/foo-up
 
-$tinc $c2 add ConnectTo foo
-sleep 1
+tinc bar add ConnectTo foo
+wait_script bar hosts/foo-up
 
-ip netns exec ping.test1 ping -W1 -c3 192.168.1.2
+ip netns exec ping.test1 ping -W1 -c3 $ip_bar
 
-# Clean up
+echo [STEP] Clean up
 
-$tinc $c2 stop
-$tinc $c1 stop
 ip netns del ping.test2
 ip netns del ping.test1