test-network.sh 9.6 KB

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