tbtest 21.5 KB
Newer Older
1 2
#!/usr/bin/perl -w

Leigh Stoller's avatar
Leigh Stoller committed
3
#
4
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24 25 26
#


27 28 29 30 31 32 33
# tbtest

# This is the toplevel of the testbed testing setup.  This command
# should be invokved by a user and script.  It well then create a
# separate object tree, configure it appropriately, setup a testing
# database, and then proceed to run tests.

34
# IMPORTANT: This command should be come from a tree configured
35 36
# for the real DB.

37
# tbtest [options] <mode stuff>
38 39
# Options:
#   -path <path> - Path to test tree, otherwise uses cwd.
40 41 42
#   -full - Do full testing.
#   -frontend - Do frontend only testing.#
#   -leavedb - Avoids removing the DB on exit.
43
#   -exitonfail - Exit on the first failure.
44
#   -session <sessionid> - Specify an alternate session id.
45
#   -flest - Generate flest.log file for flest runs.
46
#   -daikon - 
47 48
#
# <mode> is one of:
49
#    run <db> <testlist> [<pid> <eid> <num>] - Do everything.
50
#    init <db> [<pid> <eid> <num>] - Initiatlize.
51 52
#    test <testlist> - Run tests
#    single <tests> - Run only <tests>
53
#    finish - Finish run
54
# <num> is "X <type> ...", for example "10 pc 16 shark"
55 56 57

use DBI;
use POSIX;
58
use BSD::Resource;
59

60 61
#die("*** $0:\n".
#    "    Testsuite Disabled. Come edit me if need be.\n");
62

63 64
$| = 1;

65 66 67
# Lower priority since this whole thing consumes CPU like nuts
setpriority(PRIO_PROCESS, 0, 15);

68 69 70 71 72 73
#
# Set our WITH_TB_ADMIN_PRIVS environment variable, so that we have the
# privs we need to clear experiments out of the test database, etc.
#
$ENV{WITH_TB_ADMIN_PRIVS} = 1;

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
$pwd = `/bin/pwd`;
chop $pwd;

