test-network.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 CHUTNEY_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
  58. # same 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
  69. # tor 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
  103. # IPv6 address
  104. --ipv6|--v6|-6)
  105. export CHUTNEY_LISTEN_ADDRESS_V6="$2"
  106. shift
  107. ;;
  108. # The DNS server config for Tor Exits. Chutney's default is
  109. # /etc/resolv.conf, even if tor's compile time default is different.
  110. --dns-conf)
  111. export CHUTNEY_DNS_CONF="$2"
  112. shift
  113. ;;
  114. # Do not make any DNS queries. This is incompatible with external
  115. # controllers that use SETCONF.
  116. --offline)
  117. export CHUTNEY_DNS_CONF="/dev/null"
  118. ;;
  119. # Use tor's compile-time default for ServerDNSResolvConfFile.
  120. --dns-conf-default)
  121. export CHUTNEY_DNS_CONF=""
  122. ;;
  123. # Warning Options
  124. # we summarise unexpected warnings by default
  125. # this shows all warnings per-node
  126. --all-warnings)
  127. export CHUTNEY_WARNINGS_IGNORE_EXPECTED=false
  128. export CHUTNEY_WARNINGS_SUMMARY=false
  129. ;;
  130. # this doesn't run chutney, and only logs warnings
  131. --only-warnings)
  132. export CHUTNEY_WARNINGS_ONLY=true
  133. ;;
  134. # this skips warnings entirely
  135. --no-warnings)
  136. export CHUTNEY_WARNINGS_SKIP=true
  137. ;;
  138. # Expert options
  139. # Code Coverage Binary
  140. --coverage)
  141. export USE_COVERAGE_BINARY=true
  142. ;;
  143. # Do Nothing (but process arguments and set environmental variables)
  144. --dry-run)
  145. # process arguments, but don't call any other scripts
  146. export NETWORK_DRY_RUN=true
  147. ;;
  148. # The net directory, usually chutney/net
  149. --net-dir)
  150. export CHUTNEY_DATA_DIR="$2"
  151. shift
  152. ;;
  153. # Try not to say anything (applies only to this script)
  154. --quiet)
  155. export ECHO=true
  156. ;;
  157. # Oops
  158. *)
  159. $ECHO "$myname: Sorry, I don't know what to do with '$1'."
  160. $ECHO "$UPDATE_YOUR_CHUTNEY"
  161. # continue processing arguments during a dry run
  162. if [ "$NETWORK_DRY_RUN" != true ]; then
  163. exit 1
  164. fi
  165. ;;
  166. esac
  167. shift
  168. done
  169. # If the DNS server doesn't work, tor exits may reject all exit traffic, and
  170. # chutney may fail
  171. if [ "$CHUTNEY_WARNINGS_ONLY" != true ]; then
  172. $ECHO "$myname: using CHUTNEY_DNS_CONF '$CHUTNEY_DNS_CONF'"
  173. fi
  174. # optional: $TOR_DIR is the tor build directory
  175. # it's used to find the location of tor binaries
  176. # if it's not set:
  177. # - set it to $BUILDDIR, or
  178. # - if $PWD looks like a tor build directory, set it to $PWD, or
  179. # - unset $TOR_DIR, and let chutney fall back to finding tor binaries in
  180. # $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, or $PATH
  181. #
  182. # Find the Tor build dir using the src/tools dir
  183. if [ ! -d "$TOR_DIR" ]; then
  184. if [ -d "$BUILDDIR/src/tools" ]; then
  185. # Choose the build directory
  186. # But only if it looks like one
  187. $ECHO "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
  188. export TOR_DIR="$BUILDDIR"
  189. elif [ -d "$PWD/src/tools" ]; then
  190. # Guess the tor directory is the current directory
  191. # But only if it looks like one
  192. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD"
  193. export TOR_DIR="$PWD"
  194. elif [ -d "$PWD/../tor" -a -d "$PWD/../tor/src/tools" ]; then
  195. # Guess the tor directory is next to the current directory
  196. # But only if it looks like one
  197. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD/../tor"
  198. export TOR_DIR="$PWD/../tor"
  199. else
  200. $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"
  201. unset TOR_DIR
  202. fi
  203. fi
  204. # Now find the name of the Tor app dir, which changed in Tor 0.3.5
  205. if [ -d "$TOR_DIR" ]; then
  206. if [ -d "$TOR_DIR/src/app" -a -d "$TOR_DIR/src/or" ]; then
  207. $ECHO "$myname: \$TOR_DIR has a Tor 0.3.5 or later build directory, and a Tor 0.3.4 or earlier build directory"
  208. $ECHO "$myname: Please remove $TOR_DIR/src/app or $TOR_DIR/src/or, or set \$CHUTNEY_TOR"
  209. exit 1
  210. elif [ -d "$TOR_DIR/src/app" ]; then
  211. $ECHO "$myname: \$TOR_DIR is a Tor 0.3.5 or later build directory"
  212. TOR_APP_DIR="$TOR_DIR/src/app"
  213. elif [ -d "$TOR_DIR/src/or" ]; then
  214. $ECHO "$myname: \$TOR_DIR is a Tor 0.3.4 or earlier build directory"
  215. TOR_APP_DIR="$TOR_DIR/src/or"
  216. else
  217. $ECHO "$myname: \$TOR_DIR has no src/app or src/or, looking elsewhere"
  218. unset TOR_DIR
  219. fi
  220. fi
  221. # make TOR_DIR and TOR_APP_DIR absolute
  222. if [ -d "$PWD/$TOR_DIR" -a -d "$PWD/$TOR_APP_DIR" -a \
  223. -d "$PWD/$TOR_DIR/src/tools" ]; then
  224. export TOR_DIR="$PWD/$TOR_DIR"
  225. export TOR_APP_DIR="$PWD/$TOR_APP_DIR"
  226. fi
  227. # mandatory: $CHUTNEY_PATH is the path to the chutney launch script
  228. # if it's not set:
  229. # - if $PWD looks like a chutney directory, set it to $PWD, or
  230. # - set it based on $TOR_DIR, expecting chutney to be next to tor, or
  231. # - fail and tell the user how to clone the chutney repository
  232. if [ ! -d "$CHUTNEY_PATH" -o ! -x "$CHUTNEY_PATH/chutney" -o \
  233. ! -f "$CHUTNEY_PATH/chutney" ]; then
  234. if [ -x "$PWD/chutney" -a -f "$PWD/chutney" ]; then
  235. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
  236. export CHUTNEY_PATH="$PWD"
  237. elif [ -d "`dirname \"$0\"`/.." -a \
  238. -x "`dirname \"$0\"`/../chutney" -a \
  239. -f "`dirname \"$0\"`/../chutney" ]; then
  240. $ECHO "$myname: \$CHUTNEY_PATH not valid, using this script's location"
  241. export CHUTNEY_PATH="`dirname \"$0\"`/.."
  242. elif [ -d "$TOR_DIR" -a -d "$TOR_DIR/../chutney" -a \
  243. -x "$TOR_DIR/../chutney/chutney" -a \
  244. -f "$TOR_DIR/../chutney/chutney" ]; then
  245. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
  246. export CHUTNEY_PATH="$TOR_DIR/../chutney"
  247. else
  248. # TODO: work out how to package and install chutney,
  249. # so users can find it in $PATH
  250. $ECHO "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
  251. $ECHO "$myname: Get chutney: git clone https://git.torproject.org/chutney.git"
  252. $ECHO "$myname: Set \$CHUTNEY_PATH to a non-standard location: export CHUTNEY_PATH=\`pwd\`/chutney"
  253. unset CHUTNEY_PATH
  254. exit 1
  255. fi
  256. fi
  257. # make chutney path absolute
  258. if [ -d "$PWD/$CHUTNEY_PATH" -a -x "$PWD/$CHUTNEY_PATH/chutney" ]; then
  259. export CHUTNEY_PATH="$PWD/$CHUTNEY_PATH"
  260. fi
  261. # For picking up the right tor binaries
  262. # Choose coverage binaries, if selected
  263. tor_name=tor
  264. tor_gencert_name=tor-gencert
  265. if [ "$USE_COVERAGE_BINARY" = true ]; then
  266. tor_name=tor-cov
  267. fi
  268. # If $TOR_DIR isn't set, chutney looks for tor binaries by name or path
  269. # using $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, and then falls back to
  270. # looking for tor and tor-gencert in $PATH
  271. if [ -d "$TOR_DIR" ]; then
  272. $ECHO "$myname: Setting \$CHUTNEY_TOR and \$CHUTNEY_TOR_GENCERT based on TOR_DIR: '$TOR_DIR'"
  273. # TOR_DIR is absolute, so these are absolute paths
  274. export CHUTNEY_TOR="$TOR_APP_DIR/$tor_name"
  275. export CHUTNEY_TOR_GENCERT="$TOR_DIR/src/tools/$tor_gencert_name"
  276. else
  277. if [ -x "$CHUTNEY_TOR" ]; then
  278. $ECHO "$myname: Assuming \$CHUTNEY_TOR is a path to a binary"
  279. elif [ ! -z "$CHUTNEY_TOR" ]; then
  280. $ECHO "$myname: Assuming \$CHUTNEY_TOR is a binary name in \$PATH"
  281. else
  282. $ECHO "$myname: Setting \$CHUTNEY_TOR to the standard binary name in \$PATH"
  283. export CHUTNEY_TOR="$tor_name"
  284. fi
  285. if [ -x "$CHUTNEY_TOR_GENCERT" ]; then
  286. $ECHO "$myname: Assuming \$CHUTNEY_TOR_GENCERT is a path to a binary"
  287. elif [ ! -z "$CHUTNEY_TOR_GENCERT" ]; then
  288. $ECHO "$myname: Assuming \$CHUTNEY_TOR_GENCERT is a binary name in \$PATH"
  289. else
  290. $ECHO "$myname: Setting \$CHUTNEY_TOR_GENCERT to the standard binary name in \$PATH"
  291. export CHUTNEY_TOR_GENCERT="$tor_gencert_name"
  292. fi
  293. fi
  294. $ECHO "$myname: Using \$CHUTNEY_TOR: '$CHUTNEY_TOR' and \$CHUTNEY_TOR_GENCERT: '$CHUTNEY_TOR_GENCERT'"
  295. # Set the variables for the chutney network flavour
  296. export NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs-v2"}
  297. export CHUTNEY_NETWORK="$CHUTNEY_PATH/networks/$NETWORK_FLAVOUR"
  298. WARNING_COMMAND="$CHUTNEY_PATH/tools/warnings.sh"
  299. if [ "$CHUTNEY_WARNINGS_SKIP" = true ]; then
  300. WARNINGS=true
  301. else
  302. WARNINGS="$WARNING_COMMAND"
  303. fi
  304. # And finish up if we're doing a dry run
  305. if [ "$NETWORK_DRY_RUN" = true -o "$CHUTNEY_WARNINGS_ONLY" = true ]; then
  306. if [ "$CHUTNEY_WARNINGS_ONLY" = true ]; then
  307. "$WARNINGS"
  308. fi
  309. $ECHO "Finished dry run"
  310. # This breaks sourcing this script: that is intentional, as the previous
  311. # behaviour only worked with bash as /bin/sh
  312. exit 0
  313. fi
  314. if ! "$CHUTNEY_PATH/tools/bootstrap-network.sh" "$NETWORK_FLAVOUR"; then
  315. CHUTNEY_WARNINGS_IGNORE_EXPECTED=false CHUTNEY_WARNINGS_SUMMARY=false \
  316. "$WARNING_COMMAND"
  317. "$WARNINGS"
  318. $ECHO "bootstrap-network.sh failed"
  319. exit 1
  320. fi
  321. # chutney starts verifying after 20 seconds, keeps on trying for 60 seconds,
  322. # and then stops immediately (by default)
  323. # Even the fastest chutney networks take 5-10 seconds for their first consensus
  324. # and then 10 seconds after that for relays to bootstrap and upload descriptors
  325. export CHUTNEY_START_TIME=${CHUTNEY_START_TIME:-40}
  326. export CHUTNEY_BOOTSTRAP_TIME=${CHUTNEY_BOOTSTRAP_TIME:-60}
  327. export CHUTNEY_STOP_TIME=${CHUTNEY_STOP_TIME:-0}
  328. CHUTNEY="$CHUTNEY_PATH/chutney"
  329. if [ "$CHUTNEY_START_TIME" -ge 0 ]; then
  330. $ECHO "Waiting $CHUTNEY_START_TIME seconds for a consensus containing relays to be generated..."
  331. sleep "$CHUTNEY_START_TIME"
  332. else
  333. $ECHO "Chutney network launched and running. To stop the network, use:"
  334. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  335. "$WARNINGS"
  336. exit 0
  337. fi
  338. if [ "$CHUTNEY_BOOTSTRAP_TIME" -ge 0 ]; then
  339. # Chutney will try to verify for $CHUTNEY_BOOTSTRAP_TIME seconds each round
  340. n_rounds=0
  341. # Run CHUTNEY_ROUNDS verification rounds
  342. $ECHO "Running $CHUTNEY_ROUNDS verify rounds..."
  343. while [ "$n_rounds" -lt "$CHUTNEY_ROUNDS" ]; do
  344. n_rounds=$((n_rounds+1))
  345. if ! "$CHUTNEY" verify "$CHUTNEY_NETWORK"; then
  346. CHUTNEY_WARNINGS_IGNORE_EXPECTED=false \
  347. CHUTNEY_WARNINGS_SUMMARY=false \
  348. "$WARNING_COMMAND"
  349. "$WARNINGS"
  350. $ECHO "chutney verify $n_rounds/$CHUTNEY_ROUNDS failed"
  351. exit 1
  352. fi
  353. $ECHO "Completed $n_rounds/$CHUTNEY_ROUNDS verify rounds."
  354. done
  355. else
  356. $ECHO "Chutney network ready and running. To stop the network, use:"
  357. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  358. "$WARNINGS"
  359. exit 0
  360. fi
  361. if [ "$CHUTNEY_STOP_TIME" -ge 0 ]; then
  362. if [ "$CHUTNEY_STOP_TIME" -gt 0 ]; then
  363. $ECHO "Waiting $CHUTNEY_STOP_TIME seconds before stopping the network..."
  364. fi
  365. sleep "$CHUTNEY_STOP_TIME"
  366. # work around a bug/feature in make -j2 (or more)
  367. # where make hangs if any child processes are still alive
  368. if ! "$CHUTNEY" stop "$CHUTNEY_NETWORK"; then
  369. CHUTNEY_WARNINGS_IGNORE_EXPECTED=false CHUTNEY_WARNINGS_SUMMARY=false \
  370. "$WARNING_COMMAND"
  371. "$WARNINGS"
  372. $ECHO "chutney stop failed"
  373. exit 1
  374. fi
  375. # Give tor time to exit gracefully
  376. sleep 3
  377. else
  378. $ECHO "Chutney network verified and running. To stop the network, use:"
  379. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  380. "$WARNINGS"
  381. exit 0
  382. fi
  383. "$WARNINGS"
  384. exit 0