111a82d14SPhilippe Mathieu-Daudé#!/usr/bin/env bash 2*9dd003a9SVladimir Sementsov-Ogievskiy# group: rw 3dc668dedSStefan Hajnoczi# 4dc668dedSStefan Hajnoczi# Test NBD client unexpected disconnect 5dc668dedSStefan Hajnoczi# 6dc668dedSStefan Hajnoczi# Copyright Red Hat, Inc. 2014 7dc668dedSStefan Hajnoczi# 8dc668dedSStefan Hajnoczi# This program is free software; you can redistribute it and/or modify 9dc668dedSStefan Hajnoczi# it under the terms of the GNU General Public License as published by 10dc668dedSStefan Hajnoczi# the Free Software Foundation; either version 2 of the License, or 11dc668dedSStefan Hajnoczi# (at your option) any later version. 12dc668dedSStefan Hajnoczi# 13dc668dedSStefan Hajnoczi# This program is distributed in the hope that it will be useful, 14dc668dedSStefan Hajnoczi# but WITHOUT ANY WARRANTY; without even the implied warranty of 15dc668dedSStefan Hajnoczi# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16dc668dedSStefan Hajnoczi# GNU General Public License for more details. 17dc668dedSStefan Hajnoczi# 18dc668dedSStefan Hajnoczi# You should have received a copy of the GNU General Public License 19dc668dedSStefan Hajnoczi# along with this program. If not, see <http://www.gnu.org/licenses/>. 20dc668dedSStefan Hajnoczi# 21dc668dedSStefan Hajnoczi 22dc668dedSStefan Hajnoczi# creator 23dc668dedSStefan Hajnocziowner=stefanha@redhat.com 24dc668dedSStefan Hajnoczi 25dc668dedSStefan Hajnocziseq=`basename $0` 26dc668dedSStefan Hajnocziecho "QA output created by $seq" 27dc668dedSStefan Hajnoczi 28dc668dedSStefan Hajnoczistatus=1 # failure is the default! 29dc668dedSStefan Hajnoczi 3002d2d860SStefan Hajnoczi_cleanup() 3102d2d860SStefan Hajnoczi{ 32a7552b52SMax Reitz rm -f "$SOCK_DIR/nbd.sock" 3302d2d860SStefan Hajnoczi rm -f nbd-fault-injector.out 3402d2d860SStefan Hajnoczi rm -f nbd-fault-injector.conf 3502d2d860SStefan Hajnoczi} 3602d2d860SStefan Hajnoczitrap "_cleanup; exit \$status" 0 1 2 3 15 3702d2d860SStefan Hajnoczi 38dc668dedSStefan Hajnoczi# get standard environment, filters and checks 39dc668dedSStefan Hajnoczi. ./common.rc 40dc668dedSStefan Hajnoczi. ./common.filter 41dc668dedSStefan Hajnoczi 42d98205c5SCleber Rosa_supported_fmt raw 43dc668dedSStefan Hajnoczi_supported_proto nbd 44dc668dedSStefan Hajnoczi_supported_os Linux 45dc668dedSStefan Hajnoczi 46dc668dedSStefan Hajnoczicheck_disconnect() { 4702d2d860SStefan Hajnoczi local event export_name=foo extra_args nbd_addr nbd_url proto when 4802d2d860SStefan Hajnoczi 4902d2d860SStefan Hajnoczi while true; do 5002d2d860SStefan Hajnoczi case $1 in 5102d2d860SStefan Hajnoczi --classic-negotiation) 5202d2d860SStefan Hajnoczi shift 5302d2d860SStefan Hajnoczi extra_args=--classic-negotiation 5402d2d860SStefan Hajnoczi export_name= 5502d2d860SStefan Hajnoczi ;; 5602d2d860SStefan Hajnoczi --tcp) 5702d2d860SStefan Hajnoczi shift 5802d2d860SStefan Hajnoczi proto=tcp 5902d2d860SStefan Hajnoczi ;; 6002d2d860SStefan Hajnoczi --unix) 6102d2d860SStefan Hajnoczi shift 6202d2d860SStefan Hajnoczi proto=unix 6302d2d860SStefan Hajnoczi ;; 6402d2d860SStefan Hajnoczi *) 6502d2d860SStefan Hajnoczi break 6602d2d860SStefan Hajnoczi ;; 6702d2d860SStefan Hajnoczi esac 6802d2d860SStefan Hajnoczi done 6902d2d860SStefan 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 8102d2d860SStefan Hajnoczi if [ "$proto" = "tcp" ]; then 8202d2d860SStefan Hajnoczi nbd_addr="127.0.0.1:0" 83dc668dedSStefan Hajnoczi else 84a7552b52SMax Reitz nbd_addr="$SOCK_DIR/nbd.sock" 85dc668dedSStefan Hajnoczi fi 86dc668dedSStefan Hajnoczi 87a7552b52SMax Reitz rm -f "$SOCK_DIR/nbd.sock" 8802d2d860SStefan Hajnoczi 89ddc7093eSMax Reitz echo > "$TEST_DIR/nbd-fault-injector.out" 9002d2d860SStefan Hajnoczi $PYTHON nbd-fault-injector.py $extra_args "$nbd_addr" "$TEST_DIR/nbd-fault-injector.conf" >"$TEST_DIR/nbd-fault-injector.out" 2>&1 & 9102d2d860SStefan Hajnoczi 9202d2d860SStefan Hajnoczi # Wait for server to be ready 9302d2d860SStefan Hajnoczi while ! grep -q 'Listening on ' "$TEST_DIR/nbd-fault-injector.out"; do 9402d2d860SStefan Hajnoczi sleep 0.1 9502d2d860SStefan Hajnoczi done 9602d2d860SStefan Hajnoczi 9702d2d860SStefan Hajnoczi # Extract the final address (port number has now been assigned in tcp case) 98ddc7093eSMax Reitz nbd_addr=$(sed -n 's/^Listening on //p' \ 99ddc7093eSMax Reitz "$TEST_DIR/nbd-fault-injector.out") 10002d2d860SStefan Hajnoczi 10102d2d860SStefan Hajnoczi if [ "$proto" = "tcp" ]; then 10202d2d860SStefan Hajnoczi nbd_url="nbd+tcp://$nbd_addr/$export_name" 10302d2d860SStefan Hajnoczi else 10402d2d860SStefan Hajnoczi nbd_url="nbd+unix:///$export_name?socket=$nbd_addr" 10502d2d860SStefan Hajnoczi fi 10602d2d860SStefan Hajnoczi 10705d0fce4SMax Reitz $QEMU_IO -c "read 0 512" "$nbd_url" 2>&1 | _filter_qemu_io | _filter_nbd 108dc668dedSStefan Hajnoczi 109dc668dedSStefan Hajnoczi echo 110dc668dedSStefan Hajnoczi} 111dc668dedSStefan Hajnoczi 11202d2d860SStefan Hajnoczifor proto in tcp unix; do 113dc668dedSStefan Hajnoczi for event in neg1 "export" neg2 request reply data; do 114dc668dedSStefan Hajnoczi for when in before after; do 11502d2d860SStefan Hajnoczi check_disconnect "--$proto" "$event" "$when" 116dc668dedSStefan Hajnoczi done 117dc668dedSStefan Hajnoczi 118dc668dedSStefan Hajnoczi # Also inject short replies from the NBD server 119dc668dedSStefan Hajnoczi case "$event" in 120dc668dedSStefan Hajnoczi neg1) 121dc668dedSStefan Hajnoczi for when in 8 16; do 12202d2d860SStefan Hajnoczi check_disconnect "--$proto" "$event" "$when" 123dc668dedSStefan Hajnoczi done 124dc668dedSStefan Hajnoczi ;; 125dc668dedSStefan Hajnoczi "export") 126dc668dedSStefan Hajnoczi for when in 4 12 16; do 12702d2d860SStefan Hajnoczi check_disconnect "--$proto" "$event" "$when" 128dc668dedSStefan Hajnoczi done 129dc668dedSStefan Hajnoczi ;; 130dc668dedSStefan Hajnoczi neg2) 131dc668dedSStefan Hajnoczi for when in 8 10; do 13202d2d860SStefan Hajnoczi check_disconnect "--$proto" "$event" "$when" 133dc668dedSStefan Hajnoczi done 134dc668dedSStefan Hajnoczi ;; 135dc668dedSStefan Hajnoczi reply) 136dc668dedSStefan Hajnoczi for when in 4 8; do 13702d2d860SStefan Hajnoczi check_disconnect "--$proto" "$event" "$when" 138dc668dedSStefan Hajnoczi done 139dc668dedSStefan Hajnoczi ;; 140dc668dedSStefan Hajnoczi esac 141dc668dedSStefan Hajnoczi done 142dc668dedSStefan Hajnoczi 143dc668dedSStefan Hajnoczi # Also check classic negotiation without export information 144dc668dedSStefan Hajnoczi for when in before 8 16 24 28 after; do 14502d2d860SStefan Hajnoczi check_disconnect "--$proto" --classic-negotiation "neg-classic" "$when" 14602d2d860SStefan Hajnoczi done 147dc668dedSStefan Hajnoczidone 148dc668dedSStefan Hajnoczi 149dc668dedSStefan Hajnoczi# success, all done 150dc668dedSStefan Hajnocziecho "*** done" 151dc668dedSStefan Hajnoczirm -f $seq.full 152dc668dedSStefan Hajnoczistatus=0 153