ImageTest.pm 13.4 KB
Newer Older
Kevin Atkinson's avatar
Kevin Atkinson committed
1 2
#
# Copyright (c) 2005-2006 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 
# {{{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/>.
# 
# }}}
Kevin Atkinson's avatar
Kevin Atkinson committed
22 23
#

24 25 26 27
package ImageTest;
use Exporter;

@ISA = "Exporter";
28
@EXPORT = qw (test test_cmd test_ssh test_rcmd test_experiment exit_str
29
	      ERR_MASK ERR_NONE ERR_EXPECTED ERR_FAILED ERR_FATAL ERR_INT
30 31
	      STATUS_MASK STATUS_NONE STATUS_SWAPPEDIN STATUS_EXISTS STATUS_CLEANUP);

32 33 34 35 36 37
use IO::File;
use strict;

use vars qw(%parms %dependencies %tally);
use vars qw($eid $pid $datadir $resultsdir);
use vars qw(@mapping @nodes @pnodes %to_physical %from_physical);
38
use vars qw($EXPECTED $FAILED);
39 40 41 42 43 44 45 46
use vars qw(%ERR %STATUS);

sub true() {1}
sub false() {0}

#
# exit values, or two parts together
#
47

48 49 50 51 52 53
sub ERR_MASK     {7};
sub ERR_NONE     {0};
sub ERR_EXPECTED {1}; # expected failures
sub ERR_FAILED   {2}; # tests failed
sub ERR_FATAL    {3}; # fatal error
sub ERR_INT      {4}; # interrupted
54
%ERR = (ERR_NONE, 'ERR_NONE',
55
	ERR_EXPECTED, 'ERR_EXPECTED',
56 57 58 59
	ERR_FAILED, 'ERR_FAILED',
	ERR_FATAL, 'ERR_FATAL',
	ERR_INT, 'ERR_INT');

60 61 62 63 64
sub STATUS_MASK      {3 << 3};
sub STATUS_NONE      {0 << 3};
sub STATUS_SWAPPEDIN {1 << 3}; # experment still swapped in
sub STATUS_EXISTS    {2 << 3}; # experment still exists
sub STATUS_CLEANUP   {3 << 3}; # requires cleanup
65 66 67 68 69 70 71 72 73
%STATUS = (STATUS_NONE, 'STATUS_NONE',
	   STATUS_SWAPPEDIN, 'STATUS_SWAPPEDIN',
	   STATUS_EXISTS, 'STATUS_EXISTS',
	   STATUS_CLEANUP, 'STATUS_CLEANUP');

sub exit_str($) {
  my ($exit) = @_;
  return join(' ', $ERR{$exit &  ERR_MASK}, $STATUS{$exit & STATUS_MASK});
}
74

75 76 77 78 79 80 81 82 83 84 85 86
#
#
#

sub oneof ($$) {
  my ($what, $which) = @_;
  foreach (@{$parms{$which}}) {
    return true if ($_ eq $what);
  }
  return false;
}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#
# Performs a test on a swapped in experient.  Returns true if the test
# passed.
#
# Each test can depend and on any number of other tests.  The test
# will be skipped unless all the dependencies are satisfied.
#
# Examples:
#   test 'test 1', [], sub {...};
#   test 'test 2', ['test 1'], sub {...};
#   test 'login prompt', [], sub {
#     local $_ = cat "/var/log/tiplogs/$pnode.run";
#     /login\: /;
#   }
#
sub test ($$&) {
  my ($name,$requires,$test) = @_;
104 105 106 107 108

  if (oneof($name, 'skip')) {
    return 0;
  }

109 110 111 112 113 114 115 116 117 118
  $tally{total}++;

  my $deps_sat = 1;
  foreach (@$requires) {$deps_sat = 0 unless $dependencies{$_}}

  unless ($deps_sat) {
    print "Skipping test \"$name\" due to unsatisfied dependies.\n";
    return 0;
  }

119
  print "<=== Starting Test: $name\n";
120

121 122 123 124 125 126
  my $res;
  eval {$res = &$test(%parms)};

  if ($res) {
    $tally{passed}++;
    $dependencies{$name} = 1;
127
    print ">=== \"$name\" succeeded\n";
128
    return true;
129
  } elsif ($@) {
130 131 132 133 134 135 136 137 138
    if (oneof($name, 'ignore')) {
      $tally{expected}++;
      print $EXPECTED "$name\n";
      print ">=== \"$name\" died: $@ -- ignored";
    } else {
      $tally{failed}++;
      print $FAILED "$name\n";
      print ">*** \"$name\" died: $@";
    }
139
    return false;
140
  } else {
141 142 143 144 145 146 147 148 149
    if (oneof($name, 'ignore')) {
      $tally{expected}++;
      print $EXPECTED "$name\n";
      print ">=== \"$name\" failed -- ignored\n";
    } else {
      $tally{failed}++;
      print $FAILED "$name\n";
      print ">*** \"$name\" failed\n";
    }
150
    return false;
151 152 153
  }
}

