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