start.pl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 $RUN_BIND10 = $ENV{'RUN_BIND10'};
  50. my $NAMED = $ENV{'NAMED'};
  51. my $LWRESD = $ENV{'LWRESD'};
  52. my $DIG = $ENV{'DIG'};
  53. my $PERL = $ENV{'PERL'};
  54. my $TESTSOCK = $ENV{'TESTSOCK'};
  55. # Start the server(s)
  56. if ($server) {
  57. if ($server =~ /^ns/) {
  58. &check_ports($server);
  59. }
  60. &start_server($server, $options);
  61. if ($server =~ /^ns/) {
  62. &verify_server($server);
  63. }
  64. } else {
  65. # Determine which servers need to be started for this test.
  66. opendir DIR, $testdir;
  67. my @files = sort readdir DIR;
  68. closedir DIR;
  69. my @ns = grep /^nsx?[0-9]*$/, @files;
  70. my @lwresd = grep /^lwresd[0-9]*$/, @files;
  71. my @ans = grep /^ans[0-9]*$/, @files;
  72. # Start the servers we found.
  73. &check_ports();
  74. foreach my $s (@ns, @lwresd, @ans) {
  75. &start_server($s);
  76. }
  77. foreach my $s (@ns) {
  78. &verify_server($s);
  79. }
  80. }
  81. # Subroutines
  82. sub check_ports {
  83. my $server = shift;
  84. my $options = "";
  85. if ($server && $server =~ /(\d+)$/) {
  86. $options = "-i $1";
  87. }
  88. my $tries = 0;
  89. while (1) {
  90. my $return = system("$PERL $TESTSOCK -p 53210 $options");
  91. last if ($return == 0);
  92. if (++$tries > 4) {
  93. print "$0: could not bind to server addresses, still running?\n";
  94. print "I:server sockets not available\n";
  95. print "R:FAIL\n";
  96. system("$PERL $topdir/stop.pl $testdir"); # Is this the correct behavior?
  97. exit 1;
  98. }
  99. print "I:Couldn't bind to socket (yet)\n";
  100. sleep 2;
  101. }
  102. }
  103. sub start_server {
  104. my $server = shift;
  105. my $options = shift;
  106. my $cleanup_files;
  107. my $command;
  108. my $pid_file;
  109. if ($server =~ /^nsx/) {
  110. $cleanup_files = "{bind10.run}";
  111. $command = "B10_FROM_SOURCE_LOCALSTATEDIR=$testdir/$server/ ";
  112. $command .= "$RUN_BIND10 ";
  113. if ($options) {
  114. $command .= "$options";
  115. } else {
  116. $command .= "--msgq-socket-file=$testdir/$server/msgq_socket ";
  117. $command .= "--pid-file=$testdir/$server/bind10.pid ";
  118. $command .= "-v";
  119. }
  120. $command .= " >bind10.run 2>&1 &";
  121. $pid_file = "bind10.pid";
  122. } elsif ($server =~ /^ns/) {
  123. $cleanup_files = "{*.jnl,*.bk,*.st,named.run}";
  124. $command = "$NAMED ";
  125. if ($options) {
  126. $command .= "$options";
  127. } else {
  128. $command .= "-m record,size,mctx ";
  129. $command .= "-T clienttest ";
  130. $command .= "-T nosoa "
  131. if (-e "$testdir/$server/named.nosoa");
  132. $command .= "-T noaa "
  133. if (-e "$testdir/$server/named.noaa");
  134. $command .= "-c named.conf -d 99 -g";
  135. }
  136. $command .= " >named.run 2>&1 &";
  137. $pid_file = "named.pid";
  138. } elsif ($server =~ /^lwresd/) {
  139. $cleanup_files = "{lwresd.run}";
  140. $command = "$LWRESD ";
  141. if ($options) {
  142. $command .= "$options";
  143. } else {
  144. $command .= "-m record,size,mctx ";
  145. $command .= "-T clienttest ";
  146. $command .= "-C resolv.conf -d 99 -g ";
  147. $command .= "-i lwresd.pid -P 9210 -p 53210";
  148. }
  149. $command .= " >lwresd.run 2>&1 &";
  150. $pid_file = "lwresd.pid";
  151. } elsif ($server =~ /^ans/) {
  152. $cleanup_files = "{ans.run}";
  153. $command = "$PERL ./ans.pl ";
  154. if ($options) {
  155. $command .= "$options";
  156. } else {
  157. $command .= "";
  158. }
  159. $command .= " >ans.run 2>&1 &";
  160. $pid_file = "ans.pid";
  161. } else {
  162. print "I:Unknown server type $server\n";
  163. print "R:FAIL\n";
  164. system "$PERL $topdir/stop.pl $testdir";
  165. exit 1;
  166. }
  167. # print "I:starting server $server\n";
  168. chdir "$testdir/$server";
  169. unless ($noclean) {
  170. unlink glob $cleanup_files;
  171. }
  172. system "$command";
  173. my $tries = 0;
  174. while (!-f $pid_file) {
  175. if (++$tries > 14) {
  176. print "I:Couldn't start server $server\n";
  177. print "R:FAIL\n";
  178. system "$PERL $topdir/stop.pl $testdir";
  179. exit 1;
  180. }
  181. sleep 1;
  182. }
  183. }
  184. sub verify_server {
  185. my $server = shift;
  186. my $n = $server;
  187. $n =~ s/^nsx?//;
  188. my $tries = 0;
  189. while (1) {
  190. my $return = system("$DIG +tcp +noadd +nosea +nostat +noquest +nocomm +nocmd -p 53210 version.bind. chaos txt \@10.53.0.$n > dig.out");
  191. last if ($return == 0);
  192. print `grep ";" dig.out`;
  193. if (++$tries >= 30) {
  194. print "I:no response from $server\n";
  195. print "R:FAIL\n";
  196. system("$PERL $topdir/stop.pl $testdir");
  197. exit 1;
  198. }
  199. sleep 2;
  200. }
  201. unlink "dig.out";
  202. }