test-network.sh 10 KB

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