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