xref: /freebsd-src/tools/regression/tmpfs/h_funcs.subr (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1*f8c94cecSXin LI#!/bin/sh
2*f8c94cecSXin LI#
3*f8c94cecSXin LI# $NetBSD: h_funcs.subr,v 1.5 2006/11/09 16:20:06 jmmv Exp $
4*f8c94cecSXin LI#
5*f8c94cecSXin LI# Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
6*f8c94cecSXin LI# All rights reserved.
7*f8c94cecSXin LI#
8*f8c94cecSXin LI# This code is derived from software contributed to The NetBSD Foundation
9*f8c94cecSXin LI# by Julio M. Merino Vidal, developed as part of Google's Summer of Code
10*f8c94cecSXin LI# 2005 program.
11*f8c94cecSXin LI#
12*f8c94cecSXin LI# Redistribution and use in source and binary forms, with or without
13*f8c94cecSXin LI# modification, are permitted provided that the following conditions
14*f8c94cecSXin LI# are met:
15*f8c94cecSXin LI# 1. Redistributions of source code must retain the above copyright
16*f8c94cecSXin LI#    notice, this list of conditions and the following disclaimer.
17*f8c94cecSXin LI# 2. Redistributions in binary form must reproduce the above copyright
18*f8c94cecSXin LI#    notice, this list of conditions and the following disclaimer in the
19*f8c94cecSXin LI#    documentation and/or other materials provided with the distribution.
20*f8c94cecSXin LI#
21*f8c94cecSXin LI# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22*f8c94cecSXin LI# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23*f8c94cecSXin LI# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24*f8c94cecSXin LI# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25*f8c94cecSXin LI# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26*f8c94cecSXin LI# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27*f8c94cecSXin LI# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*f8c94cecSXin LI# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29*f8c94cecSXin LI# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30*f8c94cecSXin LI# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31*f8c94cecSXin LI# POSSIBILITY OF SUCH DAMAGE.
32*f8c94cecSXin LI#
33*f8c94cecSXin LI#
34*f8c94cecSXin LI
35*f8c94cecSXin LI#
36*f8c94cecSXin LI# Helper functions for tests written in shell script.
37*f8c94cecSXin LI#
38*f8c94cecSXin LI
39*f8c94cecSXin LIProg_Name=${0##*/}
40*f8c94cecSXin LISrc_Dir=$(pwd)
41*f8c94cecSXin LIUnprived_User=
42*f8c94cecSXin LIVerbose=2
43*f8c94cecSXin LIWork_Dir=$(pwd)/tmp
44*f8c94cecSXin LI
45*f8c94cecSXin LI# -------------------------------------------------------------------------
46*f8c94cecSXin LI
47*f8c94cecSXin LI# die
48*f8c94cecSXin LI#
49*f8c94cecSXin LI#	Called by tests when a command fails unexpectedly.  Terminates
50*f8c94cecSXin LI#	execution and tries to clean up the mount point.
51*f8c94cecSXin LI#
52*f8c94cecSXin LIdie() {
53*f8c94cecSXin LI	if [ -d ${Work_Dir} ]; then
54*f8c94cecSXin LI		cd ${Src_Dir}
55*f8c94cecSXin LI		umount ${Work_Dir}
56*f8c94cecSXin LI		rmdir ${Work_Dir}
57*f8c94cecSXin LI	fi
58*f8c94cecSXin LI	[ ${Verbose} -eq 2 ] && err "Test ended unexpectedly"
59*f8c94cecSXin LI	[ ${Verbose} -eq 1 ] && echo " failed."
60*f8c94cecSXin LI	exit 1
61*f8c94cecSXin LI}
62*f8c94cecSXin LI
63*f8c94cecSXin LI# -------------------------------------------------------------------------
64*f8c94cecSXin LI
65*f8c94cecSXin LI# err message
66*f8c94cecSXin LI#
67*f8c94cecSXin LI#	Shows the given error message and terminates the program.
68*f8c94cecSXin LI#
69*f8c94cecSXin LIerr() {
70*f8c94cecSXin LI	echo "${Prog_Name}: $*" 1>&2
71*f8c94cecSXin LI	exit 1
72*f8c94cecSXin LI}
73*f8c94cecSXin LI
74*f8c94cecSXin LI# -------------------------------------------------------------------------
75*f8c94cecSXin LI
76*f8c94cecSXin LI# test_mount [args]
77*f8c94cecSXin LI#
78*f8c94cecSXin LI#	Mounts tmpfs over ${Work_Dir} and changes the current directory
79*f8c94cecSXin LI#	to the mount point.  Optional arguments may be passed to the
80*f8c94cecSXin LI#	mount command.
81*f8c94cecSXin LI#
82*f8c94cecSXin LItest_mount() {
83*f8c94cecSXin LI	mkdir ${Work_Dir} || die
84*f8c94cecSXin LI	if [ $# -gt 0 ]; then
85*f8c94cecSXin LI		mount -t tmpfs "$@" tmpfs ${Work_Dir} || die
86*f8c94cecSXin LI	else
87*f8c94cecSXin LI		mount -t tmpfs tmpfs ${Work_Dir} || die
88*f8c94cecSXin LI	fi
89*f8c94cecSXin LI	cd ${Work_Dir}
90*f8c94cecSXin LI}
91*f8c94cecSXin LI
92*f8c94cecSXin LI# -------------------------------------------------------------------------
93*f8c94cecSXin LI
94*f8c94cecSXin LI# test_name message
95*f8c94cecSXin LI#
96*f8c94cecSXin LI#	Prints a message about what a test is going to do.
97*f8c94cecSXin LI#
98*f8c94cecSXin LItest_name() {
99*f8c94cecSXin LI	[ ${Verbose} -gt 1 ] && echo "    $*..."
100*f8c94cecSXin LI}
101*f8c94cecSXin LI
102*f8c94cecSXin LI# -------------------------------------------------------------------------
103*f8c94cecSXin LI
104*f8c94cecSXin LI# test_unmount
105*f8c94cecSXin LI#
106*f8c94cecSXin LI#	Unmounts the file system mounted by test_mount.
107*f8c94cecSXin LI#
108*f8c94cecSXin LItest_unmount() {
109*f8c94cecSXin LI	cd -
110*f8c94cecSXin LI	umount ${Work_Dir} || die
111*f8c94cecSXin LI	rmdir ${Work_Dir} || die
112*f8c94cecSXin LI}
113*f8c94cecSXin LI
114*f8c94cecSXin LI# -------------------------------------------------------------------------
115*f8c94cecSXin LI
116*f8c94cecSXin LI# kqueue_monitor expected_nevents file1 [.. fileN]
117*f8c94cecSXin LI#
118*f8c94cecSXin LI#	Monitors the commands given through stdin (one per line) using
119*f8c94cecSXin LI#	kqueue and stores the events raised in a log that can be later
120*f8c94cecSXin LI#	verified with kqueue_check.
121*f8c94cecSXin LI#
122*f8c94cecSXin LIkqueue_monitor() {
123*f8c94cecSXin LI	nev=${1}; shift
124*f8c94cecSXin LI	test_name "Running kqueue-monitored commands and expecting" \
125*f8c94cecSXin LI	    "${nev} events"
126*f8c94cecSXin LI	${Src_Dir}/h_tools kqueue ${*} >kqueue.log || return 1
127*f8c94cecSXin LI	got=$(wc -l kqueue.log | awk '{ print $1 }')
128*f8c94cecSXin LI	test ${got} -eq ${nev}
129*f8c94cecSXin LI}
130*f8c94cecSXin LI
131*f8c94cecSXin LI# -------------------------------------------------------------------------
132*f8c94cecSXin LI
133*f8c94cecSXin LI# kqueue_check file event
134*f8c94cecSXin LI#
135*f8c94cecSXin LI#	Checks if kqueue raised the given event when monitoring the
136*f8c94cecSXin LI#	given file.
137*f8c94cecSXin LI#
138*f8c94cecSXin LIkqueue_check() {
139*f8c94cecSXin LI	grep "^${1} - ${2}$" kqueue.log >/dev/null
140*f8c94cecSXin LI}
141*f8c94cecSXin LI
142*f8c94cecSXin LI# -------------------------------------------------------------------------
143*f8c94cecSXin LI
144*f8c94cecSXin LImain() {
145*f8c94cecSXin LI	local args
146*f8c94cecSXin LI
147*f8c94cecSXin LI	[ $(id -un) = root ] || err "Must be run as root"
148*f8c94cecSXin LI
149*f8c94cecSXin LI	args=$(getopt u:v:w: $*)
150*f8c94cecSXin LI	if [ $? -ne 0 ]; then
151*f8c94cecSXin LI		echo "Usage: ${Prog_Name} [-u unprived_user] [-v level] " \
152*f8c94cecSXin LI		    "[-w root_dir]" 1>&2
153*f8c94cecSXin LI		return 1
154*f8c94cecSXin LI	fi
155*f8c94cecSXin LI	set -- ${args}
156*f8c94cecSXin LI	while [ $# -gt 0 ]; do
157*f8c94cecSXin LI		case "$1" in
158*f8c94cecSXin LI			-u)
159*f8c94cecSXin LI				Unprived_User="$2"; shift
160*f8c94cecSXin LI				;;
161*f8c94cecSXin LI			-v)
162*f8c94cecSXin LI				Verbose="$2"; shift
163*f8c94cecSXin LI				;;
164*f8c94cecSXin LI			-w)
165*f8c94cecSXin LI				Work_Dir="$2"; shift
166*f8c94cecSXin LI				;;
167*f8c94cecSXin LI			--)
168*f8c94cecSXin LI				shift; break
169*f8c94cecSXin LI				;;
170*f8c94cecSXin LI		esac
171*f8c94cecSXin LI		shift
172*f8c94cecSXin LI	done
173*f8c94cecSXin LI
174*f8c94cecSXin LI	[ ${Verbose} -eq 1 ] && echo -n "${Prog_Name}:"
175*f8c94cecSXin LI	[ ${Verbose} -eq 2 ] && echo "${Prog_Name}: Running tests"
176*f8c94cecSXin LI	test_run
177*f8c94cecSXin LI	[ ${Verbose} -eq 1 ] && echo " ok."
178*f8c94cecSXin LI	[ ${Verbose} -eq 2 ] && echo "${Prog_Name}: All tests were successful"
179*f8c94cecSXin LI
180*f8c94cecSXin LI	return 0
181*f8c94cecSXin LI}
182*f8c94cecSXin LI
183*f8c94cecSXin LImain "$@"
184