summaryrefslogtreecommitdiff
path: root/mysql-test/mysql-test-run.sh
blob: 6fff899b0db4167a1dbd2c72733f7e7c01f8901c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#! /bin/sh
# mysql-test-run - originally written by Matt Wagner <matt@mysql.com>
# modified by Sasha Pachev <sasha@mysql.com>
# Sligtly updated by Monty

#++
# Access Definitions
#--
DB=test
DBUSER=test
DBPASSWD=

# Are we on source or binary distribution?

if [ $0 = scripts/mysql-test-run ] ;
then
 BINARY_DIST=1
 cd mysql-test
else
 if [ -d mysql-test ] ; then
  cd mysql-test
 fi
  
 if [ -f ./mysql-test-run ] && [ -d ../sql ] ; then
 SOURCE_DIST=1
 else
  echo "If you are using binary distribution, run me from install root as"
  echo "scripts/mysql-test-run. On source distribution run me from source root"
  echo "as mysql-test/mysql-test-run or from mysql-test as ./mysql-test-run"
  exit 1
 fi
 
fi
  


#++
# Misc. Definitions
#--

#BASEDIR is always one above mysql-test directory 
CWD=`pwd`
cd ..
BASEDIR=`pwd`
cd $CWD
MYSQL_TEST_DIR=$BASEDIR/mysql-test
STD_DATA=$MYSQL_TEST_DIR/std_data
SED=sed
  
TESTDIR="$MYSQL_TEST_DIR/t/"
TESTSUFFIX=test
TOT_PASS=0
TOT_FAIL=0
TOT_TEST=0
USERT=0
SYST=0
REALT=0
MY_TMP_DIR=$MYSQL_TEST_DIR/var/tmp
TIMEFILE="$MYSQL_TEST_DIR/var/tmp/mysqltest-time"
RES_SPACE="      "
MYSQLD_SRC_DIRS="strings mysys include extra regex isam merge myisam \
 myisammrg heap sql"
GCOV_MSG=/tmp/mysqld-gcov.out
GCOV_ERR=/tmp/mysqld-gcov.err  

MASTER_RUNNING=0
SLAVE_RUNNING=0

[ -d $MY_TMP_DIR ]  || mkdir -p $MY_TMP_DIR

#++
# mysqld Environment Parameters
#--
MYRUN_DIR=var/run
MASTER_MYPORT=9306
MASTER_MYDDIR="$MYSQL_TEST_DIR/var/lib"
MASTER_MYSOCK="$MYSQL_TEST_DIR/var/tmp/mysql.sock"
MASTER_MYPID="$MYSQL_TEST_DIR/var/run/mysqld.pid"
MASTER_MYLOG="$MYSQL_TEST_DIR/var/log/mysqld.log"
MASTER_MYERR="$MYSQL_TEST_DIR/var/log/mysqld.err"


SLAVE_MYPORT=9307
SLAVE_MYDDIR="$MYSQL_TEST_DIR/var/slave-data"
SLAVE_MYSOCK="$MYSQL_TEST_DIR/var/tmp/mysql-slave.sock"
SLAVE_MYPID="$MYSQL_TEST_DIR/var/run/mysqld-slave.pid"
SLAVE_MYLOG="$MYSQL_TEST_DIR/var/log/mysqld-slave.log"
SLAVE_MYERR="$MYSQL_TEST_DIR/var/log/mysqld-slave.err"

if [ x$SOURCE_DIST = x1 ] ; then
 MY_BASEDIR=$MYSQL_TEST_DIR
else
 MY_BASEDIR=$BASEDIR
fi  

#++
# Program Definitions
#--
BASENAME=`which basename | head -1`
CAT=/bin/cat
CUT=/usr/bin/cut
ECHO=echo # use internal echo if possible
EXPR=expr # use internal if possible
FIND=/usr/bin/find
GCOV=`which gcov | head -1`
PRINTF=/usr/bin/printf
RM=/bin/rm
TIME=/usr/bin/time
TR=/usr/bin/tr
XARGS=`which xargs | head -1`

[ -z "$COLUMNS" ] && COLUMNS=80
E=`$EXPR $COLUMNS - 8`
#DASH72=`expr substr '________________________________________________________________________' 1 $E`
DASH72=`$ECHO '________________________________________________________________________'|$CUT -c 1-$E`

# on source dist, we pick up freshly build executables
# on binary, use what is installed
if [ x$SOURCE_DIST = x1 ] ; then
 MYSQLD="$BASEDIR/sql/mysqld"
 MYSQL_TEST="$BASEDIR/client/mysqltest"
 MYSQLADMIN="$BASEDIR/client/mysqladmin"
 INSTALL_DB="./install_test_db"
