pt-summary for 2.1, also changes to pt-mysql-summary from Daniel's review

This commit is contained in:
Brian Fraser fraserb@gmail.com
2012-03-24 14:20:09 -03:00
parent 88a9a66946
commit 3ffda5677b
96 changed files with 14459 additions and 2260 deletions

View File

@@ -50,7 +50,7 @@ no_diff \
# collect_mysql_status
$CMD_MYSQL $EXT_ARGV -ss -e 'SHOW /*!50000 GLOBAL*/ STATUS' > "$TMPDIR/collect_mysql_status"
pat='Com_\|Bytes_\|Handler_\|Created_\|Que\|Uptime\|Select_scan\|Connections\|Opened_files\|_created\|Table_locks'
pat='Com_\|Bytes_\|Handler_\|Created_\|Que\|Uptime\|Select_scan\|Connections\|Opened_files\|_created\|Table_locks\|Innodb'
grep -v $pat "$p/percona-toolkit-mysql-status" > "$TMPDIR/collect_mysql_status_collect"
grep -v $pat "$TMPDIR/collect_mysql_status" > "$TMPDIR/collect_mysql_status_manual"

View File

@@ -0,0 +1,293 @@
#!/usr/bin/env bash
plan 32
TMPDIR="$TEST_TMPDIR"
PATH="$PATH:$PERCONA_TOOLKIT_SANDBOX/bin"
TOOL="pt-summary"
. "$LIB_DIR/log_warn_die.sh"
. "$LIB_DIR/alt_cmds.sh"
. "$LIB_DIR/parse_options.sh"
. "$LIB_DIR/summary_common.sh"
. "$LIB_DIR/collect_system_info.sh"
# Prefix (with path) for the collect files.
p="$TMPDIR/collect_mysql_info"
samples="$PERCONA_TOOLKIT_BRANCH/t/pt-summary/samples"
mkdir "$p"
OPT_SLEEP="1"
collect_system_data "$p"
cat <<EOF > "$TMPDIR/expected"
Fusion-MPT SAS
EOF
find_raid_controller_lspci "$samples/lspci-001.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "lspci-001.txt"
cat <<EOF > "$TMPDIR/expected"
LSI Logic Unknown
EOF
find_raid_controller_lspci "$samples/lspci-002.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "lspci-002.txt"
cat <<EOF > "$TMPDIR/expected"
AACRAID
EOF
find_raid_controller_lspci "$samples/lspci-003.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "lspci-003.txt"
cat <<EOF > "$TMPDIR/expected"
LSI Logic MegaRAID SAS
EOF
find_raid_controller_lspci "$samples/lspci-004.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "lspci-004.txt"
cat <<EOF > "$TMPDIR/expected"
Fusion-MPT SAS
EOF
find_raid_controller_lspci "$samples/lspci-005.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "lspci-005.txt"
cat <<EOF > "$TMPDIR/expected"
HP Smart Array
EOF
find_raid_controller_lspci "$samples/lspci-006.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "lspci-006.txt"
# find_raid_controller_dmesg
cat <<EOF > "$TMPDIR/expected"
Fusion-MPT SAS
EOF
find_raid_controller_dmesg "$samples/dmesg-001.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "dmesg-001.txt"
cat <<EOF > "$TMPDIR/expected"
AACRAID
EOF
find_raid_controller_dmesg "$samples/dmesg-002.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "dmesg-002.txt"
cat <<EOF > "$TMPDIR/expected"
LSI Logic MegaRAID SAS
EOF
find_raid_controller_dmesg "$samples/dmesg-003.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "dmesg-003.txt"
cat <<EOF > "$TMPDIR/expected"
AACRAID
EOF
find_raid_controller_dmesg "$samples/dmesg-004.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "dmesg-004.txt"
cat <<EOF > "$TMPDIR/expected"
Fusion-MPT SAS
EOF
find_raid_controller_dmesg "$samples/dmesg-005.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "dmesg-005.txt"
# TODO is this right?
cat <<EOF > "$TMPDIR/expected"
EOF
find_raid_controller_dmesg "$samples/dmesg-006.txt" > "$TMPDIR/got"
cat "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "dmesg-006.txt"
# TODO is this right?
cat <<EOF > "$TMPDIR/expected"
EOF
find_raid_controller_dmesg "$samples/dmesg-007.txt" > "$TMPDIR/got"
cat "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "dmesg-007.txt"
# raid_controller
rm "$TMPDIR/raid_controller_outfile.tmp" 2>/dev/null
raid_controller "$TMPDIR/raid_controller_outfile.tmp" "" ""
is \
"$(get_var raid_controller "$TMPDIR/raid_controller_outfile.tmp")" \
"No RAID controller detected" \
"raid_controller has a sane default"
rm "$TMPDIR/raid_controller_outfile.tmp" 2>/dev/null
raid_controller "$TMPDIR/raid_controller_outfile.tmp" "" "$samples/lspci-001.txt"
is \
"$(get_var raid_controller "$TMPDIR/raid_controller_outfile.tmp")" \
"Fusion-MPT SAS" \
"raid_controller gets the correct result from an lspci file"
rm "$TMPDIR/raid_controller_outfile.tmp" 2>/dev/null
raid_controller "$TMPDIR/raid_controller_outfile.tmp" "$samples/dmesg-004.txt" ""
is \
"$(get_var raid_controller "$TMPDIR/raid_controller_outfile.tmp")" \
"AACRAID" \
"...Or from a dmseg file"
# find_virtualization_dmesg
i=1
for expected in "" "" "" "" "" "Xen" "VirtualBox"; do
find_virtualization_dmesg "$samples/dmesg-00$i.txt" > "$TMPDIR/got"
is "$(cat "$TMPDIR/got")" "$expected" "dmesg-00$i.txt"
i=$(($i + 1))
done
# linux_exclusive_collection
fake_command () {
local cmd="$1"
local output="$2"
printf "#!/usr/bin/env bash\necho \"${output}\"\n" > "$TMPDIR/${cmd}_replacement"
chmod +x "$TMPDIR/${cmd}_replacement"
eval "CMD_$(echo $cmd | tr '[a-z]' '[A-Z]')=\"$TMPDIR/${cmd}_replacement\""
}
test_linux_exclusive_collection () {
local dir="$1"
# First, let's try what happens if none of the commands are available
local CMD_LVS=""
local CMD_VGS=""
local CMD_NETSTAT=""
local PT_SUMMARY_SKIP=""
mkdir "$dir/1"
linux_exclusive_collection "$dir/1"
is \
"$(ls "$dir/1" | grep 'lvs\|vgs\|netstat' )" \
"" \
'linux_exclusive_collection: If a command isnt available, doesnt create spurious files'
local i=1
for f in lvs vgs netstat; do
fake_command "$f" "ok $i"
i=$(($i + 1))
done
mkdir "$dir/2"
linux_exclusive_collection "$dir/2"
is \
"$(ls "${dir}/2" | grep 'lvs\|vgs\|netstat' | sort | xargs echo )" \
"lvs netstat vgs" \
"linux_exclusive_collection: And works as expected if they are there"
local i=1
for f in lvs vgs netstat; do
is \
"$(cat "${dir}/2/${f}")" \
"ok $i" \
"linux_exclusive_collection: output for $f is correct"
i=$(($i + 1))
done
}
platform="$(get_var platform "$p/summary")"
if [ "$platform" = "Linux" ]; then
mkdir "$TMPDIR/linux_data"
test_linux_exclusive_collection "$TMPDIR/linux_data"
else
skip 1 5 "Tests exclusive for Linux"
fi
# propietary_raid_controller
test_propietary_raid_controller () {
local dir="$1"
local CMD_ARCCONF=""
local CMD_HPACUCLI=""
local CMD_MEGACLI64=""
local controller=""
mkdir "$dir/1"
for controller in "AACRAID" "HP Smart Array" "LSI Logic MegaRAID SAS"; do
rm "$dir/1/summary" 2>/dev/null
touch "$dir/1/summary"
propietary_raid_controller "$dir/1/raid-controller" "$dir/1/summary" "$dir/1" "$controller"
is \
"$(get_var "internal::raid_opt" "$dir/1/summary")" \
0 \
"propietary_raid_controller: correct raid_opt default for $controller"
cmd_ok \
"grep -q 'RAID controller software not found' \"$dir/1/raid-controller\"" \
"propietary_raid_controller: correct default for $controller if the command isn't available"
done
mkdir "$dir/2"
fake_command arcconf "ok arcconf"
propietary_raid_controller "$dir/2/raid-controller" "$dir/2/summary" "$dir/2" "AACRAID"
is \
"$(get_var "internal::raid_opt" "$dir/2/summary")" \
1 \
"propietary_raid_controller: correct raid_opt default for $controller when arcconf is there"
is \
"$(cat "$dir/2/raid-controller")" \
"ok arcconf" \
"AACRAID calls arcconf"
}
mkdir "$TMPDIR/raid_controller"
test_propietary_raid_controller "$TMPDIR/raid_controller"
# notable_processes_info
(
sleep 50000
) 2>/dev/null &
forked_pid="$!"
if [ -e /proc/$forked_pid/oom_adj ] \
&& echo "-17" > /proc/$forked_pid/oom_adj 2>/dev/null; then
notable_processes_info "$TMPDIR/notable_procs"
like \
"$(cat "$TMPDIR/notable_procs")" \
"${forked_pid}\\s+-17" \
"notable_proccesses_info finds the process we manually changed earlier"
else
skip 1 1 "Either this OS doesn't have an oom, or this user doesn't have enough privileges to change the oom of other processes"
fi
disown $forked_pid
kill -9 $forked_pid
# dmidecode_system_info
test_dmidecode_system_info () {
local dir="$1"
local CMD_DMIDECODE=""
touch "$dir/outfile"
dmidecode_system_info "$dir/outfile"
cmd_ok '! test -s "$dir/outfile"' "If dmidecode isn't found, produces nothing"
fake_command dmidecode '[$@]'
dmidecode_system_info "$dir/outfile"
cat <<EOF >> "$dir/expected"
vendor [-s system-manufacturer]
system [-s system-manufacturer]; [-s system-product-name]; v[-s system-version] ([-s chassis-type])
servicetag [-s system-serial-number]
EOF
no_diff \
"$dir/outfile" \
"$dir/expected" \
"..but if it's there, it gets called with the expected parameters "
}
mkdir "$TMPDIR/dmidecode_system_info"
test_dmidecode_system_info "$TMPDIR/dmidecode_system_info"

