fstests: Make ./new work for non-root user
[xfstests-dev.git] / new
1 #! /bin/bash
2 #
3 #-----------------------------------------------------------------------
4 #  Copyright (c) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it would be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write the Free Software Foundation,
17 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 #
19 #-----------------------------------------------------------------------
20 #
21 # Make a new test
22 #
23
24 # generic initialization
25 iam=new
26 . ./common/test_names
27
28 trap "rm -f /tmp/$$.; exit" 0 1 2 3 15
29
30 _cleanup()
31 {
32     :
33 }
34
35 SRC_GROUPS=`find tests -not -path tests -type d -printf "%f "`
36 usage()
37 {
38     echo "Usage $0 test_dir"
39     echo "Available dirs are: $SRC_GROUPS"
40     exit
41 }
42
43 [ $# -eq 0 ] && usage
44 tdir=tests/$1
45 shift
46
47 if [ ! -f $tdir/group ]
48 then
49     echo "Creating the $tdir/group index ..."
50     cat <<'End-of-File' >$tdir/group
51 # QA groups control
52 #
53 # define groups and default group owners
54 # do not start group name with a digit
55 #
56
57 # catch-all
58 #
59 other           some-user-login
60
61 # test-group association ... one line per test
62 #
63 End-of-File
64 fi
65
66 if [ ! -w $tdir/group ]
67 then
68     chmod u+w $tdir/group
69     echo "Warning: making the index file \"$tdir/group\" writeable"
70 fi
71
72 if make
73 then
74     :
75 else
76     echo "Warning: make failed -- some tests may be missing"
77 fi
78
79 i=0
80 line=0
81 eof=1
82 [ -f "$tdir/group" ] || usage
83
84 export AWK_PROG="$(type -P awk)"
85 [ "$AWK_PROG" = "" ] && { echo "awk not found"; exit; }
86
87 for found in `cat $tdir/group | tr - ' ' | $AWK_PROG '{ print $1 }'`
88 do
89     line=$((line+1))
90     if [ -z "$found" ] || [ "$found" == "#" ]; then
91         continue
92     elif ! echo "$found" | grep -q "^$VALID_TEST_NAME$"; then
93         # this one is for tests not named by a number
94         continue
95     fi
96     i=$((i+1))
97     id=`printf "%03d" $i`
98     if [ "$id" != "$found" ];then
99         eof=0
100         break
101     fi
102 done
103 if [ $eof -eq 1 ]; then
104    line=$((line+1))
105    i=$((i+1))
106    id=`printf "%03d" $i`
107 fi
108
109 echo "Next test id is $id"
110
111 read -p "Append a name to the ID? Test name will be $id-\$name. y,[n]: " -r
112 if [[ $REPLY = [Yy] ]]; then
113         # get the new name from user
114         name=""
115         while [ "$name" = "" ]; do
116                 read -p "Enter the name: "
117                 if [ "$REPLY" = "" ]; then
118                         echo "For canceling, use ctrl+c."
119                 elif echo "$id-$REPLY" | grep -q "^$VALID_TEST_NAME$"; then
120                         if [ -e "$tdir/$id-$REPLY" ]; then
121                                 echo "File '$id-$REPLY' already exists, use another one."
122                                 echo
123                         else
124                                 name="$REPLY"
125                         fi
126                 else
127                         echo "A name can contain only alphanumeric symbols and dash!"
128                         echo
129                 fi
130         done
131
132         # now find where to insert this name
133         eof=1
134         for found in `tail -n +$line $tdir/group | $AWK_PROG '{ print $1 }'`; do
135                 found_id=$(echo "$found" | cut -d "-" -f 1 - )
136                 line=$((line+1))
137                 if [ -z "$found" ] || [ "$found" == "#" ]; then
138                         continue
139                 elif [ $found_id -gt $id ]; then
140                         eof=0
141                         break
142                 fi
143         done
144         if [ $eof -eq 0 ]; then
145                 # If place wasn't found, let $line be the end of the file
146                 line=$((line-1))
147         fi
148         id="$id-$name"
149 fi
150 echo "Creating test file '$id'"
151
152 if [ -f $tdir/$id ]
153 then
154     echo "Error: test $id already exists!"
155     _cleanup
156     exit 1
157 fi
158
159 echo -n "Creating skeletal script for you to edit ..."
160
161 year=`date +%Y`
162
163 cat <<End-of-File >$tdir/$id
164 #! /bin/bash
165 # FS QA Test $id
166 #
167 # what am I here for?
168 #
169 #-----------------------------------------------------------------------
170 # Copyright (c) $year YOUR NAME HERE.  All Rights Reserved.
171 #
172 # This program is free software; you can redistribute it and/or
173 # modify it under the terms of the GNU General Public License as
174 # published by the Free Software Foundation.
175 #
176 # This program is distributed in the hope that it would be useful,
177 # but WITHOUT ANY WARRANTY; without even the implied warranty of
178 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
179 # GNU General Public License for more details.
180 #
181 # You should have received a copy of the GNU General Public License
182 # along with this program; if not, write the Free Software Foundation,
183 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
184 #-----------------------------------------------------------------------
185 #
186
187 seq=\`basename \$0\`
188 seqres=\$RESULT_DIR/\$seq
189 echo "QA output created by \$seq"
190
191 here=\`pwd\`
192 tmp=/tmp/\$\$
193 status=1        # failure is the default!
194 trap "_cleanup; exit \\\$status" 0 1 2 3 15
195
196 _cleanup()
197 {
198         cd /
199         rm -f \$tmp.*
200 }
201
202 # get standard environment, filters and checks
203 . ./common/rc
204 . ./common/filter
205
206 # remove previous \$seqres.full before test
207 rm -f \$seqres.full
208
209 # real QA test starts here
210
211 # Modify as appropriate.
212 _supported_fs generic
213 _supported_os Linux
214 _require_test
215
216 # if error
217 exit
218
219 # optional stuff if your test has verbose output to help resolve problems
220 #echo
221 #echo "If failure, check \$seqres.full (this) and \$seqres.full.ok (reference)"
222
223 # success, all done
224 status=0
225 exit
226 End-of-File
227
228 sleep 2         # latency to read messages to this point
229 echo ""
230
231 chmod 755 $tdir/$id
232 ${EDITOR-vi} $tdir/$id
233
234 # Create default .out file
235 cat <<End-of-File >$tdir/$id.out
236 QA output created by $id
237 Silence is golden
238 End-of-File
239
240 if [ $# -eq 0 ]
241 then
242
243     while true
244     do
245         echo -n "Add to group(s) [other] (separate by space, ? for list): "
246         read ans
247         [ -z "$ans" ] && ans=other
248         if [ "X$ans" = "X?" ]
249         then
250             for d in $SRC_GROUPS; do
251                 l=$(sed -n < tests/$d/group \
252                     -e 's/#.*//' \
253                     -e 's/$/ /' \
254                     -e 's;\(^[0-9][0-9][0-9]\)\(.*$\);\2;p')
255                 grpl="$grpl $l"
256             done
257             lst=`for word in $grpl; do echo $word; done | sort| uniq `
258             echo $lst
259         else
260             # only allow lower cases, spaces, digits and underscore in group
261             inval=`echo $ans | tr -d '[:lower:][:space:][:digit:]_'`
262             if [ "$inval" != "" ]; then
263                 echo "Invalid characters in group(s): $inval"
264                 echo "Only lower cases, digits and underscore are allowed in groups, separated by space"
265                 continue
266             else
267                 # remove redundant spaces/tabs
268                 ans=`echo "$ans" | sed 's/\s\+/ /g'`
269                 break
270             fi
271         fi
272     done
273 else
274     # expert mode, groups are on the command line
275     #
276     for g in $*
277     do
278         if grep "^$g[   ]" $tdir/group >/dev/null
279         then
280             :
281         else
282             echo "Warning: group \"$g\" not defined in $tdir/group"
283         fi
284     done
285     ans="$*"
286 fi
287
288 echo -n "Adding $id to group index ..."
289 head -n $(($line-1)) $tdir/group > /tmp/$$.group
290 echo "$id $ans" >> /tmp/$$.group
291 tail -n +$((line)) $tdir/group >> /tmp/$$.group
292 mv /tmp/$$.group $tdir/group
293 echo " done."
294
295 exit 0