xref: /illumos-gate/usr/src/test/util-tests/tests/du/dutest.ksh (revision 43c208b5f450eb6cd6064cb062ac44d41549e785)
1*43c208b5SToomas Soome#! /usr/bin/ksh
2*43c208b5SToomas Soome#
3*43c208b5SToomas Soome# This file and its contents are supplied under the terms of the
4*43c208b5SToomas Soome# Common Development and Distribution License ("CDDL"), version 1.0.
5*43c208b5SToomas Soome# You may only use this file in accordance with the terms of version
6*43c208b5SToomas Soome# 1.0 of the CDDL.
7*43c208b5SToomas Soome#
8*43c208b5SToomas Soome# A full copy of the text of the CDDL should have accompanied this
9*43c208b5SToomas Soome# source.  A copy of the CDDL is also available via the Internet at
10*43c208b5SToomas Soome# http://www.illumos.org/license/CDDL.
11*43c208b5SToomas Soome#
12*43c208b5SToomas Soome
13*43c208b5SToomas Soome#
14*43c208b5SToomas Soome# Copyright 2025 Edgecast Cloud LLC
15*43c208b5SToomas Soome#
16*43c208b5SToomas Soome
17*43c208b5SToomas Soome. "$(dirname $0)/du.kshlib"
18*43c208b5SToomas Soome
19*43c208b5SToomas Soomedu="/bin/du"
20*43c208b5SToomas Soome
21*43c208b5SToomas Soomefunction fail {
22*43c208b5SToomas Soome	echo "FAIL $@"
23*43c208b5SToomas Soome	((fail++))
24*43c208b5SToomas Soome}
25*43c208b5SToomas Soome
26*43c208b5SToomas Soomefunction pass {
27*43c208b5SToomas Soome	echo "PASS $@"
28*43c208b5SToomas Soome}
29*43c208b5SToomas Soome
30*43c208b5SToomas Soomefunction A_flag
31*43c208b5SToomas Soome{
32*43c208b5SToomas Soome	typeset size
33*43c208b5SToomas Soome	typeset fn="$0"
34*43c208b5SToomas Soome	typeset errors=$fail
35*43c208b5SToomas Soome
36*43c208b5SToomas Soome	require_sparse_file_support
37*43c208b5SToomas Soome
38*43c208b5SToomas Soome	truncate -s 10g sparse.file
39*43c208b5SToomas Soome
40*43c208b5SToomas Soome	set -- $($du -s sparse.file)
41*43c208b5SToomas Soome	size=$1
42*43c208b5SToomas Soome
43*43c208b5SToomas Soome	if [[ "$size" != "1" ]]; then
44*43c208b5SToomas Soome		fail "$fn unexpected size: \"$size\""
45*43c208b5SToomas Soome	fi
46*43c208b5SToomas Soome
47*43c208b5SToomas Soome	set -- $($du -Ah sparse.file)
48*43c208b5SToomas Soome	size="$1"
49*43c208b5SToomas Soome
50*43c208b5SToomas Soome	if [[ "$size" != "10G" ]]; then
51*43c208b5SToomas Soome		fail "$fn unexpected size \"$size\""
52*43c208b5SToomas Soome	fi
53*43c208b5SToomas Soome	rm sparse.file
54*43c208b5SToomas Soome	(( errors == fail )) && pass $fn
55*43c208b5SToomas Soome}
56*43c208b5SToomas Soome
57*43c208b5SToomas Soomefunction H_flag
58*43c208b5SToomas Soome{
59*43c208b5SToomas Soome	typeset paths1='testdir/A/B testdir/A testdir/C testdir'
60*43c208b5SToomas Soome        typeset paths2='testdir/C/B testdir/C'
61*43c208b5SToomas Soome	typeset lineprefix=$'^[0-9]+\t'
62*43c208b5SToomas Soome	typeset sep="\$\n${lineprefix}"
63*43c208b5SToomas Soome	typeset fn="$0"
64*43c208b5SToomas Soome	typeset errors=$fail
65*43c208b5SToomas Soome
66*43c208b5SToomas Soome	mkdir testdir
67*43c208b5SToomas Soome	(cd testdir && mkdir A && touch A/B && ln -s A C)
68*43c208b5SToomas Soome	$du -aAH testdir > du.out
69*43c208b5SToomas Soome	egrep "${lineprefix}$(echo $paths1 | sed -e "s/ /$sep/g")\$" \
70*43c208b5SToomas Soome	    du.out > egrep.out
71*43c208b5SToomas Soome	if ! cmp -s du.out egrep.out
72*43c208b5SToomas Soome	then
73*43c208b5SToomas Soome		fail "$fn unexpected output"
74*43c208b5SToomas Soome	fi
75*43c208b5SToomas Soome	# Check that the output doesn't contain any lines (i.e. paths) that we
76*43c208b5SToomas Soome	# did not expect it to contain from $paths1.
77*43c208b5SToomas Soome	egrep -v "${lineprefix}$(echo $paths1 | sed -e "s/ /$sep/g")\$" \
78*43c208b5SToomas Soome	    du.out > egrep.out
79*43c208b5SToomas Soome	if [[ -s egrep.out ]]
80*43c208b5SToomas Soome	then
81*43c208b5SToomas Soome		fail "$fn unexpected output"
82*43c208b5SToomas Soome	fi
83*43c208b5SToomas Soome	$du -aAH testdir/C > du.out
84*43c208b5SToomas Soome
85*43c208b5SToomas Soome	egrep "${lineprefix}$(echo $paths2 | sed -e "s/ /$sep/g")\$" \
86*43c208b5SToomas Soome	    du.out > egrep.out
87*43c208b5SToomas Soome	if ! cmp -s du.out egrep.out
88*43c208b5SToomas Soome	then
89*43c208b5SToomas Soome		fail "$fn unexpected output"
90*43c208b5SToomas Soome	fi
91*43c208b5SToomas Soome	# Check that the output doesn't contain any lines (i.e. paths) that we
92*43c208b5SToomas Soome	# did not expect it to contain from $paths2.
93*43c208b5SToomas Soome	egrep -v "${lineprefix}$(echo $paths2 | sed -e "s/ /$sep/g")\$" \
94*43c208b5SToomas Soome	    du.out > egrep.out
95*43c208b5SToomas Soome	if [[ -s egrep.out ]]
96*43c208b5SToomas Soome	then
97*43c208b5SToomas Soome		fail "$fn unexpected output"
98*43c208b5SToomas Soome	fi
99*43c208b5SToomas Soome	rm -rf testdir du.out egrep.out
100*43c208b5SToomas Soome	(( errors == fail )) && pass $fn
101*43c208b5SToomas Soome}
102*43c208b5SToomas Soome
103*43c208b5SToomas Soomefunction L_flag
104*43c208b5SToomas Soome{
105*43c208b5SToomas Soome	typeset fn="$0"
106*43c208b5SToomas Soome	typeset errors=$fail
107*43c208b5SToomas Soome
108*43c208b5SToomas Soome	mkdir testdir
109*43c208b5SToomas Soome	truncate -s 8192 testdir/A
110*43c208b5SToomas Soome	ln -s A testdir/B
111*43c208b5SToomas Soome
112*43c208b5SToomas Soome	if [[ $($du -A testdir) != $(printf "17\ttestdir\n") ]]; then
113*43c208b5SToomas Soome		fail "$fn unexpected size"
114*43c208b5SToomas Soome	fi
115*43c208b5SToomas Soome	if [[ $($du -AL testdir) != $(printf "17\ttestdir\n") ]]; then
116*43c208b5SToomas Soome		fail "$fn unexpected size"
117*43c208b5SToomas Soome	fi
118*43c208b5SToomas Soome	rm -rf testdir
119*43c208b5SToomas Soome	(( errors == fail )) && pass $fn
120*43c208b5SToomas Soome}
121*43c208b5SToomas Soome
122*43c208b5SToomas Soomefunction a_flag
123*43c208b5SToomas Soome{
124*43c208b5SToomas Soome	typeset fn="$0"
125*43c208b5SToomas Soome	typeset errors=$fail
126*43c208b5SToomas Soome
127*43c208b5SToomas Soome	mkdir testdir
128*43c208b5SToomas Soome	truncate -s 0 testdir/A
129*43c208b5SToomas Soome
130*43c208b5SToomas Soome	if [[ $($du -A testdir) != $(printf "1\ttestdir\n") ]]; then
131*43c208b5SToomas Soome		fail "$fn unexpected size"
132*43c208b5SToomas Soome	fi
133*43c208b5SToomas Soome	if [[ $($du -Aa testdir) != $(printf "0\ttestdir/A\n1\ttestdir\n") ]]
134*43c208b5SToomas Soome	then
135*43c208b5SToomas Soome		fail "$fn unexpected size"
136*43c208b5SToomas Soome	fi
137*43c208b5SToomas Soome
138*43c208b5SToomas Soome	rm -rf testdir
139*43c208b5SToomas Soome	(( errors == fail )) && pass $fn
140*43c208b5SToomas Soome}
141*43c208b5SToomas Soome
142*43c208b5SToomas Soomefunction h_flag
143*43c208b5SToomas Soome{
144*43c208b5SToomas Soome	typeset fn="$0"
145*43c208b5SToomas Soome	typeset errors=$fail
146*43c208b5SToomas Soome
147*43c208b5SToomas Soome	require_sparse_file_support
148*43c208b5SToomas Soome
149*43c208b5SToomas Soome	truncate -s 1k A
150*43c208b5SToomas Soome	truncate -s 1m B
151*43c208b5SToomas Soome	truncate -s 1g C
152*43c208b5SToomas Soome	truncate -s 1t D
153*43c208b5SToomas Soome
154*43c208b5SToomas Soome	if [[ $($du -Ah A B C D) != $(printf "1K\tA\n1M\tB\n1G\tC\n1T\tD\n") ]]
155*43c208b5SToomas Soome	then
156*43c208b5SToomas Soome		fail "$fn unexpected size"
157*43c208b5SToomas Soome	fi
158*43c208b5SToomas Soome
159*43c208b5SToomas Soome	rm A B C D
160*43c208b5SToomas Soome	(( errors == fail )) && pass $fn
161*43c208b5SToomas Soome}
162*43c208b5SToomas Soome
163*43c208b5SToomas Soomefunction k_flag
164*43c208b5SToomas Soome{
165*43c208b5SToomas Soome	typeset fn="$0"
166*43c208b5SToomas Soome	typeset errors=$fail
167*43c208b5SToomas Soome
168*43c208b5SToomas Soome	require_sparse_file_support
169*43c208b5SToomas Soome
170*43c208b5SToomas Soome	truncate -s 1k A
171*43c208b5SToomas Soome	truncate -s 1m B
172*43c208b5SToomas Soome
173*43c208b5SToomas Soome	if [[ $($du -Ak A B) != $(printf "1\tA\n1024\tB\n") ]]; then
174*43c208b5SToomas Soome		fail "$fn unexpected size"
175*43c208b5SToomas Soome	fi
176*43c208b5SToomas Soome
177*43c208b5SToomas Soome	rm A B
178*43c208b5SToomas Soome	(( errors == fail )) && pass $fn
179*43c208b5SToomas Soome}
180*43c208b5SToomas Soome
181*43c208b5SToomas Soomefunction m_flag
182*43c208b5SToomas Soome{
183*43c208b5SToomas Soome	typeset fn="$0"
184*43c208b5SToomas Soome	typeset errors=$fail
185*43c208b5SToomas Soome
186*43c208b5SToomas Soome	require_sparse_file_support
187*43c208b5SToomas Soome
188*43c208b5SToomas Soome	truncate -s 1k A
189*43c208b5SToomas Soome	truncate -s 1m B
190*43c208b5SToomas Soome	truncate -s 1g C
191*43c208b5SToomas Soome
192*43c208b5SToomas Soome	if [[ $($du -Am A B C) != $(printf "1\tA\n1\tB\n1024\tC\n") ]]; then
193*43c208b5SToomas Soome		fail "$fn unexpected size"
194*43c208b5SToomas Soome	fi
195*43c208b5SToomas Soome
196*43c208b5SToomas Soome	rm A B C
197*43c208b5SToomas Soome	(( errors == fail )) && pass $fn
198*43c208b5SToomas Soome}
199*43c208b5SToomas Soome
200*43c208b5SToomas Soomefunction s_flag
201*43c208b5SToomas Soome{
202*43c208b5SToomas Soome	typeset fn="$0"
203*43c208b5SToomas Soome	typeset errors=$fail
204*43c208b5SToomas Soome
205*43c208b5SToomas Soome	require_sparse_file_support
206*43c208b5SToomas Soome
207*43c208b5SToomas Soome	mkdir -p testdir/testdir1
208*43c208b5SToomas Soome	truncate -s 0 testdir/testdir1/A testdir/testdir1/B
209*43c208b5SToomas Soome
210*43c208b5SToomas Soome	if [[ $($du -As testdir) != $(printf "1\ttestdir\n") ]]; then
211*43c208b5SToomas Soome		fail "$fn unexpected size"
212*43c208b5SToomas Soome	fi
213*43c208b5SToomas Soome	if [[ $($du -As testdir/A) != $(printf "0\ttestdir/A\n") ]]; then
214*43c208b5SToomas Soome		fail "$fn unexpected size"
215*43c208b5SToomas Soome	fi
216*43c208b5SToomas Soome	if [[ $($du -As testdir/testdir1) != \
217*43c208b5SToomas Soome	    $(printf "1\ttestdir/testdir1\n") ]]; then
218*43c208b5SToomas Soome		fail "$fn unexpected size"
219*43c208b5SToomas Soome	fi
220*43c208b5SToomas Soome	if [[ $($du -As testdir/testdir1/B) != \
221*43c208b5SToomas Soome	    $(printf "0\ttestdir/testdir1/B\n") ]]; then
222*43c208b5SToomas Soome		fail "$fn unexpected size"
223*43c208b5SToomas Soome	fi
224*43c208b5SToomas Soome
225*43c208b5SToomas Soome	rm -rf testdir
226*43c208b5SToomas Soome	(( errors == fail )) && pass $fn
227*43c208b5SToomas Soome}
228*43c208b5SToomas Soome
229*43c208b5SToomas Soomemkdir -p $test_dir
230*43c208b5SToomas Soomecd $test_dir
231*43c208b5SToomas Soome
232*43c208b5SToomas SoomeA_flag
233*43c208b5SToomas SoomeH_flag
234*43c208b5SToomas SoomeL_flag
235*43c208b5SToomas Soomea_flag
236*43c208b5SToomas Soomeh_flag
237*43c208b5SToomas Soomek_flag
238*43c208b5SToomas Soomem_flag
239*43c208b5SToomas Soomes_flag
240*43c208b5SToomas Soome
241*43c208b5SToomas Soomecd -
242*43c208b5SToomas Soomerm -rf $test_dir
243*43c208b5SToomas Soome
244*43c208b5SToomas Soomeexit $fail
245