xref: /titanic_51/usr/src/cmd/mdb/test/mtest.sh (revision 0a47c91c895e274dd0990009919e30e984364a8b)
1*0a47c91cSRobert Mustacchi#!/bin/bash
2*0a47c91cSRobert Mustacchi#
3*0a47c91cSRobert Mustacchi# This file and its contents are supplied under the terms of the
4*0a47c91cSRobert Mustacchi# Common Development and Distribution License ("CDDL"), version 1.0.
5*0a47c91cSRobert Mustacchi# You may only use this file in accordance with the terms of version
6*0a47c91cSRobert Mustacchi# 1.0 of the CDDL.
7*0a47c91cSRobert Mustacchi#
8*0a47c91cSRobert Mustacchi# A full copy of the text of the CDDL should have accompanied this
9*0a47c91cSRobert Mustacchi# source.  A copy of the CDDL is also available via the Internet at
10*0a47c91cSRobert Mustacchi# http://www.illumos.org/license/CDDL.
11*0a47c91cSRobert Mustacchi#
12*0a47c91cSRobert Mustacchi
13*0a47c91cSRobert Mustacchi#
14*0a47c91cSRobert Mustacchi# Copyright 2012 (c), Joyent, Inc.
15*0a47c91cSRobert Mustacchi#
16*0a47c91cSRobert Mustacchi
17*0a47c91cSRobert Mustacchi#
18*0a47c91cSRobert Mustacchi# mdb test driver
19*0a47c91cSRobert Mustacchi#
20*0a47c91cSRobert Mustacchiunalias -a
21*0a47c91cSRobert Mustacchishopt -s xpg_echo
22*0a47c91cSRobert Mustacchi#set -o xtrace
23*0a47c91cSRobert Mustacchi
24*0a47c91cSRobert Mustacchimt_arg0=$(basename $0)
25*0a47c91cSRobert Mustacchimt_ksh="/usr/bin/ksh"
26*0a47c91cSRobert Mustacchimt_mdb="/usr/bin/mdb"
27*0a47c91cSRobert Mustacchimt_outdir=
28*0a47c91cSRobert Mustacchimt_keep=
29*0a47c91cSRobert Mustacchimt_all=
30*0a47c91cSRobert Mustacchimt_tests=
31*0a47c91cSRobert Mustacchimt_tnum=0
32*0a47c91cSRobert Mustacchimt_tfail=0
33*0a47c91cSRobert Mustacchimt_tsuc=0
34*0a47c91cSRobert Mustacchi
35*0a47c91cSRobert Mustacchifunction usage
36*0a47c91cSRobert Mustacchi{
37*0a47c91cSRobert Mustacchi	local msg="$*"
38*0a47c91cSRobert Mustacchi	[[ -z "$msg" ]] || echo "$msg" 2>&1
39*0a47c91cSRobert Mustacchi	cat <<USAGE >&2
40*0a47c91cSRobert MustacchiUsage: $mt_arg0  [ -o dir ] [ -k ] [ -m executable ] [ -a | test ... ]
41*0a47c91cSRobert Mustacchi
42*0a47c91cSRobert Mustacchi	-o dir		Sets 'dir' as the output directory
43*0a47c91cSRobert Mustacchi	-a		Runs all tests, ignores tests passed in
44*0a47c91cSRobert Mustacchi	-k		Keep output from all tests, not just failures
45*0a47c91cSRobert Mustacchi	-m 		mdb binary to test
46*0a47c91cSRobert MustacchiUSAGE
47*0a47c91cSRobert Mustacchi	exit 2
48*0a47c91cSRobert Mustacchi}
49*0a47c91cSRobert Mustacchi
50*0a47c91cSRobert Mustacchifunction fatal
51*0a47c91cSRobert Mustacchi{
52*0a47c91cSRobert Mustacchi	local msg="$*"
53*0a47c91cSRobert Mustacchi	[[ -z "$msg" ]] && msg="failed"
54*0a47c91cSRobert Mustacchi	echo "$mt_arg0: $msg" >&2
55*0a47c91cSRobert Mustacchi	exit 1
56*0a47c91cSRobert Mustacchi}
57*0a47c91cSRobert Mustacchi
58*0a47c91cSRobert Mustacchifunction setup_outdir
59*0a47c91cSRobert Mustacchi{
60*0a47c91cSRobert Mustacchi	mt_outdir="$mt_outdir/$mt_arg0.$$"
61*0a47c91cSRobert Mustacchi	mkdir -p $mt_outdir || fatal "failed to make output dir $mt_outdir"
62*0a47c91cSRobert Mustacchi}
63*0a47c91cSRobert Mustacchi
64*0a47c91cSRobert Mustacchifunction run_single
65*0a47c91cSRobert Mustacchi{
66*0a47c91cSRobert Mustacchi	local name=$1
67*0a47c91cSRobert Mustacchi	local expect base ext exe command odir res reason input
68*0a47c91cSRobert Mustacchi
69*0a47c91cSRobert Mustacchi	[[ -z "$name" ]] && fail "missing test to run"
70*0a47c91cSRobert Mustacchi	base=${name##*/}
71*0a47c91cSRobert Mustacchi	ext=${base##*.}
72*0a47c91cSRobert Mustacchi	expect=${base%%.*}
73*0a47c91cSRobert Mustacchi	odir="$mt_outdir/current"
74*0a47c91cSRobert Mustacchi	[[ -z "$ext" ]] && fatal "found test without ext: $name"
75*0a47c91cSRobert Mustacchi	[[ -z "$expect" ]] && fatal "found test without prefix: $name"
76*0a47c91cSRobert Mustacchi
77*0a47c91cSRobert Mustacchi	case "$ext" in
78*0a47c91cSRobert Mustacchi	"ksh")
79*0a47c91cSRobert Mustacchi		command="$mt_ksh $name"
80*0a47c91cSRobert Mustacchi		;;
81*0a47c91cSRobert Mustacchi	"mdb")
82*0a47c91cSRobert Mustacchi		command="$mt_mdb"
83*0a47c91cSRobert Mustacchi		input="$name"
84*0a47c91cSRobert Mustacchi		;;
85*0a47c91cSRobert Mustacchi	"out")
86*0a47c91cSRobert Mustacchi		#
87*0a47c91cSRobert Mustacchi		# This is the file format for checking output against.
88*0a47c91cSRobert Mustacchi		#
89*0a47c91cSRobert Mustacchi		return 0
90*0a47c91cSRobert Mustacchi		;;
91*0a47c91cSRobert Mustacchi	*)
92*0a47c91cSRobert Mustacchi		echo "skipping test $name (unknown extensino)"
93*0a47c91cSRobert Mustacchi		return 0
94*0a47c91cSRobert Mustacchi		;;
95*0a47c91cSRobert Mustacchi	esac
96*0a47c91cSRobert Mustacchi
97*0a47c91cSRobert Mustacchi	echo "Executing test $name ... \c"
98*0a47c91cSRobert Mustacchi	mkdir -p "$odir" >/dev/null || fatal "can't make output directory"
99*0a47c91cSRobert Mustacchi	if [[ -z "$input" ]]; then
100*0a47c91cSRobert Mustacchi		MDB=$mt_mdb $command > "$odir/stdout" 2>"$odir/stderr"
101*0a47c91cSRobert Mustacchi		res=$?
102*0a47c91cSRobert Mustacchi	else
103*0a47c91cSRobert Mustacchi		MDB=$mt_mdb $command < $input > "$odir/stdout" 2>"$odir/stderr"
104*0a47c91cSRobert Mustacchi		res=$?
105*0a47c91cSRobert Mustacchi	fi
106*0a47c91cSRobert Mustacchi
107*0a47c91cSRobert Mustacchi	if [[ -f "$name.out" ]] && ! diff "$name.out" "$odir/stdout" >/dev/null; then
108*0a47c91cSRobert Mustacchi		cp $name.out $odir/$base.out
109*0a47c91cSRobert Mustacchi		reason="stdout mismatch"
110*0a47c91cSRobert Mustacchi	elif [[ "$expect" == "tst" && $res -ne 0 ]]; then
111*0a47c91cSRobert Mustacchi		reason="test exited $res, not zero"
112*0a47c91cSRobert Mustacchi	elif [[ "$expect" == "err" && $res -eq 0 ]]; then
113*0a47c91cSRobert Mustacchi		reason="test exited $res, not non-zero"
114*0a47c91cSRobert Mustacchi	fi
115*0a47c91cSRobert Mustacchi
116*0a47c91cSRobert Mustacchi	if [[ -n "$reason" ]]; then
117*0a47c91cSRobert Mustacchi		echo "$reason"
118*0a47c91cSRobert Mustacchi		((mt_tfail++))
119*0a47c91cSRobert Mustacchi		mv "$odir" "$mt_outdir/failure.$mt_tfail" || fatal \
120*0a47c91cSRobert Mustacchi		    "failed to move test output directory"
121*0a47c91cSRobert Mustacchi		cp "$name" "$mt_outdir/failure.$mt_tfail/test" || fatal \
122*0a47c91cSRobert Mustacchi		    "failed to copy test into output directory"
123*0a47c91cSRobert Mustacchi	else
124*0a47c91cSRobert Mustacchi		echo "passed"
125*0a47c91cSRobert Mustacchi		((mt_tsuc++))
126*0a47c91cSRobert Mustacchi		mv "$odir" "$mt_outdir/success.$mt_tsuc" || fatal \
127*0a47c91cSRobert Mustacchi		    "failed to move test directory"
128*0a47c91cSRobert Mustacchi	fi
129*0a47c91cSRobert Mustacchi
130*0a47c91cSRobert Mustacchi	((mt_tnum++))
131*0a47c91cSRobert Mustacchi}
132*0a47c91cSRobert Mustacchi
133*0a47c91cSRobert Mustacchifunction run_all
134*0a47c91cSRobert Mustacchi{
135*0a47c91cSRobert Mustacchi	local tests t
136*0a47c91cSRobert Mustacchi
137*0a47c91cSRobert Mustacchi	tests=$(find . -type f -name '[tst,err]*.*.[ksh,mdb]*')
138*0a47c91cSRobert Mustacchi	for t in $tests; do
139*0a47c91cSRobert Mustacchi		run_single $t
140*0a47c91cSRobert Mustacchi	done
141*0a47c91cSRobert Mustacchi}
142*0a47c91cSRobert Mustacchi
143*0a47c91cSRobert Mustacchifunction welcome
144*0a47c91cSRobert Mustacchi{
145*0a47c91cSRobert Mustacchi	cat <<WELCOME
146*0a47c91cSRobert MustacchiStarting tests...
147*0a47c91cSRobert Mustacchimtest target: $mt_mdb
148*0a47c91cSRobert Mustacchioutput directory: $mt_outdir
149*0a47c91cSRobert MustacchiWELCOME
150*0a47c91cSRobert Mustacchi}
151*0a47c91cSRobert Mustacchi
152*0a47c91cSRobert Mustacchifunction cleanup
153*0a47c91cSRobert Mustacchi{
154*0a47c91cSRobert Mustacchi	[[ -n "$mt_keep" ]] && return
155*0a47c91cSRobert Mustacchi	rm -rf "$mt_outdir"/success.* || fatal \
156*0a47c91cSRobert Mustacchi	     "failed to remove successful test cases"
157*0a47c91cSRobert Mustacchi	if [[ $mt_tfail -eq 0 ]]; then
158*0a47c91cSRobert Mustacchi		rmdir "$mt_outdir" || fatal \
159*0a47c91cSRobert Mustacchi		    "failed to remove test output directory"
160*0a47c91cSRobert Mustacchi	fi
161*0a47c91cSRobert Mustacchi}
162*0a47c91cSRobert Mustacchi
163*0a47c91cSRobert Mustacchifunction goodbye
164*0a47c91cSRobert Mustacchi{
165*0a47c91cSRobert Mustacchi	cat <<EOF
166*0a47c91cSRobert Mustacchi
167*0a47c91cSRobert Mustacchi-------------
168*0a47c91cSRobert MustacchiResults
169*0a47c91cSRobert Mustacchi-------------
170*0a47c91cSRobert Mustacchi
171*0a47c91cSRobert MustacchiTests passed: $mt_tsuc
172*0a47c91cSRobert MustacchiTests failed: $mt_tfail
173*0a47c91cSRobert MustacchiTests ran:    $mt_tnum
174*0a47c91cSRobert Mustacchi
175*0a47c91cSRobert MustacchiEOF
176*0a47c91cSRobert Mustacchi	if [[ $mt_tfail  -eq 0 ]]; then
177*0a47c91cSRobert Mustacchi		echo "Congrats, mdb isn't completely broken, the tests pass".
178*0a47c91cSRobert Mustacchi	else
179*0a47c91cSRobert Mustacchi		echo "Some tests failed, you have some work to do."
180*0a47c91cSRobert Mustacchi	fi
181*0a47c91cSRobert Mustacchi}
182*0a47c91cSRobert Mustacchi
183*0a47c91cSRobert Mustacchiwhile getopts ":ahko:m:" c $@; do
184*0a47c91cSRobert Mustacchi	case "$c" in
185*0a47c91cSRobert Mustacchi	a)
186*0a47c91cSRobert Mustacchi		mt_all="y"
187*0a47c91cSRobert Mustacchi		;;
188*0a47c91cSRobert Mustacchi	k)
189*0a47c91cSRobert Mustacchi		mt_keep="y"
190*0a47c91cSRobert Mustacchi		;;
191*0a47c91cSRobert Mustacchi	m)
192*0a47c91cSRobert Mustacchi		mt_mdb="$OPTARG"
193*0a47c91cSRobert Mustacchi		;;
194*0a47c91cSRobert Mustacchi	o)
195*0a47c91cSRobert Mustacchi		mt_outdir="$OPTARG"
196*0a47c91cSRobert Mustacchi		;;
197*0a47c91cSRobert Mustacchi	h)
198*0a47c91cSRobert Mustacchi		usage
199*0a47c91cSRobert Mustacchi		;;
200*0a47c91cSRobert Mustacchi	:)
201*0a47c91cSRobert Mustacchi		usage "option requires an argument -- $OPTARG"
202*0a47c91cSRobert Mustacchi		;;
203*0a47c91cSRobert Mustacchi	*)
204*0a47c91cSRobert Mustacchi		usage "invalid option -- $OPTARG"
205*0a47c91cSRobert Mustacchi		;;
206*0a47c91cSRobert Mustacchi	esac
207*0a47c91cSRobert Mustacchidone
208*0a47c91cSRobert Mustacchi
209*0a47c91cSRobert Mustacchishift $((OPTIND-1))
210*0a47c91cSRobert Mustacchi
211*0a47c91cSRobert Mustacchi[[ -z "$mt_all" && $# == 0 ]] && usage "no tests to run"
212*0a47c91cSRobert Mustacchi
213*0a47c91cSRobert Mustacchi[[ -x "$mt_mdb" ]] || fatal "unable to execute mdb binary: $mt_mdb"
214*0a47c91cSRobert Mustacchi
215*0a47c91cSRobert Mustacchi[[ -z "$mt_outdir" ]] && mt_outdir=/var/tmp
216*0a47c91cSRobert Mustacchi
217*0a47c91cSRobert Mustacchisetup_outdir
218*0a47c91cSRobert Mustacchiwelcome
219*0a47c91cSRobert Mustacchi
220*0a47c91cSRobert Mustacchiif [[ ! -z "$mt_all" ]]; then
221*0a47c91cSRobert Mustacchi	run_all
222*0a47c91cSRobert Mustacchielse
223*0a47c91cSRobert Mustacchi	for t in $@; do
224*0a47c91cSRobert Mustacchi		[[ -f $t ]] || fatal "cannot find test $t"
225*0a47c91cSRobert Mustacchi		run_single $t
226*0a47c91cSRobert Mustacchi	done
227*0a47c91cSRobert Mustacchifi
228*0a47c91cSRobert Mustacchi
229*0a47c91cSRobert Mustacchigoodbye
230*0a47c91cSRobert Mustacchicleanup
231*0a47c91cSRobert Mustacchi
232*0a47c91cSRobert Mustacchi#
233*0a47c91cSRobert Mustacchi# Exit 1 if we have tests that return non-zero
234*0a47c91cSRobert Mustacchi#
235*0a47c91cSRobert Mustacchi[[ $mt_tfai -eq 0 ]]
236