mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-10 21:19:59 +00:00
More parse_options tests. Make --opt yes have value "yes" instead of "1". Change ok() to cmd_ok() in test-bash-functions. Mimic Perl modulue headers in Bash libs so update-modules will work with the latter.
This commit is contained in:
@@ -15,14 +15,15 @@
|
||||
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
# Place, Suite 330, Boston, MA 02111-1307 USA.
|
||||
# ###########################################################################
|
||||
# Begin log_warn_die lib
|
||||
# log_warn_die package
|
||||
# ###########################################################################
|
||||
|
||||
# Library: log_warn_die
|
||||
# Package: log_warn_die
|
||||
# log_warn_die provides standard log(), warn(), and die() subs.
|
||||
|
||||
set -u
|
||||
|
||||
# Global variables.
|
||||
EXIT_STATUS=0
|
||||
|
||||
log() {
|
||||
@@ -41,5 +42,5 @@ die() {
|
||||
}
|
||||
|
||||
# ###########################################################################
|
||||
# End log_warn_die lib
|
||||
# End log_warn_die package
|
||||
# ###########################################################################
|
||||
|
@@ -15,10 +15,10 @@
|
||||
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
# Place, Suite 330, Boston, MA 02111-1307 USA.
|
||||
# ###########################################################################
|
||||
# Begin parse_options lib
|
||||
# parse_options package
|
||||
# ###########################################################################
|
||||
|
||||
# Library: parse_options
|
||||
# Package: parse_options
|
||||
# parse_options parses Perl POD options from Bash tools and creates
|
||||
# global variables for each option.
|
||||
|
||||
@@ -36,7 +36,7 @@ declare EXT_ARGV # everything after -- (args for an external command)
|
||||
# file - Program file with Perl POD which has usage and options.
|
||||
#
|
||||
# Required Global Variables:
|
||||
# TIMDIR - Temp directory.
|
||||
# TIMDIR - Temp directory set by <set_TMPDIR()>.
|
||||
# TOOL - Tool's name.
|
||||
#
|
||||
# Optional Global Variables:
|
||||
@@ -66,9 +66,9 @@ usage() {
|
||||
# file - Program file with Perl POD options.
|
||||
#
|
||||
# Required Global Variables:
|
||||
# TIMDIR - Temp directory.
|
||||
# TIMDIR - Temp directory set by <set_TMPDIR()>.
|
||||
#
|
||||
# Declared Global Variables:
|
||||
# Set Global Variables:
|
||||
# This sub decalres a global var for each option by uppercasing the
|
||||
# option, removing the option's leading --, changing all - to _, and
|
||||
# prefixing with "OPT_". E.g. --foo-bar becomes OPT_FOO_BAR.
|
||||
@@ -121,7 +121,7 @@ parse_options() {
|
||||
while read spec; do
|
||||
opt=$(echo $spec | cut -d',' -f1 | sed 's/-/_/g' | tr [:lower:] [:upper:])
|
||||
default=$(echo $spec | cut -d',' -f4)
|
||||
eval "$opt"="$default"
|
||||
eval "OPT_${opt}"="$default"
|
||||
done < <(cat $TMPDIR/options)
|
||||
|
||||
for opt; do
|
||||
@@ -156,7 +156,7 @@ parse_options() {
|
||||
fi
|
||||
opt=$(echo $spec | cut -d',' -f1)
|
||||
required_arg=$(echo $spec | cut -d',' -f3)
|
||||
val=1
|
||||
val="yes"
|
||||
if [ -n "$required_arg" ]; then
|
||||
if [ $# -eq 0 ]; then
|
||||
die "--$opt requires a $required_arg argument"
|
||||
@@ -166,10 +166,10 @@ parse_options() {
|
||||
fi
|
||||
fi
|
||||
opt=$(echo $opt | sed 's/-/_/g' | tr [:lower:] [:upper:])
|
||||
eval "$opt"="$val"
|
||||
eval "OPT_${opt}"="$val"
|
||||
done
|
||||
}
|
||||
|
||||
# ###########################################################################
|
||||
# End parse_options lib
|
||||
# End parse_options package
|
||||
# ###########################################################################
|
||||
|
@@ -15,17 +15,26 @@
|
||||
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
# Place, Suite 330, Boston, MA 02111-1307 USA.
|
||||
# ###########################################################################
|
||||
# Begin tmpdir lib
|
||||
# tmpdir package
|
||||
# ###########################################################################
|
||||
|
||||
# Library: tmpdir
|
||||
# Package: tmpdir
|
||||
# tmpdir make a secure temporary directory using mktemp.
|
||||
|
||||
set -u
|
||||
|
||||
# Global variables.
|
||||
TMPDIR=""
|
||||
OPT_TMPDIR={OPT_TMPDIR:""}
|
||||
|
||||
# Sub: set_TMPDIR
|
||||
# Create a secure tmpdir and set TMPDIR.
|
||||
#
|
||||
# Optional Global Variables:
|
||||
# OPT_TMPDIR - User-specified --tmpdir (default none).
|
||||
#
|
||||
# Set Global Variables:
|
||||
# TMPDIR - Absolute path of secure temp directory.
|
||||
set_TMPDIR() {
|
||||
if [ -n "$OPT_TMPDIR" ]; then
|
||||
TMPDIR="$OPT_TMPDIR"
|
||||
@@ -38,6 +47,14 @@ set_TMPDIR() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Sub: rm_TMPDIR
|
||||
# Remove the tmpdir and unset TMPDIR.
|
||||
#
|
||||
# Optional Global Variables:
|
||||
# TMPDIR - TMPDIR set by <set_TMPDIR()>.
|
||||
#
|
||||
# Set Global Variables:
|
||||
# TMPDIR - Set to "".
|
||||
rm_TMPDIR() {
|
||||
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then
|
||||
rm -rf $TMPDIR
|
||||
@@ -46,5 +63,5 @@ rm_TMPDIR() {
|
||||
}
|
||||
|
||||
# ###########################################################################
|
||||
# End tmpdir lib
|
||||
# End tmpdir package
|
||||
# ###########################################################################
|
||||
|
0
t/lib/bash.t
Executable file → Normal file
0
t/lib/bash.t
Executable file → Normal file
@@ -1,25 +1,81 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
TESTS=17
|
||||
TESTS=37
|
||||
|
||||
TMPFILE="$TEST_TMPDIR/parse-opts-output"
|
||||
|
||||
source "$LIB_DIR/log_warn_die.sh"
|
||||
source "$LIB_DIR/parse_options.sh"
|
||||
|
||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh"
|
||||
# ############################################################################
|
||||
# Parse options from POD using all default values.
|
||||
# ############################################################################
|
||||
|
||||
is "$THRESHOLD" "100"
|
||||
is "$VARIABLE" "Threads_connected"
|
||||
is "$CYCLES" "1"
|
||||
is "$GDB" "no"
|
||||
is "$OPROFILE" "yes"
|
||||
is "$STRACE" "no"
|
||||
is "$TCPDUMP" "yes"
|
||||
is "$EMAIL" ""
|
||||
is "$INTERVAL" "30"
|
||||
is "$MAYBE_EMPTY" "no"
|
||||
is "$COLLECT" "${HOME}/bin/pt-collect"
|
||||
is "$DEST" "${HOME}/collected/"
|
||||
is "$DURATION" "30"
|
||||
is "$SLEEP" "300"
|
||||
is "$PCT_THRESHOLD" "95"
|
||||
is "$MB_THRESHOLD" "100"
|
||||
is "$PURGE" "30"
|
||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" "" 2>$TMPFILE
|
||||
|
||||
TEST_NAME="No warnings or errors"
|
||||
is "`cat $TMPFILE`" ""
|
||||
|
||||
TEST_NAME="Default opts"
|
||||
is "$OPT_THRESHOLD" "100"
|
||||
is "$OPT_VARIABLE" "Threads_connected"
|
||||
is "$OPT_CYCLES" "1"
|
||||
is "$OPT_GDB" "no"
|
||||
is "$OPT_OPROFILE" "yes"
|
||||
is "$OPT_STRACE" "no"
|
||||
is "$OPT_TCPDUMP" "yes"
|
||||
is "$OPT_EMAIL" ""
|
||||
is "$OPT_INTERVAL" "30"
|
||||
is "$OPT_MAYBE_EMPTY" "no"
|
||||
is "$OPT_COLLECT" "${HOME}/bin/pt-collect"
|
||||
is "$OPT_DEST" "${HOME}/collected/"
|
||||
is "$OPT_DURATION" "30"
|
||||
is "$OPT_SLEEP" "300"
|
||||
is "$OPT_PCT_THRESHOLD" "95"
|
||||
is "$OPT_MB_THRESHOLD" "100"
|
||||
is "$OPT_PURGE" "30"
|
||||
|
||||
# ############################################################################
|
||||
# Specify some opts, but use default values for the rest.
|
||||
# ############################################################################
|
||||
|
||||
parse_options "$T_LIB_DIR/samples/bash/po001.sh" --threshold 50 --gdb yes --email user@example.com
|
||||
|
||||
TEST_NAME="User-specified opts with defaults"
|
||||
is "$OPT_THRESHOLD" "50" # specified
|
||||
is "$OPT_VARIABLE" "Threads_connected"
|
||||
is "$OPT_CYCLES" "1"
|
||||
is "$OPT_GDB" "yes" # specified
|
||||
is "$OPT_OPROFILE" "yes"
|
||||
is "$OPT_STRACE" "no"
|
||||
is "$OPT_TCPDUMP" "yes"
|
||||
is "$OPT_EMAIL" "user@example.com" # specified
|
||||
is "$OPT_INTERVAL" "30"
|
||||
is "$OPT_MAYBE_EMPTY" "no"
|
||||
is "$OPT_COLLECT" "${HOME}/bin/pt-collect"
|
||||
is "$OPT_DEST" "${HOME}/collected/"
|
||||
is "$OPT_DURATION" "30"
|
||||
is "$OPT_SLEEP" "300"
|
||||
is "$OPT_PCT_THRESHOLD" "95"
|
||||
is "$OPT_MB_THRESHOLD" "100"
|
||||
is "$OPT_PURGE" "30"
|
||||
|
||||
# ############################################################################
|
||||
# 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
|
||||
)
|
||||
local err=$?
|
||||
TEST_NAME="Non-zero exit on unknown option"
|
||||
is "$err" "1"
|
||||
|
||||
TEST_NAME="Error on unknown option"
|
||||
cmd_ok "grep -q 'Unknown option: foo' $TMPFILE"
|
||||
|
||||
# ############################################################################
|
||||
# Done
|
||||
# ############################################################################
|
||||
exit
|
||||
|
@@ -10,13 +10,13 @@ is "$TMPDIR" ""
|
||||
|
||||
TEST_NAME="set_TMPDIR makes secure tmpdir"
|
||||
set_TMPDIR
|
||||
ok "test -d $TMPDIR"
|
||||
cmd_ok "test -d $TMPDIR"
|
||||
|
||||
tmpdir=$TMPDIR;
|
||||
|
||||
TEST_NAME="rm_TMPDIR"
|
||||
rm_TMPDIR
|
||||
ok "test ! -d $tmpdir"
|
||||
cmd_ok "test ! -d $tmpdir"
|
||||
|
||||
TEST_NAME="rm_TMPDIR resets TMPDIR"
|
||||
is "$TMPDIR" ""
|
||||
@@ -28,17 +28,17 @@ TEST_NAME="TMPDIR not defined"
|
||||
is "$TMPDIR" ""
|
||||
|
||||
TEST_NAME="--tmpdir does not exist yet"
|
||||
ok "test ! -d $OPT_TMPDIR"
|
||||
cmd_ok "test ! -d $OPT_TMPDIR"
|
||||
|
||||
set_TMPDIR
|
||||
TEST_NAME="set_TMPDIR uses --tmpdir"
|
||||
is "$TMPDIR" "/tmp/use--tmpdir"
|
||||
|
||||
TEST_NAME="set_TMPDIR creates --tmpdir"
|
||||
ok "test -d $TMPDIR"
|
||||
cmd_ok "test -d $TMPDIR"
|
||||
|
||||
tmpdir=$TMPDIR;
|
||||
|
||||
TEST_NAME="rm_TMPDIR removes --tmpdir"
|
||||
rm_TMPDIR
|
||||
ok "test ! -d $tmpdir"
|
||||
cmd_ok "test ! -d $tmpdir"
|
||||
|
@@ -128,9 +128,9 @@ is() {
|
||||
result $?
|
||||
}
|
||||
|
||||
ok() {
|
||||
cmd_ok() {
|
||||
local test_command=$1
|
||||
$test_command
|
||||
eval $test_command
|
||||
result $?
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user