else
 MYSQLD="$BASEDIR/bin/mysqld"
 MYSQL_TEST="$BASEDIR/bin/mysqltest"
 MYSQLADMIN="$BASEDIR/bin/mysqladmin"
 INSTALL_DB="../scripts/install_test_db -bin"
fi


SLAVE_MYSQLD=$MYSQLD #this will be changed later if we are doing gcov


MYSQL_TEST="$MYSQL_TEST --no-defaults --socket=$MASTER_MYSOCK --database=$DB --user=$DBUSER --password=$DBPASSWD --silent"
GDB_MASTER_INIT=/tmp/gdbinit.master
GDB_SLAVE_INIT=/tmp/gdbinit.slave

while test $# -gt 0; do
  case "$1" in
    --force ) FORCE=1 ;;
    --record ) RECORD=1 ;;
    --gcov )
      if [ x$BINARY_DIST = x1 ] ; then
	echo "Cannot do coverage test without the source - please use source dist"
	exit 1
      fi
      DO_GCOV=1
      ;;
    --gdb )
      if [ x$BINARY_DIST = x1 ] ; then
	echo "Note: you will get more meaningful output on a source distribution compiled with debugging option when running tests with -gdb option"
      fi
      DO_GDB=1
      ;;
    -- )  shift; break ;;
    --* ) echo "Unrecognized option: $1"; exit 1 ;;
    * ) break ;;
  esac
  shift
done

#++
# Function Definitions
#--

prompt_user ()
{
 echo $1
 read unused
}


error () {

    $ECHO  "Error:  $1"
    exit 1
}

prefix_to_8() {
 echo "        $1" | $SED -e 's:.*\(........\)$:\1:'
}

pass_inc () {
    TOT_PASS=`$EXPR $TOT_PASS + 1`
}

fail_inc () {
    TOT_FAIL=`$EXPR $TOT_FAIL + 1`
}

skip_inc () {
    TOT_SKIP=`$EXPR $TOT_SKIP + 1`
}

total_inc () {
    TOT_TEST=`$EXPR $TOT_TEST + 1`
}

report_stats () {
    if [ $TOT_FAIL = 0 ]; then
	$ECHO "All tests successful."
    else
	xten=`$EXPR $TOT_PASS \* 10000`   
	raw=`$EXPR $xten / $TOT_TEST`     
	raw=`$PRINTF %.4d $raw`           
	whole=`$PRINTF %.2s $raw`         
	xwhole=`$EXPR $whole \* 100`      
	deci=`$EXPR $raw - $xwhole`       
	$ECHO  "Failed ${TOT_FAIL}/${TOT_TEST} tests ${whole}.${deci}% successful."
    fi
}

mysql_install_db () {
    echo "Removing stale files from previous run"
    $RM -rf $MASTER_MYDDIR $SLAVE_MYDDIR $SLAVE_MYLOG $MASTER_MYLOG \
     $SLAVE_MYERR $MASTER_MYERR
    [ -d $MYRUN_DIR ] || mkdir -p $MYRUN_DIR
    echo "installing master databases"
    $INSTALL_DB
    if [ $? != 0 ]; then
	error "Could not install master test DBs"
	exit 1
    fi
    echo "Installing slave databases"
    $INSTALL_DB -slave
    if [ $? != 0 ]; then
	error "Could not install slave test DBs"
	exit 1
    fi
    return 0
}

gcov_prepare () {
    $FIND $BASEDIR -name \*.gcov \
    -or -name \*.da | $XARGS $RM
}

gcov_collect () {
    $ECHO "Collecting source coverage info..."
    [ -f $GCOV_MSG ] && $RM $GCOV_MSG
    [ -f $GCOV_ERR ] && $RM $GCOV_ERR
    for d in $MYSQLD_SRC_DIRS; do
	cd $BASEDIR/$d
	for f in *.h *.cc *.c; do
	    $GCOV $f 2>>$GCOV_ERR  >>$GCOV_MSG
	done
	cd $MYSQL_TEST_DIR
    done

    $ECHO "gcov  info in $GCOV_MSG, errors in $GCOV_ERR"
}

