start.pl revision 6e9b7dc9c7aa9351dcebe815417de7ffe101e5ea
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews#!/usr/bin/perl -w
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews#
a3128c1995310262648e575a9ff148d5741fd167Tinderbox User# Copyright (C) 2010, 2012 Internet Systems Consortium, Inc. ("ISC")
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# Permission to use, copy, modify, and/or distribute this software for any
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# purpose with or without fee is hereby granted, provided that the above
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# copyright notice and this permission notice appear in all copies.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews# PERFORMANCE OF THIS SOFTWARE.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# $Id: start.pl,v 1.2 2010/06/17 05:38:05 marka Exp $
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Framework for starting test servers.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Based on the type of server specified, check for port availability, remove
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# temporary files, start the server, and verify that the server is running.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# If a server is specified, start it. Otherwise, start all servers for test.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsuse strict;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsuse Cwd 'abs_path';
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsuse Getopt::Long;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Option handling
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# --noclean test [server [options]]
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews#
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# --noclean - Do not cleanup files in server directory
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# test - name of the test directory
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# server - name of the server directory
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# options - alternate options for the server
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $usage = "usage: $0 [--noclean] test-directory [server-directory [server-options]]";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $noclean;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark AndrewsGetOptions('noclean' => \$noclean);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $test = $ARGV[0];
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $server = $ARGV[1];
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $options = $ARGV[2];
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsif (!$test) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "$usage\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsif (!-d $test) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "No test directory: \"$test\"\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsif ($server && !-d "$test/$server") {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "No server directory: \"$test/$server\"\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Global variables
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $topdir = abs_path("$test/..");
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $testdir = abs_path("$test");
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $NAMED = $ENV{'NAMED'};
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $DIG = $ENV{'DIG'};
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsmy $PERL = $ENV{'PERL'};
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Start the server(s)
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewsif ($server) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if ($server =~ /^ns/) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews &check_ports($server);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews &start_server($server, $options);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if ($server =~ /^ns/) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews &verify_server($server);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews} else {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews # Determine which servers need to be started for this test.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews opendir DIR, $testdir;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my @files = sort readdir DIR;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews closedir DIR;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my @ns = grep /^ns[0-9]*$/, @files;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews # Start the servers we found.
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews &check_ports();
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews foreach (@ns) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews &start_server($_);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews foreach (@ns) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews &verify_server($_);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# Subroutines
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewssub check_ports {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $server = shift;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $options = "";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if ($server && $server =~ /(\d+)$/) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $options = "-i $1";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $tries = 0;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews while (1) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $return = system("$PERL $topdir/testsock.pl -p 5300 $options");
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews last if ($return == 0);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if (++$tries > 4) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "$0: could not bind to server addresses, still running?\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "I:server sockets not available\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "R:FAIL\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews system("$PERL $topdir/stop.pl $testdir"); # Is this the correct behavior?
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews exit 1;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "I:Couldn't bind to socket (yet)\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews sleep 2;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewssub start_server {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $server = shift;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $options = shift;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $cleanup_files;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $command;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $pid_file;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if ($server =~ /^ns/) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $cleanup_files = "{*.jnl,*.bk,*.st,named.run}";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $command = "sh wrap.sh ";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $command .= "$NAMED ";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if ($options) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $command .= "$options";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews } else {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $command .= "-m record,size,mctx ";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $command .= "-T clienttest ";
6e9b7dc9c7aa9351dcebe815417de7ffe101e5eaJeremy C. Reed $command .= "-X named.lock ";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $command .= "-c named.conf -d 99 -g";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $command .= " >named.run 2>&1 &";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $pid_file = "named.pid";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews } else {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "I:Unknown server type $server\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "R:FAIL\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews system "$PERL $topdir/stop.pl $testdir";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews exit 1;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews# print "I:starting server $server\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews chdir "$testdir/$server";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews unless ($noclean) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews unlink glob $cleanup_files;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews system "$command";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $tries = 0;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews while (!-f $pid_file) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if (++$tries > 14) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "I:Couldn't start server $server\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "R:FAIL\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews system "$PERL $topdir/stop.pl $testdir";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews exit 1;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews sleep 1;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrewssub verify_server {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $server = shift;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $n = $server;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews $n =~ s/^ns//;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $tries = 0;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews while (1) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews my $return = system("$DIG +tcp +noadd +nosea +nostat +noquest +nocomm +nocmd -p 5300 version.bind. chaos txt \@10.53.0.$n > dig.out");
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews last if ($return == 0);
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print `grep ";" dig.out`;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews if (++$tries >= 30) {
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "I:no response from $server\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews print "R:FAIL\n";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews system("$PERL $topdir/stop.pl $testdir");
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews exit 1;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews sleep 2;
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews }
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews unlink "dig.out";
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews}