#
# Copyright (C) 2010, 2012, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# $Id: start.pl,v 1.2 2010/06/17 05:38:05 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 '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
my $usage = "usage: $0 [--noclean] test-directory [server-directory [server-options]]";
my $noclean;
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;
# Start the servers we found.
&check_ports();
foreach (@ns) {
&start_server($_);
}
foreach (@ns) {
&verify_server($_);
}
}
# 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 = "sh wrap.sh ";
$command .= "$NAMED ";
if ($options) {
$command .= "$options";
} else {
$command .= "-m record,size,mctx ";
$command .= "-T clienttest ";
$command .= "-X named.lock ";
$command .= "-c named.conf -d 99 -g";
}
$command .= " >named.run 2>&1 &";
$pid_file = "named.pid";
} else {
print "I:Unknown server type $server\n";
print "R:FAIL\n";
exit 1;
}
# print "I:starting server $server\n";
chdir "$testdir/$server";
unless ($noclean) {
unlink glob $cleanup_files;
}
system "$command";
my $tries = 0;
while (!-f $pid_file) {
if (++$tries > 14) {
print "I:Couldn't start server $server\n";
print "R:FAIL\n";
exit 1;
}
sleep 1;
}
}
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";
}