Test collect.sh. Use --run-time instead of --interval for collect loop. Fix and require per-test test names in util/test-bash-functions. Fix OPT_ERR in parse_options.sh.

This commit is contained in:
Daniel Nichter
2011-12-06 13:23:47 -07:00
parent c804079c0d
commit f6306c9ec0
7 changed files with 182 additions and 70 deletions

View File

@@ -160,7 +160,7 @@ collect() {
# This loop gathers data for the rest of the duration, and defines the time # This loop gathers data for the rest of the duration, and defines the time
# of the whole job. # of the whole job.
echo "Loop start: $(date +'TS %s.%N %F %T')" echo "Loop start: $(date +'TS %s.%N %F %T')"
for a in $(_seq $OPT_INTERVAL); do for a in $(_seq $OPT_RUN_TIME); do
# We check the disk, but don't exit, because we need to stop jobs if we # We check the disk, but don't exit, because we need to stop jobs if we
# need to exit. # need to exit.
disk_space $d > $d/$p-disk-space disk_space $d > $d/$p-disk-space

View File

@@ -28,7 +28,7 @@ set -u
# sub will be scoped locally. # sub will be scoped locally.
declare -a ARGV # non-option args (probably input files) declare -a ARGV # non-option args (probably input files)
declare EXT_ARGV # everything after -- (args for an external command) declare EXT_ARGV # everything after -- (args for an external command)
OPT_ERR=${OPT_ERR:""} OPT_ERR=${OPT_ERR:-""}
# Sub: usage # Sub: usage
# Print usage (--help) and list the program's options. # Print usage (--help) and list the program's options.

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
TESTS=24 TESTS=19
TMPFILE="$TEST_TMPDIR/parse-opts-output" TMPFILE="$TEST_TMPDIR/parse-opts-output"
TMPDIR="$TEST_TMPDIR" TMPDIR="$TEST_TMPDIR"
@@ -14,11 +14,105 @@ source "$LIB_DIR/safeguards.sh"
source "$LIB_DIR/alt_cmds.sh" source "$LIB_DIR/alt_cmds.sh"
source "$LIB_DIR/collect.sh" source "$LIB_DIR/collect.sh"
parse_options "$T_LIB_DIR/samples/bash/po002.sh" -- --defaults-file=/tmp/12345/my.sandbox.cnf parse_options "$T_LIB_DIR/samples/bash/po002.sh" --run-time 1 -- --defaults-file=/tmp/12345/my.sandbox.cnf
collect "$TMPDIR/collect" "2011_12_05" # Prefix (with path) for the collect files.
local p="$TMPDIR/collect/2011_12_05"
# Default collect, no extras like gdb, tcpdump, etc.
collect "$TMPDIR/collect" "2011_12_05" > $p-output 2>&1
# Even if this system doesn't have all the cmds, collect should still
# create all the default files.
ls -1 $TMPDIR/collect | sort > $TMPDIR/collect-files
no_diff \
$TMPDIR/collect-files \
$T_LIB_DIR/samples/bash/collect001.txt \
"Default collect files"
cmd_ok \
"grep -q 'Avail' $p-df" \
"df"
# hostname is the last thing collected, so if it's ok,
# then the sub reached its end.
is \
"`cat $p-hostname`" \
"`hostname`" \
"hostname"
cmd_ok \
"grep -q -i 'buffer pool' $p-innodbstatus1" \
"innodbstatus1"
cmd_ok \
"grep -q -i 'buffer pool' $p-innodbstatus2" \
"innodbstatus2"
cmd_ok \
"grep -q 'error log seems to be /tmp/12345/data/mysqld.log' $p-output" \
"Finds MySQL error log"
cmd_ok \
"grep -q 'Status information:' $p-log_error" \
"debug"
cmd_ok \
"grep -q 'COMMAND[ ]\+PID[ ]\+USER' $p-lsof" \
"lsof"
cmd_ok \
"grep -q 'buf/buf0buf.c' $p-mutex-status1" \
"mutex-status1"
cmd_ok \
"grep -q 'buf/buf0buf.c' $p-mutex-status2" \
"mutex-status2"
cmd_ok \
"grep -q '^| Uptime' $p-mysqladmin" \
"mysqladmin ext"
cmd_ok \
"grep -qP 'Database\tTable\tIn_use' $p-opentables1" \
"opentables1"
cmd_ok \
"grep -qP 'Database\tTable\t\In_use' $p-opentables2" \
"opentables2"
cmd_ok \
"grep -q '1. row' $p-processlist1" \
"processlist1"
cmd_ok \
"grep -q '1. row' $p-processlist2" \
"processlist2"
cmd_ok \
"grep -q 'mysqld' $p-ps" \
"ps"
cmd_ok \
"grep -qP '^warning_count\t\d' $p-variables" \
"variables"
local iters=$(cat $p-df | grep -c '^TS ')
is "$iters" "1" "1 iteration/1s run time"
# ###########################################################################
# Try longer run time.
# ###########################################################################
parse_options "$T_LIB_DIR/samples/bash/po002.sh" --run-time 2 -- --defaults-file=/tmp/12345/my.sandbox.cnf
rm $TMPDIR/collect/*
collect "$TMPDIR/collect" "2011_12_05" > $p-output 2>&1
local iters=$(cat $p-df | grep -c '^TS ')
is "$iters" "2" "2 iteration/2s run time"
# ############################################################################ # ############################################################################
# Done # Done
# ############################################################################ # ############################################################################
exit

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
TESTS=24 TESTS=25
TMPFILE="$TEST_TMPDIR/parse-opts-output" TMPFILE="$TEST_TMPDIR/parse-opts-output"
@@ -14,17 +14,15 @@ source "$LIB_DIR/parse_options.sh"
TMPDIR="$TEST_TMPDIR" TMPDIR="$TEST_TMPDIR"
parse_options "$T_LIB_DIR/samples/bash/po001.sh" "" 2>$TMPFILE parse_options "$T_LIB_DIR/samples/bash/po001.sh" "" 2>$TMPFILE
TEST_NAME="No warnings or errors" is "`cat $TMPFILE`" "" "No warnings or errors"
is "`cat $TMPFILE`" ""
TEST_NAME="Default opts" is "$OPT_STRING_OPT" "" "Default string option"
is "$OPT_STRING_OPT" "" is "$OPT_STRING_OPT2" "foo" "Default string option with default"
is "$OPT_STRING_OPT2" "foo" is "$OPT_TYPELESS_OPTION" "" "Default typeless option"
is "$OPT_TYPELESS_OPTION" "" is "$OPT_NOPTION" "yes" "Defailt neg option"
is "$OPT_NOPTION" "yes" is "$OPT_INT_OPT" "" "Default int option"
is "$OPT_INT_OPT" "" is "$OPT_INT_OPT2" "42" "Default int option with default"
is "$OPT_INT_OPT2" "42" is "$OPT_VERSION" "" "--version"
is "$OPT_VERSION" ""
# ############################################################################ # ############################################################################
# Specify some opts, but use default values for the rest. # Specify some opts, but use default values for the rest.
@@ -32,14 +30,13 @@ is "$OPT_VERSION" ""
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --int-opt 50 --typeless-option --string-opt bar parse_options "$T_LIB_DIR/samples/bash/po001.sh" --int-opt 50 --typeless-option --string-opt bar
TEST_NAME="User-specified opts with defaults" is "$OPT_STRING_OPT" "bar" "Specified string option (spec)"
is "$OPT_STRING_OPT" "bar" # specified is "$OPT_STRING_OPT2" "foo" "Default string option with default (spec)"
is "$OPT_STRING_OPT2" "foo" is "$OPT_TYPELESS_OPTION" "yes" "Specified typeless option (spec)"
is "$OPT_TYPELESS_OPTION" "yes" # specified is "$OPT_NOPTION" "yes" "Default neg option (spec)"
is "$OPT_NOPTION" "yes" is "$OPT_INT_OPT" "50" "Specified int option (spec)"
is "$OPT_INT_OPT" "50" # specified is "$OPT_INT_OPT2" "42" "Default int option with default (spec)"
is "$OPT_INT_OPT2" "42" is "$OPT_VERSION" "" "--version (spec)"
is "$OPT_VERSION" ""
# ############################################################################ # ############################################################################
# Negate an option like --no-option. # Negate an option like --no-option.
@@ -47,23 +44,20 @@ is "$OPT_VERSION" ""
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --no-noption parse_options "$T_LIB_DIR/samples/bash/po001.sh" --no-noption
TEST_NAME="Negated option" is "$OPT_STRING_OPT" "" "Default string option (neg)"
is "$OPT_STRING_OPT" "" is "$OPT_STRING_OPT2" "foo" "Default string option with default (net)"
is "$OPT_STRING_OPT2" "foo" is "$OPT_TYPELESS_OPTION" "" "Default typeless option (neg)"
is "$OPT_TYPELESS_OPTION" "" is "$OPT_NOPTION" "no" "Negated option (neg)"
is "$OPT_NOPTION" "no" # negated is "$OPT_INT_OPT" "" "Default int option (neg)"
is "$OPT_INT_OPT" "" is "$OPT_INT_OPT2" "42" "Default int option with default (neg)"
is "$OPT_INT_OPT2" "42" is "$OPT_VERSION" "" "--version (neg)"
is "$OPT_VERSION" ""
# ############################################################################ # ############################################################################
# Short form. # Short form.
# ############################################################################ # ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" -v parse_options "$T_LIB_DIR/samples/bash/po001.sh" -v
is "$OPT_VERSION" "yes" "Short form"
TEST_NAME="Short form"
is "$OPT_VERSION" "yes"
# ############################################################################ # ############################################################################
# An unknown option should produce an error. # An unknown option should produce an error.
@@ -74,13 +68,9 @@ is "$OPT_VERSION" "yes"
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --foo >$TMPFILE 2>&1 parse_options "$T_LIB_DIR/samples/bash/po001.sh" --foo >$TMPFILE 2>&1
) )
local err=$? local err=$?
TEST_NAME="Non-zero exit on unknown option" is "$err" "1" "Non-zero exit on unknown option"
is "$err" "1" cmd_ok "grep -q 'Unknown option: foo' $TMPFILE" "Error on unknown option"
TEST_NAME="Error on unknown option"
cmd_ok "grep -q 'Unknown option: foo' $TMPFILE"
# ############################################################################ # ############################################################################
# Done # Done
# ############################################################################ # ############################################################################
exit

View File

@@ -5,40 +5,31 @@ TESTS=9
source "$LIB_DIR/log_warn_die.sh" source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/tmpdir.sh" source "$LIB_DIR/tmpdir.sh"
TEST_NAME="TMPDIR not defined" is "$TMPDIR" "" "TMPDIR not defined"
is "$TMPDIR" ""
TEST_NAME="mk_tmpdir makes secure tmpdir"
mk_tmpdir mk_tmpdir
cmd_ok "test -d $TMPDIR" cmd_ok "test -d $TMPDIR" "mk_tmpdir makes secure tmpdir"
tmpdir=$TMPDIR; tmpdir=$TMPDIR;
TEST_NAME="rm_tmpdir"
rm_tmpdir rm_tmpdir
cmd_ok "test ! -d $tmpdir" cmd_ok "test ! -d $tmpdir" "rm_tmpdir"
TEST_NAME="rm_tmpdir resets TMPDIR" is "$TMPDIR" "" "rm_tmpdir resets TMPDIR"
is "$TMPDIR" ""
# --tmpdir # --tmpdir
OPT_TMPDIR="/tmp/use--tmpdir" OPT_TMPDIR="/tmp/use--tmpdir"
TEST_NAME="TMPDIR not defined" is "$TMPDIR" "" "TMPDIR not defined"
is "$TMPDIR" ""
TEST_NAME="--tmpdir does not exist yet" cmd_ok "test ! -d $OPT_TMPDIR" "--tmpdir does not exist yet"
cmd_ok "test ! -d $OPT_TMPDIR"
mk_tmpdir mk_tmpdir
TEST_NAME="mk_tmpdir uses --tmpdir" is "$TMPDIR" "/tmp/use--tmpdir" "mk_tmpdir uses --tmpdir"
is "$TMPDIR" "/tmp/use--tmpdir"
TEST_NAME="mk_tmpdir creates --tmpdir" cmd_ok "test -d $TMPDIR" "mk_tmpdir creates --tmpdir"
cmd_ok "test -d $TMPDIR"
tmpdir=$TMPDIR; tmpdir=$TMPDIR;
TEST_NAME="rm_tmpdir removes --tmpdir"
rm_tmpdir rm_tmpdir
cmd_ok "test ! -d $tmpdir" cmd_ok "test ! -d $tmpdir" "rm_tmpdir removes --tmpdir"

View File

@@ -0,0 +1,34 @@
2011_12_05-df
2011_12_05-disk-space
2011_12_05-diskstats
2011_12_05-hostname
2011_12_05-innodbstatus1
2011_12_05-innodbstatus2
2011_12_05-interrupts
2011_12_05-iostat
2011_12_05-iostat-overall
2011_12_05-log_error
2011_12_05-lsof
2011_12_05-meminfo
2011_12_05-mpstat
2011_12_05-mpstat-overall
2011_12_05-mutex-status1
2011_12_05-mutex-status2
2011_12_05-mysqladmin
2011_12_05-netstat
2011_12_05-netstat_s
2011_12_05-opentables1
2011_12_05-opentables2
2011_12_05-output
2011_12_05-processlist1
2011_12_05-processlist2
2011_12_05-procstat
2011_12_05-procvmstat
2011_12_05-ps
2011_12_05-slabinfo
2011_12_05-stacktrace
2011_12_05-sysctl
2011_12_05-top
2011_12_05-variables
2011_12_05-vmstat
2011_12_05-vmstat-overall

View File

@@ -48,6 +48,7 @@ fi
load_tests() { load_tests() {
local test_files="$@" local test_files="$@"
local i=0 local i=0
local n_tests=0
for t in $test_files; do for t in $test_files; do
# Return unless the test file is bash. There may be other types of # Return unless the test file is bash. There may be other types of
# files in the tool's test dir. # files in the tool's test dir.
@@ -57,14 +58,16 @@ load_tests() {
head -n 1 $t | grep -q bash || continue head -n 1 $t | grep -q bash || continue
tests[$i]=$t tests[$i]=$t
i=$((i + 1))
number_of_tests=$(grep --max-count 1 '^TESTS=[0-9]' $t | cut -d'=' -f2) number_of_tests=$(grep --max-count 1 '^TESTS=[0-9]' $t | cut -d'=' -f2)
if [ -z "$number_of_tests" ]; then if [ -z "$number_of_tests" ]; then
i=$(( i + 1 )) n_tests=$(( $n_tests + 1 ))
else else
i=$(( i + $number_of_tests )) n_tests=$(( $n_tests + $number_of_tests ))
fi fi
done done
echo "1..$i" echo "1..$n_tests"
} }
# Source a test file to run whatever it contains (hopefully tests!). # Source a test file to run whatever it contains (hopefully tests!).
@@ -72,8 +75,6 @@ run_test() {
local t=$1 # test file name, e.g. "group-by-all-01" for pt-diskstats local t=$1 # test file name, e.g. "group-by-all-01" for pt-diskstats
rm -rf $TEST_TMPDIR/* >/dev/null 2>&1 rm -rf $TEST_TMPDIR/* >/dev/null 2>&1
TEST_NUMBER=1 # test number in this test file
# Tests assume that they're being ran from their own dir, so they access # Tests assume that they're being ran from their own dir, so they access
# sample files like "samples/foo.txt". So cd to the dir of the test file # sample files like "samples/foo.txt". So cd to the dir of the test file
# and run it. But the test file may have been given as a relative path, # and run it. But the test file may have been given as a relative path,
@@ -92,7 +93,7 @@ run_test() {
# Print a TAP-style test result. # Print a TAP-style test result.
result() { result() {
local result=$1 local result=$1
local test_name=${TEST_NAME:-"$TEST_NUMBER"} local test_name=$2
if [ $result -eq 0 ]; then if [ $result -eq 0 ]; then
echo "ok $testno - $TEST_FILE $test_name" echo "ok $testno - $TEST_FILE $test_name"
else else
@@ -104,7 +105,6 @@ result() {
fi fi
fi fi
testno=$((testno + 1)) testno=$((testno + 1))
TEST_NUMBER=$((TEST_NUMBER + 1))
return $result return $result
} }
@@ -115,23 +115,26 @@ result() {
no_diff() { no_diff() {
local got=$1 local got=$1
local expected=$2 local expected=$2
local test_name=$3
test_command="diff $got $expected" test_command="diff $got $expected"
eval $test_command > $TEST_TMPDIR/failed_result 2>&1 eval $test_command > $TEST_TMPDIR/failed_result 2>&1
result $? result $? "$test_name"
} }
is() { is() {
local got=$1 local got=$1
local expected=$2 local expected=$2
local test_name=$3
test_command="\"$got\" == \"$expected\"" test_command="\"$got\" == \"$expected\""
test "$got" = "$expected" test "$got" = "$expected"
result $? result $? "$test_name"
} }
cmd_ok() { cmd_ok() {
local test_command=$1 local test_command=$1
local test_name=$2
eval $test_command eval $test_command
result $? result $? "$test_name"
} }
# ############################################################################ # ############################################################################