test-network.sh 10 KB

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