test-network.sh 10.0 KB

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