test-network.sh 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. # Warning Options
  90. # we summarise unexpected warnings by default
  91. # this shows all warnings per-node
  92. --all-warnings)
  93. export CHUTNEY_WARNINGS_IGNORE_EXPECTED=false
  94. export CHUTNEY_WARNINGS_SUMMARY=false
  95. ;;
  96. # this skips warnings entirely
  97. --no-warnings)
  98. export CHUTNEY_WARNINGS_SKIP=true
  99. ;;
  100. # Expert options
  101. # Code Coverage Binary
  102. --coverage)
  103. export USE_COVERAGE_BINARY=true
  104. ;;
  105. # Do Nothing (but process arguments and set environmental variables)
  106. --dry-run)
  107. # process arguments, but don't call any other scripts
  108. export NETWORK_DRY_RUN=true
  109. ;;
  110. # The net directory, usually chutney/net
  111. --net-dir)
  112. export CHUTNEY_DATA_DIR="$2"
  113. shift
  114. ;;
  115. # Oops
  116. *)
  117. echo "$myname: Sorry, I don't know what to do with '$1'."
  118. echo "$UPDATE_YOUR_CHUTNEY"
  119. # continue processing arguments during a dry run
  120. if [ "$NETWORK_DRY_RUN" != true ]; then
  121. exit 2
  122. fi
  123. ;;
  124. esac
  125. shift
  126. done
  127. # optional: $TOR_DIR is the tor build directory
  128. # it's used to find the location of tor binaries
  129. # if it's not set:
  130. # - set it ro $BUILDDIR, or
  131. # - if $PWD looks like a tor build directory, set it to $PWD, or
  132. # - unset $TOR_DIR, and let chutney fall back to finding tor binaries in $PATH
  133. if [ ! -d "$TOR_DIR" ]; then
  134. if [ -d "$BUILDDIR/src/or" -a -d "$BUILDDIR/src/tools" ]; then
  135. # Choose the build directory
  136. # But only if it looks like one
  137. echo "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
  138. export TOR_DIR="$BUILDDIR"
  139. elif [ -d "$PWD/src/or" -a -d "$PWD/src/tools" ]; then
  140. # Guess the tor directory is the current directory
  141. # But only if it looks like one
  142. echo "$myname: \$TOR_DIR not set, trying \$PWD"
  143. export TOR_DIR="$PWD"
  144. elif [ -d "$PWD/../tor" -a -d "$PWD/../tor/src/or" -a \
  145. -d "$PWD/../tor/src/tools" ]; then
  146. # Guess the tor directory is next to the current directory
  147. # But only if it looks like one
  148. echo "$myname: \$TOR_DIR not set, trying \$PWD/../tor"
  149. export TOR_DIR="$PWD/../tor"
  150. else
  151. echo "$myname: no \$TOR_DIR, chutney will use \$PATH for tor binaries"
  152. unset TOR_DIR
  153. fi
  154. fi
  155. # make TOR_DIR absolute
  156. if [ -d "$PWD/$TOR_DIR" -a -d "$PWD/$TOR_DIR/src/or" -a \
  157. -d "$PWD/$TOR_DIR/src/tools" ]; then
  158. export TOR_DIR="$PWD/$TOR_DIR"
  159. fi
  160. # mandatory: $CHUTNEY_PATH is the path to the chutney launch script
  161. # if it's not set:
  162. # - if $PWD looks like a chutney directory, set it to $PWD, or
  163. # - set it based on $TOR_DIR, expecting chutney to be next to tor, or
  164. # - fail and tell the user how to clone the chutney repository
  165. if [ ! -d "$CHUTNEY_PATH" -o ! -x "$CHUTNEY_PATH/chutney" -o \
  166. ! -f "$CHUTNEY_PATH/chutney" ]; then
  167. if [ -x "$PWD/chutney" -a -f "$PWD/chutney" ]; then
  168. echo "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
  169. export CHUTNEY_PATH="$PWD"
  170. elif [ -d "`dirname \"$0\"`/.." -a \
  171. -x "`dirname \"$0\"`/../chutney" -a \
  172. -f "`dirname \"$0\"`/../chutney" ]; then
  173. echo "$myname: \$CHUTNEY_PATH not valid, using this script's location"
  174. export CHUTNEY_PATH="`dirname \"$0\"`/.."
  175. elif [ -d "$TOR_DIR" -a -d "$TOR_DIR/../chutney" -a \
  176. -x "$TOR_DIR/../chutney/chutney" -a \
  177. -f "$TOR_DIR/../chutney/chutney" ]; then
  178. echo "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
  179. export CHUTNEY_PATH="$TOR_DIR/../chutney"
  180. else
  181. # TODO: work out how to package and install chutney,
  182. # so users can find it in $PATH
  183. echo "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
  184. echo "$myname: Get chutney: git clone https://git.torproject.org/\
  185. chutney.git"
  186. echo "$myname: Set \$CHUTNEY_PATH to a non-standard location: export \
  187. CHUTNEY_PATH=\`pwd\`/chutney"
  188. unset CHUTNEY_PATH
  189. exit 1
  190. fi
  191. fi
  192. # make chutney path absolute
  193. if [ -d "$PWD/$CHUTNEY_PATH" -a -x "$PWD/$CHUTNEY_PATH/chutney" ]; then
  194. export CHUTNEY_PATH="$PWD/$CHUTNEY_PATH"
  195. fi
  196. # For picking up the right tor binaries
  197. # Choose coverage binaries, if selected
  198. tor_name=tor
  199. tor_gencert_name=tor-gencert
  200. if [ "$USE_COVERAGE_BINARY" = true ]; then
  201. tor_name=tor-cov
  202. fi
  203. # If $TOR_DIR isn't set, chutney looks for tor binaries by name or path
  204. # using $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, and then falls back to
  205. # looking for tor and tor-gencert in $PATH
  206. if [ -d "$TOR_DIR" ]; then
  207. # TOR_DIR is absolute, so these are absolute paths
  208. export CHUTNEY_TOR="${TOR_DIR}/src/or/${tor_name}"
  209. export CHUTNEY_TOR_GENCERT="${TOR_DIR}/src/tools/${tor_gencert_name}"
  210. else
  211. # these are binary names, they will be searched for in $PATH
  212. export CHUTNEY_TOR="${tor_name}"
  213. export CHUTNEY_TOR_GENCERT="${tor_gencert_name}"
  214. fi
  215. # Set the variables for the chutney network flavour
  216. export NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs"}
  217. export CHUTNEY_NETWORK="$CHUTNEY_PATH/networks/$NETWORK_FLAVOUR"
  218. # And finish up if we're doing a dry run
  219. if [ "$NETWORK_DRY_RUN" = true ]; then
  220. # we can't exit here, it breaks argument processing
  221. # this only works in bash: return semantics are shell-specific
  222. return 2>/dev/null || exit
  223. fi
  224. "$CHUTNEY_PATH/tools/bootstrap-network.sh" "$NETWORK_FLAVOUR" || exit 2
  225. # chutney starts verifying after 20 seconds, keeps on trying for 60 seconds,
  226. # and then stops immediately (by default)
  227. # Even the fastest chutney networks take 5-10 seconds for their first consensus
  228. # and then 10 seconds after that for relays to bootstrap and upload descriptors
  229. export CHUTNEY_START_TIME=${CHUTNEY_START_TIME:-20}
  230. export CHUTNEY_BOOTSTRAP_TIME=${CHUTNEY_BOOTSTRAP_TIME:-60}
  231. export CHUTNEY_STOP_TIME=${CHUTNEY_STOP_TIME:-0}
  232. CHUTNEY="$CHUTNEY_PATH/chutney"
  233. if [ "$CHUTNEY_WARNINGS_SKIP" = true ]; then
  234. WARNINGS=true
  235. else
  236. WARNINGS="$CHUTNEY_PATH/tools/warnings.sh"
  237. fi
  238. if [ "$CHUTNEY_START_TIME" -ge 0 ]; then
  239. echo "Waiting ${CHUTNEY_START_TIME} seconds for a consensus containing relays to be generated..."
  240. sleep "$CHUTNEY_START_TIME"
  241. else
  242. echo "Chutney network launched and running. To stop the network, use:"
  243. echo "$CHUTNEY stop $CHUTNEY_NETWORK"
  244. "$WARNINGS"
  245. exit 0
  246. fi
  247. if [ "$CHUTNEY_BOOTSTRAP_TIME" -ge 0 ]; then
  248. # Chutney will try to verify for $CHUTNEY_BOOTSTRAP_TIME seconds
  249. "$CHUTNEY" verify "$CHUTNEY_NETWORK"
  250. VERIFY_EXIT_STATUS="$?"
  251. else
  252. echo "Chutney network ready and running. To stop the network, use:"
  253. echo "$CHUTNEY" stop "$CHUTNEY_NETWORK"
  254. "$WARNINGS"
  255. exit 0
  256. fi
  257. if [ "$CHUTNEY_STOP_TIME" -ge 0 ]; then
  258. if [ "$CHUTNEY_STOP_TIME" -gt 0 ]; then
  259. echo "Waiting ${CHUTNEY_STOP_TIME} seconds before stopping the network..."
  260. fi
  261. sleep "$CHUTNEY_STOP_TIME"
  262. # work around a bug/feature in make -j2 (or more)
  263. # where make hangs if any child processes are still alive
  264. "$CHUTNEY" stop "$CHUTNEY_NETWORK"
  265. "$WARNINGS"
  266. exit "$VERIFY_EXIT_STATUS"
  267. else
  268. echo "Chutney network verified and running. To stop the network, use:"
  269. echo "$CHUTNEY stop $CHUTNEY_NETWORK"
  270. "$WARNINGS"
  271. exit 0
  272. fi