test-network.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #
  181. # Find the Tor build dir using the src/tools dir
  182. if [ ! -d "$TOR_DIR" ]; then
  183. if [ -d "$BUILDDIR/src/tools" ]; then
  184. # Choose the build directory
  185. # But only if it looks like one
  186. $ECHO "$myname: \$TOR_DIR not set, trying \$BUILDDIR"
  187. export TOR_DIR="$BUILDDIR"
  188. elif [ -d "$PWD/src/tools" ]; then
  189. # Guess the tor directory is the current directory
  190. # But only if it looks like one
  191. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD"
  192. export TOR_DIR="$PWD"
  193. elif [ -d "$PWD/../tor" -a -d "$PWD/../tor/src/tools" ]; then
  194. # Guess the tor directory is next to the current directory
  195. # But only if it looks like one
  196. $ECHO "$myname: \$TOR_DIR not set, trying \$PWD/../tor"
  197. export TOR_DIR="$PWD/../tor"
  198. else
  199. $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"
  200. unset TOR_DIR
  201. fi
  202. fi
  203. # Now find the name of the Tor app dir, which changed in Tor 0.3.5
  204. if [ -d "$TOR_DIR" ]; then
  205. if [ -d "$TOR_DIR/src/app" -a -d "$TOR_DIR/src/or" ]; then
  206. $ECHO "$myname: \$TOR_DIR has a Tor 0.3.5 or later build directory, and a Tor 0.3.4 or earlier build directory"
  207. $ECHO "$myname: Please remove $TOR_DIR/src/app or $TOR_DIR/src/or, or set \$CHUTNEY_TOR"
  208. exit 1
  209. elif [ -d "$TOR_DIR/src/app" ]; then
  210. $ECHO "$myname: \$TOR_DIR is a Tor 0.3.5 or later build directory"
  211. TOR_APP_DIR="$TOR_DIR/src/app"
  212. elif [ -d "$TOR_DIR/src/or" ]; then
  213. $ECHO "$myname: \$TOR_DIR is a Tor 0.3.4 or earlier build directory"
  214. TOR_APP_DIR="$TOR_DIR/src/or"
  215. else
  216. $ECHO "$myname: \$TOR_DIR has no src/app or src/or, chutney will use \$CHUTNEY_TOR and \$CHUTNEY_TOR_GENCERT as tor binary paths, or search \$PATH for tor binary names"
  217. unset TOR_DIR
  218. fi
  219. fi
  220. # make TOR_DIR and TOR_APP_DIR absolute
  221. if [ -d "$PWD/$TOR_DIR" -a -d "$PWD/$TOR_APP_DIR" -a \
  222. -d "$PWD/$TOR_DIR/src/tools" ]; then
  223. export TOR_DIR="$PWD/$TOR_DIR"
  224. export TOR_APP_DIR="$PWD/$TOR_APP_DIR"
  225. fi
  226. # mandatory: $CHUTNEY_PATH is the path to the chutney launch script
  227. # if it's not set:
  228. # - if $PWD looks like a chutney directory, set it to $PWD, or
  229. # - set it based on $TOR_DIR, expecting chutney to be next to tor, or
  230. # - fail and tell the user how to clone the chutney repository
  231. if [ ! -d "$CHUTNEY_PATH" -o ! -x "$CHUTNEY_PATH/chutney" -o \
  232. ! -f "$CHUTNEY_PATH/chutney" ]; then
  233. if [ -x "$PWD/chutney" -a -f "$PWD/chutney" ]; then
  234. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$PWD"
  235. export CHUTNEY_PATH="$PWD"
  236. elif [ -d "`dirname \"$0\"`/.." -a \
  237. -x "`dirname \"$0\"`/../chutney" -a \
  238. -f "`dirname \"$0\"`/../chutney" ]; then
  239. $ECHO "$myname: \$CHUTNEY_PATH not valid, using this script's location"
  240. export CHUTNEY_PATH="`dirname \"$0\"`/.."
  241. elif [ -d "$TOR_DIR" -a -d "$TOR_DIR/../chutney" -a \
  242. -x "$TOR_DIR/../chutney/chutney" -a \
  243. -f "$TOR_DIR/../chutney/chutney" ]; then
  244. $ECHO "$myname: \$CHUTNEY_PATH not valid, trying \$TOR_DIR/../chutney"
  245. export CHUTNEY_PATH="$TOR_DIR/../chutney"
  246. else
  247. # TODO: work out how to package and install chutney,
  248. # so users can find it in $PATH
  249. $ECHO "$myname: missing 'chutney' in \$CHUTNEY_PATH ($CHUTNEY_PATH)"
  250. $ECHO "$myname: Get chutney: git clone https://git.torproject.org/\
  251. chutney.git"
  252. $ECHO "$myname: Set \$CHUTNEY_PATH to a non-standard location: export \
  253. CHUTNEY_PATH=\`pwd\`/chutney"
  254. unset CHUTNEY_PATH
  255. exit 1
  256. fi
  257. fi
  258. # make chutney path absolute
  259. if [ -d "$PWD/$CHUTNEY_PATH" -a -x "$PWD/$CHUTNEY_PATH/chutney" ]; then
  260. export CHUTNEY_PATH="$PWD/$CHUTNEY_PATH"
  261. fi
  262. # For picking up the right tor binaries
  263. # Choose coverage binaries, if selected
  264. tor_name=tor
  265. tor_gencert_name=tor-gencert
  266. if [ "$USE_COVERAGE_BINARY" = true ]; then
  267. tor_name=tor-cov
  268. fi
  269. # If $TOR_DIR isn't set, chutney looks for tor binaries by name or path
  270. # using $CHUTNEY_TOR and $CHUTNEY_TOR_GENCERT, and then falls back to
  271. # looking for tor and tor-gencert in $PATH
  272. if [ -d "$TOR_DIR" ]; then
  273. $ECHO "$myname: Setting \$CHUTNEY_TOR and \$CHUTNEY_TOR_GENCERT based on TOR_DIR: '$TOR_DIR'"
  274. # TOR_DIR is absolute, so these are absolute paths
  275. export CHUTNEY_TOR="$TOR_APP_DIR/$tor_name"
  276. export CHUTNEY_TOR_GENCERT="$TOR_DIR/src/tools/$tor_gencert_name"
  277. else
  278. if [ -x "$CHUTNEY_TOR" ]; then
  279. $ECHO "$myname: Assuming \$CHUTNEY_TOR is a path to a binary"
  280. elif [ ! -z "$CHUTNEY_TOR" ]; then
  281. $ECHO "$myname: Assuming \$CHUTNEY_TOR is a binary name in PATH"
  282. else
  283. $ECHO "$myname: Setting \$CHUTNEY_TOR to the standard binary name in PATH"
  284. export CHUTNEY_TOR="$tor_name"
  285. fi
  286. if [ -x "$CHUTNEY_TOR_GENCERT" ]; then
  287. $ECHO "$myname: Assuming \$CHUTNEY_TOR_GENCERT is a path to a binary"
  288. elif [ ! -z "$CHUTNEY_TOR_GENCERT" ]; then
  289. $ECHO "$myname: Assuming \$CHUTNEY_TOR_GENCERT is a binary name in PATH"
  290. else
  291. $ECHO "$myname: Setting \$CHUTNEY_TOR_GENCERT to the standard binary name in PATH"
  292. export CHUTNEY_TOR_GENCERT="$tor_gencert_name"
  293. fi
  294. fi
  295. $ECHO "$myname: Using \$CHUTNEY_TOR: '$CHUTNEY_TOR' and \$CHUTNEY_TOR_GENCERT: '$CHUTNEY_TOR_GENCERT'"
  296. # Set the variables for the chutney network flavour
  297. export NETWORK_FLAVOUR=${NETWORK_FLAVOUR:-"bridges+hs-v2"}
  298. export CHUTNEY_NETWORK="$CHUTNEY_PATH/networks/$NETWORK_FLAVOUR"
  299. if [ "$CHUTNEY_WARNINGS_SKIP" = true ]; then
  300. WARNINGS=true
  301. else
  302. WARNINGS="$CHUTNEY_PATH/tools/warnings.sh"
  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. # This breaks sourcing this script: that is intentional, as the previous
  310. # behaviour only worked with bash as /bin/sh
  311. exit
  312. fi
  313. "$CHUTNEY_PATH/tools/bootstrap-network.sh" "$NETWORK_FLAVOUR" || exit 3
  314. # chutney starts verifying after 20 seconds, keeps on trying for 60 seconds,
  315. # and then stops immediately (by default)
  316. # Even the fastest chutney networks take 5-10 seconds for their first consensus
  317. # and then 10 seconds after that for relays to bootstrap and upload descriptors
  318. export CHUTNEY_START_TIME=${CHUTNEY_START_TIME:-20}
  319. export CHUTNEY_BOOTSTRAP_TIME=${CHUTNEY_BOOTSTRAP_TIME:-60}
  320. export CHUTNEY_STOP_TIME=${CHUTNEY_STOP_TIME:-0}
  321. CHUTNEY="$CHUTNEY_PATH/chutney"
  322. if [ "$CHUTNEY_START_TIME" -ge 0 ]; then
  323. $ECHO "Waiting $CHUTNEY_START_TIME seconds for a consensus containing relays to be generated..."
  324. sleep "$CHUTNEY_START_TIME"
  325. else
  326. $ECHO "Chutney network launched and running. To stop the network, use:"
  327. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  328. "$WARNINGS"
  329. exit 0
  330. fi
  331. if [ "$CHUTNEY_BOOTSTRAP_TIME" -ge 0 ]; then
  332. # Chutney will try to verify for $CHUTNEY_BOOTSTRAP_TIME seconds each round
  333. n_rounds=0
  334. VERIFY_EXIT_STATUS=0
  335. # Run CHUTNEY_ROUNDS verification rounds
  336. $ECHO "Running $CHUTNEY_ROUNDS verify rounds..."
  337. while [ "$CHUTNEY_ROUNDS" -gt "$n_rounds" \
  338. -a "$VERIFY_EXIT_STATUS" -eq 0 ]; do
  339. "$CHUTNEY" verify "$CHUTNEY_NETWORK"
  340. VERIFY_EXIT_STATUS="$?"
  341. n_rounds=$((n_rounds+1))
  342. done
  343. $ECHO "Completed $n_rounds of $CHUTNEY_ROUNDS verify rounds."
  344. else
  345. $ECHO "Chutney network ready and running. To stop the network, use:"
  346. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  347. "$WARNINGS"
  348. exit 0
  349. fi
  350. if [ "$CHUTNEY_STOP_TIME" -ge 0 ]; then
  351. if [ "$CHUTNEY_STOP_TIME" -gt 0 ]; then
  352. $ECHO "Waiting $CHUTNEY_STOP_TIME seconds before stopping the network..."
  353. fi
  354. sleep "$CHUTNEY_STOP_TIME"
  355. # work around a bug/feature in make -j2 (or more)
  356. # where make hangs if any child processes are still alive
  357. "$CHUTNEY" stop "$CHUTNEY_NETWORK"
  358. # Give tor time to exit gracefully
  359. sleep 3
  360. "$WARNINGS"
  361. exit "$VERIFY_EXIT_STATUS"
  362. else
  363. $ECHO "Chutney network verified and running. To stop the network, use:"
  364. $ECHO "$CHUTNEY stop $CHUTNEY_NETWORK"
  365. "$WARNINGS"
  366. exit 0
  367. fi