Add cleanup hook for integration tests
[tinc] / test / ns-ping.test
1 #!/bin/sh
2
3 # shellcheck source=testlib.sh
4 . ./testlib.sh
5
6 echo "[STEP] Skip this test if we aren't root or if 'ip netns' does not exist"
7
8 test "$(id -u)" = "0" || exit $EXIT_SKIP_TEST
9 test -e /dev/net/tun || exit $EXIT_SKIP_TEST
10 ip netns list || exit $EXIT_SKIP_TEST
11
12 ip_foo=192.168.1.1
13 ip_bar=192.168.1.2
14 mask=24
15
16 echo [STEP] Create network namespaces
17
18 ip netns add ping.test1
19 ip netns add ping.test2
20
21 cleanup_hook() {
22   ip netns del ping.test1
23   ip netns del ping.test2
24 }
25
26 echo [STEP] Initialize two nodes
27
28 tinc foo <<EOF
29 init foo
30 set Subnet $ip_foo
31 set Interface ping.test1
32 set Port 30090
33 set Address localhost
34 set AutoConnect no
35 EOF
36
37 # shellcheck disable=SC2016
38 create_script foo tinc-up "
39   ip link set dev \$INTERFACE netns ping.test1
40   ip netns exec ping.test1 ip addr add $ip_foo/$mask dev \$INTERFACE
41   ip netns exec ping.test1 ip link set \$INTERFACE up
42 "
43
44 tinc bar <<EOF
45 init bar
46 set Subnet $ip_bar
47 set Interface ping.test2
48 set Port 30091
49 set AutoConnect no
50 EOF
51
52 # shellcheck disable=SC2016
53 create_script bar tinc-up "
54   ip link set dev \$INTERFACE netns ping.test2
55   ip netns exec ping.test2 ip addr add $ip_bar/$mask dev \$INTERFACE
56   ip netns exec ping.test2 ip link set \$INTERFACE up
57 "
58
59 echo [STEP] Exchange configuration files
60
61 tinc foo export | tinc bar exchange | tinc foo import
62
63 echo [STEP] Start tinc
64
65 start_tinc foo
66 start_tinc bar
67
68 wait_script foo tinc-up
69 wait_script bar tinc-up
70
71 echo [STEP] The nodes should not be able to ping each other if there is no connection
72
73 must_fail ip netns exec ping.test1 ping -W1 -c3 $ip_bar
74
75 echo [STEP] After connecting they should be
76
77 create_script bar hosts/foo-up
78
79 tinc bar add ConnectTo foo
80 wait_script bar hosts/foo-up
81
82 ip netns exec ping.test1 ping -W1 -c3 $ip_bar