xref: /freebsd/contrib/netbsd-tests/net/carp/t_basic.sh (revision 1a36faad54665288ed4eb839d2a4699ae2ead45e)
1*3b0a9131SEnji Cooper#	$NetBSD: t_basic.sh,v 1.1 2017/01/16 08:18:11 ozaki-r Exp $
2*3b0a9131SEnji Cooper#
3*3b0a9131SEnji Cooper# Copyright (c) 2017 Internet Initiative Japan Inc.
4*3b0a9131SEnji Cooper# All rights reserved.
5*3b0a9131SEnji Cooper#
6*3b0a9131SEnji Cooper# Redistribution and use in source and binary forms, with or without
7*3b0a9131SEnji Cooper# modification, are permitted provided that the following conditions
8*3b0a9131SEnji Cooper# are met:
9*3b0a9131SEnji Cooper# 1. Redistributions of source code must retain the above copyright
10*3b0a9131SEnji Cooper#    notice, this list of conditions and the following disclaimer.
11*3b0a9131SEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright
12*3b0a9131SEnji Cooper#    notice, this list of conditions and the following disclaimer in the
13*3b0a9131SEnji Cooper#    documentation and/or other materials provided with the distribution.
14*3b0a9131SEnji Cooper#
15*3b0a9131SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16*3b0a9131SEnji Cooper# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17*3b0a9131SEnji Cooper# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18*3b0a9131SEnji Cooper# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19*3b0a9131SEnji Cooper# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*3b0a9131SEnji Cooper# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*3b0a9131SEnji Cooper# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*3b0a9131SEnji Cooper# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*3b0a9131SEnji Cooper# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*3b0a9131SEnji Cooper# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*3b0a9131SEnji Cooper# POSSIBILITY OF SUCH DAMAGE.
26*3b0a9131SEnji Cooper#
27*3b0a9131SEnji Cooper
28*3b0a9131SEnji CooperSOCK_CLIENT=unix://carp_client
29*3b0a9131SEnji CooperSOCK_MASTER=unix://carp_master
30*3b0a9131SEnji CooperSOCK_BACKUP=unix://carp_backup
31*3b0a9131SEnji CooperBUS=bus_carp
32*3b0a9131SEnji CooperIP_CLIENT=10.1.1.240
33*3b0a9131SEnji CooperIP_MASTER=10.1.1.1
34*3b0a9131SEnji CooperIP_BACKUP=10.1.1.2
35*3b0a9131SEnji CooperIP_CARP=10.1.1.100
36*3b0a9131SEnji CooperTIMEOUT=3
37*3b0a9131SEnji Cooper
38*3b0a9131SEnji Cooperatf_test_case carp_handover cleanup
39*3b0a9131SEnji Cooper
40*3b0a9131SEnji Coopercarp_handover_head()
41*3b0a9131SEnji Cooper{
42*3b0a9131SEnji Cooper
43*3b0a9131SEnji Cooper	atf_set "descr" "Tests for CARP handover"
44*3b0a9131SEnji Cooper	atf_set "require.progs" "rump_server"
45*3b0a9131SEnji Cooper}
46*3b0a9131SEnji Cooper
47*3b0a9131SEnji Coopersetup_carp()
48*3b0a9131SEnji Cooper{
49*3b0a9131SEnji Cooper	local sock=$1
50*3b0a9131SEnji Cooper	local master=$2
51*3b0a9131SEnji Cooper	local carpif= ip= advskew=
52*3b0a9131SEnji Cooper
53*3b0a9131SEnji Cooper	if $master; then
54*3b0a9131SEnji Cooper		carpif=carp0
55*3b0a9131SEnji Cooper		ip=$IP_MASTER
56*3b0a9131SEnji Cooper		advskew=0
57*3b0a9131SEnji Cooper	else
58*3b0a9131SEnji Cooper		carpif=carp1
59*3b0a9131SEnji Cooper		ip=$IP_BACKUP
60*3b0a9131SEnji Cooper		advskew=200
61*3b0a9131SEnji Cooper	fi
62*3b0a9131SEnji Cooper
63*3b0a9131SEnji Cooper	export RUMP_SERVER=$sock
64*3b0a9131SEnji Cooper	atf_check -s exit:0 rump.ifconfig $carpif create
65*3b0a9131SEnji Cooper	atf_check -s exit:0 rump.ifconfig shmif0 $ip/24 up
66*3b0a9131SEnji Cooper	atf_check -s exit:0 rump.ifconfig $carpif \
67*3b0a9131SEnji Cooper	    vhid 175 advskew $advskew advbase 1 pass s3cret \
68*3b0a9131SEnji Cooper	    $IP_CARP netmask 255.255.255.0
69*3b0a9131SEnji Cooper	atf_check -s exit:0 rump.ifconfig -w 10
70*3b0a9131SEnji Cooper}
71*3b0a9131SEnji Cooper
72*3b0a9131SEnji Cooperwait_handover()
73*3b0a9131SEnji Cooper{
74*3b0a9131SEnji Cooper	local i=0
75*3b0a9131SEnji Cooper
76*3b0a9131SEnji Cooper	export RUMP_SERVER=$SOCK_CLIENT
77*3b0a9131SEnji Cooper
78*3b0a9131SEnji Cooper	while [ $i -ne 5 ]; do
79*3b0a9131SEnji Cooper		$DEBUG && echo "Trying ping $IP_CARP"
80*3b0a9131SEnji Cooper		rump.ping -n -w 1 -c 1 $IP_CARP >/dev/null
81*3b0a9131SEnji Cooper		if [ $? = 0 ]; then
82*3b0a9131SEnji Cooper			$DEBUG && echo "Passed ping $IP_CARP"
83*3b0a9131SEnji Cooper			break;
84*3b0a9131SEnji Cooper		fi
85*3b0a9131SEnji Cooper		$DEBUG && echo "Failed ping $IP_CARP"
86*3b0a9131SEnji Cooper		i=$((i + 1))
87*3b0a9131SEnji Cooper	done
88*3b0a9131SEnji Cooper
89*3b0a9131SEnji Cooper	if [ $i -eq 5 ]; then
90*3b0a9131SEnji Cooper		atf_fail "Failed to failover (5 sec)"
91*3b0a9131SEnji Cooper	fi
92*3b0a9131SEnji Cooper}
93*3b0a9131SEnji Cooper
94*3b0a9131SEnji Coopercarp_handover_body()
95*3b0a9131SEnji Cooper{
96*3b0a9131SEnji Cooper
97*3b0a9131SEnji Cooper	rump_server_start $SOCK_CLIENT
98*3b0a9131SEnji Cooper	rump_server_start $SOCK_MASTER
99*3b0a9131SEnji Cooper	rump_server_start $SOCK_BACKUP
100*3b0a9131SEnji Cooper
101*3b0a9131SEnji Cooper	rump_server_add_iface $SOCK_CLIENT shmif0 $BUS
102*3b0a9131SEnji Cooper	rump_server_add_iface $SOCK_MASTER shmif0 $BUS
103*3b0a9131SEnji Cooper	rump_server_add_iface $SOCK_BACKUP shmif0 $BUS
104*3b0a9131SEnji Cooper
105*3b0a9131SEnji Cooper	setup_carp $SOCK_MASTER true
106*3b0a9131SEnji Cooper	setup_carp $SOCK_BACKUP false
107*3b0a9131SEnji Cooper
108*3b0a9131SEnji Cooper	export RUMP_SERVER=$SOCK_CLIENT
109*3b0a9131SEnji Cooper	atf_check -s exit:0 rump.ifconfig shmif0 $IP_CLIENT/24 up
110*3b0a9131SEnji Cooper	atf_check -s exit:0 rump.ifconfig -w 10
111*3b0a9131SEnji Cooper
112*3b0a9131SEnji Cooper	# Check that the primary addresses are up
113*3b0a9131SEnji Cooper	atf_check -s exit:0 -o ignore \
114*3b0a9131SEnji Cooper	    rump.ping -n -w $TIMEOUT -c 1 $IP_MASTER
115*3b0a9131SEnji Cooper	atf_check -s exit:0 -o ignore \
116*3b0a9131SEnji Cooper	    rump.ping -n -w $TIMEOUT -c 1 $IP_BACKUP
117*3b0a9131SEnji Cooper
118*3b0a9131SEnji Cooper	# Give carp a while to croak
119*3b0a9131SEnji Cooper	sleep 4
120*3b0a9131SEnji Cooper
121*3b0a9131SEnji Cooper	# Check state
122*3b0a9131SEnji Cooper	export RUMP_SERVER=$SOCK_MASTER
123*3b0a9131SEnji Cooper	$DEBUG && rump.ifconfig
124*3b0a9131SEnji Cooper	atf_check -s exit:0 -o match:'carp: MASTER carpdev shmif0' \
125*3b0a9131SEnji Cooper	    rump.ifconfig carp0
126*3b0a9131SEnji Cooper	export RUMP_SERVER=$SOCK_BACKUP
127*3b0a9131SEnji Cooper	$DEBUG && rump.ifconfig
128*3b0a9131SEnji Cooper	atf_check -s exit:0 -o match:'carp: BACKUP carpdev shmif0' \
129*3b0a9131SEnji Cooper	    rump.ifconfig carp1
130*3b0a9131SEnji Cooper	export RUMP_SERVER=$SOCK_CLIENT
131*3b0a9131SEnji Cooper
132*3b0a9131SEnji Cooper	# Check that the shared IP works
133*3b0a9131SEnji Cooper	atf_check -s exit:0 -o ignore \
134*3b0a9131SEnji Cooper	    rump.ping -n -w $TIMEOUT -c 1 $IP_CARP
135*3b0a9131SEnji Cooper
136*3b0a9131SEnji Cooper	# KILLING SPREE
137*3b0a9131SEnji Cooper	env RUMP_SERVER=$SOCK_MASTER rump.halt
138*3b0a9131SEnji Cooper	sleep 1
139*3b0a9131SEnji Cooper
140*3b0a9131SEnji Cooper	# Check that primary is now dead
141*3b0a9131SEnji Cooper	atf_check -s not-exit:0 -o ignore \
142*3b0a9131SEnji Cooper	    rump.ping -n -w $TIMEOUT -c 1 $IP_MASTER
143*3b0a9131SEnji Cooper
144*3b0a9131SEnji Cooper	# Do it in installments. carp will cluck meanwhile
145*3b0a9131SEnji Cooper	wait_handover
146*3b0a9131SEnji Cooper
147*3b0a9131SEnji Cooper	# Check state
148*3b0a9131SEnji Cooper	export RUMP_SERVER=$SOCK_BACKUP
149*3b0a9131SEnji Cooper	$DEBUG && rump.ifconfig
150*3b0a9131SEnji Cooper	atf_check -s exit:0 -o match:'carp: MASTER carpdev shmif0' \
151*3b0a9131SEnji Cooper	    rump.ifconfig carp1
152*3b0a9131SEnji Cooper}
153*3b0a9131SEnji Cooper
154*3b0a9131SEnji Coopercarp_handover_cleanup()
155*3b0a9131SEnji Cooper{
156*3b0a9131SEnji Cooper
157*3b0a9131SEnji Cooper	$DEBUG && dump
158*3b0a9131SEnji Cooper	cleanup
159*3b0a9131SEnji Cooper}
160*3b0a9131SEnji Cooper
161*3b0a9131SEnji Cooperatf_init_test_cases()
162*3b0a9131SEnji Cooper{
163*3b0a9131SEnji Cooper
164*3b0a9131SEnji Cooper	atf_add_test_case carp_handover
165*3b0a9131SEnji Cooper}
166