test-network.sh 11 KB

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