test-network.sh 11 KB

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