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
19TCID=df01
20TST_TOTAL=12
21. test.sh
22
23setup()
24{
25	tst_require_root
26
27	tst_check_cmds df mkfs.${FS_TYPE} stat
28
29	tst_tmpdir
30
31	TST_CLEANUP="cleanup"
32
33	tst_acquire_device
34
35	tst_mkfs ${FS_TYPE} ${TST_DEVICE}
36
37	ROD_SILENT mkdir -p mntpoint
38
39	mount ${TST_DEVICE} mntpoint
40	ret=$?
41	if [ $ret -eq 32 ]; then
42		tst_brkm TCONF "Cannot mount ${FS_TYPE}, missing driver?"
43	fi
44
45	if [ $ret -ne 0 ]; then
46		tst_brkm TBROK "Failed to mount device: mount exit = $ret"
47	fi
48
49	DF_FS_TYPE=$(mount | grep "$TST_DEVICE" | awk '{print $5}')
50}
51
52cleanup()
53{
54	tst_umount ${TST_DEVICE}
55
56	tst_release_device
57
58	tst_rmdir
59}
60
61usage()
62{
63	cat << EOF
64	usage: $0 [-f <ext2|ext3|ext4|vfat|...>]
65
66	OPTIONS
67		-f	Specify the type of filesystem to be built.  If not
68			specified, the default filesystem type (currently ext2)
69			is used.
70		-h	Display help text and exit.
71
72EOF
73	tst_brkm TCONF "Display help text or unknown options"
74}
75
76df_test()
77{
78	cmd="$1 -P"
79
80	df_verify $cmd
81	if [ $? -ne 0 ]; then
82		return
83	fi
84
85	df_check $cmd
86	if [ $? -ne 0 ]; then
87		tst_resm TFAIL "'$cmd' failed, not expected."
88		return
89	fi
90
91	ROD_SILENT dd if=/dev/zero of=mntpoint/testimg bs=1024 count=1024
92
93	df_verify $cmd
94
95	df_check $cmd
96	if [ $? -eq 0 ]; then
97		tst_resm TPASS "'$cmd' passed."
98	else
99		tst_resm TFAIL "'$cmd' failed."
100	fi
101
102	ROD_SILENT rm -rf mntpoint/testimg
103
104	# flush file system buffers, then we can get the actual sizes.
105	sync
106}
107
108df_verify()
109{
110	$@ >output 2>&1
111	if [ $? -ne 0 ]; then
112		grep -q -E "unrecognized option | invalid option" output
113		if [ $? -eq 0 ]; then
114			tst_resm TCONF "'$1' not supported."
115			return 32
116		else
117			tst_resm TFAIL "'$1' failed."
118			cat output
119			return 1
120		fi
121	fi
122}
123
124df_check()
125{
126	if [ "$(echo $@)" = "df -i -P" ]; then
127		local total=$(stat -f mntpoint --printf=%c)
128		local free=$(stat -f mntpoint --printf=%d)
129		local used=$((total-free))
130	else
131		local total=$(stat -f mntpoint --printf=%b)
132		local free=$(stat -f mntpoint --printf=%f)
133		local used=$((total-free))
134		local bsize=$(stat -f mntpoint --printf=%s)
135		total=$(($total * $bsize / 1024))
136		used=$(($used * $bsize / 1024))
137	fi
138
139	grep ${TST_DEVICE} output | grep -q "${total}.*${used}"
140	if [ $? -ne 0 ]; then
141		return 1
142	fi
143}
144
145test1()
146{
147	df_test "df"
148}
149
150test2()
151{
152	df_test "df -a"
153}
154
155test3()
156{
157	df_test "df -i"
158}
159
160test4()
161{
162	df_test "df -k"
163}
164
165test5()
166{
167	df_test "df -t ${DF_FS_TYPE}"
168}
169
170test6()
171{
172	df_test "df -T"
173}
174
175test7()
176{
177	df_test "df -v ${TST_DEVICE}"
178}
179
180test8()
181{
182	df_verify "df -h"
183	if [ $? -eq 0 ]; then
184		tst_resm TPASS "'df -h' passed."
185	fi
186}
187
188test9()
189{
190	df_verify "df -H"
191	if [ $? -eq 0 ]; then
192		tst_resm TPASS "'df -H' passed."
193	fi
194}
195
196test10()
197{
198	df_verify "df -m"
199	if [ $? -eq 0 ]; then
200		tst_resm TPASS "'df -m' passed."
201	fi
202}
203
204test11()
205{
206	df_verify "df --version"
207	if [ $? -eq 0 ]; then
208		tst_resm TPASS "'df --version' passed."
209	fi
210}
211
212test12()
213{
214	cmd="df -x ${DF_FS_TYPE} -P"
215
216	df_verify $cmd
217	if [ $? -ne 0 ]; then
218		return
219	fi
220
221	grep ${TST_DEVICE} output | grep -q mntpoint
222	if [ $? -ne 0 ]; then
223		tst_resm TPASS "'$cmd' passed."
224	else
225		tst_resm TFAIL "'$cmd' failed."
226	fi
227}
228
229FS_TYPE=ext2
230while getopts f:h: OPTION; do
231	case $OPTION in
232	f)
233		FS_TYPE=$OPTARG;;
234	h)
235		usage;;
236	?)
237		usage;;
238	esac
239done
240
241setup
242
243for i in $(seq 1 ${TST_TOTAL})
244do
245	test$i
246done
247
248tst_exit
249