2
0

validate-linux.sh 14 KB

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