154 155 156 157
#
#
#
sub sys (@) {
158
  print "<- Executing: ", join(' ', @_), "\n";
159
  system @_;
160
  print ">- Done\n";
161 162 163
  return $? >> 8 == 0;
}

164 165 166 167 168 169 170 171 172 173
#
# Performs a test by executing a command and optionally checking the output
#
# Example :
#   test_cmd "ssh $node", [], "ssh $node.$eid.$pid true";
#
sub test_cmd ($$$;&) {
  my ($name,$requires,$cmd,$output_test) = @_;
  test $name, $requires, sub {
    if (not defined $output_test) {
174
      return sys($cmd);
175 176 177
    } else {
      local $/ = undef;
      my $F = new IO::File;
178
      print "<- Executing: $cmd\n";
179
      open $F, "$cmd |" or return false;
180 181
      local $_ = <$F>;
      close $F;
182
      print ">- Done\n";
183 184 185 186 187
      return 0 unless ($? >> 8 == 0);
      open $F, ">$resultsdir/$name.out";
      print $F $_;
      close $F;
      my $res = &$output_test;
188
      print "*** Output of \"$cmd\" did not match expected output\n" unless $res;
189 190 191 192 193 194 195 196 197 198 199 200 201
      return $res;
    }
  };
}

#
# Test that ssh is working on a node
#
# Example:
#   test_ssh 'node';
#
sub test_ssh ($) {
  my ($node) = @_;
202
  test_cmd "ssh-$node", [], "ssh-node $node true";
203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

#
# Performs a test by executing a remote command and optionally
# checking the output.  test_ssh must be executed on the node before
# any remote commands for the node.
#
# Examples:
#   test_rcmd 'sudo', [], 'node', 'sudo touch /afile.txt';
#   test_rcmd 'hostname' , [], 'node', 'hostname', sub {/^node\.$eid\.$pid/};
#
sub test_rcmd ($$$$;&) {
  my ($name,$requires,$node,$cmd,$output_test) = @_;
  &test_cmd($name, ["ssh-$node", @$requires],
217
	    "ssh-node $node $cmd", $output_test);
218 219
}

220 221 222 223 224
#
#
#
sub test_traceroute ($$@) {
  my ($src,$dest,@path) = @_;
225
  test_rcmd "traceroute-${src}_$dest", [], $src, "traceroute $dest",
226
    sub {
227
      local @_ = grep {!/^traceroute/} split /\n/;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
      if (@_+0 != @path+0) {
	printf "*** traceroute $src->$dest: expected %d hops but got %d.\n",
	  @path+0, @_+0;
	return 0;
      }
      for (my $i = 0; $i < @_; $i++) {
	local $_ = $_[$i];
	my ($n) = /^\s*\d+\s*(\S+)/;
	next if $n eq $path[$i];
	printf "*** traceroute $src->$dest: expected %s for hop %d but got %s\n",
	  $path[0], $i+1, $n;
	return 0;
      }
      return 1;
    };
}


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
#
# Scans a log file for any errors or serious warnings.  The log file
# may also be a pipe as the string is passed directly to open.
#
# Example:
#   test_scanlog 'error free log', [], 'log';
#
sub test_scanlog ($$$) {
  my ($name,$requires,$log) = @_;
  test $name, $requires, sub {
    my $F = new IO::File;
    open $F, $log or die "Unable to open \"$log\" for reading.";
    my $errors = 0;
    while (<$F>) {
      next unless /^\*\*\* /;
      print;
      $errors = 1;
    }
    close $F;
    return $errors == 0;
  };
}

#
#
#
sub cat ($) {
  open F, $_[0];
  local $/ = undef;
  local $_ = <F>;
  close F;
  return $_;
};

#
#
#
sub single_node_tests ($) {

  my ($node) = @_;
  my ($pnode) = $to_physical{$node};

  test_ssh $node;

290 291
  return if $parms{skip_std_tests};

292 293 294 295 296 297 298 299 300 301 302
  test_rcmd "sudo-$node", [], $node, 'sudo touch /afile.txt';

  test_rcmd "hostname-$node" , [], $node, 'hostname', sub {
    /^$node\.$eid\.$pid/i || /^$pnode/i;
  };

  test "login_prompt-$node", [], sub {
    local $_ = cat "/var/log/tiplogs/$pnode.run";
    /login\: /;
  };

303 304 305 306 307 308
  test "proj_mount-$node", ["ssh-$node"], sub {
    sys "ssh-node $node touch $resultsdir/working/$node"
      or return false;
    return -e "$resultsdir/working/$node";
  }

309 310 311 312 313 314 315 316 317
}

#
#
#
sub multi_node_tests () {

  #test_cmd 'linktest', [], "run_linktest.pl -v -e $pid/$eid";

318 319
  return if $parms{skip_std_tests};

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
  sleep 10;
  test_cmd 'linktest1', [], "run_linktest.pl -v -L 1 -l 1 -e $pid/$eid";
  sleep 2;
  test_cmd 'linktest2', [], "run_linktest.pl -v -L 2 -l 2 -e $pid/$eid";
  sleep 2;
  test_cmd 'linktest3', [], "run_linktest.pl -v -L 3 -l 3 -e $pid/$eid";
  sleep 2;
  test_cmd 'linktest4', [], "run_linktest.pl -v -L 4 -l 4 -e $pid/$eid";

}

#
# Creats and swaps in a test experment in and performs tests on the
# images after it is done.  When all the tests are done swap the experment out,
# copy the experment data dir to EXP-YYYYMMDDHHMM and terminate the experiment.
#
# Will fork a child to do the actual work and return the pid of the child
#
# Expects a hash of parmaters as input for example:
#   test_experiment
#     pid => 'tbres',
#     eid => 'it-single',
#     os => 'RHL73-STD',
#     hardware => 'pc850',
#     datadir => '...',
#     resultsdir => '...',
#
# The .ns file for the experiment is expected to be named "<datadir>/nsfile.ns".
# The file will be scanned and any instances of @PARM@ will be substituted
# for the value of PARM.  Any image specific tests should be located in
# "<datadir>/tests.ns".
#
sub test_experiment (%) {

  {
    my $pid = fork();
    die unless defined $pid;

    return $pid if $pid != 0; # child
  }

  %parms = @_;
  %dependencies = ();
363
  %tally = (total => 0, passed => 0, expected => 0, failed => 0);
364 365 366 367 368

  $eid = $parms{eid};
  $pid = $parms{pid};
  $datadir = $parms{datadir};
  $resultsdir = $parms{resultsdir};
369 370 371

  my $err = ERR_NONE;
  my $status = STATUS_NONE;
372 373 374

  $SIG{__DIE__} = sub {
    return unless defined $^S && !$^S;
375
    $! = (ERR_FATAL | $status);
376 377 378
    die $_[0];
  };

379 380 381
  $SIG{INT} = 'IGNORE';

  $SIG{TERM} = sub {
382 383
    print "TERMINATING\n";
    exit (ERR_INT | $status);
384 385
  };

386
  mkdir $resultsdir, 0777;
387 388 389 390
  chdir $resultsdir;

  mkdir "working", 0777;
  mkdir "bin",     0777;
391

392 393 394
  #sleep 5;
  #exit 0;

395
  $ENV{PATH} = "$resultsdir/bin:$ENV{PATH}";
396

397 398
  open STDOUT, ">log"      or die;
  open STDERR, ">&STDOUT"  or die;
399 400 401

  my ($F,$O);

402 403 404 405 406
  $F = new IO::File ">pid" or die;
  print $F "$$\n";
  close $F;

  $FAILED = new IO::File ">failed-tests" or die;
407
  $EXPECTED = new IO::File ">failed-but-ignored" or die;
408 409

  $F = new IO::File ">parms" or die;
410
  foreach (sort keys %parms) {
411 412 413 414 415
    unless (ref $parms{$_}) {
      print $F "$_: $parms{$_}\n";
    } else {
      print $F "$_: @{$parms{$_}}\n";
    }
416
  }
417 418 419 420 421 422 423 424 425 426 427 428 429 430
  close $F;

  $F = new IO::File ">bin/ssh-node" or die;
  print $F "#!/bin/sh\n";
  print $F "\n";
  print $F 'cmd=$1',"\n";
  print $F 'shift', "\n";
  print $F join(' ',
		'ssh', "-x",
		"-o BatchMode=yes", "-o StrictHostKeyChecking=no",
		"-o UserKnownHostsFile=$resultsdir/working/known_hosts",
		"\$cmd.$parms{eid}.$parms{pid}", '"$@"', "\n");
  close $F;
  chmod 0755, "bin/ssh-node";
431 432 433 434

  if ($parms{stages} =~ /c/) {

    $F = new IO::File "$datadir/nsfile.ns" or die;
435
    $O = new IO::File ">nsfile.ns" or die;
436 437 438 439 440 441 442
    while (<$F>) {
      s/\@([^@]+)\@/$parms{lc $1}/g;
      print $O $_;
    }
    close $O;
    close $F;

443 444 445 446
    $status = STATUS_EXISTS;
    sys("/usr/testbed/bin/startexp -w -i -f".
	" -E \"Experiment For Testing Images\"".
	" -p $pid -e $eid nsfile.ns");
447 448
    if ($? >> 8 != 0) {
      print "*** Could not create experment\n";
449
      exit (ERR_FATAL | STATUS_NONE);
450 451 452 453 454
    }
  }

  my $swapin_success = 1;
  if ($parms{stages} =~ /s/) {
455
    $status = STATUS_SWAPPEDIN;
456 457
    $swapin_success = test_cmd 'swapin', [],
      "/usr/testbed/bin/swapexp -w -e $pid,$eid in";
458
    $status = STATUS_EXISTS unless $swapin_success;
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
  }

  if ($swapin_success) {

    if ($parms{stages} =~ /s/) {

      # FIXME: need proper way to get the log file
      test_scanlog 'error_free_swapin', [],
	`ls -t /proj/$pid/exp/$eid/tbdata/swapexp.* | head -1`;
    }

    local (@mapping, @nodes, @pnodes, %to_physical, %from_physical);

    open F, "/usr/testbed/bin/expinfo -m $pid $eid | ";

    while (<F>) {
      last if /^---/;
    }

    while (<F>) {
      next unless /\w/;
      local @_ = split /\s+/;
      push @mapping, [@_];
      push @nodes, $_[0];
      push @pnodes, $_[3];
      $to_physical{$_[0]} = $_[3];
      $from_physical{$_[3]} = $_[0];
    }

    if ($parms{stages} =~ /t/) {

      foreach my $node (@nodes) {
	single_node_tests($node);
      }

      if (@nodes > 1) {
	multi_node_tests();
      }

      if (-e "$datadir/tests.pl") {
	
	do "$datadir/tests.pl";
	
	if ($@) {
	  print "*** Unable to complete tests: $@";
	  print "*** Results may not be accurate.\n";
505
	  $err = ERR_FATAL
506 507 508 509
	}
      }
    }

510 511 512
    if ($parms{stages} =~ /[oe]/ && 
	(!$parms{dont_swapout_unexpected} || $tally{failed} == 0)) {
      
513 514 515 516
      test_cmd 'loghole', [], "loghole -e $pid/$eid sync";
	
      foreach my $node (@nodes) {
	my $pnode = $to_physical{$node};
517 518
	sys "cp -pr /var/log/tiplogs/$pnode.run tiplog-$node"
	  or print "*** WARNING: Unable to copy tiplog for node $node.\n";
519 520 521
      }

      test_cmd 'swapout', [],
522 523
	"/usr/testbed/bin/swapexp -w -e $pid,$eid out"
	  and $status = STATUS_EXISTS;
524 525 526 527 528 529 530 531

      # FIXME: need proper way to get the log file
      test_scanlog 'error_free_swapout', ['swapout'],
	`ls -t /proj/$pid/exp/$eid/tbdata/swapexp.* | head -1`;
    }

  } else {

532
    $err = ERR_FATAL;
533 534 535

  }

536 537
  $err = ERR_FAILED   if $err == ERR_NONE && $tally{failed} > 0;
  $err = ERR_EXPECTED if $err == ERR_NONE && $tally{expected} > 0;
538

539 540
  if ($parms{stages} =~ /e/ &&
      (!$parms{dont_swapout_unexpected} || $tally{failed} == 0)) {
541

542
    sys("cp -pr /proj/$pid/exp/$eid exp-data");
543 544
    if ($? >> 8 != 0) {
      print "*** Unable to copy exp data.  Not terminating exp\n";
545
      exit ($err | STATUS_CLEANUP);
546 547
    }

548
    sys("/usr/testbed/bin/endexp -w -e $pid,$eid");
549 550
    if ($? >> 8 != 0) {
      print "*** Could not terminate experiment.  Must do manually\n";
551
      exit ($err | STATUS_CLEANUP);
552
    }
553 554 555 556 557 558 559

    $status = STATUS_NONE;
  }

  if ($parms{stages} =~ /t/) {

    print "\n";
560 561 562 563 564 565
    print "Num Tests:           $tally{total}\n";
    print "Passed:              $tally{passed}\n";
    print "Expected Failures:   $tally{expected}\n";
    print "Unexpected Failures: $tally{failed}\n";
    my $unex = $tally{total} - $tally{passed} - $tally{expected} - $tally{failed};
    print "Unable to Execute:   $unex\n";
566

567 568
  }

569
  exit ($err | $status);
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
}