test-network.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #! /bin/sh
  2. ECHO_N="/bin/echo -n"
  3. # Output is prefixed with the name of the script
  4. myname=$(basename "$0")
  5. # default to summarising unexpected warnings
  6. export CHUTNEY_WARNINGS_IGNORE_EXPECTED=${CHUTNEY_WARNINGS_IGNORE_EXPECTED:-true}
  7. export CHUTNEY_WARNINGS_SUMMARY=${CHUTNEY_WARNINGS_SUMMARY:-true}
  8. until [ -z "$1" ]
  9. do
  10. case "$1" in
  11. # the path to the chutney directory
  12. --chutney-path)
  13. export CHUTNEY_PATH="$2"
  14. shift
  15. ;;
  16. --tor-path)
  17. # the path of a tor build directory
  18. # --tor-path overrides --tor and --tor-gencert
  19. export TOR_DIR="$2"
  20. shift
  21. ;;
  22. --tor)
  23. # the name or path of a tor binary
  24. export CHUTNEY_TOR="$2"
  25. shift
  26. ;;
  27. --tor-gencert)
  28. # the name or path of a tor-gencert binary
  29. export CHUTNEY_TOR_GENCERT="$2"
  30. shift
  31. ;;
  32. --flavor|--flavour|--network-flavor|--network-flavour)
  33. export NETWORK_FLAVOUR="$2"
  34. shift
  35. ;;
  36. # The amount of time chutney will wait before starting to verify
  37. # If negative, chutney exits straight after launching the network
  38. --start-time)
  39. export CHUTNEY_START_TIME="$2"
  40. shift
  41. ;;
  42. # The amount of time chutney will try to verify, before failing
  43. # If negative, chutney exits without verifying
  44. --delay|--sleep|--bootstrap-time|--time|--verify-time)
  45. # This isn't the best name for this variable, but we kept it the same
  46. # for backwards compatibility
  47. export CHUTNEY_BOOTSTRAP_TIME="$2"
  48. shift
  49. ;;
  50. # The amount of time chutney will wait after successfully verifying
  51. # If negative, chutney exits without stopping
  52. --stop-time)
  53. export CHUTNEY_STOP_TIME="$2"
  54. shift
  55. ;;
  56. # Environmental variables used by chutney verify performance tests
  57. # Send this many bytes per client connection (10 KBytes)
  58. --data|--data-bytes|--data-byte|--bytes|--byte)
  59. export CHUTNEY_DATA_BYTES="$2"
  60. shift
  61. ;;
  62. # Make this many connections per client (1)
  63. # Note: If you create 7 or more connections to a hidden service from
  64. # a single Tor 0.2.7 client, you'll likely get a verification failure due
  65. # to #15937. This is fixed in 0.2.8.
  66. --connections|--connection|--connection-count|--count)
  67. export CHUTNEY_CONNECTIONS="$2"
  68. shift
  69. ;;
  70. # Make each client connect to each HS (0)
  71. # 0 means a single client connects to each HS
  72. # 1 means every client connects to every HS
  73. --hs-multi-client|--hs-multi-clients|--hs-client|--hs-clients)
  74. export CHUTNEY_HS_MULTI_CLIENT="$2"
  75. shift
  76. ;;
  77. # The IPv4 address to bind to, defaults to 127.0.0.1
  78. --ipv4|--v4|-4|--ip)
  79. export CHUTNEY_LISTEN_ADDRESS="$2"
  80. shift
  81. ;;
  82. # The IPv6 address to bind to, default is not to bind to an IPv6 address
  83. --ipv6|--v6|-6)
  84. export CHUTNEY_LISTEN_ADDRESS_V6="$2"
  85. shift
  86. ;;
  87. --coverage)
  88. export USE_COVERAGE_BINARY=true
  89. ;;
  90. --dry-run)
  91. # process arguments, but don't call any other scripts
  92. export NETWORK_DRY_RUN=true
  93. ;;
  94. # we summarise unexpected warnings by default
  95. # this shows all warnings per-node
  96. --all-warnings)
  97. export CHUTNEY_WARNINGS_IGNORE_EXPECTED=false
  98. export CHUTNEY_WARNINGS_SUMMARY=false
  99. ;;
  100. # this skips warnings entirely
  101. --no-warnings)
  102. export CHUTNEY_WARNINGS_SKIP=true
  103. ;;
  104. *)
  105. echo "$myname: Sorry, I don't know what to do with '$1'."
  106. # continue processing arguments during a dry run
  107. if [ "$NETWORK_DRY_RUN" != true ]; then
  108. exit 2
  109. fi
  110. ;;
  111. esac
  112. shift
  113. done
  114. # optional: $TOR_DIR is the tor build directory
  115. # it's used to find the location of tor binaries
  116. # if it's not set:
  117. # - set it ro $BUILDDIR, or
  118. # - if $PWD looks like a tor build directory, set it to $PWD, or
  119. # - unset $TOR_DIR, and let chutney fall back to finding tor binaries in $PATH
  120. if [ ! -d "$TOR_DIR" ]; then
  121. if [ -d "$BUILDDIR/src/or" -a -d "$BUILDDIR/src/tools" ]; then
  122. # Choose the build directory
  123. # But only if it looks like one
  124. echo "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
  125. export TOR_DIR="$BUILDDIR"
  126. elif [ -d "$PWD/src/or" -a -d "$PWD/src/tools" ]; then
  127. # Guess the tor directory is the current directory
  128. # But only if it looks like one
  129. echo "$myname: \$TOR_DIR not set, trying \$PWD"
  130. export TOR_DIR="$PWD"
  131. elif [ -d "$PWD/../tor" -a -d "$PWD/../tor/src/or" -a \
  132. -d "$PWD/../tor/src/tools" ]; then
  133. # Guess the tor directory is next to the current directory
  134. # But only if it looks like one
  135. echo "$myname: \$TOR_DIR not set, trying \$PWD/../tor"
  136. export TOR_DIR="$PWD/../tor"
  137. else
  138. echo "$myname: no \$TOR_DIR, chutney will use \$PATH for tor binaries"
  139. unset TOR_DIR
  140. fi
  141. fi
  142. # make TOR_DIR absolute
  143. if [ -d "$PWD/$TOR_DIR" ]; then
  144. export TOR_DIR="$PWD/$TOR_DIR"
  145. fi
  146. # mandatory: $CHUTNEY_PATH is the path to the chutney launch script
  147. # if it's not set:
  148. # - if $PWD looks like a chutney directory, set it to $PWD, or
  149. # - set it based on $TOR_DIR, expecting chutney to be next to tor, or
  150. # - fail and tell the user how to clone the chutney repository
  151. if [ ! -d "$CHUTNEY_PATH" -o ! -x "$CHUTNEY_PATH/chutney" -o \
  152. ! -f "$CHUTNEY_PATH/chutney" ]; then
  153. if [ -x "$PWD/chutney" -a -f "$PWD/chutney" ]; then
  154. echo "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
  155. export CHUTNEY_PATH="$PWD"
  156. elif [ -d "`dirname \"$0\"`/.." -a \
  157. -x "`dirname \"$0\"`/../chutney" -a \
  158. -f "`dirname \"$0\"`/../chutney" ]; then
  159. echo "$myname: \$CHUTNEY_PATH not valid, using this script's location"
  160. export CHUTNEY_PATH="`dirname \"$0\"`/.."
  161. elif [ -d "$TOR_DIR" -a -d "$TOR_DIR/../chutney" -a \
  162. -x "$TOR_DIR/../chutney/chutney" -a \
  163. -f "$TOR_DIR/../chutney/chutney" ]; then
  164. echo "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
  165. export CHUTNEY_PATH="$TOR_DIR/../chutney"
  166. else
  167. # TODO: work out how to package and install chutney,
  168. # so users can find it in $PATH
  169. echo "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
  170. echo "$myname: Get chutney: git clone https://git.torproject.org/\
  171. chutney.git"
  172. echo "$myname: Set \$CHUTNEY_PATH to a non-standard location: export \
  173. CHUTNEY_PATH=\`pwd\`/chutney"
  174. unset CHUTNEY_PATH
  175. exit 1
  176. fi
  177. fi
  178. # make chutney path absolute
  179. if [ -d "$PWD/$CHUTNEY_PATH" ]; then
  180. export CHUTNEY_PATH="$PWD/$CHUTNEY_PATH"
  181. fi
  182. # For picking up the right tor binaries.
  183. # If $TOR_DIR isn't set, chutney looks for tor binaries by name or path
  184. # using $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, and then falls back to
  185. # looking for tor and tor-gencert in $PATH
  186. if [ -d "$TOR_DIR" ]; then
  187. tor_name=tor
  188. tor_gencert_name=tor-gencert
  189. if [ "$USE_COVERAGE_BINARY" = true ]; then
  190. tor_name=tor-cov
  191. fi
  192. export CHUTNEY_TOR="${TOR_DIR}/src/or/${tor_name}"
  193. export CHUTNEY_TOR_GENCERT="${TOR_DIR}/src/tools/${tor_gencert_name}"
  194. else
  195. # make binary paths absolute
  196. if [ -x "$PWD/$CHUTNEY_TOR" ]; then
  197. export CHUTNEY_TOR="$PWD/$CHUTNEY_TOR"
  198. fi
  199. if [ -x "$PWD/$CHUTNEY_TOR_GENCERT" ]; then
  200. export CHUTNEY_TOR_GENCERT="$PWD/$CHUTNEY_TOR_GENCERT"
  201. fi
  202. fi
  203. # Set the variables for the chutney network flavour
  204. export NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs"}
  205. export CHUTNEY_NETWORK="$CHUTNEY_PATH/networks/$NETWORK_FLAVOUR"
  206. # And finish up if we're doing a dry run
  207. if [ "$NETWORK_DRY_RUN" = true ]; then
  208. # we can't exit here, it breaks argument processing
  209. return
  210. fi
  211. # Chutney must be launched at $CHUTNEY_PATH, at least until #21521 is fixed
  212. cd "$CHUTNEY_PATH"
  213. "$CHUTNEY_PATH/tools/bootstrap-network.sh" "$NETWORK_FLAVOUR" || exit 2
  214. # chutney starts verifying after 20 seconds, keeps on trying for 60 seconds,
  215. # and then stops immediately (by default)
  216. # Even the fastest chutney networks take 5-10 seconds for their first consensus
  217. # and then 10 seconds after that for relays to bootstrap and upload descriptors
  218. export CHUTNEY_START_TIME=${CHUTNEY_START_TIME:-20}
  219. export CHUTNEY_BOOTSTRAP_TIME=${CHUTNEY_BOOTSTRAP_TIME:-60}
  220. export CHUTNEY_STOP_TIME=${CHUTNEY_STOP_TIME:-0}
  221. CHUTNEY="$CHUTNEY_PATH/chutney"
  222. if [ "$CHUTNEY_WARNINGS_SKIP" = true ]; then
  223. WARNINGS=true
  224. else
  225. WARNINGS="$CHUTNEY_PATH/tools/warnings.sh"
  226. fi
  227. if [ "$CHUTNEY_START_TIME" -ge 0 ]; then
  228. echo "Waiting ${CHUTNEY_START_TIME} seconds for a consensus containing relays to be generated..."
  229. sleep "$CHUTNEY_START_TIME"
  230. else
  231. echo "Chutney network launched and running. To stop the network, use:"
  232. echo "$CHUTNEY stop $CHUTNEY_NETWORK"
  233. "$WARNINGS"
  234. exit 0
  235. fi
  236. if [ "$CHUTNEY_BOOTSTRAP_TIME" -ge 0 ]; then
  237. # Chutney will try to verify for $CHUTNEY_BOOTSTRAP_TIME seconds
  238. "$CHUTNEY" verify "$CHUTNEY_NETWORK"
  239. VERIFY_EXIT_STATUS="$?"
  240. else
  241. echo "Chutney network ready and running. To stop the network, use:"
  242. echo "$CHUTNEY" stop "$CHUTNEY_NETWORK"
  243. "$WARNINGS"
  244. exit 0
  245. fi
  246. if [ "$CHUTNEY_STOP_TIME" -ge 0 ]; then
  247. if [ "$CHUTNEY_STOP_TIME" -gt 0 ]; then
  248. echo "Waiting ${CHUTNEY_STOP_TIME} seconds before stopping the network..."
  249. fi
  250. sleep "$CHUTNEY_STOP_TIME"
  251. # work around a bug/feature in make -j2 (or more)
  252. # where make hangs if any child processes are still alive
  253. "$CHUTNEY" stop "$CHUTNEY_NETWORK"
  254. "$WARNINGS"
  255. exit "$VERIFY_EXIT_STATUS"
  256. else
  257. echo "Chutney network verified and running. To stop the network, use:"
  258. echo "$CHUTNEY stop $CHUTNEY_NETWORK"
  259. "$WARNINGS"
  260. exit 0
  261. fi