start.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (C) 2004-2008, 2010 Internet Systems Consortium, Inc. ("ISC")
  4. # Copyright (C) 2001 Internet Software Consortium.
  5. #
  6. # Permission to use, copy, modify, and/or distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  11. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  12. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  13. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  14. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  15. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. # PERFORMANCE OF THIS SOFTWARE.
  17. # Framework for starting test servers.
  18. # Based on the type of server specified, check for port availability, remove
  19. # temporary files, start the server, and verify that the server is running.
  20. # If a server is specified, start it. Otherwise, start all servers for test.
  21. use strict;
  22. use Cwd 'abs_path';
  23. use Getopt::Long;
  24. # Option handling
  25. # --noclean test [server [options]]
  26. #
  27. # --noclean - Do not cleanup files in server directory
  28. # test - name of the test directory
  29. # server - name of the server directory
  30. # options - alternate options for the server
  31. my $usage = "usage: $0 [--noclean] test-directory [server-directory [server-options]]";
  32. my $noclean;
  33. GetOptions('noclean' => \$noclean);
  34. my $test = $ARGV[0];
  35. my $server = $ARGV[1];
  36. my $options = $ARGV[2];
  37. if (!$test) {
  38. print "$usage\n";
  39. }
  40. if (!-d $test) {
  41. print "No test directory: \"$test\"\n";
  42. }
  43. if ($server && !-d "$test/$server") {
  44. print "No server directory: \"$test/$server\"\n";
  45. }
  46. # Global variables
  47. my $topdir = abs_path("$test/..");
  48. my $testdir = abs_path("$test");
  49. my $NAMED = $ENV{'NAMED'};
  50. my $LWRESD = $ENV{'LWRESD'};
  51. my $DIG = $ENV{'DIG'};
  52. my $PERL = $ENV{'PERL'};
  53. # Start the server(s)
  54. if ($server) {
  55. if ($server =~ /^ns/) {
  56. &check_ports($server);
  57. }
  58. &start_server($server, $options);
  59. if ($server =~ /^ns/) {
  60. &verify_server($server);
  61. }
  62. } else {
  63. # Determine which servers need to be started for this test.
  64. opendir DIR, $testdir;
  65. my @files = sort readdir DIR;
  66. closedir DIR;
  67. my @ns = grep /^ns[0-9]*$/, @files;
  68. my @lwresd = grep /^lwresd[0-9]*$/, @files;
  69. my @ans = grep /^ans[0-9]*$/, @files;
  70. # Start the servers we found.
  71. &check_ports();
  72. foreach (@ns, @lwresd, @ans) {
  73. &start_server($_);
  74. }
  75. foreach (@ns) {
  76. &verify_server($_);
  77. }
  78. }
  79. # Subroutines
  80. sub check_ports {
  81. my $server = shift;
  82. my $options = "";
  83. if ($server && $server =~ /(\d+)$/) {
  84. $options = "-i $1";
  85. }
  86. my $tries = 0;
  87. while (1) {
  88. my $return = system("$PERL $topdir/testsock.pl -p 5300 $options");
  89. last if ($return == 0);
  90. if (++$tries > 4) {
  91. print "$0: could not bind to server addresses, still running?\n";
  92. print "I:server sockets not available\n";
  93. print "R:FAIL\n";
  94. system("$PERL $topdir/stop.pl $testdir"); # Is this the correct behavior?
  95. exit 1;
  96. }
  97. print "I:Couldn't bind to socket (yet)\n";
  98. sleep 2;
  99. }
  100. }
  101. sub start_server {
  102. my $server = shift;
  103. my $options = shift;
  104. my $cleanup_files;
  105. my $command;
  106. my $pid_file;
  107. if ($server =~ /^ns/) {
  108. $cleanup_files = "{*.jnl,*.bk,*.st,named.run}";
  109. $command = "$NAMED ";
  110. if ($options) {
  111. $command .= "$options";
  112. } else {
  113. $command .= "-m record,size,mctx ";
  114. $command .= "-T clienttest ";
  115. $command .= "-T nosoa "
  116. if (-e "$testdir/$server/named.nosoa");
  117. $command .= "-T noaa "
  118. if (-e "$testdir/$server/named.noaa");
  119. $command .= "-c named.conf -d 99 -g";
  120. }
  121. $command .= " >named.run 2>&1 &";
  122. $pid_file = "named.pid";
  123. } elsif ($server =~ /^lwresd/) {
  124. $cleanup_files = "{lwresd.run}";
  125. $command = "$LWRESD ";
  126. if ($options) {
  127. $command .= "$options";
  128. } else {
  129. $command .= "-m record,size,mctx ";
  130. $command .= "-T clienttest ";
  131. $command .= "-C resolv.conf -d 99 -g ";
  132. $command .= "-i lwresd.pid -P 9210 -p 5300";
  133. }
  134. $command .= " >lwresd.run 2>&1 &";
  135. $pid_file = "lwresd.pid";
  136. } elsif ($server =~ /^ans/) {
  137. $cleanup_files = "{ans.run}";
  138. $command = "$PERL ./ans.pl ";
  139. if ($options) {
  140. $command .= "$options";
  141. } else {
  142. $command .= "";
  143. }
  144. $command .= " >ans.run 2>&1 &";
  145. $pid_file = "ans.pid";
  146. } else {
  147. print "I:Unknown server type $server\n";
  148. print "R:FAIL\n";
  149. system "$PERL $topdir/stop.pl $testdir";
  150. exit 1;
  151. }
  152. # print "I:starting server $server\n";
  153. chdir "$testdir/$server";
  154. unless ($noclean) {
  155. unlink glob $cleanup_files;
  156. }
  157. system "$command";
  158. my $tries = 0;
  159. while (!-f $pid_file) {
  160. if (++$tries > 14) {
  161. print "I:Couldn't start server $server\n";
  162. print "R:FAIL\n";
  163. system "$PERL $topdir/stop.pl $testdir";
  164. exit 1;
  165. }
  166. sleep 1;
  167. }
  168. }
  169. sub verify_server {
  170. my $server = shift;
  171. my $n = $server;
  172. $n =~ s/^ns//;
  173. my $tries = 0;
  174. while (1) {
  175. my $return = system("$DIG +tcp +noadd +nosea +nostat +noquest +nocomm +nocmd -p 5300 version.bind. chaos txt \@10.53.0.$n > dig.out");
  176. last if ($return == 0);
  177. print `grep ";" dig.out`;
  178. if (++$tries >= 30) {
  179. print "I:no response from $server\n";
  180. print "R:FAIL\n";
  181. system("$PERL $topdir/stop.pl $testdir");
  182. exit 1;
  183. }
  184. sleep 2;
  185. }
  186. unlink "dig.out";
  187. }