43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews#!/usr/bin/perl -w
43c770b9987375e9e0efa19617b22e8e6a748a63Mark Andrews#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# Copyright (C) 2010, 2012, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews# file, You can obtain one at http://mozilla.org/MPL/2.0/.
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}