View File

@@ -0,0 +1 @@
../../../util/test-bash-functions

View File

@@ -0,0 +1,241 @@
#!/usr/bin/env bash
plan 80
TMPFILE="$TEST_TMPDIR/parse-opts-output"
TOOL="pt-stalk"
TMPDIR="$TEST_TMPDIR"
source "$LIB_DIR/log_warn_die.sh"
source "$LIB_DIR/parse_options.sh"
# ############################################################################
# Parse options from POD using all default values.
# ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" 2>$TMPFILE
is "`cat $TMPFILE`" "" "No warnings or errors"
is "$OPT_STRING_OPT" "" "Default string option"
is "$OPT_STRING_OPT2" "foo" "Default string option with default"
is "$OPT_TYPELESS_OPTION" "" "Default typeless option"
is "$OPT_NOPTION" "yes" "Default neg option"
is "$OPT_INT_OPT" "" "Default int option"
is "$OPT_INT_OPT2" "42" "Default int option with default"
is "$OPT_VERSION" "" "--version"
# ############################################################################
# Specify some opts, but use default values for the rest.
# ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --int-opt 50 --typeless-option --string-opt bar
is "$OPT_STRING_OPT" "bar" "Specified string option (spec)"
is "$OPT_STRING_OPT2" "foo" "Default string option with default (spec)"
is "$OPT_TYPELESS_OPTION" "yes" "Specified typeless option (spec)"
is "$OPT_NOPTION" "yes" "Default neg option (spec)"
is "$OPT_INT_OPT" "50" "Specified int option (spec)"
is "$OPT_INT_OPT2" "42" "Default int option with default (spec)"
is "$OPT_VERSION" "" "--version (spec)"
is "$ARGV" "" "ARGV"
is "$EXT_ARGV" "" "External ARGV"
# ############################################################################
# --option=value should work like --option value.
# ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --int-opt=42
is "$OPT_INT_OPT" "42" "Specified int option (--option=value)"
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --string-opt="hello world"
is "$OPT_STRING_OPT" "hello world" "Specified int option (--option=\"value\")"
# ############################################################################
# Negate an option like --no-option.
# ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --no-noption
is "$OPT_STRING_OPT" "" "Default string option (neg)"
is "$OPT_STRING_OPT2" "foo" "Default string option with default (neg)"
is "$OPT_TYPELESS_OPTION" "" "Default typeless option (neg)"
is "$OPT_NOPTION" "" "Negated option (neg)"
is "$OPT_INT_OPT" "" "Default int option (neg)"
is "$OPT_INT_OPT2" "42" "Default int option with default (neg)"
is "$OPT_VERSION" "" "--version (neg)"
# ############################################################################
# Short form.
# ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" -v
is "$OPT_VERSION" "yes" "Short form"
# ############################################################################
# Command line options plus externals args.
# ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --no-noption -- --foo
is "$OPT_NOPTION" "" "Negated option (--)"
is "$ARGV" "" "ARGV (--)"
is "$EXT_ARGV" "--foo" "External ARGV (--)"
# ############################################################################
# An unknown option should produce an error.
# ############################################################################
# Have to call this in a subshell because the error will cause an exit.
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --foo >$TMPFILE 2>&1
cmd_ok "grep -q 'Unknown option: --foo' $TMPFILE" "Error on unknown option"
usage_or_errors "$T_LIB_DIR/samples/bash/po001.sh" >$TMPFILE 2>&1
err=$?
is "$err" "1" "Non-zero exit on unknown option"
# ###########################################################################
# --help
# ###########################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --help
usage_or_errors "$T_LIB_DIR/samples/bash/po001.sh" >$TMPFILE 2>&1
cmd_ok \
"grep -q \"For more information, 'man pt-stalk' or 'perldoc\" $TMPFILE" \
"--help"
cmd_ok \
"grep -q ' --string-opt2[ ]*String option with a default.' $TMPFILE" \
"Command line options"
cmd_ok \
"grep -q '\-\-string-opt[ ]*(No value)' $TMPFILE" \
"Options and values after processing arguments"
# Don't interpolate.
parse_options "$T_LIB_DIR/samples/bash/po003.sh" --help
usage_or_errors "$T_LIB_DIR/samples/bash/po003.sh" >$TMPFILE 2>&1
cmd_ok \
"grep -q 'Exit if the disk is less than this %full.' $TMPFILE" \
"Don't interpolate --help descriptions"
# ###########################################################################
# Config files.
# ###########################################################################
TOOL="pt-test"
cp "$T_LIB_DIR/samples/bash/config001.conf" "$HOME/.$TOOL.conf"
parse_options "$T_LIB_DIR/samples/bash/po001.sh"
is "$OPT_STRING_OPT" "abc" "Default string option (conf)"
is "$OPT_STRING_OPT2" "foo" "Default string option with default (conf)"
is "$OPT_TYPELESS_OPTION" "yes" "Default typeless option (conf)"
is "$OPT_NOPTION" "yes" "Default neg option (conf)"
is "$OPT_INT_OPT" "" "Default int option (conf)"
is "$OPT_INT_OPT2" "42" "Default int option with default (conf)"
is "$OPT_VERSION" "" "--version (conf)"
is "$ARGV" "" "ARGV (conf)"
is "$EXT_ARGV" "--host=127.1 --user=daniel" "External ARGV (conf)"
# Command line should override config file.
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --string-opt zzz
is "$OPT_STRING_OPT" "zzz" "Command line overrides config file"
# User-specified --config
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --config "$T_LIB_DIR/samples/bash/config003.conf" --string-opt bar
is "$OPT_STRING_OPT" "bar" "--config string option"
is "$OPT_STRING_OPT2" "foo" "--config string option2"
is "$OPT_TYPELESS_OPTION" "" "--config typeless option"
is "$OPT_NOPTION" "yes" "--config negatable option"
is "$OPT_INT_OPT" "123" "--config int option"
is "$OPT_INT_OPT2" "42" "--config int option2"
is "$OPT_VERSION" "" "--config version option"
is "$ARGV" "" "--config ARGV"
is "$EXT_ARGV" "" "--config External ARGV"
# Multiple --config files, last should take precedence.
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --config $T_LIB_DIR/samples/bash/config001.conf,$T_LIB_DIR/samples/bash/config002.conf
is "$OPT_STRING_OPT" "hello world" "Two --config string option"
is "$OPT_TYPELESS_OPTION" "yes" "Two --config typeless option"
is "$OPT_INT_OPT" "100" "Two --config int option"
is "$ARGV" "" "Two --config ARGV"
is "$EXT_ARGV" "--host=127.1 --user=daniel" "Two--config External ARGV"
# Spaces before and after the option[=value] lines.
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --config $T_LIB_DIR/samples/bash/config004.conf
is "$OPT_STRING_OPT" "foo" "Default string option (spacey)"
is "$OPT_TYPELESS_OPTION" "yes" "Default typeless option (spacey)"
is "$OPT_INT_OPT" "123" "Default int option (spacey)"
is "$ARGV" "" "ARGV (spacey)"
is "$EXT_ARGV" "" "External ARGV (spacey)"
# ############################################################################
# Option values with spaces.
# ############################################################################
# Config file
cp "$T_LIB_DIR/samples/bash/config002.conf" "$HOME/.$TOOL.conf"
parse_options "$T_LIB_DIR/samples/bash/po001.sh" ""
is "$OPT_STRING_OPT" "hello world" "Option value with space (conf)"
is "$OPT_INT_OPT" "100" "Option = value # comment (conf)"
rm "$HOME/.$TOOL.conf"
TOOL="pt-stalk"
# Command line
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --string-opt "hello world"
is "$OPT_STRING_OPT" "hello world" "Option value with space (cmd line)"
is "$ARGV" "" "ARGV (cmd line)"
is "$EXT_ARGV" "" "External ARGV (cmd line)"
# ############################################################################
# Size options.
# ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po004.sh" --disk-bytes-free 1T
is "$OPT_DISK_BYTES_FREE" "1099511627776" "Size: 1T"
parse_options "$T_LIB_DIR/samples/bash/po004.sh" --disk-bytes-free 1G
is "$OPT_DISK_BYTES_FREE" "1073741824" "Size: 1G"
parse_options "$T_LIB_DIR/samples/bash/po004.sh" --disk-bytes-free 1M
is "$OPT_DISK_BYTES_FREE" "1048576" "Size: 1M"
parse_options "$T_LIB_DIR/samples/bash/po004.sh" --disk-bytes-free 1K
is "$OPT_DISK_BYTES_FREE" "1024" "Size: 1K"
parse_options "$T_LIB_DIR/samples/bash/po004.sh" --disk-bytes-free 1k
is "$OPT_DISK_BYTES_FREE" "1024" "Size: 1k"
parse_options "$T_LIB_DIR/samples/bash/po004.sh" --disk-bytes-free 1
is "$OPT_DISK_BYTES_FREE" "1" "Size: 1"
parse_options "$T_LIB_DIR/samples/bash/po004.sh" --disk-bytes-free 100M
is "$OPT_DISK_BYTES_FREE" "104857600" "Size: 100M"
parse_options "$T_LIB_DIR/samples/bash/po004.sh"
is "$OPT_DISK_BYTES_FREE" "104857600" "Size: 100M default"
# ############################################################################
# --version
# ############################################################################
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --version
usage_or_errors "$T_LIB_DIR/samples/bash/po001.sh" >$TMPFILE 2>&1
is "$OPT_VERSION" "yes" "--version works"
is \
"$(cat "$TMPFILE")" \
"pt-fake 1.0.1" \
"And gets the correct output"
# ############################################################################
# Done
# ############################################################################

