mirror of
https://github.com/percona/percona-toolkit.git
synced 2026-02-01 03:02:53 +08:00
Replace test-bash-tool with test-bash-functions. Begin revamping the Bash tests.
This commit is contained in:
@@ -341,19 +341,22 @@ parse_filesystems () {
|
||||
# requires two passes through the file. The first pass finds the max size of
|
||||
# these columns and prints out a printf spec, and the second prints out the
|
||||
# file nicely aligned.
|
||||
cat > /tmp/pt-summary.awk <<-EOF
|
||||
local file=$1
|
||||
local platform=$2
|
||||
|
||||
local spec=$(awk '
|
||||
BEGIN {
|
||||
device = 10;
|
||||
fstype = 4;
|
||||
options = 4;
|
||||
}
|
||||
/./ {
|
||||
f_device = \$1;
|
||||
f_fstype = \$10;
|
||||
f_options = substr(\$11, 2, length(\$11) - 2);
|
||||
f_device = $1;
|
||||
f_fstype = $10;
|
||||
f_options = substr($11, 2, length($11) - 2);
|
||||
if ( "$2" == "FreeBSD" ) {
|
||||
f_fstype = substr(\$9, 2, length(\$9) - 2);
|
||||
f_options = substr(\$0, index(\$0, ",") + 2);
|
||||
f_fstype = substr($9, 2, length($9) - 2);
|
||||
f_options = substr($0, index($0, ",") + 2);
|
||||
f_options = substr(f_options, 1, length(f_options) - 1);
|
||||
}
|
||||
if ( length(f_device) > device ) {
|
||||
@@ -369,28 +372,24 @@ parse_filesystems () {
|
||||
END{
|
||||
print "%-" device "s %5s %4s %-" fstype "s %-" options "s %s";
|
||||
}
|
||||
EOF
|
||||
spec="$( awk -f /tmp/pt-summary.awk "$1" )";
|
||||
#awk -f /tmp/pt-summary.awk "$1"
|
||||
#return;
|
||||
' $file)
|
||||
|
||||
cat > /tmp/pt-summary.awk <<-EOF
|
||||
awk "
|
||||
BEGIN {
|
||||
spec=" ${spec}\\n";
|
||||
printf spec, "Filesystem", "Size", "Used", "Type", "Opts", "Mountpoint";
|
||||
spec=\" ${spec}\\n\";
|
||||
printf spec, \"Filesystem\", \"Size\", \"Used\", \"Type\", \"Opts\", \"Mountpoint\";
|
||||
}
|
||||
{
|
||||
f_fstype = \$10;
|
||||
f_options = substr(\$11, 2, length(\$11) - 2);
|
||||
if ( "$2" == "FreeBSD" ) {
|
||||
if ( \"$2\" == \"FreeBSD\" ) {
|
||||
f_fstype = substr(\$9, 2, length(\$9) - 2);
|
||||
f_options = substr(\$0, index(\$0, ",") + 2);
|
||||
f_options = substr(\$0, index(\$0, \",\") + 2);
|
||||
f_options = substr(f_options, 1, length(f_options) - 1);
|
||||
}
|
||||
printf spec, \$1, \$2, \$5, f_fstype, f_options, \$6;
|
||||
}
|
||||
EOF
|
||||
awk -f /tmp/pt-summary.awk "$1"
|
||||
" $file
|
||||
}
|
||||
|
||||
# ##############################################################################
|
||||
@@ -766,15 +765,16 @@ parse_lsi_megaraid_virtual_devices () {
|
||||
# system activity is enough.
|
||||
# ##############################################################################
|
||||
format_vmstat () {
|
||||
cat > /tmp/pt-summary.awk <<-EOF
|
||||
local file=$1
|
||||
awk "
|
||||
BEGIN {
|
||||
format = " %2s %2s %4s %4s %5s %5s %6s %6s %3s %3s %3s %3s %3s\n";
|
||||
format = \" %2s %2s %4s %4s %5s %5s %6s %6s %3s %3s %3s %3s %3s\n\";
|
||||
}
|
||||
/procs/ {
|
||||
print " procs ---swap-- -----io---- ---system---- --------cpu--------";
|
||||
print \" procs ---swap-- -----io---- ---system---- --------cpu--------\";
|
||||
}
|
||||
/bo/ {
|
||||
printf format, "r", "b", "si", "so", "bi", "bo", "ir", "cs", "us", "sy", "il", "wa", "st";
|
||||
printf format, \"r\", \"b\", \"si\", \"so\", \"bi\", \"bo\", \"ir\", \"cs\", \"us\", \"sy\", \"il\", \"wa\", \"st\";
|
||||
}
|
||||
\$0 !~ /r/ {
|
||||
fuzzy_var = \$1; ${fuzzy_formula} r = fuzzy_var;
|
||||
@@ -792,9 +792,8 @@ format_vmstat () {
|
||||
fuzzy_var = \$17; st = fuzzy_var;
|
||||
printf format, r, b, si, so, bi, bo, ir, cs, us, sy, il, wa, st;
|
||||
}
|
||||
EOF
|
||||
awk -f /tmp/pt-summary.awk /tmp/percona-toolkit
|
||||
}
|
||||
" $file
|
||||
}
|
||||
|
||||
# ##############################################################################
|
||||
# The main() function is called at the end of the script. This makes it
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#!/bin/bash
|
||||
#parse_filesystems samples/df-mount-001.txt Linux
|
||||
|
||||
cat <<EOF > $1
|
||||
Filesystem Size Used Type Opts Mountpoint
|
||||
/dev/mapper/vg_ginger-lv_root 454G 6% ext4 rw /
|
||||
/dev/sda1 194M 31% ext4 rw /boot
|
||||
tmpfs 2.0G 1% tmpfs rw /dev/shm
|
||||
EOF
|
||||
@@ -1,10 +0,0 @@
|
||||
#!/bin/bash
|
||||
#parse_filesystems samples/df-mount-002.txt Linux
|
||||
|
||||
cat <<EOF > $1
|
||||
Filesystem Size Used Type Opts Mountpoint
|
||||
/dev/mapper/VolGroup00-LogVol00 62G 56% ext3 rw /
|
||||
/dev/mapper/VolGroup01-MySQLData00 67G 20% ext3 rw /var/lib/mysql
|
||||
/dev/sda3 190M 11% ext3 rw /boot
|
||||
tmpfs 7.9G 0% tmpfs rw /dev/shm
|
||||
EOF
|
||||
26
t/pt-summary/parse_filesystems.sh
Normal file
26
t/pt-summary/parse_filesystems.sh
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This test file must be ran by util/test-bash-functions.
|
||||
|
||||
TESTS=2
|
||||
|
||||
TEST_NAME="df-mount-001.txt"
|
||||
cat <<EOF > $TMPDIR/expected
|
||||
Filesystem Size Used Type Opts Mountpoint
|
||||
/dev/mapper/vg_ginger-lv_root 454G 6% ext4 rw /
|
||||
/dev/sda1 194M 31% ext4 rw /boot
|
||||
tmpfs 2.0G 1% tmpfs rw /dev/shm
|
||||
EOF
|
||||
parse_filesystems "samples/df-mount-001.txt" "Linux" > $TMPDIR/got
|
||||
no_diff $TMPDIR/got $TMPDIR/expected
|
||||
|
||||
TEST_NAME="df-mount-002.txt"
|
||||
cat <<EOF > $TMPDIR/expected
|
||||
Filesystem Size Used Type Opts Mountpoint
|
||||
/dev/mapper/VolGroup00-LogVol00 62G 56% ext3 rw /
|
||||
/dev/mapper/VolGroup01-MySQLData00 67G 20% ext3 rw /var/lib/mysql
|
||||
/dev/sda3 190M 11% ext3 rw /boot
|
||||
tmpfs 7.9G 0% tmpfs rw /dev/shm
|
||||
EOF
|
||||
parse_filesystems "samples/df-mount-002.txt" "Linux" > $TMPDIR/got
|
||||
no_diff $TMPDIR/got $TMPDIR/expected
|
||||
@@ -12,6 +12,6 @@ use English qw(-no_match_vars);
|
||||
|
||||
use PerconaTest;
|
||||
|
||||
system("$trunk/util/test-bash-tool pt-summary @ARGV");
|
||||
system("$trunk/util/test-bash-functions pt-summary @ARGV");
|
||||
|
||||
exit;
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
#!/bin/bash
|
||||
#format_vmstat
|
||||
|
||||
cat <<EOF > $1
|
||||
cat <<EOF > $TMPDIR/in
|
||||
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
|
||||
r b swpd free buff cache si so bi bo in cs us sy id wa st
|
||||
0 0 68 288220 372588 13534140 0 0 24 138 1 1 4 1 94 1 0
|
||||
1 0 68 288080 372588 13534184 0 0 0 187 721 943 3 0 96 0 0
|
||||
0 0 68 287708 372588 13534276 0 0 0 1058 747 992 8 3 89 0 0
|
||||
1 0 68 287468 372588 13534340 0 0 0 1058 552 856 15 2 84 0 0
|
||||
1 0 68 287460 372588 13534388 0 0 0 322 859 1014 10 3 87 0 0
|
||||
0 0 68 287460 372588 13534444 0 0 0 214 612 729 10 3 88 0 0
|
||||
0 0 68 287460 372588 13534484 0 0 0 309 553 741 0 0 99 0 0
|
||||
1 0 68 287460 372588 13534532 0 0 2 197 605 727 2 1 97 0 0
|
||||
0 0 68 287344 372588 13534596 0 0 0 1037 485 626 0 0 99 0 0
|
||||
0 0 68 287220 372588 13534656 0 0 0 235 875 1004 0 0 99 0 0
|
||||
EOF
|
||||
|
||||
cat <<EOF > $TMPDIR/expected
|
||||
procs ---swap-- -----io---- ---system---- --------cpu--------
|
||||
r b si so bi bo ir cs us sy il wa st
|
||||
0 0 0 0 25 150 1 1 4 1 94 1 0
|
||||
@@ -16,17 +30,5 @@ cat <<EOF > $1
|
||||
0 0 0 0 0 225 900 1000 0 0 99 0 0
|
||||
EOF
|
||||
|
||||
cat <<EOF > $2
|
||||
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
|
||||
r b swpd free buff cache si so bi bo in cs us sy id wa st
|
||||
0 0 68 288220 372588 13534140 0 0 24 138 1 1 4 1 94 1 0
|
||||
1 0 68 288080 372588 13534184 0 0 0 187 721 943 3 0 96 0 0
|
||||
0 0 68 287708 372588 13534276 0 0 0 1058 747 992 8 3 89 0 0
|
||||
1 0 68 287468 372588 13534340 0 0 0 1058 552 856 15 2 84 0 0
|
||||
1 0 68 287460 372588 13534388 0 0 0 322 859 1014 10 3 87 0 0
|
||||
0 0 68 287460 372588 13534444 0 0 0 214 612 729 10 3 88 0 0
|
||||
0 0 68 287460 372588 13534484 0 0 0 309 553 741 0 0 99 0 0
|
||||
1 0 68 287460 372588 13534532 0 0 2 197 605 727 2 1 97 0 0
|
||||
0 0 68 287344 372588 13534596 0 0 0 1037 485 626 0 0 99 0 0
|
||||
0 0 68 287220 372588 13534656 0 0 0 235 875 1004 0 0 99 0 0
|
||||
EOF
|
||||
format_vmstat $TMPDIR/in > $TMPDIR/got
|
||||
no_diff $TMPDIR/got $TMPDIR/expected
|
||||
|
||||
148
util/test-bash-functions
Executable file
148
util/test-bash-functions
Executable file
@@ -0,0 +1,148 @@
|
||||
#!/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
|
||||
# ############################################################################
|
||||
|
||||
die() {
|
||||
echo $1 >&2
|
||||
exit 255
|
||||
}
|
||||
|
||||
(
|
||||
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
|
||||
)
|
||||
|
||||
# ############################################################################
|
||||
# Paths
|
||||
# ############################################################################
|
||||
|
||||
TMPDIR="/tmp/percona-toolkit.test"
|
||||
if [ ! -d $TMPDIR ]; then
|
||||
mkdir $TMPDIR
|
||||
fi
|
||||
|
||||
# ############################################################################
|
||||
# Subroutines
|
||||
# ############################################################################
|
||||
|
||||
# Load (count) the tests and print a TAP-style test plan.
|
||||
load_tests() {
|
||||
local test_files="$@"
|
||||
local i=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
|
||||
number_of_tests=$(grep --max-count 1 '^TESTS=[0-9]' $t | cut -d'=' -f2)
|
||||
if [ -z "$number_of_tests" ]; then
|
||||
i=$(( i + 1 ))
|
||||
else
|
||||
i=$(( i + $number_of_tests ))
|
||||
fi
|
||||
done
|
||||
echo "1..$i"
|
||||
}
|
||||
|
||||
# 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 $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)
|
||||
local t_file=$(basename $t)
|
||||
cd $t_dir
|
||||
source $t_file
|
||||
cd $cwd
|
||||
|
||||
return $?
|
||||
}
|
||||
|
||||
# Print a TAP-style test result.
|
||||
result() {
|
||||
local result=$1
|
||||
local test_name=${TEST_NAME:-"$t"}
|
||||
if [ $result -eq 0 ]; then
|
||||
echo "ok $testno - $test_name"
|
||||
else
|
||||
echo "not ok $testno - $test_name"
|
||||
failed_tests=$(( failed_tests + 1))
|
||||
echo "# Failed '$test_command'" >&2
|
||||
cat $TMPDIR/failed_result | sed -e 's/^/# /' -e '30q' >&2
|
||||
fi
|
||||
testno=$(( testno + 1))
|
||||
return $result
|
||||
}
|
||||
|
||||
#
|
||||
# The following subs are for the test files to call.
|
||||
#
|
||||
|
||||
no_diff() {
|
||||
local got=$1
|
||||
local expected=$2
|
||||
local test_command="diff $got $expected"
|
||||
eval $test_command > $TMPDIR/failed_result 2>&1
|
||||
result $?
|
||||
}
|
||||
|
||||
# ############################################################################
|
||||
# Script starts here
|
||||
# ############################################################################
|
||||
|
||||
if [ $# -lt 2 ]; then
|
||||
die "Usage: test-back-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.
|
||||
testno=1
|
||||
failed_tests=0
|
||||
for t in "${tests[@]}"; do
|
||||
run_test $t
|
||||
done
|
||||
|
||||
rm -rf $TMPDIR
|
||||
|
||||
exit $failed_tests
|
||||
@@ -1,120 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
die() {
|
||||
echo $1 >&2
|
||||
exit 255
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
result() {
|
||||
result=$1
|
||||
if [ $result -eq 0 ]; then
|
||||
echo "ok $testno - $t"
|
||||
else
|
||||
echo "not ok $testno - $t"
|
||||
failed_tests=$(( failed_tests + 1))
|
||||
# Indent and display the first 30 lines
|
||||
diff "${GL_result}" "${GL_expected}" | sed -e 's/^/# /' -e '30q'
|
||||
fi
|
||||
|
||||
testno=$(( testno + 1))
|
||||
}
|
||||
|
||||
# All variables are named GL_whatever so they don't get overwritten with stuff
|
||||
# that happens in the functions sourced.
|
||||
GL_input="/tmp/percona-toolkit"
|
||||
GL_expected="/tmp/percona-toolkit-reference"
|
||||
GL_result="/tmp/percona-toolkit-result"
|
||||
|
||||
run_test() {
|
||||
t=$1 # test file name, e.g. "group-by-all-01" for pt-diskstats
|
||||
|
||||
# The format of the test file is as follows:
|
||||
# - line 1 is the shebang
|
||||
# - line 2 is the command to run, commented out
|
||||
# - argument 1 is the file to which the expected result should be printed
|
||||
# - argument 2 is the file to which the input should be printed
|
||||
|
||||
# Get the command to run
|
||||
GL_cmd="$(head -n2 $t | tail -n1 | cut -b2-)"
|
||||
|
||||
# Execute the file and tell it where to save the input & expected result
|
||||
if [ -x "${t}" ]; then
|
||||
./$t "${GL_expected}" "${GL_input}"
|
||||
else
|
||||
die "$t is not executable"
|
||||
fi
|
||||
|
||||
# Execute the command
|
||||
${GL_cmd} > "${GL_result}"
|
||||
|
||||
# Is the result the same as the expected result?
|
||||
diff -q "${GL_result}" "${GL_expected}" >/dev/null
|
||||
result $?
|
||||
|
||||
# Clean up
|
||||
rm -f /tmp/percona-toolkit*
|
||||
}
|
||||
|
||||
load_tests() {
|
||||
test_files="$@"
|
||||
i=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 ))
|
||||
done
|
||||
echo "1..$i"
|
||||
}
|
||||
|
||||
tool=$1 # bash tool, e.g. pt-diskstats
|
||||
t=$2 # optional test file, e.g. group-by-all-01
|
||||
if [ -z "$tool" ]; then
|
||||
die "No tool specified"
|
||||
fi
|
||||
|
||||
cd $BRANCH/bin
|
||||
if [ ! -f $tool ]; then
|
||||
die "$tool does not exist"
|
||||
fi
|
||||
head -n1 $tool | grep -q -E 'bash|sh' || die "$tool is not a bash file"
|
||||
|
||||
# Source the tool, i.e. import its functions.
|
||||
source $tool
|
||||
|
||||
cd $BRANCH/t/$tool
|
||||
testno=1
|
||||
failed_tests=0
|
||||
declare -a tests
|
||||
if [ -z "$t" ]; then
|
||||
# Run all the tool's tests.
|
||||
load_tests *
|
||||
else
|
||||
# Run just the specified test.
|
||||
load_tests $t
|
||||
fi
|
||||
|
||||
for t in "${tests[@]}"; do
|
||||
run_test $t
|
||||
done
|
||||
|
||||
exit $failed_tests
|
||||
Reference in New Issue
Block a user