xref: /netbsd-src/tests/sbin/resize_ffs/common.sh (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1
2# Common settings and functions for the various resize_ffs tests.
3#
4
5# called from atf_init_test_cases
6setupvars()
7{
8	IMG=fsimage
9	TDBASE64=$(atf_get_srcdir)/testdata.tar.gz.base64
10	GOODMD5=$(atf_get_srcdir)/testdata.md5
11	# set BYTESWAP to opposite-endian.
12	if [ $(sysctl -n hw.byteorder) = "1234" ]; then
13		BYTESWAP=be
14	else
15		BYTESWAP=le
16	fi
17}
18
19# test_case() taken from the tests/ipf/h_common.sh
20# Used to declare the atf goop for a test.
21test_case()
22{
23	local name="${1}"; shift
24	local check_function="${1}"; shift
25
26	atf_test_case "${name}" cleanup
27	eval "${name}_head() { \
28		atf_set "require.user" "root" ; \
29	}"
30	eval "${name}_body() { \
31		${check_function} " "${@}" "; \
32	}"
33	eval "${name}_cleanup() { \
34		umount -f mnt  ; \
35		: reset error ; \
36	}"
37}
38
39# Used to declare the atf goop for a test expected to fail.
40test_case_xfail()
41{
42	local name="${1}"; shift
43	local reason="${1}"; shift
44	local check_function="${1}"; shift
45
46	atf_test_case "${name}" cleanup
47	eval "${name}_head() { \
48		atf_set "require.user" "root" ; \
49	}"
50	eval "${name}_body() { \
51		atf_expect_fail "${reason}" ; \
52		${check_function} " "${@}" "; \
53	}"
54	eval "${name}_cleanup() { \
55		umount -f mnt  ; \
56		: reset error ; \
57	}"
58}
59
60# copy_data requires the mount already done;  makes one copy of the test data
61copy_data ()
62{
63	uudecode -p ${TDBASE64} | (cd mnt; tar xzf - -s/testdata/TD$1/)
64}
65
66copy_multiple ()
67{
68	local i
69	for i in $(seq $1); do
70		copy_data $i
71	done
72}
73
74# remove_data removes one directory worth of test data; the purpose
75# is to ensure data exists near the end of the fs under test.
76remove_data ()
77{
78	rm -rf mnt/TD$1
79}
80
81remove_multiple ()
82{
83	local i
84	for i in $(seq $1); do
85		remove_data $i
86	done
87}
88
89# verify that the data in a particular directory is still OK
90# generated md5 file doesn't need explicit cleanup thanks to ATF
91check_data ()
92{
93	(cd mnt/TD$1 && md5 *) > TD$1.md5
94	atf_check diff -u ${GOODMD5} TD$1.md5
95}
96
97# supply begin and end arguments
98check_data_range ()
99{
100	local i
101	for i in $(seq $1 $2); do
102		check_data $i
103	done
104}
105
106
107resize_ffs()
108{
109	echo "in resize_ffs:" ${@}
110	local bs=$1
111	local fragsz=$2
112	local osize=$3
113	local nsize=$4
114	local fslevel=$5
115	local numdata=$6
116	local swap=$7
117	mkdir -p mnt
118	echo "bs is ${bs} numdata is ${numdata}"
119	echo "****resizing fs with blocksize ${bs}"
120
121	# we want no more than 16K/inode to allow test files to copy.
122	local fpi=$((fragsz * 4))
123	local i
124	if [ $fpi -gt 16384 ]; then
125		i="-i 16384"
126	fi
127	if [ x$swap != x ]; then
128		newfs -B ${BYTESWAP} -O${fslevel} -b ${bs} -f ${fragsz} \
129			-s ${osize} ${i} -F ${IMG}
130	else
131		newfs -O${fslevel} -b ${bs} -f ${fragsz} -s ${osize} ${i} \
132			-F ${IMG}
133	fi
134
135	# we're specifying relative paths, so rump_ffs warns - ignore.
136	atf_check -s exit:0 -e ignore rump_ffs ${IMG} mnt
137	copy_multiple ${numdata}
138
139	if [ ${nsize} -lt ${osize} ]; then
140	    # how much data to remove so fs can be shrunk
141	    local remove=$((numdata-numdata*nsize/osize))
142	    local dataleft=$((numdata-remove))
143	    echo remove is $remove dataleft is $dataleft
144	    remove_multiple ${remove}
145	fi
146
147	umount mnt
148	atf_check -s exit:0 -o ignore resize_ffs -y -s ${nsize} ${IMG}
149	atf_check -s exit:0 -o ignore fsck_ffs -f -n -F ${IMG}
150	atf_check -s exit:0 -e ignore rump_ffs ${IMG} mnt
151	if [ ${nsize} -lt ${osize} ]; then
152	    check_data_range $((remove + 1)) ${numdata}
153	else
154	    # checking everything because we don't delete on grow
155	    check_data_range 1 ${numdata}
156	fi
157	umount mnt
158}
159