start_master()
{
    [ x$MASTER_RUNNING = 1 ] && return
    cd $BASEDIR # for gcov
    #start master
    master_args="--no-defaults --log-bin=master-bin \
    	    --server-id=1 \
            --basedir=$MY_BASEDIR \
	    --port=$MASTER_MYPORT \
	    --exit-info=256 \
            --datadir=$MASTER_MYDDIR \
	    --pid-file=$MASTER_MYPID \
	    --socket=$MASTER_MYSOCK \
            --log=$MASTER_MYLOG \
	    --language=english $EXTRA_MASTER_OPT"
    if [ x$DO_GDB = x1 ]
    then
      echo "set args $master_args" > $GDB_MASTER_INIT
      xterm -title "Master" -e gdb -x $GDB_MASTER_INIT $MYSQLD &
      prompt_user "Hit enter to continue after you've started the master"
    else	    
      $MYSQLD $master_args  >> $MASTER_MYERR 2>&1 &
    fi  
  MASTER_RUNNING=1
}

start_slave()
{
    [ x$SKIP_SLAVE = x1 ] && return
    [ x$SLAVE_RUNNING = 1 ] && return
    if [ -z "$SLAVE_MASTER_INFO" ] ; then
      master_info="--master-user=root \
	    --master-connect-retry=1 \
	    --master-host=127.0.0.1 \
	    --master-port=$MASTER_MYPORT \
	    --server-id=2"
   else
     master_info=$SLAVE_MASTER_INFO
   fi	    
    
    slave_args="--no-defaults $master_info \
    	    --exit-info=256 \
	    --log-bin=slave-bin --log-slave-updates \
            --basedir=$MY_BASEDIR \
            --datadir=$SLAVE_MYDDIR \
	    --pid-file=$SLAVE_MYPID \
	    --port=$SLAVE_MYPORT \
	    --socket=$SLAVE_MYSOCK \
            --log=$SLAVE_MYLOG \
            --language=english $EXTRA_SLAVE_OPT"
    if [ x$DO_GDB = x1 ]
    then
      echo "set args $slave_args" > $GDB_SLAVE_INIT
      xterm -title "Slave" -e gdb -x $GDB_SLAVE_INIT $SLAVE_MYSQLD &
      prompt_user "Hit enter to continue after you've started the slave"
    else
      $SLAVE_MYSQLD $slave_args  >> $SLAVE_MYERR 2>&1 &
    fi
    SLAVE_RUNNING=1
}

mysql_start () {
    start_master
    start_slave
    cd $MYSQL_TEST_DIR
    return 1
}

stop_slave ()
{
  if [ x$SLAVE_RUNNING = x1 ]
  then
    $MYSQLADMIN --no-defaults --socket=$SLAVE_MYSOCK -u root shutdown
    if [ $? != 0 ] ; then # try harder!
     echo "slave not cooperating with mysqladmin, will try manual kill"
     kill `cat $SLAVE_MYPID`
     sleep 2
     if [ -f $SLAVE_MYPID ] ; then
       echo "slave refused to die, resorting to SIGKILL murder"
       kill -9 `cat $SLAVE_MYPID`
       $RM -f $SLAVE_MYPID
     else
      echo "slave responded to SIGTERM " 
     fi
    fi
    SLAVE_RUNNING=0
  fi  
}

stop_master ()
{
  if [ x$MASTER_RUNNING = x1 ]
  then
    $MYSQLADMIN --no-defaults --socket=$MASTER_MYSOCK -u root shutdown
    if [ $? != 0 ] ; then # try harder!
     echo "master not cooperating with mysqladmin, will try manual kill"
     kill `cat $MASTER_MYPID`
     sleep 2
     if [ -f $MASTER_MYPID ] ; then
       echo "master refused to die, resorting to SIGKILL murder"
       kill -9 `cat $MASTER_MYPID`
       $RM -f $MASTER_MYPID
     else
      echo "master responded to SIGTERM " 
     fi
    fi
    MASTER_RUNNING=0
  fi
}

mysql_stop ()
{
 stop_master
 stop_slave
 return 1
}

mysql_restart () {

    mysql_stop
    mysql_start

    return 1
}

