1#!/bin/sh
2#
3# Copyright (c) 2015 Fujitsu Ltd.
4# Author: Zeng Linggang <zenglg.jy@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 the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19#
20# Test du command with some basic options.
21#
22
23TCID=du01
24TST_TOTAL=23
25. test.sh
26
27setup()
28{
29	tst_check_cmds dd du stat
30
31	tst_tmpdir
32	TST_CLEANUP=cleanup
33
34	ROD_SILENT dd if=/dev/zero of=testfile bs=1M count=10
35
36	ROD_SILENT mkdir -p testdir
37
38	ROD_SILENT ln -s ../testfile testdir/testsymlink
39
40	# Display values are in units of the first available SIZE
41	# from --block-size, and the DU_BLOCK_SIZE, BLOCK_SIZE and
42	# BLOCKSIZE environment variables. Here we need to
43	# set DU_BLOCK_SIZE to 1024 to ensure the result is expected.
44	export DU_BLOCK_SIZE=1024
45}
46
47cleanup()
48{
49	tst_rmdir
50}
51
52du_test()
53{
54	local test_return
55
56	$1 > temp 2>&1
57	test_return=$?
58
59	if [ ${test_return} -ne 0 ]; then
60		grep -q -E "unrecognized option|invalid option" temp
61		if [ $? -eq 0 ]; then
62			tst_resm TCONF "'$1' not supported"
63		else
64			tst_resm TFAIL "'$1' failed"
65		fi
66		return
67	fi
68
69	grep -q $2 temp
70	if [ $? -eq 0 ]; then
71		tst_resm TPASS "'$1' passed"
72	else
73		tst_resm TFAIL "'$1' failed"
74		tst_resm TINFO "Looking for '$2' in:"
75		cat temp
76	fi
77}
78
79setup
80
81block_size=512
82
83# The output could be different in some systems, if we use du to
84# estimate file space usage with the same filesystem and the same size.
85# So we use the approximate value to check.
86check1="10[2-3][0-9][0-9][[:space:]]\."
87check2="10[2-3][0-9][0-9][[:space:]]testfile"
88check3="[0-4][[:space:]]\.\/testdir\/testsymlink"
89check5="20[4-5][0-9][0-9][[:space:]]\."
90check7="10[4-5][0-9][0-9]\{4\}[[:space:]]\."
91check9="10[2-3][0-9][0-9][[:space:]]total"
92check11="10[2-3][0-9][0-9][[:space:]]testdir\/testsymlink"
93check14="1[0,1]M[[:space:]]\."
94check16="10[2-3][0-9][0-9][[:space:]]testdir\/"
95check20="11M[[:space:]]\."
96check23="[0-9]\{1,2\}[[:space:]]\."
97
98du_test "du" ${check1}
99du_test "du testfile" ${check2}
100du_test "du -a" ${check3}
101du_test "du --all" ${check3}
102du_test "du -B ${block_size}" ${check5}
103du_test "du --block-size=${block_size}" ${check5}
104du_test "du -b" ${check7}
105du_test "du --bytes" ${check7}
106du_test "du -c" ${check9}
107du_test "du --total" ${check9}
108du_test "du -D testdir/testsymlink" ${check11}
109du_test "du --dereference-args testdir/testsymlink" ${check11}
110du_test "du --max-depth=1" ${check1}
111du_test "du --human-readable" ${check14}
112du_test "du -k" ${check1}
113du_test "du -L testdir/" ${check16}
114du_test "du --dereference testdir/" ${check16}
115du_test "du -P" ${check1}
116du_test "du --no-dereference" ${check1}
117du_test "du --si" ${check20}
118du_test "du -s" ${check1}
119du_test "du --summarize" ${check1}
120du_test "du --exclude=testfile" ${check23}
121
122tst_exit
123