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