xfstests: move generic tests out of top level dir
[xfstests-dev.git] / tests / generic / 001
1 #! /bin/bash
2 # FS QA Test No. 001
3 #
4 # Random file copier to produce chains of identical files so the head
5 # and the tail can be diff'd at the end of each iteration.
6 #
7 # Exercises creat, write and unlink for a variety of directory sizes, and
8 # checks for data corruption.
9 #
10 # run [config]
11 #
12 # config has one line per file with filename and byte size, else use
13 # the default one below.
14 #
15 #-----------------------------------------------------------------------
16 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
17 #
18 # This program is free software; you can redistribute it and/or
19 # modify it under the terms of the GNU General Public License as
20 # published by the Free Software Foundation.
21 #
22 # This program is distributed in the hope that it would be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 #
27 # You should have received a copy of the GNU General Public License
28 # along with this program; if not, write the Free Software Foundation,
29 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30 #
31 #-----------------------------------------------------------------------
32 #
33
34 seq=`basename $0`
35 echo "QA output created by $seq"
36
37 # get standard environment, filters and checks
38 . ./common.rc
39 . ./common.filter
40
41 tmp=/tmp/$$
42 here=`pwd`
43 status=1
44 done_cleanup=false
45 trap "_cleanup; rm -f $tmp.*; exit \$status" 0 1 2 3 15
46
47 # real QA test starts here
48 _supported_fs generic
49 _supported_os IRIX Linux
50
51 verbose=true
52 verify=$here/verify_fill
53
54 if [ $# -eq 0 ]
55 then
56     # use the default config
57     #
58     cat <<End-of-File >$tmp.config
59 # pathname      size in bytes
60 #
61 small           10
62 big             102400
63 sub/small       10
64 sub/big         102400
65 #
66 sub/a           1
67 sub/b           2
68 sub/c           4
69 sub/d           8
70 sub/e           16
71 sub/f           32
72 sub/g           64
73 sub/h           128
74 sub/i           256
75 sub/j           512
76 sub/k           1024
77 sub/l           2048
78 sub/m           4096
79 sub/n           8192
80 #
81 sub/a00         100
82 sub/b00         200
83 sub/c00         400
84 sub/d00         800
85 sub/e00         1600
86 sub/f00         3200
87 sub/g00         6400
88 sub/h00         12800
89 sub/i00         25600
90 sub/j00         51200
91 sub/k00         102400
92 sub/l00         204800
93 sub/m00         409600
94 sub/n00         819200
95 #
96 sub/a000        1000
97 sub/e000        16000
98 sub/h000        128000
99 sub/k000        1024000
100 End-of-File
101 elif [ $# -eq 1 ]
102 then
103     if [ -f $1 ]
104     then
105         cp $1 $tmp.config
106     else
107         echo "Error: cannot open config \"$1\""
108         exit 1
109     fi
110 else
111     echo "Usage: run [config]"
112     exit 1
113 fi
114
115 ncopy=200               # number of file copies in the chain step
116 udf_fsize=20240         # number of sectors for UDF
117
118 _setup()
119 {
120     if mkdir -p $testdir/$$
121     then
122         :
123     else
124         echo "Error: cannot mkdir \"$testdir/$$\""
125         exit 1
126     fi
127     cd $testdir/$$
128
129     $verbose && echo -n "setup "
130     sed -e '/^#/d' $tmp.config \
131     | while read file nbytes
132     do
133         dir=`dirname $file`
134         if [ "$dir" != "." ]
135         then
136             if [ ! -d $dir ]
137             then
138                 if mkdir $dir
139                 then
140                     :
141                 else
142                     $verbose && echo
143                     echo "Error: cannot mkdir \"$dir\""
144                     exit 1
145                 fi
146             fi
147         fi
148         rm -f $file
149         if $here/src/fill $file $file $nbytes
150         then
151             :
152         else
153             $verbose && echo
154             echo "Error: cannot create \"$file\""
155             exit 1
156         fi
157         $verbose && echo -n "."
158     done
159     $verbose && echo
160 }
161
162 _mark_iteration()
163 {
164     $verbose && echo -n "mark_iteration "
165     sed -e '/^#/d' $tmp.config \
166     | while read file nbytes
167     do
168         if [ ! -f $file ]
169         then
170             $verbose && echo
171             echo "Error: $file vanished!"
172             touch $tmp.bad
173             continue
174         fi
175         sed -e "s/ [0-9][0-9]* / $1 /" <$file >$file.tmp
176         mv $file.tmp $file
177         $verbose && echo -n "."
178     done
179     $verbose && echo
180 }
181
182 # for each file, make a number of copies forming a chain like foo.0,
183 # foo.1, foo.2, ... foo.N
184 #
185 # files are chosen at random, so the lengths of the chains are different
186 #
187 # then rename foo.N to foo.last and remove all of the other files in
188 # the chain
189 #
190 _chain()
191 {
192     $AWK_PROG -v full_file=$here/$seq.full -v verify=$verify <$tmp.config '
193 BEGIN   { nfile = 0 }
194 /^\#/   { next }
195         { file[nfile] = $1
196           size[nfile] = $2
197           link[nfile] = 0
198           nfile++
199           total_size += $2
200         }
201 END     { srand('$iter')
202           for (i=0; i < '$ncopy'; i++) {
203             # choose a file at random, and add one copy to that chain
204             j = -1
205             while (j < 0 || j >= nfile)
206                 j = int(rand() * nfile)
207             if (link[j] == 0) {
208                 # previous should already exist and next one should not exist
209                 printf "if [ ! -f %s ]; then echo \"%s missing!\"; exit; fi\n",file[j],file[j]
210                 printf "if [ -f %s.0 ]; then echo \"%s.0 already present!\"; exit; fi\n",file[j],file[j]
211                 printf "cp %s %s.0 || exit 1\n",file[j],file[j]
212                 printf "ls -i %s.0\n", file[j] >full_file;
213                 total_size += size[j]
214                 printf "# total size = %d\n", total_size 
215             }
216             else {
217                 # previous should already exist and next one should not exist
218                 printf "if [ ! -f %s.%d ]; then echo \"%s.%d missing!\"; exit; fi\n",file[j],link[j]-1,file[j],link[j]-1
219                 printf "if [ -f %s.%d ]; then echo \"%s.%d already present!\"; exit; fi\n",file[j],link[j],file[j],link[j]
220                 printf "cp %s.%d %s.%d || exit 1\n",file[j],link[j]-1,file[j],link[j]
221                 printf "ls -i %s.%d\n", file[j], link[j] >full_file;
222                 total_size += size[j]
223                 printf "# total size = %d\n", total_size 
224             }
225             link[j]++
226           }
227           # close all the chains, 
228           # if have at least one copy then move the last copy to "file[j].last"
229           # and remove all of the other files except the head of the chain
230           for (j=0; j<nfile; j++) {
231             if (link[j] > 0) {
232                 printf "mv %s.%d %s.last\n",file[j],link[j]-1,file[j]
233                 printf "ls -i %s.last\n", file[j] >full_file;
234             }
235             for (i=0; i<link[j]-1; i++) {
236                 printf "rm -f %s.%d\n",file[j],i
237             }
238           }
239         }' \
240         | tee -a $here/$seq.full | sh
241 }
242
243 _check()
244 {
245     rm -f $tmp.bad
246     $verbose && echo -n "check "
247     sed -e '/^#/d' $tmp.config \
248     | while read file nbytes
249     do
250         # the file is never removed so it should exist
251         if [ ! -f $file ]
252         then
253             $verbose && echo
254             echo "Error: $file vanished!"
255             touch $tmp.bad
256             continue
257         fi
258         # checks that the file and its last copy are the same
259         if [ -f $file.last ]
260         then
261             if cmp $file $file.last >/dev/null 2>&1
262             then
263                 $verbose && echo -n "."
264             else
265                 $verbose && echo
266                 echo "Error: corruption for $file ..."
267                 diff -c $file $file.last
268                 touch $tmp.bad
269             fi
270         else
271             $verbose && echo -n "."
272         fi
273     done
274     $verbose && echo
275 }
276
277 _cleanup()
278 {
279     # cleanup
280     #
281     if $done_cleanup
282     then
283         :
284     elif [ $status -eq 0 ]
285     then
286         $verbose && echo "cleanup"
287         cd /
288         rm -rf $testdir/$$
289         _cleanup_testdir
290         done_cleanup=true
291     fi
292 }
293
294 rm -f $here/$seq.full
295 status=0
296 _cleanup
297 status=1
298 done_cleanup=false
299
300 _setup_testdir
301 _setup
302
303 # do the test
304 #
305 for iter in 1 2 3 4 5
306 do
307     echo -n "iter $iter chain ... "
308     echo "iter $iter" >> $here/$seq.full
309     _chain
310     _check
311     if [ -f $tmp.bad ]
312     then
313         echo "Fatal error: test abandoned without changes"
314         exit 1
315     fi
316 done
317
318 status=0
319 exit