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