Home | History | Annotate | Line # | Download | only in tmpfs
h_funcs.subr revision 1.6
      1  1.1    jmmv #!/bin/sh
      2  1.1    jmmv #
      3  1.6  rillig # $NetBSD: h_funcs.subr,v 1.6 2024/04/28 07:27:41 rillig 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.6  rillig 	atf_check -s exit:0 -o empty -e empty mkdir "${Mount_Point}"
     44  1.5    jmmv 	echo "mount -t tmpfs ${*} tmpfs ${Mount_Point}"
     45  1.5    jmmv 	mount -t tmpfs "${@}" tmpfs "${Mount_Point}" 2>mounterr
     46  1.5    jmmv 	if [ "${?}" -ne 0 ]; then
     47  1.5    jmmv 		cat mounterr 1>&2
     48  1.5    jmmv 		if grep 'Operation not supported' mounterr > /dev/null; then
     49  1.5    jmmv 			atf_skip "tmpfs not supported"
     50  1.5    jmmv 		fi 
     51  1.5    jmmv 		atf_fail "Failed to mount a tmpfs file system"
     52  1.1    jmmv 	fi
     53  1.5    jmmv 	cd "${Mount_Point}"
     54  1.1    jmmv }
     55  1.1    jmmv 
     56  1.1    jmmv #
     57  1.1    jmmv # test_unmount
     58  1.1    jmmv #
     59  1.1    jmmv #	Unmounts the file system mounted by test_mount.
     60  1.1    jmmv #
     61  1.1    jmmv test_unmount() {
     62  1.1    jmmv 	cd - >/dev/null
     63  1.6  rillig 	atf_check -s exit:0 -o empty -e empty umount ${Mount_Point}
     64  1.6  rillig 	atf_check -s exit:0 -o empty -e empty rmdir ${Mount_Point}
     65  1.1    jmmv 	Mount_Point=
     66  1.1    jmmv }
     67  1.1    jmmv 
     68  1.1    jmmv #
     69  1.1    jmmv # kqueue_monitor expected_nevents file1 [.. fileN]
     70  1.1    jmmv #
     71  1.1    jmmv #	Monitors the commands given through stdin (one per line) using
     72  1.1    jmmv #	kqueue and stores the events raised in a log that can be later
     73  1.1    jmmv #	verified with kqueue_check.
     74  1.1    jmmv #
     75  1.1    jmmv kqueue_monitor() {
     76  1.1    jmmv 	nev=${1}; shift
     77  1.1    jmmv 	echo "Running kqueue-monitored commands and expecting" \
     78  1.1    jmmv 	    "${nev} events"
     79  1.1    jmmv 	$(atf_get_srcdir)/h_tools kqueue ${*} >kqueue.log || \
     80  1.1    jmmv 	    atf_fail "Could not launch kqueue monitor"
     81  1.1    jmmv 	got=$(wc -l kqueue.log | awk '{ print $1 }')
     82  1.1    jmmv 	test ${got} -eq ${nev} || \
     83  1.1    jmmv 	    atf_fail "Got ${got} events but expected ${nev}"
     84  1.1    jmmv }
     85  1.1    jmmv 
     86  1.1    jmmv #
     87  1.1    jmmv # kqueue_check file event
     88  1.1    jmmv #
     89  1.1    jmmv #	Checks if kqueue raised the given event when monitoring the
     90  1.1    jmmv #	given file.
     91  1.1    jmmv #
     92  1.1    jmmv kqueue_check() {
     93  1.1    jmmv 	echo "Checking if ${1} received ${2}"
     94  1.1    jmmv 	grep "^${1} - ${2}$" kqueue.log >/dev/null || \
     95  1.1    jmmv 	    atf_fail "${1} did not receive ${2}"
     96  1.1    jmmv }
     97