summaryrefslogtreecommitdiffstats
path: root/bin/tests/system/start.pl
blob: 3ef49a946cb5333704b1fc2cd28d0d01f1744a18 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/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;
use Cwd 'abs_path';
use Getopt::Long;

# Usage:
#   perl start.pl [--noclean] [--restart] [--port port] test [server [options]]
#
#   --noclean       Do not cleanup files in server directory.
#
#   --restart       Indicate that the server is being restarted, so get the
#                   server to append output to an existing log file instead of
#                   starting a new one.
#
#   --port port     Specify the default port being used by the server to answer
#                   queries (default 5300).  This script will interrogate the
#                   server on this port to see if it is running. (Note: for
#                   "named" nameservers, this can be overridden by the presence
#                   of the file "named.port" in the server directory containing
#                   the number of the query port.)
#
#   test            Name of the test directory.
#
#   server          Name of the server directory.  This will be of the form
#                   "nsN" or "ansN", where "N" is an integer between 1 and 8.
#                   If not given, the script will start all the servers in the
#                   test directory.
#
#   options         Alternate options for the server.
#
#                   NOTE: options must be specified with '-- "<option list>"',
#                   for instance: start.pl . ns1 -- "-c n.conf -d 43"
#
#                   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] [--port <port>] test-directory [server-directory [server-options]]";
my $noclean = '';
my $restart = '';
my $queryport = 5300;

GetOptions('noclean' => \$noclean, 'restart' => \$restart, 'port=i' => \$queryport) or die "$usage\n";

my $test = $ARGV[0];
my $server = $ARGV[1];
my $options = $ARGV[2];

if (!$test) {
	die "$usage\n";
}
if (!-d $test) {
	die "No test directory: \"$test\"\n";
}
if ($server && !-d "$test/$server") {
	die "No server directory: \"$test/$server\"\n";
}

# Global variables
my $topdir = abs_path("$test/..");
my $testdir = abs_path("$test");
my $NAMED = $ENV{'NAMED'};
my $LWRESD = $ENV{'LWRESD'};
my $DIG = $ENV{'DIG'};
my $PERL = $ENV{'PERL'};
my $PYTHON = $ENV{'PYTHON'};

# 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;
	my @lwresd = grep /^lwresd[0-9]*$/, @files;
	my @ans = grep /^ans[0-9]*$/, @files;
	my $name;

	# Start the servers we found.
	&check_ports();
	foreach $name(@ns, @lwresd, @ans) {
		&start_server($name);
		&verify_server($name) if ($name =~ /^ns/);
	}
}

# Subroutines

sub check_ports {
	my $server = shift;
	my $options = "";
	my $port = $queryport;
	my $file = "";

	$file = $testdir . "/" . $server . "/named.port" if ($server);

	if ($server && $server =~ /(\d+)$/) {
		$options = "-i $1";
	}

	if ($file ne "" && -e $file) {
		open(FH, "<", $file);
		while(my $line=<FH>) {
			chomp $line;
			$port = $line;
			last;
		}
		close FH;
	}

	my $tries = 0;
	while (1) {
		my $return = system("$PERL $topdir/testsock.pl -p $port $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 "I:failed\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;
        my $cwd = getcwd();
	my $args_file = $cwd . "/" . $test . "/" . $server . "/" . "named.args";

	if ($server =~ /^ns/) {
		$cleanup_files = "{*.jnl,*.bk,*.st,named.run}";
		if ($ENV{'USE_VALGRIND'}) {
			$command = "valgrind -q --gen-suppressions=all --num-callers=48 --fullpath-after= --log-file=named-$server-valgrind-%p.log ";
			if ($ENV{'USE_VALGRIND'} eq 'helgrind') {
				$command .= "--tool=helgrind ";
			} else {
				$command .= "--tool=memcheck --track-origins=yes --leak-check=full ";
			}
			$command .= "$NAMED -m none -M external ";
		} else {
			$command = "$NAMED ";
		}
		if ($options) {
			$command .= "$options";
		} elsif (-e $args_file) {
			open(FH, "<", $args_file);
			while(my $line=<FH>)
			{
				#$line =~ s/\R//g;
				chomp $line;
				next if ($line =~ /^\s*$/); #discard blank lines
				next if ($line =~ /^\s*#/); #discard comment lines
				$line =~ s/#.*$//g;
				$options = $line;
				last;
			}
			close FH;
			$command .= "$options";
		} else {
			$command .= "-D $test-$server ";
			$command .= "-X named.lock ";
			$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 .= "-T noedns "
				if (-e "$testdir/$server/named.noedns");
			$command .= "-T dropedns "
				if (-e "$testdir/$server/named.dropedns");
			$command .= "-T maxudp512 "
				if (-e "$testdir/$server/named.maxudp512");
			$command .= "-T maxudp1460 "
				if (-e "$testdir/$server/named.maxudp1460");
			$command .= "-c named.conf -d 99 -g -U 4";
		}
		$command .= " -T notcp"
			if (-e "$testdir/$server/named.notcp");
		if ($restart) {
			$command .= " >>named.run 2>&1 &";
		} else {
			$command .= " >named.run 2>&1 &";
		}
		$pid_file = "named.pid";
	} elsif ($server =~ /^lwresd/) {
		$cleanup_files = "{lwresd.run}";
		if ($ENV{'USE_VALGRIND'}) {
			$command = "valgrind -q --gen-suppressions=all --num-callers=48 --fullpath-after= --log-file=lwresd-valgrind-%p.log ";
			if ($ENV{'USE_VALGRIND'} eq 'helgrind') {
				$command .= "--tool=helgrind ";
			} else {
				$command .= "--tool=memcheck --track-origins=yes --leak-check=full ";
			}
			$command .= "$LWRESD -m none -M external ";
		} else {
			$command = "$LWRESD ";
		}
		if ($options) {
			$command .= "$options";
		} else {
			$command .= "-X lwresd.lock ";
			$command .= "-m record,size,mctx ";
			$command .= "-T clienttest ";
			$command .= "-C resolv.conf -d 99 -g -U 4 ";
			$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";
	} elsif ($server =~ /^ans/) {
		$cleanup_files = "{ans.run}";
                if (-e "$testdir/$server/ans.py") {
                        $command = "$PYTHON -u ans.py 10.53.0.$' $queryport";
                } elsif (-e "$testdir/$server/ans.pl") {
                        $command = "$PERL ans.pl";
                } else {
                        $command = "$PERL $topdir/ans.pl 10.53.0.$'";
                }
		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 "I:failed\n";
		system "$PERL $topdir/stop.pl $testdir";
		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
	my $child = `$command`;
	$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 "I:failed\n";
			system "kill -9 $child" if ("$child" ne "");
			system "$PERL $topdir/stop.pl $testdir";
			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;
	my $port = $queryport;
	my $tcp = "+tcp";

	$n =~ s/^ns//;

	if (-e "$testdir/$server/named.port") {
		open(FH, "<", "$testdir/$server/named.port");
		while(my $line=<FH>) {
			chomp $line;
			$port = $line;
			last;
		}
		close FH;
	}

	$tcp = "" if (-e "$testdir/$server/named.notcp");

	my $tries = 0;
	while (1) {
		my $return = system("$DIG $tcp +noadd +nosea +nostat +noquest +nocomm +nocmd +noedns -p $port 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 "I:failed\n";
			system("$PERL $topdir/stop.pl $testdir");
			exit 1;
		}
		sleep 2;
	}
	unlink "dig.out";
}