• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# Copyright (c) 2015 Fujitsu Ltd.
4# Author: Zhang Jin <jy_zhangjin@cn.fujitsu.com>
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14# the GNU General Public License for more details.
15#
16# Test df command with some basic options.
17#
18
19TST_CNT=12
20TST_SETUP=setup
21TST_CLEANUP=cleanup
22TST_TESTFUNC=test
23TST_OPTS="f:"
24TST_USAGE=usage
25TST_PARSE_ARGS=parse_args
26TST_NEEDS_ROOT=1
27TST_NEEDS_TMPDIR=1
28TST_NEEDS_DEVICE=1
29. tst_test.sh
30
31usage()
32{
33	cat << EOF
34usage: $0 [-f <ext2|ext3|ext4|vfat|...>]
35
36OPTIONS
37-f	Specify the type of filesystem to be built.  If not
38	specified, the default filesystem type (currently ext2)
39	is used.
40EOF
41}
42
43FS_TYPE=ext2
44
45parse_args()
46{
47	FS_TYPE="$2"
48}
49
50setup()
51{
52	local ret
53
54	if [ -n "$FS_TYPE" ]; then
55		tst_test_cmds mkfs.${FS_TYPE}
56	fi
57
58	tst_mkfs ${FS_TYPE} ${TST_DEVICE}
59
60	ROD_SILENT mkdir -p mntpoint
61
62	mount ${TST_DEVICE} mntpoint
63	ret=$?
64	if [ $ret -eq 32 ]; then
65		tst_brk TCONF "Cannot mount ${FS_TYPE}, missing driver?"
66	fi
67
68	if [ $ret -ne 0 ]; then
69		tst_brk TBROK "Failed to mount device: mount exit = $ret"
70	fi
71
72	DF_FS_TYPE=$(mount | grep "$TST_DEVICE" | awk '{print $5}')
73}
74
75cleanup()
76{
77	tst_umount ${TST_DEVICE}
78}
79
80df_test()
81{
82	local cmd="$1 -P"
83
84	df_verify $cmd
85	if [ $? -ne 0 ]; then
86		return
87	fi
88
89	df_check $cmd
90	if [ $? -ne 0 ]; then
91		tst_res TFAIL "'$cmd' failed, not expected."
92		return
93	fi
94
95	ROD_SILENT dd if=/dev/zero of=mntpoint/testimg bs=1024 count=1024
96
97	df_verify $cmd
98
99	df_check $cmd
100	if [ $? -eq 0 ]; then
101		tst_res TPASS "'$cmd' passed."
102	else
103		tst_res TFAIL "'$cmd' failed."
104	fi
105
106	ROD_SILENT rm -rf mntpoint/testimg
107
108	# flush file system buffers, then we can get the actual sizes.
109	sync
110}
111
112df_verify()
113{
114	$@ >output 2>&1
115	if [ $? -ne 0 ]; then
116		grep -q -E "unrecognized option | invalid option" output
117		if [ $? -eq 0 ]; then
118			tst_res TCONF "'$1' not supported."
119			return 32
120		else
121			tst_res TFAIL "'$1' failed."
122			cat output
123			return 1
124		fi
125	fi
126}
127
128df_check()
129{
130	if [ "$(echo $@)" = "df -i -P" ]; then
131		local total=$(stat -f mntpoint --printf=%c)
132		local free=$(stat -f mntpoint --printf=%d)
133		local used=$((total-free))
134	else
135		local total=$(stat -f mntpoint --printf=%b)
136		local free=$(stat -f mntpoint --printf=%f)
137		local used=$((total-free))
138		local bsize=$(stat -f mntpoint --printf=%s)
139		total=$(($total * $bsize / 1024))
140		used=$(($used * $bsize / 1024))
141	fi
142
143	grep ${TST_DEVICE} output | grep -q "${total}.*${used}"
144	if [ $? -ne 0 ]; then
145		return 1
146	fi
147}
148
149test1()
150{
151	df_test "df"
152}
153
154test2()
155{
156	df_test "df -a"
157}
158
159test3()
160{
161	df_test "df -i"
162}
163
164test4()
165{
166	df_test "df -k"
167}
168
169test5()
170{
171	df_test "df -t ${DF_FS_TYPE}"
172}
173
174test6()
175{
176	df_test "df -T"
177}
178
179test7()
180{
181	df_test "df -v ${TST_DEVICE}"
182}
183
184test8()
185{
186	df_verify "df -h"
187	if [ $? -eq 0 ]; then
188		tst_res TPASS "'df -h' passed."
189	fi
190}
191
192test9()
193{
194	df_verify "df -H"
195	if [ $? -eq 0 ]; then
196		tst_res TPASS "'df -H' passed."
197	fi
198}
199
200test10()
201{
202	df_verify "df -m"
203	if [ $? -eq 0 ]; then
204		tst_res TPASS "'df -m' passed."
205	fi
206}
207
208test11()
209{
210	df_verify "df --version"
211	if [ $? -eq 0 ]; then
212		tst_res TPASS "'df --version' passed."
213	fi
214}
215
216test12()
217{
218	local cmd="df -x ${DF_FS_TYPE} -P"
219
220	df_verify $cmd
221	if [ $? -ne 0 ]; then
222		return
223	fi
224
225	grep ${TST_DEVICE} output | grep -q mntpoint
226	if [ $? -ne 0 ]; then
227		tst_res TPASS "'$cmd' passed."
228	else
229		tst_res TFAIL "'$cmd' failed."
230	fi
231}
232
233tst_run
234