start.pl 5.7 KB

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