Whamcloud - gitweb
b=4726
[fs/lustre-release.git] / lustre / tests / test-framework.sh
1 #!/bin/sh
2
3 set -e
4
5 export REFORMAT=""
6 export VERBOSE=false
7
8 # eg, assert_env LUSTRE MDSNODES OSTNODES CLIENTS
9 assert_env() {
10     local failed=""
11     for name in $@; do
12       if [ -z "${!name}" ]; then
13           echo "$0: $name must be set"
14           failed=1
15       fi
16     done
17     [ $failed ] && exit 1 || true
18 }
19
20 usage() {
21     echo "usage: $0 [-r] [-f cfgfile]"
22     echo "       -r: reformat"
23
24     exit
25 }
26
27 init_test_env() {
28     export LUSTRE=`absolute_path $LUSTRE`
29     export TESTSUITE=`basename $0 .sh`
30     export XMLCONFIG=${XMLCONFIG:-${TESTSUITE}.xml}
31     export LTESTDIR=${LTESTDIR:-$LUSTRE/../ltest}
32
33     [ -d /r ] && export ROOT=/r
34     export TMP=${TMP:-$ROOT/tmp}
35
36     export PATH=:$PATH:$LUSTRE/utils:$LUSTRE/tests
37     export LLMOUNT=${LLMOUNT:-"llmount"}
38     export LCONF=${LCONF:-"lconf"}
39     export LMC=${LMC:-"lmc"}
40     export LCTL=${LCTL:-"$LUSTRE/utils/lctl"}
41     export CHECKSTAT="${CHECKSTAT:-checkstat} "
42     export FSYTPE=${FSTYPE:-"ext3"}
43
44     # Paths on remote nodes, if different 
45     export RLUSTRE=${RLUSTRE:-$LUSTRE}
46     export RPWD=${RPWD:-$PWD}
47
48     # command line
49     
50     while getopts "rvf:" opt $*; do 
51         case $opt in
52             f) CONFIG=$OPTARG;;
53             r) REFORMAT=--reformat;;
54             v) VERBOSE=true;;
55             \?) usage;;
56         esac
57     done
58
59     shift $((OPTIND - 1))
60     ONLY=${ONLY:-$*}
61
62     # save the name of the config file for the upcall
63     echo "XMLCONFIG=$LUSTRE/tests/$XMLCONFIG"  > $LUSTRE/tests/XMLCONFIG
64 #    echo "CONFIG=`canonical_path $CONFIG`"  > $LUSTRE/tests/CONFIG
65 }
66
67 # Facet functions
68 start() {
69     facet=$1
70     shift
71     active=`facet_active $facet`
72     do_facet $facet $LCONF --select ${facet}_svc=${active}_facet \
73         --node ${active}_facet  --ptldebug $PTLDEBUG --subsystem $SUBSYSTEM \
74         $@ $XMLCONFIG
75 }
76
77 stop() {
78     facet=$1
79     active=`facet_active $facet`
80     shift
81     do_facet $facet $LCONF --select ${facet}_svc=${active}_facet \
82         --node ${active}_facet  --ptldebug $PTLDEBUG --subsystem $SUBSYSTEM \
83         $@ --cleanup $XMLCONFIG
84 }
85
86 zconf_mount() {
87     client=$1
88     mnt=$2
89
90     do_node $client mkdir $mnt 2> /dev/null || :
91
92     if [ -x /sbin/mount.lustre ] ; then
93         do_node $client mount -t lustre -o nettype=$NETTYPE `facet_active_host mds`:/mds_svc/client_facet $mnt || return 1
94     else
95        # this is so cheating
96        do_node $client $LCONF --nosetup --node client_facet $XMLCONFIG  > /dev/null || return 2
97        do_node $client $LLMOUNT `facet_active_host mds`:/mds_svc/client_facet $mnt -o nettype=$NETTYPE|| return 4
98     fi
99
100     [ -d /r ] && $LCTL modules > /r/tmp/ogdb-`hostname`
101     return 0
102 }
103
104 zconf_umount() {
105     client=$1
106     mnt=$2
107     [ "$3" ] && force=-f
108     do_node $client umount $force  $mnt || :
109     do_node $client $LCONF --cleanup --nosetup --node client_facet $XMLCONFIG > /dev/null || :
110 }
111
112 shutdown_facet() {
113     facet=$1
114     if [ "$FAILURE_MODE" = HARD ]; then
115        $POWER_DOWN `facet_active_host $facet`
116        sleep 2 
117     elif [ "$FAILURE_MODE" = SOFT ]; then
118        stop $facet --force --failover --nomod
119     fi
120 }
121
122 reboot_facet() {
123     facet=$1
124     if [ "$FAILURE_MODE" = HARD ]; then
125        $POWER_UP `facet_active_host $facet`
126     fi
127 }
128
129 wait_for_host() {
130    HOST=$1
131    check_network  $HOST 900
132    while ! do_node $HOST "ls -d $LUSTRE " > /dev/null; do sleep 5; done
133 }
134
135 wait_for() {
136    facet=$1
137    HOST=`facet_active_host $facet`
138    wait_for_host $HOST
139 }
140
141 client_df() {
142     # not every config has many clients
143     if [ ! -z "$CLIENTS" ]; then
144         $PDSH $CLIENTS "df $MOUNT" > /dev/null
145     fi
146 }
147
148 facet_failover() {
149     facet=$1
150     echo "Failing $facet node `facet_active_host $facet`"
151     shutdown_facet $facet
152     reboot_facet $facet
153     client_df &
154     DFPID=$!
155     echo "df pid is $DFPID"
156     change_active $facet
157     TO=`facet_active_host $facet`
158     echo "Failover $facet to $TO"
159     wait_for $facet
160     start $facet
161 }
162
163 replay_barrier() {
164     local facet=$1
165     do_facet $facet sync
166     df $MOUNT
167     do_facet $facet $LCTL --device %${facet}_svc readonly
168     do_facet $facet $LCTL --device %${facet}_svc notransno
169     do_facet $facet $LCTL mark "REPLAY BARRIER"
170     $LCTL mark "REPLAY BARRIER"
171 }
172
173 mds_evict_client() {
174     UUID=`cat /proc/fs/lustre/mdc/*_MNT_*/uuid`
175     do_facet mds "echo $UUID > /proc/fs/lustre/mds/mds_svc/evict_client"
176 }
177
178 fail() {
179     local facet=$1
180     facet_failover $facet
181     df $MOUNT || error "post-failover df: $?"
182 }
183
184 fail_abort() {
185     local facet=$1
186     stop $facet --force --failover --nomod
187     change_active $facet
188     start $facet
189     do_facet $facet lctl --device %${facet}_svc abort_recovery
190     df $MOUNT || echo "first df failed: $?"
191     sleep 1
192     df $MOUNT || error "post-failover df: $?"
193 }
194
195 do_lmc() {
196     $LMC -m ${XMLCONFIG} $@
197 }
198
199 h2gm () {
200    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
201        $PDSH $1 $GMNALNID -l | cut -d\  -f2
202    fi
203 }
204
205 h2tcp() {
206    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
207    echo $1 
208    fi
209 }
210 declare -fx h2tcp
211
212 h2elan() {
213    if [ "$1" = "client" -o "$1" = "'*'" ]; then echo \'*\'; else
214    echo $1 | sed 's/[^0-9]*//g'
215    fi
216 }
217 declare -fx h2elan
218
219 facet_host() {
220    local facet=$1
221    varname=${facet}_HOST
222    echo -n ${!varname}
223 }
224
225 facet_nid() {
226    facet=$1
227    HOST=`facet_host $facet`
228    if [ -z "$HOST" ]; then
229         echo "The env variable ${facet}_HOST must be set."
230         exit 1
231    fi
232    echo `h2$NETTYPE $HOST`
233 }
234
235 facet_active() {
236     local facet=$1
237     local activevar=${facet}active
238
239     if [ -f ./${facet}active ] ; then
240         source ./${facet}active
241     fi
242
243     active=${!activevar}
244     if [ -z "$active" ] ; then 
245         echo -n ${facet}
246     else
247         echo -n ${active}
248     fi
249 }
250
251 facet_active_host() {
252     local facet=$1
253     local active=`facet_active $facet`
254     if [ "$facet" == client ]; then
255         hostname
256     else
257         echo `facet_host $active`
258     fi
259 }
260
261 change_active() {
262     local facet=$1
263     failover=${facet}failover 
264     host=`facet_host $failover`
265     [ -z "$host" ] && return
266     curactive=`facet_active $facet`
267     if [ -z "${curactive}" -o "$curactive" == "$failover" ] ; then
268         eval export ${facet}active=$facet
269     else
270         eval export ${facet}active=$failover
271     fi
272     # save the active host for this facet
273     activevar=${facet}active
274     echo "$activevar=${!activevar}" > ./$activevar
275 }
276
277 do_node() {
278     HOST=$1
279     shift
280
281     if $VERBOSE; then
282         echo "CMD: $HOST $@"
283         $PDSH $HOST $LCTL mark "$@" > /dev/null 2>&1 || :
284     fi
285     $PDSH $HOST "(PATH=\$PATH:$RLUSTRE/utils:$RLUSTRE/tests; cd $RPWD; sh -c \"$@\")"
286 }
287 do_facet() {
288     facet=$1
289     shift
290     HOST=`facet_active_host $facet`
291     do_node $HOST $@
292 }
293
294 add_facet() {
295     local facet=$1
296     shift
297     echo "add facet $facet: `facet_host $facet`"
298     do_lmc --add node --node ${facet}_facet $@ --timeout $TIMEOUT \
299         --lustre_upcall $UPCALL --ptldebug $PTLDEBUG --subsystem $SUBSYSTEM
300     do_lmc --add net --node ${facet}_facet --nid `facet_nid $facet` \
301         --nettype $NETTYPE
302 }
303
304 add_mds() {
305     facet=$1
306     shift
307     rm -f ${facet}active
308     add_facet $facet
309     do_lmc --add mds --node ${facet}_facet --mds ${facet}_svc --fstype $FSTYPE $*
310 }
311
312 add_mdsfailover() {
313     facet=$1
314     shift
315     add_facet ${facet}failover  --lustre_upcall $UPCALL
316     do_lmc --add mds  --node ${facet}failover_facet --mds ${facet}_svc --fstype $FSTYPE $*
317 }
318
319 add_ost() {
320     facet=$1
321     shift
322     rm -f ${facet}active
323     add_facet $facet
324     do_lmc --add ost --node ${facet}_facet --ost ${facet}_svc --fstype $FSTYPE $*
325 }
326
327 add_ostfailover() {
328     facet=$1
329     shift
330     add_facet ${facet}failover
331     do_lmc --add ost --failover --node ${facet}failover_facet --ost ${facet}_svc --fstype $FSTYPE $*
332 }
333
334 add_lov() {
335     lov=$1
336     mds_facet=$2
337     shift; shift
338     do_lmc --add lov --mds ${mds_facet}_svc --lov $lov $*
339     
340 }
341
342 add_client() {
343     facet=$1
344     mds=$2
345     shift; shift
346     add_facet $facet --lustre_upcall $UPCALL
347     do_lmc --add mtpt --node ${facet}_facet --mds ${mds}_svc $*
348
349 }
350
351
352 ####### 
353 # General functions
354
355 check_network() {
356    local NETWORK=0
357    local WAIT=0
358    local MAX=$2
359    while [ $NETWORK -eq 0 ]; do
360       ping -c 1 -w 3 $1 > /dev/null
361       if [ $? -eq 0 ]; then
362          NETWORK=1
363       else
364          WAIT=$((WAIT + 5))
365          echo "waiting for $1, $((MAX - WAIT)) secs left"
366          sleep 5
367       fi
368       if [ $WAIT -gt $MAX ]; then
369          echo "Network not available"
370          exit 1
371       fi
372    done
373 }
374 check_port() {
375    while( !($DSH2 $1 "netstat -tna | grep -q $2") ) ; do
376       sleep 9
377    done
378 }
379
380 no_dsh() {
381    shift
382    eval $@
383 }
384
385 comma_list() {
386     # the sed converts spaces to commas, but leaves the last space
387     # alone, so the line doesn't end with a comma.
388     echo "$*" | tr -s " " "\n" | sort -b -u | tr "\n" " " | sed 's/ \([^$]\)/,\1/g'
389 }
390
391 absolute_path() {
392    (cd `dirname $1`; echo $PWD/`basename $1`)
393 }
394
395 ##################################
396 # OBD_FAIL funcs
397
398 drop_request() {
399 # OBD_FAIL_MDS_ALL_REQUEST_NET
400     RC=0
401     do_facet mds "echo 0x123 > /proc/sys/lustre/fail_loc"
402     do_facet client "$1" || RC=$?
403     do_facet mds "echo 0 > /proc/sys/lustre/fail_loc"
404     return $RC
405 }
406
407 drop_reply() {
408 # OBD_FAIL_MDS_ALL_REPLY_NET
409     RC=0
410     do_facet mds "echo 0x122 > /proc/sys/lustre/fail_loc"
411     do_facet client "$@" || RC=$?
412     do_facet mds "echo 0 > /proc/sys/lustre/fail_loc"
413     return $RC
414 }
415
416 drop_reint_reply() {
417 # OBD_FAIL_MDS_REINT_NET_REP
418     RC=0
419     do_facet mds "echo 0x119 > /proc/sys/lustre/fail_loc"
420     do_facet client "$@" || RC=$?
421     do_facet mds "echo 0 > /proc/sys/lustre/fail_loc"
422     return $RC
423 }
424
425 pause_bulk() {
426 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
427     RC=0
428     do_facet ost "echo 0x214 > /proc/sys/lustre/fail_loc"
429     do_facet client "$1" || RC=$?
430     do_facet client "sync"
431     do_facet ost "echo 0 > /proc/sys/lustre/fail_loc"
432     return $RC
433 }
434
435 drop_ldlm_cancel() {
436 #define OBD_FAIL_LDLM_CANCEL             0x304
437     RC=0
438     do_facet client "echo 0x304 > /proc/sys/lustre/fail_loc"
439     do_facet client "$@" || RC=$?
440     do_facet client "echo 0 > /proc/sys/lustre/fail_loc"
441     return $RC
442 }
443
444 drop_bl_callback() {
445 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
446     RC=0
447     do_facet client "echo 0x305 > /proc/sys/lustre/fail_loc"
448     do_facet client "$@" || RC=$?
449     do_facet client "echo 0 > /proc/sys/lustre/fail_loc"
450     return $RC
451 }
452
453 clear_failloc() {
454     facet=$1
455     pause=$2
456     sleep $pause
457     echo "clearing fail_loc on $facet"
458     do_facet $facet "sysctl -w lustre.fail_loc=0"
459 }
460
461 cancel_lru_locks() {
462     $LCTL mark cancel_lru_locks
463     for d in /proc/fs/lustre/ldlm/namespaces/$1*; do
464         if [ -f $d/lru_size ]; then
465             echo clear > $d/lru_size
466             grep [0-9] $d/lock_unused_count
467         fi
468     done
469 }
470
471
472 pgcache_empty() {
473     for a in /proc/fs/lustre/llite/*/dump_page_cache; do
474         if [ `wc -l $a | awk '{print $1}'` -gt 1 ]; then
475                 echo there is still data in page cache $a ?
476                 cat $a;
477                 return 1;
478         fi
479     done
480     return 0
481 }
482
483 ##################################
484 # Test interface 
485 error() {
486     echo "${TESTSUITE}: **** FAIL:" $@
487     log "FAIL: $@"
488     exit 1
489 }
490
491 build_test_filter() {
492         for O in $ONLY; do
493             eval ONLY_${O}=true
494         done
495         for E in $EXCEPT $ALWAYS_EXCEPT; do
496             eval EXCEPT_${E}=true
497         done
498 }
499
500 _basetest() {
501     echo $*
502 }
503
504 basetest() {
505     IFS=abcdefghijklmnopqrstuvwxyz _basetest $1
506 }
507
508 run_test() {
509         export base=`basetest $1`
510         if [ ! -z "$ONLY" ]; then
511                  testname=ONLY_$1
512                  if [ ${!testname}x != x ]; then
513                      run_one $1 "$2"
514                      return $?
515                  fi
516                  testname=ONLY_$base
517                  if [ ${!testname}x != x ]; then
518                      run_one $1 "$2"
519                      return $?
520                  fi
521                  echo -n "."
522                  return 0
523         fi
524         testname=EXCEPT_$1
525         if [ ${!testname}x != x ]; then
526                  echo "skipping excluded test $1"
527                  return 0
528         fi
529         testname=EXCEPT_$base
530         if [ ${!testname}x != x ]; then
531                  echo "skipping excluded test $1 (base $base)"
532                  return 0
533         fi
534         run_one $1 "$2"
535
536         return $?
537 }
538
539 EQUALS="======================================================================"
540 equals_msg() {
541    msg="$@"
542
543    local suffixlen=$((${#EQUALS} - ${#msg}))
544    [ $suffixlen -lt 5 ] && suffixlen=5
545    printf '===== %s %.*s\n' "$msg" $suffixlen $EQUALS
546 }
547
548 log() {
549         echo "$*"
550         lctl mark "$*" 2> /dev/null || true
551 }
552
553 run_one() {
554     testnum=$1
555     message=$2
556     tfile=f${testnum}
557     tdir=d${base}
558
559     # Pretty tests run faster.
560     equals_msg $testnum: $message
561
562     log "== test $testnum: $message =========== `date +%H:%M:%S`"
563     test_${testnum} || error "test_$testnum failed with $?"
564 }
565
566 canonical_path() {
567    (cd `dirname $1`; echo $PWD/`basename $1`)
568 }
569