1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#!/usr/bin/perl -w
#
# Copyright (C) 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/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.
# 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';
use Getopt::Long;
# 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;
GetOptions('noclean' => \$noclean);
my $test = $ARGV[0];
my $server = $ARGV[1];
my $options = $ARGV[2];
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
my $topdir = abs_path("$test/..");
my $testdir = abs_path("$test");
my $NAMED = $ENV{'NAMED'};
my $DIG = $ENV{'DIG'};
my $PERL = $ENV{'PERL'};
# Start the server(s)
if ($server) {
if ($server =~ /^ns/) {
&check_ports($server);
}
&start_server($server, $options);
if ($server =~ /^ns/) {
&verify_server($server);
}
} else {
# Determine which servers need to be started for this test.
opendir DIR, $testdir;
my @files = sort readdir DIR;
closedir DIR;
my @ns = grep /^ns[0-9]*$/, @files;
# Start the servers we found.
&check_ports();
foreach (@ns) {
&start_server($_);
}
foreach (@ns) {
&verify_server($_);
}
}
# Subroutines
sub check_ports {
my $server = shift;
my $options = "";
if ($server && $server =~ /(\d+)$/) {
$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";
system("$PERL $topdir/stop.pl $testdir"); # Is this the correct behavior?
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;
if ($server =~ /^ns/) {
$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";
system "$PERL $topdir/stop.pl $testdir";
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";
system "$PERL $topdir/stop.pl $testdir";
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);
print `grep ";" dig.out`;
if (++$tries >= 30) {
print "I:no response from $server\n";
print "R:FAIL\n";
system("$PERL $topdir/stop.pl $testdir");
exit 1;
}
sleep 2;
}
unlink "dig.out";
}
|