xref: /openbmc/qemu/tests/qemu-iotests/083 (revision 02d2d860)
1dc668dedSStefan Hajnoczi#!/bin/bash
2dc668dedSStefan Hajnoczi#
3dc668dedSStefan Hajnoczi# Test NBD client unexpected disconnect
4dc668dedSStefan Hajnoczi#
5dc668dedSStefan Hajnoczi# Copyright Red Hat, Inc. 2014
6dc668dedSStefan Hajnoczi#
7dc668dedSStefan Hajnoczi# This program is free software; you can redistribute it and/or modify
8dc668dedSStefan Hajnoczi# it under the terms of the GNU General Public License as published by
9dc668dedSStefan Hajnoczi# the Free Software Foundation; either version 2 of the License, or
10dc668dedSStefan Hajnoczi# (at your option) any later version.
11dc668dedSStefan Hajnoczi#
12dc668dedSStefan Hajnoczi# This program is distributed in the hope that it will be useful,
13dc668dedSStefan Hajnoczi# but WITHOUT ANY WARRANTY; without even the implied warranty of
14dc668dedSStefan Hajnoczi# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15dc668dedSStefan Hajnoczi# GNU General Public License for more details.
16dc668dedSStefan Hajnoczi#
17dc668dedSStefan Hajnoczi# You should have received a copy of the GNU General Public License
18dc668dedSStefan Hajnoczi# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19dc668dedSStefan Hajnoczi#
20dc668dedSStefan Hajnoczi
21dc668dedSStefan Hajnoczi# creator
22dc668dedSStefan Hajnocziowner=stefanha@redhat.com
23dc668dedSStefan Hajnoczi
24dc668dedSStefan Hajnocziseq=`basename $0`
25dc668dedSStefan Hajnocziecho "QA output created by $seq"
26dc668dedSStefan Hajnoczi
27dc668dedSStefan Hajnoczihere=`pwd`
28dc668dedSStefan Hajnoczistatus=1	# failure is the default!
29dc668dedSStefan Hajnoczi
30*02d2d860SStefan Hajnoczi_cleanup()
31*02d2d860SStefan Hajnoczi{
32*02d2d860SStefan Hajnoczi	rm -f nbd.sock
33*02d2d860SStefan Hajnoczi	rm -f nbd-fault-injector.out
34*02d2d860SStefan Hajnoczi	rm -f nbd-fault-injector.conf
35*02d2d860SStefan Hajnoczi}
36*02d2d860SStefan Hajnoczitrap "_cleanup; exit \$status" 0 1 2 3 15
37*02d2d860SStefan Hajnoczi
38dc668dedSStefan Hajnoczi# get standard environment, filters and checks
39dc668dedSStefan Hajnoczi. ./common.rc
40dc668dedSStefan Hajnoczi. ./common.filter
41dc668dedSStefan Hajnoczi
42dc668dedSStefan Hajnoczi_supported_fmt generic
43dc668dedSStefan Hajnoczi_supported_proto nbd
44dc668dedSStefan Hajnoczi_supported_os Linux
45dc668dedSStefan Hajnoczi
46dc668dedSStefan Hajnoczicheck_disconnect() {
47*02d2d860SStefan Hajnoczi	local event export_name=foo extra_args nbd_addr nbd_url proto when
48*02d2d860SStefan Hajnoczi
49*02d2d860SStefan Hajnoczi	while true; do
50*02d2d860SStefan Hajnoczi		case $1 in
51*02d2d860SStefan Hajnoczi		--classic-negotiation)
52*02d2d860SStefan Hajnoczi			shift
53*02d2d860SStefan Hajnoczi			extra_args=--classic-negotiation
54*02d2d860SStefan Hajnoczi			export_name=
55*02d2d860SStefan Hajnoczi			;;
56*02d2d860SStefan Hajnoczi		--tcp)
57*02d2d860SStefan Hajnoczi			shift
58*02d2d860SStefan Hajnoczi			proto=tcp
59*02d2d860SStefan Hajnoczi			;;
60*02d2d860SStefan Hajnoczi		--unix)
61*02d2d860SStefan Hajnoczi			shift
62*02d2d860SStefan Hajnoczi			proto=unix
63*02d2d860SStefan Hajnoczi			;;
64*02d2d860SStefan Hajnoczi		*)
65*02d2d860SStefan Hajnoczi			break
66*02d2d860SStefan Hajnoczi			;;
67*02d2d860SStefan Hajnoczi		esac
68*02d2d860SStefan Hajnoczi	done
69*02d2d860SStefan Hajnoczi
70dc668dedSStefan Hajnoczi	event=$1
71dc668dedSStefan Hajnoczi	when=$2
72dc668dedSStefan Hajnoczi	echo "=== Check disconnect $when $event ==="
73dc668dedSStefan Hajnoczi	echo
74dc668dedSStefan Hajnoczi
75dc668dedSStefan Hajnoczi	cat > "$TEST_DIR/nbd-fault-injector.conf" <<EOF
76dc668dedSStefan Hajnoczi[inject-error]
77dc668dedSStefan Hajnoczievent=$event
78dc668dedSStefan Hajnocziwhen=$when
79dc668dedSStefan HajnocziEOF
80dc668dedSStefan Hajnoczi
81*02d2d860SStefan Hajnoczi	if [ "$proto" = "tcp" ]; then
82*02d2d860SStefan Hajnoczi		nbd_addr="127.0.0.1:0"
83dc668dedSStefan Hajnoczi	else
84*02d2d860SStefan Hajnoczi		nbd_addr="$TEST_DIR/nbd.sock"
85dc668dedSStefan Hajnoczi	fi
86dc668dedSStefan Hajnoczi
87*02d2d860SStefan Hajnoczi	rm -f "$TEST_DIR/nbd.sock"
88*02d2d860SStefan Hajnoczi
89*02d2d860SStefan Hajnoczi	$PYTHON nbd-fault-injector.py $extra_args "$nbd_addr" "$TEST_DIR/nbd-fault-injector.conf" >"$TEST_DIR/nbd-fault-injector.out" 2>&1 &
90*02d2d860SStefan Hajnoczi
91*02d2d860SStefan Hajnoczi	# Wait for server to be ready
92*02d2d860SStefan Hajnoczi	while ! grep -q 'Listening on ' "$TEST_DIR/nbd-fault-injector.out"; do
93*02d2d860SStefan Hajnoczi		sleep 0.1
94*02d2d860SStefan Hajnoczi	done
95*02d2d860SStefan Hajnoczi
96*02d2d860SStefan Hajnoczi	# Extract the final address (port number has now been assigned in tcp case)
97*02d2d860SStefan Hajnoczi	nbd_addr=$(sed 's/Listening on \(.*\)$/\1/' "$TEST_DIR/nbd-fault-injector.out")
98*02d2d860SStefan Hajnoczi
99*02d2d860SStefan Hajnoczi	if [ "$proto" = "tcp" ]; then
100*02d2d860SStefan Hajnoczi		nbd_url="nbd+tcp://$nbd_addr/$export_name"
101*02d2d860SStefan Hajnoczi	else
102*02d2d860SStefan Hajnoczi		nbd_url="nbd+unix:///$export_name?socket=$nbd_addr"
103*02d2d860SStefan Hajnoczi	fi
104*02d2d860SStefan Hajnoczi
10505d0fce4SMax Reitz	$QEMU_IO -c "read 0 512" "$nbd_url" 2>&1 | _filter_qemu_io | _filter_nbd
106dc668dedSStefan Hajnoczi
107dc668dedSStefan Hajnoczi	echo
108dc668dedSStefan Hajnoczi}
109dc668dedSStefan Hajnoczi
110*02d2d860SStefan Hajnoczifor proto in tcp unix; do
111dc668dedSStefan Hajnoczi	for event in neg1 "export" neg2 request reply data; do
112dc668dedSStefan Hajnoczi		for when in before after; do
113*02d2d860SStefan Hajnoczi			check_disconnect "--$proto" "$event" "$when"
114dc668dedSStefan Hajnoczi		done
115dc668dedSStefan Hajnoczi
116dc668dedSStefan Hajnoczi		# Also inject short replies from the NBD server
117dc668dedSStefan Hajnoczi		case "$event" in
118dc668dedSStefan Hajnoczi		neg1)
119dc668dedSStefan Hajnoczi			for when in 8 16; do
120*02d2d860SStefan Hajnoczi				check_disconnect "--$proto" "$event" "$when"
121dc668dedSStefan Hajnoczi			done
122dc668dedSStefan Hajnoczi			;;
123dc668dedSStefan Hajnoczi		"export")
124dc668dedSStefan Hajnoczi			for when in 4 12 16; do
125*02d2d860SStefan Hajnoczi				check_disconnect "--$proto" "$event" "$when"
126dc668dedSStefan Hajnoczi			done
127dc668dedSStefan Hajnoczi			;;
128dc668dedSStefan Hajnoczi		neg2)
129dc668dedSStefan Hajnoczi			for when in 8 10; do
130*02d2d860SStefan Hajnoczi				check_disconnect "--$proto" "$event" "$when"
131dc668dedSStefan Hajnoczi			done
132dc668dedSStefan Hajnoczi			;;
133dc668dedSStefan Hajnoczi		reply)
134dc668dedSStefan Hajnoczi			for when in 4 8; do
135*02d2d860SStefan Hajnoczi				check_disconnect "--$proto" "$event" "$when"
136dc668dedSStefan Hajnoczi			done
137dc668dedSStefan Hajnoczi			;;
138dc668dedSStefan Hajnoczi		esac
139dc668dedSStefan Hajnoczi	done
140dc668dedSStefan Hajnoczi
141dc668dedSStefan Hajnoczi	# Also check classic negotiation without export information
142dc668dedSStefan Hajnoczi	for when in before 8 16 24 28 after; do
143*02d2d860SStefan Hajnoczi		check_disconnect "--$proto" --classic-negotiation "neg-classic" "$when"
144*02d2d860SStefan Hajnoczi	done
145dc668dedSStefan Hajnoczidone
146dc668dedSStefan Hajnoczi
147dc668dedSStefan Hajnoczi# success, all done
148dc668dedSStefan Hajnocziecho "*** done"
149dc668dedSStefan Hajnoczirm -f $seq.full
150dc668dedSStefan Hajnoczistatus=0
151