xref: /openbmc/qemu/tests/qemu-iotests/083 (revision a7552b52)
111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env 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 Hajnoczistatus=1	# failure is the default!
28dc668dedSStefan Hajnoczi
2902d2d860SStefan Hajnoczi_cleanup()
3002d2d860SStefan Hajnoczi{
31*a7552b52SMax Reitz	rm -f "$SOCK_DIR/nbd.sock"
3202d2d860SStefan Hajnoczi	rm -f nbd-fault-injector.out
3302d2d860SStefan Hajnoczi	rm -f nbd-fault-injector.conf
3402d2d860SStefan Hajnoczi}
3502d2d860SStefan Hajnoczitrap "_cleanup; exit \$status" 0 1 2 3 15
3602d2d860SStefan Hajnoczi
37dc668dedSStefan Hajnoczi# get standard environment, filters and checks
38dc668dedSStefan Hajnoczi. ./common.rc
39dc668dedSStefan Hajnoczi. ./common.filter
40dc668dedSStefan Hajnoczi
41d98205c5SCleber Rosa_supported_fmt raw
42dc668dedSStefan Hajnoczi_supported_proto nbd
43dc668dedSStefan Hajnoczi_supported_os Linux
44dc668dedSStefan Hajnoczi
45dc668dedSStefan Hajnoczicheck_disconnect() {
4602d2d860SStefan Hajnoczi	local event export_name=foo extra_args nbd_addr nbd_url proto when
4702d2d860SStefan Hajnoczi
4802d2d860SStefan Hajnoczi	while true; do
4902d2d860SStefan Hajnoczi		case $1 in
5002d2d860SStefan Hajnoczi		--classic-negotiation)
5102d2d860SStefan Hajnoczi			shift
5202d2d860SStefan Hajnoczi			extra_args=--classic-negotiation
5302d2d860SStefan Hajnoczi			export_name=
5402d2d860SStefan Hajnoczi			;;
5502d2d860SStefan Hajnoczi		--tcp)
5602d2d860SStefan Hajnoczi			shift
5702d2d860SStefan Hajnoczi			proto=tcp
5802d2d860SStefan Hajnoczi			;;
5902d2d860SStefan Hajnoczi		--unix)
6002d2d860SStefan Hajnoczi			shift
6102d2d860SStefan Hajnoczi			proto=unix
6202d2d860SStefan Hajnoczi			;;
6302d2d860SStefan Hajnoczi		*)
6402d2d860SStefan Hajnoczi			break
6502d2d860SStefan Hajnoczi			;;
6602d2d860SStefan Hajnoczi		esac
6702d2d860SStefan Hajnoczi	done
6802d2d860SStefan Hajnoczi
69dc668dedSStefan Hajnoczi	event=$1
70dc668dedSStefan Hajnoczi	when=$2
71dc668dedSStefan Hajnoczi	echo "=== Check disconnect $when $event ==="
72dc668dedSStefan Hajnoczi	echo
73dc668dedSStefan Hajnoczi
74dc668dedSStefan Hajnoczi	cat > "$TEST_DIR/nbd-fault-injector.conf" <<EOF
75dc668dedSStefan Hajnoczi[inject-error]
76dc668dedSStefan Hajnoczievent=$event
77dc668dedSStefan Hajnocziwhen=$when
78dc668dedSStefan HajnocziEOF
79dc668dedSStefan Hajnoczi
8002d2d860SStefan Hajnoczi	if [ "$proto" = "tcp" ]; then
8102d2d860SStefan Hajnoczi		nbd_addr="127.0.0.1:0"
82dc668dedSStefan Hajnoczi	else
83*a7552b52SMax Reitz		nbd_addr="$SOCK_DIR/nbd.sock"
84dc668dedSStefan Hajnoczi	fi
85dc668dedSStefan Hajnoczi
86*a7552b52SMax Reitz	rm -f "$SOCK_DIR/nbd.sock"
8702d2d860SStefan Hajnoczi
88ddc7093eSMax Reitz        echo > "$TEST_DIR/nbd-fault-injector.out"
8902d2d860SStefan Hajnoczi	$PYTHON nbd-fault-injector.py $extra_args "$nbd_addr" "$TEST_DIR/nbd-fault-injector.conf" >"$TEST_DIR/nbd-fault-injector.out" 2>&1 &
9002d2d860SStefan Hajnoczi
9102d2d860SStefan Hajnoczi	# Wait for server to be ready
9202d2d860SStefan Hajnoczi	while ! grep -q 'Listening on ' "$TEST_DIR/nbd-fault-injector.out"; do
9302d2d860SStefan Hajnoczi		sleep 0.1
9402d2d860SStefan Hajnoczi	done
9502d2d860SStefan Hajnoczi
9602d2d860SStefan Hajnoczi	# Extract the final address (port number has now been assigned in tcp case)
97ddc7093eSMax Reitz        nbd_addr=$(sed -n 's/^Listening on //p' \
98ddc7093eSMax Reitz                       "$TEST_DIR/nbd-fault-injector.out")
9902d2d860SStefan Hajnoczi
10002d2d860SStefan Hajnoczi	if [ "$proto" = "tcp" ]; then
10102d2d860SStefan Hajnoczi		nbd_url="nbd+tcp://$nbd_addr/$export_name"
10202d2d860SStefan Hajnoczi	else
10302d2d860SStefan Hajnoczi		nbd_url="nbd+unix:///$export_name?socket=$nbd_addr"
10402d2d860SStefan Hajnoczi	fi
10502d2d860SStefan Hajnoczi
10605d0fce4SMax Reitz	$QEMU_IO -c "read 0 512" "$nbd_url" 2>&1 | _filter_qemu_io | _filter_nbd
107dc668dedSStefan Hajnoczi
108dc668dedSStefan Hajnoczi	echo
109dc668dedSStefan Hajnoczi}
110dc668dedSStefan Hajnoczi
11102d2d860SStefan Hajnoczifor proto in tcp unix; do
112dc668dedSStefan Hajnoczi	for event in neg1 "export" neg2 request reply data; do
113dc668dedSStefan Hajnoczi		for when in before after; do
11402d2d860SStefan Hajnoczi			check_disconnect "--$proto" "$event" "$when"
115dc668dedSStefan Hajnoczi		done
116dc668dedSStefan Hajnoczi
117dc668dedSStefan Hajnoczi		# Also inject short replies from the NBD server
118dc668dedSStefan Hajnoczi		case "$event" in
119dc668dedSStefan Hajnoczi		neg1)
120dc668dedSStefan Hajnoczi			for when in 8 16; do
12102d2d860SStefan Hajnoczi				check_disconnect "--$proto" "$event" "$when"
122dc668dedSStefan Hajnoczi			done
123dc668dedSStefan Hajnoczi			;;
124dc668dedSStefan Hajnoczi		"export")
125dc668dedSStefan Hajnoczi			for when in 4 12 16; do
12602d2d860SStefan Hajnoczi				check_disconnect "--$proto" "$event" "$when"
127dc668dedSStefan Hajnoczi			done
128dc668dedSStefan Hajnoczi			;;
129dc668dedSStefan Hajnoczi		neg2)
130dc668dedSStefan Hajnoczi			for when in 8 10; do
13102d2d860SStefan Hajnoczi				check_disconnect "--$proto" "$event" "$when"
132dc668dedSStefan Hajnoczi			done
133dc668dedSStefan Hajnoczi			;;
134dc668dedSStefan Hajnoczi		reply)
135dc668dedSStefan Hajnoczi			for when in 4 8; do
13602d2d860SStefan Hajnoczi				check_disconnect "--$proto" "$event" "$when"
137dc668dedSStefan Hajnoczi			done
138dc668dedSStefan Hajnoczi			;;
139dc668dedSStefan Hajnoczi		esac
140dc668dedSStefan Hajnoczi	done
141dc668dedSStefan Hajnoczi
142dc668dedSStefan Hajnoczi	# Also check classic negotiation without export information
143dc668dedSStefan Hajnoczi	for when in before 8 16 24 28 after; do
14402d2d860SStefan Hajnoczi		check_disconnect "--$proto" --classic-negotiation "neg-classic" "$when"
14502d2d860SStefan Hajnoczi	done
146dc668dedSStefan Hajnoczidone
147dc668dedSStefan Hajnoczi
148dc668dedSStefan Hajnoczi# success, all done
149dc668dedSStefan Hajnocziecho "*** done"
150dc668dedSStefan Hajnoczirm -f $seq.full
151dc668dedSStefan Hajnoczistatus=0
152