Commit ff41f1f9 authored by Paul Cammish's avatar Paul Cammish
Browse files

Basic testing system

parent 79c1bd86
_debug() {
if [ $DEBUG ]; then
echo -e "\033[2mDEBUG: $@\033[0m"
_log "DEBUG: $@"
fi
}
_echo() {
if [ $DEBUG ]; then
echo " INFO: $@"
elif [ $VERBOSE ]; then
echo "$@"
else
_log "$@"
fi
}
_error() {
echo -e "\033[1m\033[31mERROR:\033[0m $@"
_log "ERROR: $@"
}
_failure() {
if [ $QUIET ]; then
echo -ne "\033[1m\033[31mF\033[0m"
fi
}
_log() {
echo "$@" >> $LOG
}
_parse_parameters() {
PARAMETERS=()
while [ $# -gt 0 ]; do
key="$1"
case $key in
--all|-a)
ALL=true
shift
;;
--log|-l)
LOG="$2"
shift ; shift
;;
--path)
TEST_PATH="$2"
shift ; shift
;;
--cron)
unset VERBOSE
shift
;;
--quiet)
unset VERBOSE
QUIET=true
shift
;;
--verbose)
VERBOSE=true
shift
;;
*) # unhandled parameter
PARAMETERS+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
_debug "Command line parameters:
------------------------------------------
UNKNOWN |$PARAMETERS
VERBOSE |$VERBOSE
LOG |$LOG
TEST_PATH |$TEST_PATH
DEBUG |$DEBUG
"
}
_set_defaults() {
TIMESTAMP="$(date +%s)"
TEST_PATH="$(dirname $0)/tests.d"
VERBOSE=true
LOG="/tmp/sympl-tests.log"
}
_success() {
if [ $QUIET ]; then
echo -ne "\033[32m.\033[0m"
fi
}
_test_fail() {
echo -e "\033[1m\033[31m!FAIL: \033[0m $@"
_log "!FAIL:$@"
exit 1
}
_test_ok() {
exit 0
}
_test_ok() {
exit 0
}
_test_start() {
set -e
set -o pipefail
_set_defaults
_parse_parameters
trap "_echo $0 failed! ; exit 2" ERR
}
_warn() {
echo -e "\033[1m\033[33m WARN: \033[0m $@"
}
#!/bin/bash
set -e
#set +e
set -o pipefail
if [ "$( echo "$@" | grep -c '\-\-debug' )" != "0" ]; then DEBUG="true" ; fi
......@@ -11,34 +11,103 @@ if [ "$( echo "$@" | grep -c '\-\-debug' )" != "0" ]; then DEBUG="true" ; fi
_debug() {
if [ $DEBUG ]; then
echo -e "\033[2mDEBUG: $@\033[0m"
_log "DEBUG: $@"
fi
}
_warn() {
echo -e "\033[1m\033[33m WARN: \033[0m $@"
echo -e "\033[1m\033[33m WARN:\033[0m $@"
}
_error() {
echo -e "\033[1m\033[31mERROR: \033[0m $@"
echo -e "\033[1m\033[31mERROR:\033[0m $@"
_log "ERROR: $@"
}
_stop() {
echo -e "\033[1m\033[31m STOP: 033[0m $@"
_log " STOP: $@"
exit 1
}
_echo() {
if [ $DEBUG ]; then
echo " INFO: $@"
echo -e " INFO: $@"
elif [ $VERBOSE ]; then
echo "$@"
echo -e "$@"
else
_log "$@"
fi
}
_log() {
echo "$@" >> $LOG
}
_pass() {
if [ $QUIET ] || [ $QUIETQUIET ]; then
echo -ne "\033[32m.\033[0m"
else
_echo "$( cat /dev/shm/sympl-tests_stdout )"
_centre - "\033[1m\033[32mPASS\033[0m\033[2m"
fi
}
_fail() {
if [ $QUIETQUIET ]; then
echo -ne "\033[1m\033[31mF\033[0m"
elif [ $QUIET ]; then
echo
VERBOSE=true
_centre - "$script"
cat /dev/shm/sympl-tests_stdout
_centre - "\033[1m\033[31mFAIL\033[0m"
unset VERBOSE
else
cat /dev/shm/sympl-tests_stdout
_centre - "\033[1m\033[31mFAIL\033[0m\033[2m"
fi
}
_test_error() {
if [ $QUIETQUIET ]; then
echo -ne "\033[1m\033[1m\033[33mX\033[0m"
elif [ $QUIET ]; then
echo
VERBOSE=true
_centre - "$script"
cat /dev/shm/sympl-tests_stdout
_centre - "\033[1m\033[5m\033[33m[TEST ERROR]\033[0m"
unset VERBOSE
else
cat /dev/shm/sympl-tests_stdout
_centre - "\033[1m\033[5m\033[33m[TEST ERROR]\033[0m"
fi
}
_centre() {
spacer="$1"
line="$2"
if [ $VERBOSE ]; then
col="$( tput cols )" || col=80
col=$((col - 3))
stripped_line="$( echo $line | sed 's|\\033[[0-9;]*m||g' )"
len=$(( ( col / 2 ) - ( ( ${#stripped_line} + 1 ) / 2 ) ))
echo -e "\033[2m$(for each in $(seq 1 $len); do printf "$spacer"; done)\033[0m $line \033[2m$(for each in $(seq 1 $len); do printf "$spacer"; done)\033[0m"
fi
col=78
len=$(( ( col / 2 ) - ( ( ${#line} + 1 ) / 2 ) ))
_log "$(for each in $(seq 1 $len); do printf "$spacer"; done) $line $(for each in $(seq 1 $len); do printf "$spacer"; done)" | cut -c 1-$((col))
}
#############################################################################
# Set Defaults #
#############################################################################
TIMESTAMP="$(date +%s)"
TEST_PATH="$(dirname $0)"
TEST_PATH="$(dirname $0)/tests.d"
VERBOSE=true
LOG="/tmp/sympl-tests.log"
LOG="./sympl-tests.log"
#############################################################################
# Read Command Line Parameters #
......@@ -46,6 +115,8 @@ LOG="/tmp/sympl-tests.log"
_debug "Command line parameters: $@"
ORIGINAL_PARAMETERS=$@
PARAMETERS=()
while [ $# -gt 0 ]; do
key="$1"
......@@ -69,7 +140,15 @@ while [ $# -gt 0 ]; do
;;
--quiet)
unset VERBOSE
if [ $QUIET ]; then
QUIETQUIET=true
else
QUIET=true
fi
shift
;;
--verbose)
VERBOSE=true
shift
;;
*) # unhandled parameter
......@@ -88,9 +167,29 @@ TEST_PATH |$TEST_PATH
DEBUG |$DEBUG
"
TIMESTAMP="$(date +%s)"
_echo "Starting run at $(date ; if [ "$TEST_PATH" != "." ] ; then echo ", test path: '$TEST_PATH'" ; fi )"
#_echo "Starting run at $(date ; if [ "$TEST_PATH" != "./tests.d" ] ; then echo -n ", test path: '$TEST_PATH'" ; fi )"
_centre = Starting
for script in $( find $TEST_PATH -type f -executable | sort ) ; do
_echo
_centre - "$script"
echo -n > /dev/shm/sympl-tests_stdout
# trap "_error $script failed! ; cat /dev/shm/sympl-tests_stdout_$TIMESTAMP /dev/shm/sympl-tests_stderr_$TIMESTAMP" ERR
$script > /dev/shm/sympl-tests_stdout 2>&1
exitcode=$?
if [ $exitcode == 0 ] ; then
_pass
elif [ $exitcode == 1 ]; then
_fail
else
_test_error
fi
rm /dev/shm/sympl-tests_stdout
done
_echo
_centre = Summary
_echo "Test run completed at $(date)"
_centre = "End"
#run-parts tests.d/00-system -a ~/git/sympl-tests/sympl-tests
#!/bin/bash
source ./lib/*
echo Run start time: $(date)
#!/bin/bash
lsb_release -a
exit 0
#!/bin/bash
for file in ./lib/* ; do source $file; done ; _test_start
if [ -f /etc/apt/sources.list.d/sympl_*.list ]; then
cat /etc/apt/sources.list.d/sympl_*.list | awk '{print $3}'
else
_test_fail 'No sympl file in sources.list.d'
fi
_test_ok
#!/bin/bash
for file in ./lib/* ; do source $file; done ; _test_start
dpkg -l "sympl-*" | grep '^ii' | awk '{ print $3 " " $2}' | sort -r
_test_ok
#!/bin/bash
source ./lib/*
echo Run start time: $(date)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment