blob: f87483a686ede74db0f767e20e449417284aba6d [file] [log] [blame]
Robin Thellend663bf482014-10-01 10:27:10 -07001#!/bin/bash
2
3# Test the debug binary
4#
5# This test starts a mounttable server and then runs the debug command against
6# it.
7
Jiri Simsa7d1b28d2014-12-12 22:39:37 -08008source "$(go list -f {{.Dir}} veyron.io/veyron/shell/lib)/shell_test.sh"
Robin Thellend663bf482014-10-01 10:27:10 -07009
Jing Jin89312ff2014-10-24 10:42:08 -070010readonly WORKDIR="${shell_test_WORK_DIR}"
Matt Rosencrantzbe1a8b52014-11-21 15:14:06 -080011readonly DEBUG_FLAGS="--veyron.vtrace.sample_rate=1"
Robin Thellend663bf482014-10-01 10:27:10 -070012
13build() {
Jing Jin89312ff2014-10-24 10:42:08 -070014 DEBUG_BIN="$(shell_test::build_go_binary 'veyron.io/veyron/veyron/tools/debug')"
Robin Thellend663bf482014-10-01 10:27:10 -070015}
16
17dumplogs() {
18 for x in $*; do
19 echo "-- $(basename "${x}") --"
20 cat "${x}"
21 done
22}
23
24main() {
Jiri Simsa85aea7b2014-10-15 15:48:36 -070025 local GOT WANT
26
Robin Thellend663bf482014-10-01 10:27:10 -070027 cd "${WORKDIR}"
Jing Jin89312ff2014-10-24 10:42:08 -070028 build
Robin Thellendfaa083a2014-10-22 13:41:18 -070029 mkdir "tmp"
30 export TMPDIR="${WORKDIR}/tmp"
Robin Thellend663bf482014-10-01 10:27:10 -070031
Asim Shankarf30b2df2014-10-11 18:51:45 -070032 export VEYRON_CREDENTIALS=$(shell::tmp_dir)
Ankurd8762692014-12-12 10:50:12 -080033 # Create specific VeyronCredentials for the debug command forked from the environment's
34 # VeyronCredentials.
35 export DEBUG_CREDENTIALS=$(shell_test::forkcredentials "${VEYRON_CREDENTIALS}" debug)
36
Cosmos Nicolaouf17b6022014-10-14 10:04:20 -070037 shell_test::setup_server_test || shell_test::fail "setup_server_test failed"
38 local -r EP="${NAMESPACE_ROOT}"
39 unset NAMESPACE_ROOT
Ankurd8762692014-12-12 10:50:12 -080040 export VEYRON_CREDENTIALS="${DEBUG_CREDENTIALS}"
Robin Thellend663bf482014-10-01 10:27:10 -070041
Jiri Simsa85aea7b2014-10-15 15:48:36 -070042 # Test top level glob.
Robin Thellend663bf482014-10-01 10:27:10 -070043 local -r DBGLOG="${WORKDIR}/debug.log"
Matt Rosencrantzbe1a8b52014-11-21 15:14:06 -080044 GOT=$("${DEBUG_BIN}" "${DEBUG_FLAGS}" glob "${EP}/__debug/*" 2> "${DBGLOG}") \
Jiri Simsa85aea7b2014-10-15 15:48:36 -070045 || (dumplogs "${DBGLOG}"; shell_test::fail "line ${LINENO}: failed to run debug")
46 WANT="${EP}/__debug/logs
Robin Thellend663bf482014-10-01 10:27:10 -070047${EP}/__debug/pprof
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080048${EP}/__debug/stats
49${EP}/__debug/vtrace"
Jiri Simsa85aea7b2014-10-15 15:48:36 -070050 shell_test::assert_eq "${GOT}" "${WANT}" "${LINENO}"
Robin Thellend663bf482014-10-01 10:27:10 -070051
Jiri Simsa85aea7b2014-10-15 15:48:36 -070052 # Test logs glob.
Matt Rosencrantzbe1a8b52014-11-21 15:14:06 -080053 GOT=$("${DEBUG_BIN}" "${DEBUG_FLAGS}" glob "${EP}/__debug/logs/*" 2> "${DBGLOG}" | wc -l) \
Jiri Simsa85aea7b2014-10-15 15:48:36 -070054 || (dumplogs "${DBGLOG}"; shell_test::fail "line ${LINENO}: failed to run debug")
55 shell_test::assert_gt "${GOT}" "0" "${LINENO}"
David Why Use Two When One Will Do Presottocb8a6142014-12-02 14:27:49 -080056
Jiri Simsa85aea7b2014-10-15 15:48:36 -070057 # Test logs size.
Robin Thellend663bf482014-10-01 10:27:10 -070058 echo "This is a log file" > "${TMPDIR}/my-test-log-file"
Matt Rosencrantzbe1a8b52014-11-21 15:14:06 -080059 GOT=$("${DEBUG_BIN}" "${DEBUG_FLAGS}" logs size "${EP}/__debug/logs/my-test-log-file" 2> "${DBGLOG}") \
Jiri Simsa85aea7b2014-10-15 15:48:36 -070060 || (dumplogs "${DBGLOG}"; shell_test::fail "line ${LINENO}: failed to run debug")
Cosmos Nicolaou9348da62014-10-03 14:21:19 -070061 WANT=$(echo "This is a log file" | wc -c | tr -d ' ')
Jiri Simsa85aea7b2014-10-15 15:48:36 -070062 shell_test::assert_eq "${GOT}" "${WANT}" "${LINENO}"
Robin Thellend663bf482014-10-01 10:27:10 -070063
Jiri Simsa85aea7b2014-10-15 15:48:36 -070064 # Test logs read.
Matt Rosencrantzbe1a8b52014-11-21 15:14:06 -080065 GOT=$("${DEBUG_BIN}" "${DEBUG_FLAGS}" logs read "${EP}/__debug/logs/my-test-log-file" 2> "${DBGLOG}") \
Jiri Simsa85aea7b2014-10-15 15:48:36 -070066 || (dumplogs "${DBGLOG}"; shell_test::fail "line ${LINENO}: failed to run debug")
Robin Thellend663bf482014-10-01 10:27:10 -070067 WANT="This is a log file"
Jiri Simsa85aea7b2014-10-15 15:48:36 -070068 shell_test::assert_eq "${GOT}" "${WANT}" "${LINENO}"
Robin Thellend663bf482014-10-01 10:27:10 -070069
Robin Thellendfaa083a2014-10-22 13:41:18 -070070 # Test stats read.
Matt Rosencrantzbe1a8b52014-11-21 15:14:06 -080071 GOT=$("${DEBUG_BIN}" "${DEBUG_FLAGS}" stats read "${EP}/__debug/stats/ipc/server/routing-id/*/methods/ReadLog/latency-ms" 2> "${DBGLOG}" | wc -l) \
Robin Thellendfaa083a2014-10-22 13:41:18 -070072 || (dumplogs "${DBGLOG}"; shell_test::fail "line ${LINENO}: failed to run debug")
73 shell_test::assert_gt "${GOT}" "0" "${LINENO}"
74
Matt Rosencrantzbe1a8b52014-11-21 15:14:06 -080075 # Test fetching all vtrace traces.
76 GOT=$("${DEBUG_BIN}" "${DEBUG_FLAGS}" vtrace "${EP}/__debug/vtrace" 2> "${DBGLOG}" | egrep -o "^Trace - ([^ ]+)" | cut -b 9- | sort) \
77 || (dumplogs "${DBGLOG}"; shell_test::fail "line ${LINENO}: failed to run debug")
78 shell_test::assert_eq $(echo "${GOT}" | wc -l | tr -d ' ') "6" "${LINENO}"
79
80 # Test fetching individual traces.
81 IDS=$(echo "$GOT" | tr '\n' ' ')
82 GOT2=$("${DEBUG_BIN}" "${DEBUG_FLAGS}" vtrace "${EP}/__debug/vtrace" ${IDS} 2> "${DBGLOG}" | egrep -o "^Trace - ([^ ]+)" | cut -b 9- | sort) \
83 || (dumplogs "${DBGLOG}"; shell_test::fail "line ${LINENO}: failed to run debug")
84 shell_test::assert_eq "${GOT2}" "${GOT}" "${LINENO}"
85
Robin Thellendfaa083a2014-10-22 13:41:18 -070086 # Test stats watch.
Robin Thellend663bf482014-10-01 10:27:10 -070087 local TMP=$(shell::tmp_file)
88 touch "${TMP}"
Jiri Simsa85aea7b2014-10-15 15:48:36 -070089 local -r DEBUG_PID=$(shell::run_server "${shell_test_DEFAULT_SERVER_TIMEOUT}" "${TMP}" "${DBGLOG}" \
Bogdan Capritae7376312014-11-10 13:13:17 -080090 "${DEBUG_BIN}" stats watch -raw "${EP}/__debug/stats/ipc/server/routing-id/*/methods/ReadLog/latency-ms")
Cosmos Nicolaouf17b6022014-10-14 10:04:20 -070091 shell::timed_wait_for "${shell_test_DEFAULT_MESSAGE_TIMEOUT}" "${TMP}" "ReadLog/latency-ms"
92 kill "${DEBUG_PID}"
Jungho Ahn58e85b82014-12-01 10:01:02 -080093 grep -q "Count: 1 " "${TMP}" || (dumplogs "${TMP}"; shell_test::fail "line ${LINENO}: failed to find expected output")
Robin Thellend663bf482014-10-01 10:27:10 -070094
Jiri Simsa85aea7b2014-10-15 15:48:36 -070095 # Test pprof.
Jing Jin89312ff2014-10-24 10:42:08 -070096 if ! "${DEBUG_BIN}" pprof run "${EP}/__debug/pprof" heap --text &> "${DBGLOG}"; then
Cosmos Nicolaouf17b6022014-10-14 10:04:20 -070097 dumplogs "${DBGLOG}"
Robin Thellend663bf482014-10-01 10:27:10 -070098 shell_test::fail "line ${LINENO}: unexpected failure."
99 fi
100
101 shell_test::pass
102}
103
104main "$@"