mysql_loadstd () {
    
    cp $STD_DATA/*.frm $STD_DATA/*.MRG $MASTER_MYDDIR/test  
    return 1
}

run_testcase ()
{
 tf=$1
 tname=`$BASENAME $tf .test`
 master_opt_file=$TESTDIR/$tname-master.opt
 slave_opt_file=$TESTDIR/$tname-slave.opt
 slave_master_info_file=$TESTDIR/$tname-slave-master-info.opt
 SKIP_SLAVE=`$EXPR \( $tname : rpl \) = 0`
 if [ x$RECORD = x1 ]; then
  extra_flags="-r"
 else
  extra_flags=""
 fi
  
 if [ -f $master_opt_file ] ;
 then
  EXTRA_MASTER_OPT=`cat $master_opt_file`
  stop_master
  start_master
 else
  if [ ! -z "$EXTRA_MASTER_OPT" ] || [ x$MASTER_RUNNING != x1 ] ;
  then
    EXTRA_MASTER_OPT=""
    stop_master
    start_master
  fi  
 fi
 do_slave_restart=0
 
 if [ -f $slave_opt_file ] ;
 then
  EXTRA_SLAVE_OPT=`cat $slave_opt_file`
  do_slave_restart=1
 else
  if [ ! -z "$EXTRA_SLAVE_OPT" ] || [ x$SLAVE_RUNNING != x1 ] ;
  then
    EXTRA_SLAVE_OPT=""
    do_slave_restart=1    
  fi  
 fi

 if [ -f $slave_master_info_file ] ; then
   SLAVE_MASTER_INFO=`cat $slave_master_info_file`
   do_slave_restart=1
 else
  if [ ! -z "$SLAVE_MASTER_INFO" ] || [ x$SLAVE_RUNNING != x1 ] ;
  then
    SLAVE_MASTER_INFO=""
    do_slave_restart=1    
  fi  
 fi

 if [ x$do_slave_restart = x1 ] ; then
  stop_slave
  start_slave
 fi

 cd $MYSQL_TEST_DIR
  
 if [ -f $tf ] ; then
    $RM -f r/$tname.*.reject
    mytime=`$TIME -p $MYSQL_TEST -R r/$tname.result $extra_flags \
     < $tf 2> $TIMEFILE`
    res=$?

    if [ $res != 1 ]; then
	mytime=`$CAT $TIMEFILE | $TR '\n' '-'`

	USERT=`$ECHO $mytime | $CUT -d - -f 2 | $CUT -d ' ' -f 2`
        USERT=`prefix_to_8 $USERT`
	SYST=`$ECHO $mytime | $CUT -d - -f 3 | $CUT -d ' ' -f 2`
        SYST=`prefix_to_8 $SYST`
	REALT=`$ECHO $mytime | $CUT -d - -f 1 | $CUT -d ' ' -f 2`
        REALT=`prefix_to_8 $REALT`
    else
	USERT="    ...."
	SYST="    ...."
	REALT="    ...."
    fi

	timestr="$USERT $SYST $REALT"
	pname=`$ECHO "$tname                 "|$CUT -c 1-16`
	$SETCOLOR_NORMAL && $ECHO -n "$pname          $timestr"



    if [ $res == 0 ]; then
      total_inc
      pass_inc
      echo "$RES_SPACE [ pass ]"
    else
      if [ $res == 1 ]; then
	total_inc
        fail_inc
	echo "$RES_SPACE [ fail ]"
        $ECHO "failed output"
	$CAT $TIMEFILE
	$ECHO
	$ECHO
	if [ x$FORCE != x1 ] ; then
	 echo "Aborting, if you want to continue, re-run with --force"
	 mysql_stop
	 exit 1
	fi
	 
	echo "Restarting mysqld"
	mysql_restart
	echo "Resuming Tests"
      else
        pass_inc
	echo "$RES_SPACE [ skipped ]"
      fi
    fi
  fi  
}


[ "$DO_GCOV" -a ! -x "$GCOV" ] && error "No gcov found"

[ "$DO_GCOV" ] && gcov_prepare 

echo "Installing  test databases"
mysql_install_db

#do not automagically start deamons if we are in gdb or running only one test
#case
if [ -z "$DO_GDB" ] && [ -z "$1" ]
then
 $ECHO  "Starting mysqld for Testing" 
 mysql_start
fi

$ECHO  "Loading Standard Test Database"
mysql_loadstd

$ECHO  "Starting Tests for MySQL daemon"

$ECHO
$ECHO " TEST                         USER   SYSTEM  ELAPSED        RESULT"
$ECHO $DASH72

if [ -z "$1" ] ;
then
 if [ x$RECORD = x1 ]; then
  echo "Will not run in record mode without a specific test case"
 else
  for tf in $TESTDIR/*.$TESTSUFFIX
  do
    run_testcase $tf
  done
 fi
else
 tname=`$BASENAME $1 .test`
 tf=$TESTDIR/$tname.$TESTSUFFIX
 if [ -f $tf ] ; then
  run_testcase $tf
 else
   echo "Test case $tf does not exist"
 fi
fi

$ECHO $DASH72
$ECHO
$ECHO  "Ending Tests for MySQL daemon"
$RM $TIMEFILE

if [ -z "$DO_GDB" ] ;
then
    $ECHO  "Shutdown mysqld"
    mysql_stop
fi


$ECHO
report_stats
$ECHO

[ "$DO_GCOV" ] && gcov_collect # collect coverage information

exit 0