validate-linux.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #!/bin/bash
  2. # This test script joins Earth and pokes some stuff
  3. TEST_NETWORK=8056c2e21c000001
  4. RUN_LENGTH=30
  5. TEST_FINISHED=false
  6. ZTO_VER=$(git describe --tags $(git rev-list --tags --max-count=1))
  7. ZTO_COMMIT=$(git rev-parse HEAD)
  8. ZTO_COMMIT_SHORT=$(git rev-parse --short HEAD)
  9. TEST_DIR_PREFIX="$ZTO_VER-$ZTO_COMMIT_SHORT-test-results"
  10. TEST_OK=0
  11. TEST_FAIL=1
  12. echo "Performing test on: $ZTO_VER-$ZTO_COMMIT_SHORT"
  13. TEST_FILEPATH_PREFIX="$TEST_DIR_PREFIX/$ZTO_COMMIT_SHORT"
  14. mkdir $TEST_DIR_PREFIX
  15. # How long we will wait for ZT to come online before considering it a failure
  16. MAX_WAIT_SECS=30
  17. ################################################################################
  18. # Multi-node connectivity and performance test #
  19. ################################################################################
  20. test() {
  21. echo -e "\nPerforming pre-flight checks"
  22. check_exit_on_invalid_identity
  23. echo -e "\nRunning test for $RUN_LENGTH seconds"
  24. export NS1="ip netns exec ns1"
  25. export NS2="ip netns exec ns2"
  26. export ZT1="$NS1 ./zerotier-cli -p9996 -D$(pwd)/node1"
  27. # Specify custom port on one node to ensure that feature works
  28. export ZT2="$NS2 ./zerotier-cli -p9997 -D$(pwd)/node2"
  29. echo -e "\nSetting up network namespaces..."
  30. echo "Setting up ns1"
  31. ip netns add ns1
  32. $NS1 ip link set dev lo up
  33. ip link add veth0 type veth peer name veth1
  34. ip link set veth1 netns ns1
  35. ip addr add 192.168.0.1/24 dev veth0
  36. ip link set dev veth0 up
  37. $NS1 ip addr add 192.168.0.2/24 dev veth1
  38. $NS1 ip link set dev veth1 up
  39. # Add default route
  40. $NS1 ip route add default via 192.168.0.1
  41. iptables -t nat -A POSTROUTING -s 192.168.0.0/255.255.255.0 \
  42. -o eth0 -j MASQUERADE
  43. iptables -A FORWARD -i eth0 -o veth0 -j ACCEPT
  44. iptables -A FORWARD -o eth0 -i veth0 -j ACCEPT
  45. echo "Setting up ns2"
  46. ip netns add ns2
  47. $NS2 ip link set dev lo up
  48. ip link add veth2 type veth peer name veth3
  49. ip link set veth3 netns ns2
  50. ip addr add 192.168.1.1/24 dev veth2
  51. ip link set dev veth2 up
  52. $NS2 ip addr add 192.168.1.2/24 dev veth3
  53. $NS2 ip link set dev veth3 up
  54. $NS2 ip route add default via 192.168.1.1
  55. iptables -t nat -A POSTROUTING -s 192.168.1.0/255.255.255.0 \
  56. -o eth0 -j MASQUERADE
  57. iptables -A FORWARD -i eth0 -o veth2 -j ACCEPT
  58. iptables -A FORWARD -o eth0 -i veth2 -j ACCEPT
  59. # Allow forwarding
  60. sysctl -w net.ipv4.ip_forward=1
  61. ################################################################################
  62. # Memory Leak Check #
  63. ################################################################################
  64. export FILENAME_MEMORY_LOG="$TEST_FILEPATH_PREFIX-memory.log"
  65. echo -e "\nStarting a ZeroTier instance in each namespace..."
  66. export time_test_start=$(date +%s)
  67. # Spam the CLI as ZeroTier is starting
  68. spam_cli 100
  69. echo "Starting memory leak check"
  70. $NS1 sudo valgrind --demangle=yes --exit-on-first-error=yes \
  71. --error-exitcode=1 \
  72. --xml=yes \
  73. --xml-file=$FILENAME_MEMORY_LOG \
  74. --leak-check=full \
  75. ./zerotier-one node1 -p9996 -U >>node_1.log 2>&1 &
  76. # Second instance, not run in memory profiler
  77. # Don't set up internet access until _after_ zerotier is running
  78. # This has been a source of stuckness in the past.
  79. $NS2 ip addr del 192.168.1.2/24 dev veth3
  80. $NS2 sudo ./zerotier-one node2 -U -p9997 >>node_2.log 2>&1 &
  81. sleep 1;
  82. $NS2 ip addr add 192.168.1.2/24 dev veth3
  83. $NS2 ip route add default via 192.168.1.1
  84. echo -e "\nPing from host to namespaces"
  85. ping -c 3 192.168.0.1
  86. ping -c 3 192.168.1.1
  87. echo -e "\nPing from namespace to host"
  88. $NS1 ping -c 3 192.168.0.1
  89. $NS1 ping -c 3 192.168.0.1
  90. $NS2 ping -c 3 192.168.0.2
  91. $NS2 ping -c 3 192.168.0.2
  92. echo -e "\nPing from ns1 to ns2"
  93. $NS1 ping -c 3 192.168.0.1
  94. echo -e "\nPing from ns2 to ns1"
  95. $NS2 ping -c 3 192.168.0.1
  96. ################################################################################
  97. # Online Check #
  98. ################################################################################
  99. echo "Waiting for ZeroTier to come online before attempting test..."
  100. node1_online=false
  101. node2_online=false
  102. both_instances_online=false
  103. time_zt_node1_start=$(date +%s)
  104. time_zt_node2_start=$(date +%s)
  105. for ((s = 0; s <= $MAX_WAIT_SECS; s++)); do
  106. node1_online="$($ZT1 -j info | jq '.online' 2>/dev/null)"
  107. node2_online="$($ZT2 -j info | jq '.online' 2>/dev/null)"
  108. echo "Checking for online status: try #$s, node1:$node1_online, node2:$node2_online"
  109. if [[ "$node2_online" == "true" && "$node1_online" == "true" ]]; then
  110. export both_instances_online=true
  111. export time_to_both_nodes_online=$(date +%s)
  112. break
  113. fi
  114. sleep 1
  115. done
  116. echo -e "\n\nContents of ZeroTier home paths:"
  117. ls -lga node1
  118. tree node1
  119. ls -lga node2
  120. tree node2
  121. echo -e "\n\nRunning ZeroTier processes:"
  122. echo -e "\nNode 1:\n"
  123. $NS1 ps aux | grep zerotier-one
  124. echo -e "\nNode 2:\n"
  125. $NS2 ps aux | grep zerotier-one
  126. echo -e "\n\nStatus of each instance:"
  127. echo -e "\n\nNode 1:\n"
  128. $ZT1 status
  129. echo -e "\n\nNode 2:\n"
  130. $ZT2 status
  131. if [[ "$both_instances_online" != "true" ]]; then
  132. exit_test_and_generate_report $TEST_FAIL "one or more nodes failed to come online"
  133. fi
  134. echo -e "\nJoining networks"
  135. $ZT1 join $TEST_NETWORK
  136. $ZT2 join $TEST_NETWORK
  137. sleep 10
  138. node1_ip4=$($ZT1 get $TEST_NETWORK ip4)
  139. node2_ip4=$($ZT2 get $TEST_NETWORK ip4)
  140. echo "node1_ip4=$node1_ip4"
  141. echo "node2_ip4=$node2_ip4"
  142. echo -e "\nPinging each node"
  143. PING12_FILENAME="$TEST_FILEPATH_PREFIX-ping-1-to-2.txt"
  144. PING21_FILENAME="$TEST_FILEPATH_PREFIX-ping-2-to-1.txt"
  145. $NS1 ping -c 16 $node2_ip4 >$PING12_FILENAME
  146. $NS2 ping -c 16 $node1_ip4 >$PING21_FILENAME
  147. ping_loss_percent_1_to_2=$(cat $PING12_FILENAME |
  148. grep "packet loss" | awk '{print $6}' | sed 's/%//')
  149. ping_loss_percent_2_to_1=$(cat $PING21_FILENAME |
  150. grep "packet loss" | awk '{print $6}' | sed 's/%//')
  151. # Normalize loss value
  152. export ping_loss_percent_1_to_2=$(echo "scale=2; $ping_loss_percent_1_to_2/100.0" | bc)
  153. export ping_loss_percent_2_to_1=$(echo "scale=2; $ping_loss_percent_2_to_1/100.0" | bc)
  154. ################################################################################
  155. # CLI Check #
  156. ################################################################################
  157. echo "Testing basic CLI functionality..."
  158. spam_cli 10
  159. $ZT1 join $TEST_NETWORK
  160. $ZT1 -h
  161. $ZT1 -v
  162. $ZT1 status
  163. $ZT1 info
  164. $ZT1 listnetworks
  165. $ZT1 peers
  166. $ZT1 listpeers
  167. $ZT1 -j status
  168. $ZT1 -j info
  169. $ZT1 -j listnetworks
  170. $ZT1 -j peers
  171. $ZT1 -j listpeers
  172. $ZT1 dump
  173. $ZT1 get $TEST_NETWORK allowDNS
  174. $ZT1 get $TEST_NETWORK allowDefault
  175. $ZT1 get $TEST_NETWORK allowGlobal
  176. $ZT1 get $TEST_NETWORK allowManaged
  177. $ZT1 get $TEST_NETWORK bridge
  178. $ZT1 get $TEST_NETWORK broadcastEnabled
  179. $ZT1 get $TEST_NETWORK dhcp
  180. $ZT1 get $TEST_NETWORK id
  181. $ZT1 get $TEST_NETWORK mac
  182. $ZT1 get $TEST_NETWORK mtu
  183. $ZT1 get $TEST_NETWORK name
  184. $ZT1 get $TEST_NETWORK netconfRevision
  185. $ZT1 get $TEST_NETWORK nwid
  186. $ZT1 get $TEST_NETWORK portDeviceName
  187. $ZT1 get $TEST_NETWORK portError
  188. $ZT1 get $TEST_NETWORK status
  189. $ZT1 get $TEST_NETWORK type
  190. # Test an invalid command
  191. $ZT1 get $TEST_NETWORK derpderp
  192. # TODO: Validate JSON
  193. # Performance Test
  194. export FILENAME_PERF_JSON="$TEST_FILEPATH_PREFIX-iperf.json"
  195. echo -e "\nBeginning performance test:"
  196. echo -e "\nStarting server:"
  197. echo "$NS1 iperf3 -s &"
  198. sleep 1
  199. echo -e "\nStarting client:"
  200. sleep 1
  201. echo "$NS2 iperf3 --json -c $node1_ip4 > $FILENAME_PERF_JSON"
  202. cat $FILENAME_PERF_JSON
  203. # Let ZeroTier idle long enough for various timers
  204. echo -e "\nIdling ZeroTier for $RUN_LENGTH seconds..."
  205. sleep $RUN_LENGTH
  206. echo -e "\nLeaving networks"
  207. $ZT1 leave $TEST_NETWORK
  208. $ZT2 leave $TEST_NETWORK
  209. sleep 5
  210. exit_test_and_generate_report $TEST_OK "completed test"
  211. }
  212. ################################################################################
  213. # Generate report #
  214. ################################################################################
  215. exit_test_and_generate_report() {
  216. echo -e "\nStopping memory check..."
  217. sudo pkill -15 -f valgrind
  218. sleep 10
  219. time_test_end=$(date +%s)
  220. echo "Exiting test with reason: $2 ($1)"
  221. # Collect ZeroTier dump files
  222. echo -e "\nCollecting ZeroTier dump files"
  223. node1_id=$($ZT1 -j status | jq -r .address)
  224. node2_id=$($ZT2 -j status | jq -r .address)
  225. $ZT1 dump
  226. mv zerotier_dump.txt "$TEST_FILEPATH_PREFIX-node-dump-$node1_id.txt"
  227. $ZT2 dump
  228. mv zerotier_dump.txt "$TEST_FILEPATH_PREFIX-node-dump-$node2_id.txt"
  229. # Copy ZeroTier stdout/stderr logs
  230. cp node_1.log "$TEST_FILEPATH_PREFIX-node-log-$node1_id.txt"
  231. cp node_2.log "$TEST_FILEPATH_PREFIX-node-log-$node2_id.txt"
  232. # Generate report
  233. cat $FILENAME_MEMORY_LOG
  234. DEFINITELY_LOST=$(xmlstarlet sel -t -v '/valgrindoutput/error/xwhat' \
  235. $FILENAME_MEMORY_LOG | grep "definitely" | awk '{print $1;}')
  236. POSSIBLY_LOST=$(xmlstarlet sel -t -v '/valgrindoutput/error/xwhat' \
  237. $FILENAME_MEMORY_LOG | grep "possibly" | awk '{print $1;}')
  238. # Generate coverage report artifact and summary
  239. FILENAME_COVERAGE_JSON="$TEST_FILEPATH_PREFIX-coverage.json"
  240. FILENAME_COVERAGE_HTML="$TEST_FILEPATH_PREFIX-coverage.html"
  241. echo -e "\nGenerating coverage test report..."
  242. gcovr -r . --exclude ext --json-summary $FILENAME_COVERAGE_JSON \
  243. --html >$FILENAME_COVERAGE_HTML
  244. cat $FILENAME_COVERAGE_JSON
  245. COVERAGE_LINE_COVERED=$(cat $FILENAME_COVERAGE_JSON | jq .line_covered)
  246. COVERAGE_LINE_TOTAL=$(cat $FILENAME_COVERAGE_JSON | jq .line_total)
  247. COVERAGE_LINE_PERCENT=$(cat $FILENAME_COVERAGE_JSON | jq .line_percent)
  248. COVERAGE_LINE_COVERED="${COVERAGE_LINE_COVERED:-0}"
  249. COVERAGE_LINE_TOTAL="${COVERAGE_LINE_TOTAL:-0}"
  250. COVERAGE_LINE_PERCENT="${COVERAGE_LINE_PERCENT:-0}"
  251. # Default values
  252. DEFINITELY_LOST="${DEFINITELY_LOST:-0}"
  253. POSSIBLY_LOST="${POSSIBLY_LOST:-0}"
  254. ping_loss_percent_1_to_2="${ping_loss_percent_1_to_2:-100.0}"
  255. ping_loss_percent_2_to_1="${ping_loss_percent_2_to_1:-100.0}"
  256. time_to_both_nodes_online="${time_to_both_nodes_online:--1}"
  257. # Summarize and emit json for trend reporting
  258. FILENAME_SUMMARY="$TEST_FILEPATH_PREFIX-summary.json"
  259. time_length_test=$((time_test_end - time_test_start))
  260. if [[ $time_to_both_nodes_online != -1 ]];
  261. then
  262. time_to_both_nodes_online=$((time_to_both_nodes_online - time_test_start))
  263. fi
  264. #time_length_zt_join=$((time_zt_join_end-time_zt_join_start))
  265. #time_length_zt_leave=$((time_zt_leave_end-time_zt_leave_start))
  266. #time_length_zt_can_still_ping=$((time_zt_can_still_ping-time_zt_leave_start))
  267. summary=$(
  268. cat <<EOF
  269. {
  270. "version":"$ZTO_VER",
  271. "commit":"$ZTO_COMMIT",
  272. "arch_m":"$(uname -m)",
  273. "arch_a":"$(uname -a)",
  274. "binary_size":"$(stat -c %s zerotier-one)",
  275. "time_length_test":$time_length_test,
  276. "time_to_both_nodes_online":$time_to_both_nodes_online,
  277. "num_possible_bytes_lost": $POSSIBLY_LOST,
  278. "num_definite_bytes_lost": $DEFINITELY_LOST,
  279. "num_bad_formattings": $POSSIBLY_LOST,
  280. "coverage_lines_covered": $COVERAGE_LINE_COVERED,
  281. "coverage_lines_total": $COVERAGE_LINE_TOTAL,
  282. "coverage_lines_percent": $COVERAGE_LINE_PERCENT,
  283. "ping_loss_percent_1_to_2": $ping_loss_percent_1_to_2,
  284. "ping_loss_percent_2_to_1": $ping_loss_percent_2_to_1,
  285. "test_exit_code": $1,
  286. "test_exit_reason":"$2"
  287. }
  288. EOF
  289. )
  290. echo $summary >$FILENAME_SUMMARY
  291. cat $FILENAME_SUMMARY
  292. exit 0
  293. }
  294. ################################################################################
  295. # CLI Check #
  296. ################################################################################
  297. spam_cli() {
  298. echo "Spamming CLI..."
  299. # Rapidly spam the CLI with joins/leaves
  300. MAX_TRIES="${1:-10}"
  301. for ((s = 0; s <= MAX_TRIES; s++)); do
  302. $ZT1 status
  303. $ZT2 status
  304. sleep 0.1
  305. done
  306. SPAM_TRIES=128
  307. for ((s = 0; s <= SPAM_TRIES; s++)); do
  308. $ZT1 join $TEST_NETWORK
  309. done
  310. for ((s = 0; s <= SPAM_TRIES; s++)); do
  311. $ZT1 leave $TEST_NETWORK
  312. done
  313. for ((s = 0; s <= SPAM_TRIES; s++)); do
  314. $ZT1 leave $TEST_NETWORK
  315. $ZT1 join $TEST_NETWORK
  316. done
  317. }
  318. ################################################################################
  319. # Check for proper exit on load of invalid identity #
  320. ################################################################################
  321. check_exit_on_invalid_identity() {
  322. echo "Checking ZeroTier exits on invalid identity..."
  323. mkdir -p $(pwd)/exit_test
  324. ZT1="sudo ./zerotier-one -p9999 $(pwd)/exit_test"
  325. echo "asdfasdfasdfasdf" > $(pwd)/exit_test/identity.secret
  326. echo "asdfasdfasdfasdf" > $(pwd)/exit_test/authtoken.secret
  327. echo "Launch ZeroTier with an invalid identity"
  328. $ZT1 &
  329. my_pid=$!
  330. echo "Waiting 5 seconds"
  331. sleep 5
  332. # check if process is running
  333. kill -0 $my_pid
  334. if [ $? -eq 0 ]; then
  335. exit_test_and_generate_report $TEST_FAIL "Exit test FAILED: Process still running after being fed an invalid identity"
  336. fi
  337. }
  338. test "$@"