h_funcs.subr revision 1.3.4.1 1 1.1 jmmv #!/bin/sh
2 1.1 jmmv #
3 1.3.4.1 bouyer # $NetBSD: h_funcs.subr,v 1.3.4.1 2011/03/05 15:10:54 bouyer Exp $
4 1.1 jmmv #
5 1.1 jmmv # Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
6 1.1 jmmv # All rights reserved.
7 1.1 jmmv #
8 1.1 jmmv # Redistribution and use in source and binary forms, with or without
9 1.1 jmmv # modification, are permitted provided that the following conditions
10 1.1 jmmv # are met:
11 1.1 jmmv # 1. Redistributions of source code must retain the above copyright
12 1.1 jmmv # notice, this list of conditions and the following disclaimer.
13 1.1 jmmv # 2. Redistributions in binary form must reproduce the above copyright
14 1.1 jmmv # notice, this list of conditions and the following disclaimer in the
15 1.1 jmmv # documentation and/or other materials provided with the distribution.
16 1.1 jmmv #
17 1.1 jmmv # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1 jmmv # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1 jmmv # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1 jmmv # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1 jmmv # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1 jmmv # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1 jmmv # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1 jmmv # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1 jmmv # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1 jmmv # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1 jmmv # POSSIBILITY OF SUCH DAMAGE.
28 1.1 jmmv #
29 1.1 jmmv
30 1.1 jmmv Mount_Point=
31 1.1 jmmv
32 1.1 jmmv #
33 1.1 jmmv # test_mount [args]
34 1.1 jmmv #
35 1.1 jmmv # Mounts tmpfs over ${Mount_Point} and changes the current directory
36 1.1 jmmv # to the mount point. Optional arguments may be passed to the
37 1.1 jmmv # mount command.
38 1.1 jmmv #
39 1.1 jmmv test_mount() {
40 1.1 jmmv require_fs tmpfs
41 1.1 jmmv
42 1.1 jmmv Mount_Point=$(pwd)/mntpt
43 1.3 jmmv atf_check -s eq:0 -o empty -e empty mkdir ${Mount_Point}
44 1.1 jmmv if [ $# -gt 0 ]; then
45 1.3.4.1 bouyer mount -t tmpfs $* tmpfs ${Mount_Point} 2> mounterr
46 1.3.4.1 bouyer if [ $? -ne 0 ]; then
47 1.3.4.1 bouyer if grep 'Operation not supp' mounterr > /dev/null ; then
48 1.3.4.1 bouyer atf_skip "tmpfs not supported"
49 1.3.4.1 bouyer fi
50 1.3.4.1 bouyer atf_fail "mount tmpfs"
51 1.3.4.1 bouyer fi
52 1.3.4.1 bouyer else
53 1.3.4.1 bouyer mount -t tmpfs tmpfs ${Mount_Point} 2> mounterr
54 1.3.4.1 bouyer if [ $? -ne 0 ]; then
55 1.3.4.1 bouyer if grep 'Operation not supp' mounterr > /dev/null ; then
56 1.3.4.1 bouyer atf_skip "tmpfs not supported"
57 1.3.4.1 bouyer fi
58 1.3.4.1 bouyer atf_fail "mount tmpfs"
59 1.3.4.1 bouyer fi
60 1.1 jmmv fi
61 1.1 jmmv cd ${Mount_Point}
62 1.1 jmmv }
63 1.1 jmmv
64 1.1 jmmv #
65 1.1 jmmv # test_unmount
66 1.1 jmmv #
67 1.1 jmmv # Unmounts the file system mounted by test_mount.
68 1.1 jmmv #
69 1.1 jmmv test_unmount() {
70 1.1 jmmv cd - >/dev/null
71 1.3 jmmv atf_check -s eq:0 -o empty -e empty umount ${Mount_Point}
72 1.3 jmmv atf_check -s eq:0 -o empty -e empty rmdir ${Mount_Point}
73 1.1 jmmv Mount_Point=
74 1.1 jmmv }
75 1.1 jmmv
76 1.1 jmmv #
77 1.1 jmmv # kqueue_monitor expected_nevents file1 [.. fileN]
78 1.1 jmmv #
79 1.1 jmmv # Monitors the commands given through stdin (one per line) using
80 1.1 jmmv # kqueue and stores the events raised in a log that can be later
81 1.1 jmmv # verified with kqueue_check.
82 1.1 jmmv #
83 1.1 jmmv kqueue_monitor() {
84 1.1 jmmv nev=${1}; shift
85 1.1 jmmv echo "Running kqueue-monitored commands and expecting" \
86 1.1 jmmv "${nev} events"
87 1.1 jmmv $(atf_get_srcdir)/h_tools kqueue ${*} >kqueue.log || \
88 1.1 jmmv atf_fail "Could not launch kqueue monitor"
89 1.1 jmmv got=$(wc -l kqueue.log | awk '{ print $1 }')
90 1.1 jmmv test ${got} -eq ${nev} || \
91 1.1 jmmv atf_fail "Got ${got} events but expected ${nev}"
92 1.1 jmmv }
93 1.1 jmmv
94 1.1 jmmv #
95 1.1 jmmv # kqueue_check file event
96 1.1 jmmv #
97 1.1 jmmv # Checks if kqueue raised the given event when monitoring the
98 1.1 jmmv # given file.
99 1.1 jmmv #
100 1.1 jmmv kqueue_check() {
101 1.1 jmmv echo "Checking if ${1} received ${2}"
102 1.1 jmmv grep "^${1} - ${2}$" kqueue.log >/dev/null || \
103 1.1 jmmv atf_fail "${1} did not receive ${2}"
104 1.1 jmmv }
105