test-scripts.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. ZKS=bin/zkServer.sh
  17. ZKSI=bin/zkServer-initialize.sh
  18. if [ ! -d "conf" ]; then
  19. echo "run this from the toplevel directory"
  20. exit 1
  21. fi
  22. if [ ! `ls build/zookeeper*.jar` ]; then
  23. echo "first compile the zk jar file"
  24. exit 1
  25. fi
  26. DATADIR=test-scripts_datadir
  27. DATALOGDIR=test-scripts_datalogdir
  28. case "`uname`" in
  29. CYGWIN*) cygwin=true ;;
  30. *) cygwin=false ;;
  31. esac
  32. if $cygwin
  33. then
  34. ZOOCFG=`cygpath -wp "$ZOOCFG"`
  35. # cygwin has a "kill" in the shell itself, gets confused
  36. KILL=/bin/kill
  37. else
  38. KILL=kill
  39. fi
  40. fail() {
  41. # don't run clear_tmp to allow debugging
  42. echo "FAIL $1"
  43. $KILL -9 $(cat "$ZOOPIDFILE")
  44. $KILL -9 $$
  45. }
  46. #generate a minimal config
  47. genconfig1() {
  48. cat > test-scripts.cfg <<EOF
  49. tickTime=2000
  50. initLimit=10
  51. syncLimit=5
  52. dataDir=$DATADIR
  53. clientPort=19181
  54. EOF
  55. }
  56. genconfig2() {
  57. genconfig1
  58. cat >> test-scripts.cfg <<EOF
  59. dataLogDir=$DATALOGDIR
  60. EOF
  61. }
  62. export ZOOCFGDIR=`pwd`
  63. export ZOOCFG="test-scripts.cfg"
  64. export CLASSPATH=$ZOOCFGDIR/conf
  65. #clear out the clutter generated by scripts
  66. clear_tmp() {
  67. rm -f test-scripts.cfg
  68. rm -fr $DATADIR
  69. rm -fr $DATALOGDIR
  70. rm -f zookeeper.out
  71. }
  72. start() {
  73. $CONFIG
  74. #ensure not already running
  75. $ZKS status && (echo "already running"; fail $LINENO)
  76. export ZOOPIDFILE="$DATADIR/zookeeper_server.pid"
  77. $ZKS start
  78. }
  79. stop() {
  80. $ZKS stop
  81. }
  82. CONFIG=genconfig1
  83. clear_tmp
  84. start
  85. ls $DATADIR || fail $LINENO
  86. ls $DATALOGDIR && fail $LINENO
  87. stop
  88. CONFIG=genconfig2
  89. clear_tmp
  90. start
  91. ls $DATADIR || fail $LINENO
  92. # zk actually checks for this to exist, but doesn't create
  93. ls $DATALOGDIR && fail $LINENO
  94. clear_tmp
  95. mkdir -p "$DATALOGDIR"
  96. start
  97. ls $DATADIR || fail $LINENO
  98. ls $DATALOGDIR || fail $LINENO
  99. stop
  100. #
  101. # verify autocreate diabled
  102. #
  103. export ZOO_DATADIR_AUTOCREATE_DISABLE=1
  104. CONFIG=genconfig1
  105. clear_tmp
  106. start
  107. [ $? -eq 1 ] || fail $LINENO
  108. ls $DATADIR && fail $LINENO
  109. ls $DATALOGDIR && fail $LINENO
  110. CONFIG=genconfig2
  111. clear_tmp
  112. mkdir -p "$DATADIR/version-2"
  113. start
  114. [ $? -eq 1 ] || fail $LINENO
  115. ls $DATALOGDIR && fail $LINENO
  116. CONFIG=genconfig1
  117. clear_tmp
  118. mkdir -p "$DATADIR/version-2"
  119. start
  120. [ $? -eq 0 ] || fail $LINENO
  121. stop
  122. CONFIG=genconfig2
  123. clear_tmp
  124. mkdir -p "$DATADIR/version-2"
  125. mkdir -p "$DATALOGDIR/version-2"
  126. start
  127. [ $? -eq 0 ] || fail $LINENO
  128. stop
  129. #
  130. # validate the initialize script
  131. #
  132. CONFIG=genconfig1
  133. clear_tmp
  134. $CONFIG
  135. $ZKSI --configfile "$ZOOCFGDIR/$ZOOCFG"
  136. ls $DATADIR || fail $LINENO
  137. #ensure not already running
  138. $ZKS status && (echo "already running"; fail $LINENO)
  139. export ZOOPIDFILE="$DATADIR/zookeeper_server.pid"
  140. $ZKS start
  141. [ $? -eq 0 ] || fail $LINENO
  142. stop
  143. CONFIG=genconfig2
  144. clear_tmp
  145. $CONFIG
  146. $ZKSI --configfile "$ZOOCFGDIR/$ZOOCFG"
  147. ls $DATADIR || fail $LINENO
  148. ls $DATALOGDIR || fail $LINENO
  149. #ensure not already running
  150. $ZKS status && (echo "already running"; fail $LINENO)
  151. export ZOOPIDFILE="$DATADIR/zookeeper_server.pid"
  152. $ZKS start
  153. [ $? -eq 0 ] || fail $LINENO
  154. stop
  155. ## validate force
  156. CONFIG=genconfig1
  157. clear_tmp
  158. $CONFIG
  159. $ZKSI --configfile "$ZOOCFGDIR/$ZOOCFG" || fail $LINENO
  160. ls $DATADIR || fail $LINENO
  161. $ZKSI --configfile "$ZOOCFGDIR/$ZOOCFG" && fail $LINENO
  162. $ZKSI --force --configfile "$ZOOCFGDIR/$ZOOCFG" || fail $LINENO
  163. #ensure not already running
  164. $ZKS status && (echo "already running"; fail $LINENO)
  165. export ZOOPIDFILE="$DATADIR/zookeeper_server.pid"
  166. $ZKS start
  167. [ $? -eq 0 ] || fail $LINENO
  168. stop
  169. $ZKSI --force --myid=1 --configfile "$ZOOCFGDIR/$ZOOCFG" || fail $LINENO
  170. #done, cleanup and exit
  171. clear_tmp
  172. echo "SUCCESS"