# Figure out where the script is
if ($0 =~ m|/|) {
    # Not pwd
    @a = split("/",$0);
    $dir = join("/",@a[0..$#a-1]);
    if ($0 =~ m|^/|) {
	# absolute path
	$basedir = $dir;
    } else {
	# relative path
	chdir $dir;
	$basedir = `/bin/pwd`;
	chop $basedir;
	chdir $pwd;
    }
} else {
    $basedir = `/bin/pwd`;
    chop $basedir;
}

$path = $pwd;
98
$TB = "$path/install";
99

100
$ENV{'PATH'} = "/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/site/bin:$basedir:.";
101 102 103
$basepath = $ENV{'PATH'};

$type = "frontend";
104 105

# Parse argv
106 107 108 109 110 111
sub show_help {
    print STDERR "Syntax: $0 [-leavedb] [-path <path>] [-frontend] [-full] <mode>\n";
    print STDERR "Options:\n";
    print STDERR "  -leavedb - Do not drop the test database on exit.\n";
    print STDERR "  -path <path> - Path to directory to store test files.\n";
    print STDERR "  -frontend - Run in frontend mode.\n";
112
    print STDERR "  -full - Run in full mode.\n";
113
    print STDERR "  -exitonfail - Exit on any failure.\n";
114
    print STDERR "  -session <sessionid> - Specify an alternate session id.\n";
115
    print STDERR "  -flest - Generate flest.log for flest runs.\n";
116
    print STDERR "  -daikon\n";
117
    print STDERR "Mode:\n";
118
    print STDERR "  run <db> <testlist> [<pid> <eid> <num>]\n";
119
    print STDERR "  init <db> [<pid> <eid> <num>]\n";
120 121
    print STDERR "  test <testlist>\n";
    print STDERR "  single <tests>\n";
122
    print STDERR "  finish\n";
123 124 125 126 127
    print STDERR "<num>: Must be even lengthed list of <number> <type>.\n";
    print STDERR "   Ex: \"4 pc 16 shark\"\n";
    print STDERR "Examples:\n";
    print STDERR "   $0 run tbdb frontend\n";
    print STDERR "   $0 -full run tbdb frontend testbed mytest \"10 pc 16 shark\"\n";
128
    print STDERR "   $0 init tbdb;$0 single ping basic;$0 finish\n";
129 130 131 132 133
    exit(1);
};

$mode = "";
$leavedb = 0;
134
$exitonfail = 0;
135
$flestlog = 0;
136
$daikon = 0;
137 138
$user=`whoami`;
chop $user;
139 140

while (($#ARGV > -1) && ($mode eq "")) {
141 142
    $arg = shift;
    if ($arg eq "-path") {
143
	if ($#ARGV == -1) {show_help;}
144
	$path = shift;
145 146 147 148 149 150
    } elsif ($arg eq "-frontend") {
	$type = "frontend";
    } elsif ($arg eq "-full") {
	$type = "full";
    } elsif ($arg eq "-leavedb") {
	$leavedb = 1;
151 152
    } elsif ($arg eq "-exitonfail") {
	$exitonfail = 1;
153 154 155
    } elsif ($arg eq "-session") {
	if ($#ARGV == -1) {show_help;}
	$user = shift;
156 157
    } elsif ($arg eq "-flest") {
	$flestlog = 1;
158 159
    } elsif ($arg eq "-daikon") {
	$daikon = 1;
160
    } else {
161
	$mode = $arg;
162 163 164 165
    }
}

chdir $path;
166 167
$logfile = "test.log";
$statefile = "state";
168

169 170 171
if ($mode eq "run") {
    if ($type eq "frontend") {
	if ($#ARGV != 1) {show_help;}
172
	($TBDB,$testlist) = @ARGV;
173 174
    } else {
	if ($#ARGV != 4) {show_help;}
175
	($TBDB,$testlist,$pid,$eid,$num) = @ARGV;
176 177 178 179 180 181 182
    }
} elsif ($mode eq "init") {
    if ($type eq "frontend") {
	if ($#ARGV != 0) {show_help;}
	($TBDB) = @ARGV;
    } else {
	if ($#ARGV != 3) {show_help;}
183
	($TBDB,$pid,$eid,$num) = @ARGV;
184 185 186
    }
} elsif ($mode eq "test") {
    if ($#ARGV != 0) {show_help;}
187
    ($testlist) = @ARGV;
188
} elsif ($mode eq "single") {
189 190
    if ($#ARGV == -1) {show_help;}
    (@tests) = @ARGV;
191
    $testlist = "N/A";
192
} elsif ($mode eq "finish") {
193
    if ($#ARGV != -1) {show_help;}
194 195 196
} else {
    print STDERR "Unknown mode: $mode\n";
    show_help;
197 198
}

199 200 201 202 203 204 205 206 207 208 209
if (($mode ne "run") && ($mode ne "init")) {
    # Read state
    open(STATE,$statefile) || do {
	print STDERR "Could not find state file ($statefile).\n";
	print STDERR "  Run in init mode first.\n";
	exit(1);
    };
    ($type,$TBDB,$user,$pid,$eid) = split(" ",<STATE>);
    close(STATE);
}

210 211 212 213 214 215 216
if (($type eq "full") && (($mode eq "init") || ($mode eq "run"))) {
    @t = split(" ",$num);
    if (($#t % 2) == 0) {
	print STDERR "<num> must be list of even length.\n";
	show_help;
    }
}
217 218

$testdb = "tbdb_$user";
219
$bossnode = `hostname`;
220 221 222 223 224

# Test counts
$pass = 0;
$fail = 0;
$unknown = 0;
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

# prints sends the string to stdout and the logfile.
sub prints {
    print $_[0];
    print LOG $_[0];
};

# tbexec <cmd> <file>
# Executes <cmd> storing results to <file> and displaying appropriate
# message and exiting on failure.
sub tbexec {
    my ($cmd,$file) = @_;
    open(EXEC,"$cmd 2>&1 |");
    open(FILE,"> $file");
    while (<EXEC>) {
	print FILE $_;
    }
    close(FILE);
    close(EXEC);
    if (($? >> 8) == 0) {
245
	prints "PASS\n";
246 247
	$pass++;
    } else {
248
	prints "FAIL - See $file\n";
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
	$fail++;
	doexit(1);
    }
};

# tbexect <cmd> <logfp>
# This is similar to tbexec except is used for running commands specific
# to tests.  It runs the command appending the output to the file handle
# <logfp> and returns the exit code.
sub tbexect {
    my ($cmd,$logfp) = @_;
    print $logfp "$cmd\n";
    open(EXEC,"$cmd 2>&1 |");
    while (<EXEC>) {
	print $logfp $_;
    }
    close(EXEC);
    return ($? >> 8);
};

# doexit
# This displays exit counts and then exits with the specified exit code.
sub doexit {
    printf LOG "Total Tests: %-5d  Pass: %-5d  Fail: %-5d  Unknown: %-5d\n", $pass+$fail+$unknown, $pass, $fail, $unknown;
    printf "Total Tests: %-5d  Pass: %-5d  Fail: %-5d  Unknown: %-5d\n", $pass+$fail+$unknown, $pass, $fail, $unknown;
274 275
    if ($_[0] != 0) {
	prints "Run in finish mode to clean up any residual state.\n";
276
    }
277
    close(LOG);
278 279 280
    exit($_[0]);
};

281 282 283 284 285 286 287 288 289 290 291
# These are flest routines
sub pass {
    if ($flestlog) {
	print FLESTLOG "PASS $_[0]\n";
    }
};
sub fail {
    if ($flestlog) {
	print FLESTLOG "FAIL $_[0]\n";
    }
};
292 293 294 295 296
sub fail2 {
    if ($flestlog) {
	print FLESTLOG "FAIL: $_[0]\n";
    }
}
297

298 299
sub step_init {
    open(LOG,"> $logfile");
300

301
    prints "START INIT\n";
302
    prints "Session:  $user\n";
303 304
    prints "Logfile:  $logfile\n";
    prints "Database: $TBDB\n";
305
    prints "Type:     $type\n";
306
    prints "\n";
307
    prints "Beginning test run. " . ctime(time);
308

309
    # Initialize DB
310
    $dbh = DBI->connect("DBI:mysql:database=$TBDB;host=localhost") ||
311 312 313 314
	die "Could not connect to DB.\n";
    # Having made sure we can, we now disconnect so as not to harm DB.
    $dbh->disconnect;

315 316 317 318 319 320 321 322 323
    # Write state
    open(STATE,"> $statefile");
    if ($type eq "full") {
	print STATE "$type $TBDB $user $pid $eid\n";
    } else {
	print STATE "$type $TBDB $user\n";
    }
    close(STATE);

324 325 326
    # Reserve nodes
    if ($type eq "full") {
	prints "Reserving nodes.\n";
327

328 329 330 331 332 333 334 335 336 337 338 339 340
	my (@nums) = split(" ",$num);
	my $num;
	my $type;
	my %want;
	my %have;
	my $node;

	while ($#nums > -1) {
	    $num = shift @nums;
	    $type = shift @nums;
	    $want{$type} = $num;
	    $have{$type} = [];
	}
341

342
	open(AVAIL,"/usr/testbed/sbin/avail includevirt rand |");
343
	while (<AVAIL>) {
344 345 346 347 348 349 350 351 352 353 354 355 356
	    chomp;
	    if (! /^\|/) { next; }
	    if (/^\|node_id\s*\|/) { next; }
	    if ( ($node,$type,$class) =
		 /^\|([-a-z0-9]+)\s*\|([-a-z0-9]+)\s*\|(\w+)\s*\|$/i ) {
		if (defined($want{$type}) && $want{$type} > 0) {
		    push(@{$have{$type}},$node);
		    $want{$type}--;
		} elsif (defined($want{$class}) && $want{$class} > 0) {
		    push(@{$have{$class}},$node);
		    $want{$class}--;
		}
	    } else { print "Couldn't parse avail line '$_'\n"; }
357 358 359 360 361 362 363 364 365 366 367 368 369 370
	}
	close(AVAIL);

	my $good = 1;
	foreach $type (keys(%want)) {
	    if ($want{$type} > 0) {
		print STDERR "Could not get enough $type - Need $want{$type} more.\n";
		$good = 0;
	    }
	}
	if (! $good) {exit(1);}

	my $raw = "";
	foreach $type (keys(%have)) {
371
	    $raw .= " " . join(" ",@{$have{$type}});
372 373 374
	}
	prints "  Reserving ";
	tbexec("/usr/testbed/bin/nalloc $pid $eid $raw","$path/reserve.log");
375 376 377
	# Have to run exports_setup, or the nodes won't come up properly
	prints "  Setting up exports ";
	tbexec("/usr/testbed/sbin/exports_setup","$path/exports_setup.log");
378 379

	prints "  Setting up tmcc redirect ";
380
	$dbh = DBI->connect("DBI:mysql:database=$TBDB;host=localhost") ||
381
	    die "Could not connect to DB.\n";
382 383 384 385 386 387 388
	$dbh->do("UPDATE experiments set testdb=\"$testdb\" where" .
		 " pid=\"$pid\" and eid=\"$eid\"") || do {
		     prints "FAIL\n";
		     $fail++;
		     doexit(1);
		 };
	$pass++;
389 390
	prints "PASS\n";
	$dbh->disconnect;
391 392
    }

393 394 395 396
    # Set up object tree
    prints "Setting up object tree.\n";
    if (! -d $path) {
	mkdir $path,0755;
397
    }
398 399 400 401
    mkdir "tbobj",0755;
    mkdir "install",0755;
    chdir "tbobj";
    system("cp -f $basedir/test-defs $path/defs");
402 403 404 405
    if (($? >> 8) != 0) {
	print STDERR "ERROR: Could not copy defs file.\n";
	doexit(1);
    }
406
    open(DEFS,">>$path/defs");
407 408 409
    print DEFS "TBDBNAME=$testdb\n";
    print DEFS "TBOPSEMAIL=$user\@emulab.net\n";
    print DEFS "TBLOGSEMAIL=$user\@emulab.net\n";
410 411
    print DEFS "TBSTATEDEMAIL=$user\@emulab.net\n";
    print DEFS "OURDOMAIN=test.$user.emulab.net\n";
412
    print DEFS "BOSSNODE=$bossnode\n";
413
    close(DEFS);
414

415
    prints "  Configuring ";
416 417 418 419
    my $testmode;
    if ($type eq "full") {
	$testmode = "";
    } else {
420
	$testmode = "--enable-testmode --disable-events";
421
    }
422 423
    open(CONF,">$path/config");
    print CONF "$basedir/../configure \\\n".
424
      "--prefix=$TB \\\n$testmode \\\n--with-TBDEFS=$path/defs\n";
425
    close(CONF);
426
    tbexec("$basedir/../configure --prefix=$TB $testmode ".
427
	   "--with-TBDEFS=$path/defs",
428
	   "$path/configure.log");
429

430 431
    prints "  Building ";
    tbexec("gmake","$path/build.log");
432

433 434
    prints "  Installing ";
    tbexec("gmake boss-install","$path/install.log");
435

436
    prints "  Post-Installing ";
437
    tbexec("sudo gmake post-install","$path/postinstall.log");
438

439
    chdir $path;
440

441 442 443
    # Setup test database
    prints "Setting up test database.\n";
    prints "  Dumping Current ";
444
    tbexec("mysqldump -e -a $TBDB","$path/dbdump.txt");
445
    # Now add a line to the dumped database that sets flest as an admin
446 447
    prints "  Setting up admin account for $user in test database ";
    tbexec("echo \"update users set admin=1 where uid='$user';\"".
448
	   " >> $path/dbdump.txt", "dbdump.log");
449

450
    prints "  Creating DB ";
451
    system("echo \"drop database if exists $testdb\" | mysql");
452
    tbexec("echo \"create database $testdb\" | mysql","createdb.log");
453

454
    prints "  Filling DB ";
455
    tbexec("cat $path/dbdump.txt | mysql -q -s $testdb","fill.log");
456 457


458 459
    $dbh = DBI->connect("DBI:mysql:database=$testdb;host=localhost") || do {
	prints "ERROR: Could not connect to new DB.\n";
460
	doexit(1);
461
    };
462
    $ENV{'PATH'} = "$basepath:$TB/bin:$TB/sbin";
463 464 465

    #
    # Clear our extraneous stuff.
466
    #
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    $sth = $dbh->prepare("SELECT * from testsuite_preentables");
    $sth->execute;
    while (($table,$action) = $sth->fetchrow_array) {
	$_ = $action;
        SWITCH: {
	    /^drop$/ && do {
		prints "    Dropping table $table\n";
		$dbh->do("drop table $table");
		last SWITCH;
	    };
	    /^clean$/ && do {
		prints "    Cleaning table $table\n";
		$dbh->do("delete from $table");
		last SWITCH;
	    };
	    /^prune$/ && do {
		prints "    Pruning table $table\n";
		$dbh->do("delete from $table ".
			 "where pid!='emulab-ops' and pid!='testbed'");
		last SWITCH;
	    };
	}
    }
490 491

    if ($type eq "frontend") {
492
	# Clear out experiments and set up test experiment.
493 494
	prints "  Clearing out existing experiments ";
	open(CLEARLOG,"> clear.log");
495
	$sth = $dbh->prepare("SELECT pid,eid from experiments ".
496 497
			     "where !(pid='emulab-ops' and eid='hwdown') " .
			     "and state != 'swapped'");
498 499 500
	$sth->execute;
	while (($pid,$eid) = $sth->fetchrow_array) {
	    print CLEARLOG "$pid $eid\n";
501 502
	    print CLEARLOG "tbswap out -force $pid $eid\n";
	    open(EXEC,"tbswap out -force $pid $eid 2>&1 |");
503 504 505 506 507 508 509 510
	    while (<EXEC>) {
		print CLEARLOG $_;
	    }
	    close(EXEC);
	    if (($? >> 8) != 0) {
		prints "FAIL - For $pid $eid - On tbswapout - See clear.log\n";
		$fail++;
		doexit(1);
511
	    }
512 513
	    print CLEARLOG "tbend -force $pid $eid\n";
	    open(EXEC,"tbend -force $pid $eid 2>&1 |");
514 515 516 517 518 519 520 521
	    while (<EXEC>) {
		print CLEARLOG $_;
	    }
	    close(EXEC);
	    if (($? >> 8) != 0) {
		prints " FAIL - For $pid $eid - On tbend - See clear.log\n";
		$fail++;
		doexit(1);
522 523 524
	    }
	    #
	    # tbend does not clear the experiments or nsfiles table.
525
	    #
526 527 528 529
	    $dbh->do("delete from experiments ".
		     "where pid='$pid' and eid='$eid'");
	    $dbh->do("delete from nsfiles ".
		     "where pid='$pid' and eid='$eid'");
530
	}
531
	# Clear reserved just to make sure.
532 533
	$dbh->do("delete from reserved ".
		 "where pid='emulab-ops' and eid!='hwdown'") || do {
534 535 536 537 538
	    prints "FAIL\n";
	    $fail++;
	    doexit(1);
	};

539 540 541
	# Clear all the users except the person running the test.
	$dbh->do("delete from users where uid!='$user'");

542 543 544 545 546 547 548
	prints "PASS\n";
	$pass++;

	close(CLEARLOG);
    } else {
	# Free nodes we allocated for the experiment and allocate all
	# others.
549
	prints "  Creating testbed/unavailable in test DB ";
550
	open(AVAIL,"avail includevirt |");
551 552 553 554 555 556
	$raw = "";
	while (<AVAIL>) {
	    if (! /^\|/) {next};
	    if (/node_id/) {next;}
	    ($node) = /^\|([-a-zA-Z0-9]+)/;
	    $raw .= " $node";
557
	}
558
	close(AVAIL);
559

560 561 562 563 564 565 566 567 568
	$dbh->do("DELETE from experiments where pid=\"testbed\" and eid=\"unavailable\"");
	$dbh->do("INSERT into experiments (pid,eid) "
		 . "values (\"testbed\",\"unavailable\")") || do {
		     prints "FAIL\n";
		     $fail++;
		     doexit(1);
		 };
	print "PASS\n";
	$pass++;
569

570 571
	prints "  Marking nodes as unavailable ";
	tbexec("nalloc testbed unavailable $raw","unavailable.log");
572

573 574 575
	prints "  Freeing test nodes ";
	tbexec("nfree $pid $eid","free.log");
    }
576

577 578
    prints "  Setting up test experiment ";
    $dbh->do("DELETE from experiments where pid=\"testbed\" and eid=\"test\"");
579 580
    $dbh->do("INSERT into experiments (pid,gid,eid,testdb) "
	     . "values (\"testbed\",\"testbed\",\"test\",\"$testdb\")") || do {
581 582 583 584 585 586
		 prints "FAIL\n";
		 $fail++;
		 doexit(1);
	     };
    print "PASS\n";
    $pass++;
587

588
    prints "  Snapshotting clean state ";
589
    tbexec("mysqldump -e -a $testdb","clean.txt");
590
    prints "END INIT\n";
591 592
}

593
sub test_pass {
594
    prints "START TEST\n";
595

596 597 598
    if ($flestlog) {
	open(FLESTLOG,"> flest.log");
    }
599

600 601
    pass("Pre-Test Setup");

602 603
    # Time to run tests!
    prints "Running Tests\n";
604
    prints "Testlist: $testlist\n";
605

606
    # Let's add some stuff to our path
607
    $ENV{'PATH'} = "$basepath:$TB/bin:$TB/sbin";
608

609 610 611 612 613
    my @tests;
    my $testmode;
    my $dir;

    if ($#_ == -1) {
614 615 616 617 618 619 620
	open(TESTLIST,"$basedir/testlists/$testlist");
	@tests = ();
	while (<TESTLIST>) {
	    chop;
	    push(@tests,$_);
	}
	close(TESTLIST);
621 622
    } else {
	@tests = @_;
623 624
    }

625 626 627
    mkdir "tests",0755;
    foreach $test (@tests) {
	if (($test eq ".") || ($test eq "..") || ($test eq "CVS")) {
628 629
	    next;
	}
630
	prints "  $test ";
631

632 633
	mkdir "tests/$test",0755;
	chdir "tests/$test";
634
	open(TESTLOG,"> test.log");
635

636 637 638 639
	# Set up DB state
	if (tbexect("echo \"drop database if exists $testdb\" | mysql",TESTLOG)) {
	    prints "FAIL - Could not drop test DB.\n";
	    prints "Failure was FATAL - Exiting.\n";
640
	    fail2("$test - Drop test DB");
641 642 643 644 645 646
	    $fail++;
	    doexit(1);
	}
	if (tbexect("echo \"create database $testdb\" | mysql",TESTLOG)) {
	    prints "FAIL - Could not recreate test DB.\n";
	    prints "Failure was FATAL - Exiting.\n";
647
	    fail2("$test - Recreate test DB");
648
	    $fail++;
649 650
	    doexit(1);
	}
651
	if (tbexect("cat $path/clean.txt | mysql -q -s $testdb",TESTLOG)) {
652 653
	    prints "FAIL - Could not populate test DB.\n";
	    prints "Failure was FATAL - Exiting.\n";
654
	    fail2("$test - Populate test DB");
655 656 657
	    $fail++;
	    doexit(1);
	}
658

659 660
	if (-f "$basedir/tests/$test/dbstate") {
	    if (tbexect("cat $basedir/tests/$test/dbstate | mysql $testdb",TESTLOG)) {
661
		prints "FAIL - Could not evaluate dbstate.\n";
662
		fail2("$test - Evaluate dbstate");
663
		$fail++;
664
		if ($exitonfail) {doexit(1);}
665
		next;
666
	    }
667
	}
668 669 670

	# Start stated
	if ($type eq "full") {
671
	  # Returns non-zero for failures
672 673
	  system("$TB/sbin/stated") and
	    die "Error running '$TB/sbin/stated': $!\n";
674
	}
675

676
	# We're now ready to run the test.
677

678
	# The last line of this command will be either PASS
679
	# or FAIL <msg>, all output before that should be sent
680
	# to the log file.
681
	open(TESTFP,"tbstub $testdb testbed test $test $basedir/tests/$test |");
682 683 684 685 686 687 688 689
	while (<TESTFP>) {
	    print TESTLOG $_;
	    $status = $_;
	}
	chop $status;
	close(TESTFP);
	if (($? >> 8) != 0) {
	    prints "UNKNOWN - Test exited with non-0 code.\n";
690
	    $unknown++;
691 692 693 694 695 696
	} else {
	    if ($status =~ /^PASS/) {
		$pass++;
	    } elsif ($status =~ /^FAIL/) {
		$fail++;
		# grab copy of testbed
697
		system("mysqldump -e -a $testdb > db.txt");
698 699 700 701
		if (($? >> 8) != 0) {
		    prints "ERROR: Could not grab copy of database.\n";
		    doexit(1);
		}
702
		if ($exitonfail) {prints "FAIL\n"; doexit(1);}
703 704
	    } else {
		$unknown++;
705
		$status = "UKNOWN";
706 707
	    }
	    prints "$status\n";
708 709 710 711
	    if ($flestlog) {
		@t = split(" ",$status);
		$result = $t[0];
		$msg = join(" ",@t[1..$#t]);
712
		print FLESTLOG "$result: $test $msg\n";
713
	    }
714
	}
715

716
	if ($type eq "full") {
717
	    my $statedpidfile = "$TB/locks/stated.pid";
718 719
	    my $statedpid = `cat $statedpidfile` ||
	      prints "WARNING: Unable to find stated pid: $!\n";
720 721 722
	    if (! kill(15, $statedpid) ) {
		prints "WARNING: Unable to kill stated process ($statedpid)\n";
	    }
723
	}
724

725
	close(TESTLOG);
726

727
	chdir $path;
728
    }
729 730 731
    if ($flestlog) {
	close FLESTLOG;
    }
732
    prints "END TEST\n";
733
}
734

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
sub step_test {
    open(LOG,">> $logfile");

    if (!$daikon) {

	test_pass;

    } else {

	prints "DAIKONIZING: STAGE 1 ... ";
	system("$TB/bin/daikonize clean > daikon-clean.log 2>&1");
	if (($? >> 8) != 0 && ($? >> 8) != 1) {
	    prints "FAIL - see \"daikon-clean.log\" for why.\n";
	    prints "EXITING.\n";
	    return;
	}
	system("$TB/bin/daikonize stage1 > daikon-stage1.log 2>&1");
	if (($? >> 8) != 0 && ($? >> 8) != 1) {
	    prints "FAIL - see \"daikon-stage1.log\" for why.\n";
	    prints "EXITING.\n";
	    return;
	} else {
	    prints "PASS\n";
	}

	test_pass;

	prints "DAIKONIZING: STAGE 2 ... ";
	system("$TB/bin/daikonize stage2 > daikon-stage2.log 2>&1");
	if (($? >> 8) != 0 && ($? >> 8) != 1) {
	    prints "FAIL - see \"daikon-stage2.log\" for why.\n";
	    prints "EXITING.\n";
	    return;
	} else {
	    prints "PASS\n";
	}

	test_pass;

	prints "DAIKONIZING: ANALYZE ... ";
	system("$TB/bin/daikonize analyze > daikon-analyze.log 2>&1");
	if (($? >> 8) != 0 && ($? >> 8) != 1) {
	    prints "FAIL - see \"daikon-analyze.log\" for why.\n";
	    prints "EXITING.\n";
	    return;
	} else {
	    prints "PASS\n";
	}
    }
}

786 787
sub step_cleanup {
    open(LOG,">> $logfile");
788

789
    prints "START cleanup\n";
790

791 792 793 794 795 796 797 798 799 800 801 802
    # Try to do the necessary cleanup
    if ($leavedb == 0) {
	prints "  Dropping test DB ";
	system("echo \"drop database if exists $testdb\" | mysql");
	if (($? >> 8) != 0) {
	    prints "FAIL\n";
	    $fail++;
	} else {
	    prints "PASS\n";
	    $pass++;
	}
    }
803
    if (($type eq "full")) {
804 805 806 807 808 809 810 811 812 813
	prints "  Clearing redirects ";
	$ok = 1;
	$dbh = DBI->connect("DBI:mysql:database=$TBDB;host=localhost") || do {
	    $ok = 0;
	};
	if (! $ok) {
	    prints "FAIL - Could not connect to db\n";
	    $fail++;
	} else {
	    $ok = 1;
814 815 816 817
	    $dbh->do("UPDATE experiments set testdb=NULL" .
		     " where pid=\"$pid\" and eid=\"$eid\"") || do {
			 $ok = 0;
		     };
818
	    if ($ok) {
819
		$pass++;
820
		prints "PASS\n";
821 822 823
	    } else {
		$fail++;
		prints "FAIL\n";
824 825 826
	    }
	    $dbh->disconnect;
	}
827

828 829 830 831 832 833
	prints "  Freeing nodes ";
	system("/usr/testbed/bin/nfree $pid $eid > /dev/null");
	if (($? >> 8) != 0) {
	    prints "FAIL\n";
	    $fail++;
	} else {
834
	    prints("PASS\n");
835 836
	    $pass++;
	}
837
    }
838 839 840 841 842 843 844 845 846
    prints "  Clearing state ";
    system("rm -f $statefile");
    if (($? >> 8) != 0) {
	prints "FAIL\n";
	$fail++;
    } else {
	prints "PASS\n";
	$pass++;
    }
847
    prints "End test run. " . ctime(time);
848 849
    prints "END CLEANUP\n";
}
850

851

852 853 854 855 856 857 858 859 860 861 862 863 864
if ($mode eq "run") {
    step_init;
    step_test;
    step_cleanup;
} elsif ($mode eq "init") {
    step_init;
} elsif ($mode eq "test") {
    step_test;
} elsif ($mode eq "single") {
    step_test @tests;
} elsif ($mode eq "finish") {
    step_cleanup;
}
865

866 867
open(LOG,">> $logfile");

868 869
# Do exit does all cleanup.
doexit(0);