start.pl 5.8 KB

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