xref: /openbmc/qemu/tests/qemu-iotests/083 (revision 1ce52846)
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 Hajnoczitmp=/tmp/$$
29dc668dedSStefan Hajnoczistatus=1	# failure is the default!
30dc668dedSStefan Hajnoczi
31dc668dedSStefan Hajnoczi# get standard environment, filters and checks
32dc668dedSStefan Hajnoczi. ./common.rc
33dc668dedSStefan Hajnoczi. ./common.filter
34dc668dedSStefan Hajnoczi
35dc668dedSStefan Hajnoczi_supported_fmt generic
36dc668dedSStefan Hajnoczi_supported_proto nbd
37dc668dedSStefan Hajnoczi_supported_os Linux
38dc668dedSStefan Hajnoczi
39dc668dedSStefan Hajnoczi# Pick a TCP port based on our pid.  This way multiple instances of this test
40dc668dedSStefan Hajnoczi# can run in parallel without conflicting.
41dc668dedSStefan Hajnoczichoose_tcp_port() {
42dc668dedSStefan Hajnoczi	echo $((($$ % 31744) + 1024)) # 1024 <= port < 32768
43dc668dedSStefan Hajnoczi}
44dc668dedSStefan Hajnoczi
45dc668dedSStefan Hajnocziwait_for_tcp_port() {
46dc668dedSStefan Hajnoczi	while ! (netstat --tcp --listening --numeric | \
47f5264553SMax Reitz		 grep "$1.*0\\.0\\.0\\.0:\\*.*LISTEN") 2>&1 >/dev/null; do
48dc668dedSStefan Hajnoczi		sleep 0.1
49dc668dedSStefan Hajnoczi	done
50dc668dedSStefan Hajnoczi}
51dc668dedSStefan Hajnoczi
52dc668dedSStefan Hajnoczifilter_nbd() {
53dc668dedSStefan Hajnoczi	# nbd.c error messages contain function names and line numbers that are prone
54dc668dedSStefan Hajnoczi	# to change.  Message ordering depends on timing between send and receive
55dc668dedSStefan Hajnoczi	# callbacks sometimes, making them unreliable.
56dc668dedSStefan Hajnoczi	#
57dc668dedSStefan Hajnoczi	# Filter out the TCP port number since this changes between runs.
58f5264553SMax Reitz	sed -e 's#^.*nbd\.c:.*##g' \
59*1ce52846SMax Reitz	    -e 's#nbd:127\.0\.0\.1:[^:]*:#nbd:127\.0\.0\.1:PORT:#g' \
60*1ce52846SMax Reitz            -e 's#\(exportname=foo\|PORT\): Failed to .*$#\1#'
61dc668dedSStefan Hajnoczi}
62dc668dedSStefan Hajnoczi
63dc668dedSStefan Hajnoczicheck_disconnect() {
64dc668dedSStefan Hajnoczi	event=$1
65dc668dedSStefan Hajnoczi	when=$2
66dc668dedSStefan Hajnoczi	negotiation=$3
67dc668dedSStefan Hajnoczi	echo "=== Check disconnect $when $event ==="
68dc668dedSStefan Hajnoczi	echo
69dc668dedSStefan Hajnoczi
70dc668dedSStefan Hajnoczi	port=$(choose_tcp_port)
71dc668dedSStefan Hajnoczi
72dc668dedSStefan Hajnoczi	cat > "$TEST_DIR/nbd-fault-injector.conf" <<EOF
73dc668dedSStefan Hajnoczi[inject-error]
74dc668dedSStefan Hajnoczievent=$event
75dc668dedSStefan Hajnocziwhen=$when
76dc668dedSStefan HajnocziEOF
77dc668dedSStefan Hajnoczi
78dc668dedSStefan Hajnoczi	if [ "$negotiation" = "--classic-negotiation" ]; then
79dc668dedSStefan Hajnoczi		extra_args=--classic-negotiation
80dc668dedSStefan Hajnoczi		nbd_url="nbd:127.0.0.1:$port"
81dc668dedSStefan Hajnoczi	else
82dc668dedSStefan Hajnoczi		nbd_url="nbd:127.0.0.1:$port:exportname=foo"
83dc668dedSStefan Hajnoczi	fi
84dc668dedSStefan Hajnoczi
85ea81ca9dSMax Reitz	$PYTHON nbd-fault-injector.py $extra_args "127.0.0.1:$port" "$TEST_DIR/nbd-fault-injector.conf" 2>&1 >/dev/null &
86f5264553SMax Reitz	wait_for_tcp_port "127\\.0\\.0\\.1:$port"
87dc668dedSStefan Hajnoczi	$QEMU_IO -c "read 0 512" "$nbd_url" 2>&1 | _filter_qemu_io | filter_nbd
88dc668dedSStefan Hajnoczi
89dc668dedSStefan Hajnoczi	echo
90dc668dedSStefan Hajnoczi}
91dc668dedSStefan Hajnoczi
92dc668dedSStefan Hajnoczifor event in neg1 "export" neg2 request reply data; do
93dc668dedSStefan Hajnoczi	for when in before after; do
94dc668dedSStefan Hajnoczi		check_disconnect "$event" "$when"
95dc668dedSStefan Hajnoczi	done
96dc668dedSStefan Hajnoczi
97dc668dedSStefan Hajnoczi	# Also inject short replies from the NBD server
98dc668dedSStefan Hajnoczi	case "$event" in
99dc668dedSStefan Hajnoczi	neg1)
100dc668dedSStefan Hajnoczi		for when in 8 16; do
101dc668dedSStefan Hajnoczi			check_disconnect "$event" "$when"
102dc668dedSStefan Hajnoczi		done
103dc668dedSStefan Hajnoczi		;;
104dc668dedSStefan Hajnoczi	"export")
105dc668dedSStefan Hajnoczi		for when in 4 12 16; do
106dc668dedSStefan Hajnoczi			check_disconnect "$event" "$when"
107dc668dedSStefan Hajnoczi		done
108dc668dedSStefan Hajnoczi		;;
109dc668dedSStefan Hajnoczi	neg2)
110dc668dedSStefan Hajnoczi		for when in 8 10; do
111dc668dedSStefan Hajnoczi			check_disconnect "$event" "$when"
112dc668dedSStefan Hajnoczi		done
113dc668dedSStefan Hajnoczi		;;
114dc668dedSStefan Hajnoczi	reply)
115dc668dedSStefan Hajnoczi		for when in 4 8; do
116dc668dedSStefan Hajnoczi			check_disconnect "$event" "$when"
117dc668dedSStefan Hajnoczi		done
118dc668dedSStefan Hajnoczi		;;
119dc668dedSStefan Hajnoczi	esac
120dc668dedSStefan Hajnoczidone
121dc668dedSStefan Hajnoczi
122dc668dedSStefan Hajnoczi# Also check classic negotiation without export information
123dc668dedSStefan Hajnoczifor when in before 8 16 24 28 after; do
124dc668dedSStefan Hajnoczi	check_disconnect "neg-classic" "$when" --classic-negotiation
125dc668dedSStefan Hajnoczidone
126dc668dedSStefan Hajnoczi
127dc668dedSStefan Hajnoczi# success, all done
128dc668dedSStefan Hajnocziecho "*** done"
129dc668dedSStefan Hajnoczirm -f $seq.full
130dc668dedSStefan Hajnoczistatus=0
131