test-network.sh 13 KB

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