start.pl revision f91db44ab9a6f7261e313f16b0beb6f9b315efa9
#
# Copyright (C) 2004-2008, 2010, 2011 Internet Systems Consortium, Inc. ("ISC")
# Copyright (C) 2001 Internet Software Consortium.
#
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
# $Id: start.pl,v 1.29 2012/02/06 23:20:38 marka Exp $
# Framework for starting test servers.
# Based on the type of server specified, check for port availability, remove
# temporary files, start the server, and verify that the server is running.
# If a server is specified, start it. Otherwise, start all servers for test.
use strict;
use Cwd;
use Cwd 'abs_path';
# Option handling
# --noclean test [server [options]]
#
# --noclean - Do not cleanup files in server directory
# test - name of the test directory
# server - name of the server directory
# options - alternate options for the server
# NOTE: options must be specified with '-- "<option list>"',
# ALSO NOTE: this variable will be filled with the
# contents of the first non-commented/non-blank line of args
# in a file called "named.args" in an ns*/ subdirectory only
# the FIRST non-commented/non-blank line is used (everything
# else in the file is ignored. If "options" is already set,
# then "named.args" is ignored.
my $usage = "usage: $0 [--noclean] [--restart] test-directory [server-directory [server-options]]";
my $noclean = '';
my $restart = '';
if (!$test) {
print "$usage\n";
}
if (!-d $test) {
print "No test directory: \"$test\"\n";
}
if ($server && !-d "$test/$server") {
print "No server directory: \"$test/$server\"\n";
}
# Global variables
# Start the server(s)
if ($server) {
&check_ports($server);
}
&verify_server($server);
}
} else {
# Determine which servers need to be started for this test.
closedir DIR;
my $name;
# Start the servers we found.
&check_ports();
&start_server($name);
}
}
# Subroutines
sub check_ports {
my $server = shift;
my $options = "";
$options = "-i $1";
}
my $tries = 0;
while (1) {
my $return = system("$PERL $topdir/testsock.pl -p 5300 $options");
last if ($return == 0);
if (++$tries > 4) {
print "$0: could not bind to server addresses, still running?\n";
print "I:server sockets not available\n";
print "R:FAIL\n";
exit 1;
}
print "I:Couldn't bind to socket (yet)\n";
sleep 2;
}
}
sub start_server {
my $server = shift;
my $options = shift;
my $cleanup_files;
my $command;
my $pid_file;
$cleanup_files = "{*.jnl,*.bk,*.st,named.run}";
$command = "$NAMED ";
if ($options) {
$command .= "$options";
} elsif (-e $args_file) {
{
#$line =~ s/\R//g;
chomp $line;
next if ($line =~ /^\s*$/); #discard blank lines
next if ($line =~ /^\s*#/); #discard comment lines
$line =~ s/#.*$//g;
last;
}
close FH;
$command .= "$options";
} else {
$command .= "-m record,size,mctx ";
$command .= "-T clienttest ";
$command .= "-T nosoa "
if (-e "$testdir/$server/named.nosoa");
$command .= "-T noaa "
if (-e "$testdir/$server/named.noaa");
$command .= "-c named.conf -d 99 -g";
}
if ($restart) {
$command .= " >>named.run 2>&1 &";
} else {
$command .= " >named.run 2>&1 &";
}
$pid_file = "named.pid";
$cleanup_files = "{lwresd.run}";
$command = "$LWRESD ";
if ($options) {
$command .= "$options";
} else {
$command .= "-m record,size,mctx ";
$command .= "-T clienttest ";
$command .= "-C resolv.conf -d 99 -g ";
$command .= "-i lwresd.pid -P 9210 -p 5300";
}
if ($restart) {
$command .= " >>lwresd.run 2>&1 &";
} else {
$command .= " >lwresd.run 2>&1 &";
}
$pid_file = "lwresd.pid";
$cleanup_files = "{ans.run}";
} else {
}
if ($options) {
$command .= "$options";
} else {
$command .= "";
}
if ($restart) {
$command .= " >>ans.run 2>&1 &";
} else {
$command .= " >ans.run 2>&1 &";
}
$pid_file = "ans.pid";
} else {
print "I:Unknown server type $server\n";
print "R:FAIL\n";
exit 1;
}
# print "I:starting server %s\n",$server;
chdir "$testdir/$server";
unless ($noclean) {
unlink glob $cleanup_files;
}
# get the shell to report the pid of the server ($!)
$command .= "echo \$!";
# start the server
$child =~ s/\s+$//g;
# wait up to 14 seconds for the server to start and to write the
# pid file otherwise kill this server and any others that have
# already been started
my $tries = 0;
while (!-s $pid_file) {
if (++$tries > 140) {
print "I:Couldn't start server $server (pid=$child)\n";
print "R:FAIL\n";
system "kill -9 $child" if ("$child" ne "");
exit 1;
}
# sleep for 0.1 seconds
select undef,undef,undef,0.1;
}
# go back to the top level directory
chdir $cwd;
}
sub verify_server {
my $server = shift;
my $n = $server;
$n =~ s/^ns//;
my $tries = 0;
while (1) {
my $return = system("$DIG +tcp +noadd +nosea +nostat +noquest +nocomm +nocmd -p 5300 version.bind. chaos txt \@10.53.0.$n > dig.out");
last if ($return == 0);
if (++$tries >= 30) {
print "I:no response from $server\n";
print "R:FAIL\n";
exit 1;
}
sleep 2;
}
unlink "dig.out";
}