View File

@@ -128,23 +128,23 @@ no_diff "$TMPDIR/expected" "$TMPDIR/got" "summarize_binlogs"
# ###########################################################################
cat <<EOF > "$TMPDIR/expected"
master semisync status | 0
master semisync status |
master trace level | 32, net wait (more information about network waits)
master timeout in milliseconds | 10000
master waits for slaves | ON
master clients | 0
master net_avg_wait_time | 0
master net_wait_time | 0
master net_waits | 0
master no_times | 0
master no_tx | 0
master timefunc_failures | 0
master tx_avg_wait_time | 0
master tx_wait_time | 0
master tx_waits | 0
master wait_pos_backtraverse | 0
master wait_sessions | 0
master yes_tx | 0
master clients |
master net_avg_wait_time |
master net_wait_time |
master net_waits |
master no_times |
master no_tx |
master timefunc_failures |
master tx_avg_wait_time |
master tx_wait_time |
master tx_waits |
master wait_pos_backtraverse |
master wait_sessions |
master yes_tx |
EOF
_semi_sync_stats_for "master" "$samples/mysql-variables-with-semisync.txt" > "$TMPDIR/got"
@@ -267,36 +267,36 @@ is \
_NO_FALSE_NEGATIVES=1
cat <<EOF > $TMPDIR/expected
Port Data Directory Nice OOM Value Socket
===== ========================== ==== ========= ======
3306 /var/lib/mysql ? ? /var/run/mysqld/mysqld.sock
12345 /tmp/12345/data ? ? /tmp/12345/mysql_sandbox12345.sock
12346 /tmp/12346/data ? ? /tmp/12346/mysql_sandbox12346.sock
Port Data Directory Nice OOM Socket
===== ========================== ==== === ======
3306 /var/lib/mysql ? ? /var/run/mysqld/mysqld.sock
12345 /tmp/12345/data ? ? /tmp/12345/mysql_sandbox12345.sock
12346 /tmp/12346/data ? ? /tmp/12346/mysql_sandbox12346.sock
EOF
parse_mysqld_instances "$samples/ps-mysqld-001.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "ps-mysqld-001.txt"
cat <<EOF > "$TMPDIR/expected"
Port Data Directory Nice OOM Value Socket
===== ========================== ==== ========= ======
/var/lib/mysql ? ? /var/lib/mysql/mysql.sock
Port Data Directory Nice OOM Socket
===== ========================== ==== === ======
/var/lib/mysql ? ? /var/lib/mysql/mysql.sock
EOF
parse_mysqld_instances "$samples/ps-mysqld-002.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "ps-mysqld-002.txt"
#parse_mysqld_instances
cat <<EOF > $TMPDIR/expected
Port Data Directory Nice OOM Value Socket
===== ========================== ==== ========= ======
3306 /mnt/data-store/mysql/data ? ? /tmp/mysql.sock
Port Data Directory Nice OOM Socket
===== ========================== ==== === ======
3306 /mnt/data-store/mysql/data ? ? /tmp/mysql.sock
EOF
parse_mysqld_instances "$samples/ps-mysqld-003.txt" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "ps-mysqld-003.txt"
cat <<EOF > "$TMPDIR/expected"
Port Data Directory Nice OOM Value Socket
===== ========================== ==== ========= ======
/var/db/mysql ? ?
Port Data Directory Nice OOM Socket
===== ========================== ==== === ======
/var/db/mysql ? ?
EOF
cat <<EOF > "$TMPDIR/in"
@@ -304,7 +304,7 @@ mysql 767 0.0 0.9 3492 1100 v0 I 3:01PM 0:00.07 /bin/sh /usr/local
mysql 818 0.0 17.4 45292 20584 v0 I 3:01PM 0:02.28 /usr/local/libexec/mysqld --defaults-extra-file=/var/db/mysql/my.cnf --basedir=/usr/local --datadir=/var/db/mysql --user=mysql --log-error=/var/db/mysql/freebsd.hsd1.va.comcast.net..err --pid-file=/var/db/mysql/freebsd.hsd1.va.comcast.net..pid
EOF
parse_mysqld_instances "$TMPDIR/in" > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$TMPDIR/expected"
no_diff "$TMPDIR/got" "$TMPDIR/expected" "parse_mysqld_instances"
# ###########################################################################
# get_mysql_*
@@ -655,7 +655,7 @@ test_format_innodb () {
Page Size | 16k
Log File Size | 2 * 1.5G = 3.0G
Log Buffer Size | 8M
Flush Method | 0
Flush Method |
Flush Log At Commit | 1
XA Support | ON
Checksums | ON
@@ -670,7 +670,7 @@ test_format_innodb () {
Adaptive Checkpoint | estimate
EOF
section_innodb $samples/temp001/percona-toolkit-mysql-variables $samples/temp001/percona-toolkit-mysql-status > $TMPDIR/got
section_innodb "$samples/temp001/percona-toolkit-mysql-variables" $samples/temp001/percona-toolkit-mysql-status > $TMPDIR/got
no_diff $TMPDIR/expected $TMPDIR/got
}
@@ -689,8 +689,8 @@ format_innodb_filters_test () {
binlog_ignore_db | mysql,test
EOF
format_binlog_filters $samples/mysql-show-master-status-001.txt > $TMPDIR/got
no_diff $TMPDIR/got $TMPDIR/expected
format_binlog_filters "$samples/mysql-show-master-status-001.txt" > $TMPDIR/got
no_diff "$TMPDIR/got" "$TMPDIR/expected"
}
format_innodb_filters_test
@@ -703,9 +703,13 @@ OPT_SLEEP=1
OPT_DUMP_SCHEMAS="mysql"
NAME_VAL_LEN=25
_NO_FALSE_NEGATIVES=1
report_mysql_summary "$samples/tempdir" "percona-toolkit" | tail -n+3 > $TMPDIR/got
report_mysql_summary "$samples/tempdir" "percona-toolkit" | tail -n+3 > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$samples/expected_result_report_summary.txt"
OPT_SLEEP=10
report_mysql_summary "$samples/temp002" "percona-toolkit" 2>/dev/null | tail -n+3 > "$TMPDIR/got"
no_diff "$TMPDIR/got" "$samples/expected_output_temp002.txt"
# ###########################################################################
# Done
# ###########################################################################

File diff suppressed because it is too large Load Diff

212
t/lib/bash/report_system_info.t Executable file
View File

@@ -0,0 +1,212 @@
#!/usr/bin/env bash
# This script is a test harness and TAP producer for testing bash functions
# in a bash file. A bash and test file are sourced; the former provides the
# functions to test and the latter provides the testing.
# ############################################################################
# Standard startup, find the branch's root directory
# ############################################################################
LANG='en_US.UTF-8'
die() {
echo $1 >&2
exit 255
}
cwd="$PWD"
if [ -n "$PERCONA_TOOLKIT_BRANCH" ]; then
BRANCH=$PERCONA_TOOLKIT_BRANCH
cd $BRANCH
else
while [ ! -f Makefile.PL ] && [ $(pwd) != "/" ]; do
cd ..
done
if [ ! -f Makefile.PL ]; then
die "Cannot find the root directory of the Percona Toolkit branch"
exit 1
fi
BRANCH="$PWD"
fi
cd "$cwd"
BIN_DIR="$BRANCH/bin";
LIB_DIR="$BRANCH/lib/bash";
T_LIB_DIR="$BRANCH/t/lib";
SANDBOX_VERSION="$($BRANCH/sandbox/test-env version)"
# ############################################################################
# Paths
# ############################################################################
# Do not use TMPDIR because the tools use it for their own secure tmpdir.
TEST_TMPDIR="/tmp/percona-toolkit.test"
if [ ! -d $TEST_TMPDIR ]; then
mkdir $TEST_TMPDIR
fi
# ############################################################################
# Subroutines
# ############################################################################
# Load (count) the tests and print a TAP-style test plan.
load_tests() {
local test_files="$@"
local i=0
local n_tests=0
for t in $test_files; do
# Return unless the test file is bash. There may be other types of
# files in the tool's test dir.
if [ ! -f $t ]; then
continue
fi
head -n 1 $t | grep -q bash || continue
tests[$i]=$t
i=$((i + 1))
number_of_tests=$(grep --max-count 1 '^TESTS=[0-9]' $t | cut -d'=' -f2)
if [ -z "$number_of_tests" ]; then
n_tests=$(( $n_tests + 1 ))
else
n_tests=$(( $n_tests + $number_of_tests ))
fi
done
echo "1..$n_tests"
}
# Source a test file to run whatever it contains (hopefully tests!).
run_test() {
local t=$1 # test file name, e.g. "group-by-all-01" for pt-diskstats
rm -rf $TEST_TMPDIR/* >/dev/null 2>&1
# 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
# and run it. But the test file may have been given as a relative path,
# so run its basename after cd'ing to its directory. Then cd back in case
# other test files are in other dirs.
cwd="$PWD"
local t_dir=$(dirname $t)
TEST_FILE=$(basename $t)
cd $t_dir
source ./$TEST_FILE
cd $cwd
return $?
}
# Print a TAP-style test result.
result() {
local result=$1
local test_name=${2:-""}
if [ $result -eq 0 ]; then
echo "ok $testno - $TEST_FILE $test_name"
else
echo "not ok $testno - $TEST_FILE $test_name"
failed_tests=$(( failed_tests + 1))
echo "# Failed '$test_command'" >&2
if [ -f $TEST_TMPDIR/failed_result ]; then
cat $TEST_TMPDIR/failed_result | sed -e 's/^/# /' -e '30q' >&2
fi
fi
testno=$((testno + 1))
return $result
}
plan() {
local n_tests=${1:-""}
if [ "$n_tests" ]; then
echo "1..$n_tests"
fi
}
#
# The following subs are for the test files to call.
#
skip() {
local skip="$1"
local number_of_tests="$2"
local reason="${3:-""}"
if [ $skip ]; then
for n in $(seq $number_of_tests); do
result 0 "# skip $n $reason"
done
fi
}
# TODO unperlify
like() {
local got="$1"
local regex="$2"
local test_name=${3:-""}
test_command="$got =~ m<$regex>"
perl -e 'exit(scalar($ARGV[0] =~ m<^$ARGV[1]>msi ? 0 : 1))' "$got" "$regex"
result $? "$test_name"
}
no_diff() {
local got=$1
local expected=$2
local test_name=${3:-""}
test_command="diff $got $expected"
eval $test_command > $TEST_TMPDIR/failed_result 2>&1
result $? "$test_name"
}
is() {
local got=$1
local expected=$2
local test_name=${3:-""}
test_command="\"$got\" == \"$expected\""
test "$got" = "$expected"
result $? "$test_name"
}
cmd_ok() {
local test_command=$1
local test_name=${2:-""}
eval $test_command
result $? "$test_name"
}
# ############################################################################
# Script starts here
# ############################################################################
testno=1
failed_tests=0
if [ $# -eq 0 ]; then
TEST_FILE=$(basename "$0")
TEST="${TEST_FILE%".t"}"
source "$BRANCH/t/lib/bash/$TEST.sh"
else
if [ $# -lt 2 ]; then
die "Usage: test-bash-functions FILE TESTS"
fi
# Check and source the bash file. This is the code being tested.
# All its global vars and subs will be imported.
bash_file=$1
shift
if [ ! -f "$bash_file" ]; then
die "$bash_file does not exist"
fi
head -n1 $bash_file | grep -q -E 'bash|sh' || die "$bash_file is not a bash file"
source $bash_file
# Load (count) the tests so that we can write a TAP test plan like 1..5
# for expecting 5 tests. Perl prove needs this.
declare -a tests
load_tests "$@"
# Run the test files.
for t in "${tests[@]}"; do
run_test $t
done
fi
rm -rf $TEST_TMPDIR
exit $failed_tests

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
plan 3
TMPDIR="$TEST_TMPDIR"
PATH="$PATH:$PERCONA_TOOLKIT_SANDBOX/bin"
. "$LIB_DIR/summary_common.sh"
p="$TMPDIR/get_var_samples"
echo "test1 abcdef" > "$p"
is \
"$(get_var test1 "$p")" \
"abcdef" \
"Sanity check, get_var works"
echo "test2 abc def" > "$p"
is \
"$(get_var test2 "$p")" \
"abc def" \
"get_var works even if the value has spaces"
echo "test::1 abcdef" > "$p"
is \
"$(get_var "test::1" "$p")" \
"abcdef" \
"get_var works if the 'key' has colons"

1
t/lib/bash/summary_common.t Symbolic link
View File

@@ -0,0 +1 @@
../../../util/test-bash-functions