validate-linux.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #!/bin/bash
  2. # This test script joins Earth and pokes some stuff
  3. TEST_NETWORK=8056c2e21c000001
  4. RUN_LENGTH=20
  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=60
  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. NS1="ip netns exec ns1"
  25. NS2="ip netns exec ns2"
  26. ZT1="$NS1 ./zerotier-cli -p9996 -D$(pwd)/node1"
  27. # Specify custom port on one node to ensure that feature works
  28. 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 [[ "$node1_online" == "true" ]]; then
  110. export time_zt_node1_online=$(date +%s)
  111. fi
  112. if [[ "$node2_online" == "true" ]]; then
  113. export time_zt_node2_online=$(date +%s)
  114. fi
  115. if [[ "$node2_online" == "true" && "$node1_online" == "true" ]]; then
  116. export both_instances_online=true
  117. break
  118. fi
  119. sleep 1
  120. done
  121. echo -e "\n\nContents of ZeroTier home paths:"
  122. ls -lga node1
  123. tree node1
  124. ls -lga node2
  125. tree node2
  126. echo -e "\n\nRunning ZeroTier processes:"
  127. echo -e "\nNode 1:"
  128. $NS1 ps aux | grep zerotier-one
  129. echo -e "\nNode 2:"
  130. $NS2 ps aux | grep zerotier-one
  131. echo -e "\n\nStatus of each instance:"
  132. echo -e "\n\nNode 1:"
  133. $ZT1 status
  134. echo -e "\n\nNode 2:"
  135. $ZT2 status
  136. if [[ "$both_instances_online" != "true" ]]; then
  137. exit_test_and_generate_report $TEST_FAIL "one or more nodes failed to come online"
  138. fi
  139. echo -e "\nJoining networks"
  140. $ZT1 join $TEST_NETWORK
  141. $ZT2 join $TEST_NETWORK
  142. sleep 10
  143. node1_ip4=$($ZT1 get $TEST_NETWORK ip4)
  144. node2_ip4=$($ZT2 get $TEST_NETWORK ip4)
  145. echo "node1_ip4=$node1_ip4"
  146. echo "node2_ip4=$node2_ip4"
  147. echo -e "\nPinging each node"
  148. PING12_FILENAME="$TEST_FILEPATH_PREFIX-ping-1-to-2.txt"
  149. PING21_FILENAME="$TEST_FILEPATH_PREFIX-ping-2-to-1.txt"
  150. $NS1 ping -c 16 $node2_ip4 >$PING12_FILENAME
  151. $NS2 ping -c 16 $node1_ip4 >$PING21_FILENAME
  152. ping_loss_percent_1_to_2=$(cat $PING12_FILENAME |
  153. grep "packet loss" | awk '{print $6}' | sed 's/%//')
  154. ping_loss_percent_2_to_1=$(cat $PING21_FILENAME |
  155. grep "packet loss" | awk '{print $6}' | sed 's/%//')
  156. # Normalize loss value
  157. export ping_loss_percent_1_to_2=$(echo "scale=2; $ping_loss_percent_1_to_2/100.0" | bc)
  158. export ping_loss_percent_2_to_1=$(echo "scale=2; $ping_loss_percent_2_to_1/100.0" | bc)
  159. ################################################################################
  160. # CLI Check #
  161. ################################################################################
  162. echo "Testing basic CLI functionality..."
  163. spam_cli 10
  164. $ZT1 join $TEST_NETWORK
  165. $ZT1 -h
  166. $ZT1 -v
  167. $ZT1 status
  168. $ZT1 info
  169. $ZT1 listnetworks
  170. $ZT1 peers
  171. $ZT1 listpeers
  172. $ZT1 -j status
  173. $ZT1 -j info
  174. $ZT1 -j listnetworks
  175. $ZT1 -j peers
  176. $ZT1 -j listpeers
  177. $ZT1 dump
  178. $ZT1 get $TEST_NETWORK allowDNS
  179. $ZT1 get $TEST_NETWORK allowDefault
  180. $ZT1 get $TEST_NETWORK allowGlobal
  181. $ZT1 get $TEST_NETWORK allowManaged
  182. $ZT1 get $TEST_NETWORK bridge
  183. $ZT1 get $TEST_NETWORK broadcastEnabled
  184. $ZT1 get $TEST_NETWORK dhcp
  185. $ZT1 get $TEST_NETWORK id
  186. $ZT1 get $TEST_NETWORK mac
  187. $ZT1 get $TEST_NETWORK mtu
  188. $ZT1 get $TEST_NETWORK name
  189. $ZT1 get $TEST_NETWORK netconfRevision
  190. $ZT1 get $TEST_NETWORK nwid
  191. $ZT1 get $TEST_NETWORK portDeviceName
  192. $ZT1 get $TEST_NETWORK portError
  193. $ZT1 get $TEST_NETWORK status
  194. $ZT1 get $TEST_NETWORK type
  195. # Test an invalid command
  196. $ZT1 get $TEST_NETWORK derpderp
  197. # TODO: Validate JSON
  198. # Performance Test
  199. export FILENAME_PERF_JSON="$TEST_FILEPATH_PREFIX-iperf.json"
  200. echo -e "\nBeginning performance test:"
  201. echo -e "\nStarting server:"
  202. echo "$NS1 iperf3 -s &"
  203. sleep 1
  204. echo -e "\nStarting client:"
  205. sleep 1
  206. echo "$NS2 iperf3 --json -c $node1_ip4 > $FILENAME_PERF_JSON"
  207. cat $FILENAME_PERF_JSON
  208. # Let ZeroTier idle long enough for various timers
  209. echo -e "\nIdling ZeroTier for $RUN_LENGTH seconds..."
  210. sleep $RUN_LENGTH
  211. echo -e "\nLeaving networks"
  212. $ZT1 leave $TEST_NETWORK
  213. $ZT2 leave $TEST_NETWORK
  214. sleep 5
  215. # Stop test
  216. echo -e "\nStopping memory check..."
  217. sudo pkill -15 -f valgrind
  218. sleep 10
  219. export time_test_end=$(date +%s)
  220. exit_test_and_generate_report $TEST_OK "completed test"
  221. }
  222. ################################################################################
  223. # Generate report #
  224. ################################################################################
  225. exit_test_and_generate_report() {
  226. echo "Exiting test with reason: $2 ($1)"
  227. # Collect ZeroTier dump files
  228. echo -e "\nCollecting ZeroTier dump files"
  229. node1_id=$($ZT1 -j status | jq -r .address)
  230. node2_id=$($ZT2 -j status | jq -r .address)
  231. $ZT1 dump
  232. mv zerotier_dump.txt "$TEST_FILEPATH_PREFIX-node-dump-$node1_id.txt"
  233. $ZT2 dump
  234. mv zerotier_dump.txt "$TEST_FILEPATH_PREFIX-node-dump-$node2_id.txt"
  235. # Copy ZeroTier stdout/stderr logs
  236. cp node_1.log "$TEST_FILEPATH_PREFIX-node-log-$node1_id.txt"
  237. cp node_2.log "$TEST_FILEPATH_PREFIX-node-log-$node2_id.txt"
  238. # Generate report
  239. cat $FILENAME_MEMORY_LOG
  240. DEFINITELY_LOST=$(xmlstarlet sel -t -v '/valgrindoutput/error/xwhat' \
  241. $FILENAME_MEMORY_LOG | grep "definitely" | awk '{print $1;}')
  242. POSSIBLY_LOST=$(xmlstarlet sel -t -v '/valgrindoutput/error/xwhat' \
  243. $FILENAME_MEMORY_LOG | grep "possibly" | awk '{print $1;}')
  244. # Generate coverage report artifact and summary
  245. FILENAME_COVERAGE_JSON="$TEST_FILEPATH_PREFIX-coverage.json"
  246. FILENAME_COVERAGE_HTML="$TEST_FILEPATH_PREFIX-coverage.html"
  247. echo -e "\nGenerating coverage test report..."
  248. gcovr -r . --exclude ext --json-summary $FILENAME_COVERAGE_JSON \
  249. --html >$FILENAME_COVERAGE_HTML
  250. cat $FILENAME_COVERAGE_JSON
  251. COVERAGE_LINE_COVERED=$(cat $FILENAME_COVERAGE_JSON | jq .line_covered)
  252. COVERAGE_LINE_TOTAL=$(cat $FILENAME_COVERAGE_JSON | jq .line_total)
  253. COVERAGE_LINE_PERCENT=$(cat $FILENAME_COVERAGE_JSON | jq .line_percent)
  254. COVERAGE_LINE_COVERED="${COVERAGE_LINE_COVERED:-0}"
  255. COVERAGE_LINE_TOTAL="${COVERAGE_LINE_TOTAL:-0}"
  256. COVERAGE_LINE_PERCENT="${COVERAGE_LINE_PERCENT:-0}"
  257. # Default values
  258. DEFINITELY_LOST="${DEFINITELY_LOST:-0}"
  259. POSSIBLY_LOST="${POSSIBLY_LOST:-0}"
  260. ping_loss_percent_1_to_2="${ping_loss_percent_1_to_2:-100.0}"
  261. ping_loss_percent_2_to_1="${ping_loss_percent_2_to_1:-100.0}"
  262. # Summarize and emit json for trend reporting
  263. FILENAME_SUMMARY="$TEST_FILEPATH_PREFIX-summary.json"
  264. time_length_test=$((time_test_end - time_test_start))
  265. time_to_node1_online=$((time_zt_node1_online - time_zt_start))
  266. time_to_node2_online=$((time_zt_node2_online - time_zt_start))
  267. #time_length_zt_join=$((time_zt_join_end-time_zt_join_start))
  268. #time_length_zt_leave=$((time_zt_leave_end-time_zt_leave_start))
  269. #time_length_zt_can_still_ping=$((time_zt_can_still_ping-time_zt_leave_start))
  270. summary=$(
  271. cat <<EOF
  272. {
  273. "version":"$ZTO_VER",
  274. "commit":"$ZTO_COMMIT",
  275. "arch_m":"$(uname -m)",
  276. "arch_a":"$(uname -a)",
  277. "binary_size":"$(stat -c %s zerotier-one)"
  278. "time_length_test":$time_length_test,
  279. "time_to_node1_online":$time_to_node1_online,
  280. "time_to_node2_online":$time_to_node2_online,
  281. "num_possible_bytes_lost": $POSSIBLY_LOST,
  282. "num_definite_bytes_lost": $DEFINITELY_LOST,
  283. "num_bad_formattings": $POSSIBLY_LOST,
  284. "coverage_lines_covered": $COVERAGE_LINE_COVERED,
  285. "coverage_lines_total": $COVERAGE_LINE_TOTAL,
  286. "coverage_lines_percent": $COVERAGE_LINE_PERCENT,
  287. "ping_loss_percent_1_to_2": $ping_loss_percent_1_to_2,
  288. "ping_loss_percent_2_to_1": $ping_loss_percent_2_to_1,
  289. "test_exit_code": $1,
  290. "test_exit_reason":"$2"
  291. }
  292. EOF
  293. )
  294. echo $summary >$FILENAME_SUMMARY
  295. cat $FILENAME_SUMMARY
  296. }
  297. ################################################################################
  298. # CLI Check #
  299. ################################################################################
  300. spam_cli() {
  301. echo "Spamming CLI..."
  302. # Rapidly spam the CLI with joins/leaves
  303. MAX_TRIES="${1:-10}"
  304. for ((s = 0; s <= MAX_TRIES; s++)); do
  305. $ZT1 status
  306. $ZT2 status
  307. sleep 0.1
  308. done
  309. SPAM_TRIES=128
  310. for ((s = 0; s <= SPAM_TRIES; s++)); do
  311. $ZT1 join $TEST_NETWORK
  312. done
  313. for ((s = 0; s <= SPAM_TRIES; s++)); do
  314. $ZT1 leave $TEST_NETWORK
  315. done
  316. for ((s = 0; s <= SPAM_TRIES; s++)); do
  317. $ZT1 leave $TEST_NETWORK
  318. $ZT1 join $TEST_NETWORK
  319. done
  320. }
  321. ################################################################################
  322. # Check for proper exit on load of invalid identity #
  323. ################################################################################
  324. check_exit_on_invalid_identity() {
  325. echo "Checking ZeroTier exits on invalid identity..."
  326. mkdir -p $(pwd)/exit_test
  327. ZT1="sudo ./zerotier-one -p9999 $(pwd)/exit_test"
  328. echo "asdfasdfasdfasdf" > $(pwd)/exit_test/identity.secret
  329. echo "asdfasdfasdfasdf" > $(pwd)/exit_test/authtoken.secret
  330. echo "Launch ZeroTier with an invalid identity"
  331. $ZT1 &
  332. my_pid=$!
  333. echo "Waiting 5 seconds"
  334. sleep 5
  335. # check if process is running
  336. kill -0 $my_pid
  337. if [ $? -eq 0 ]; then
  338. exit_test_and_generate_report $TEST_FAIL "Exit test FAILED: Process still running after being fed an invalid identity"
  339. fi
  340. }
  341. test "$@"