Refactor the hook logic into a reusable function

Hook logic exists twice. Let's refactor it into a function
so that it can be reused for more hooks.

Include some test cases.

Test cases don't like doing 'sudo mv', so let's not use it if not necessary.

Change-Id: I84d2fd66265c8e2e77faf16ce03e92790f2f51b8
This commit is contained in:
Ramy Asselin 2014-09-23 13:22:09 -07:00
parent 2a47c2237d
commit 1ba5b62550
3 changed files with 75 additions and 16 deletions

View File

@ -391,14 +391,7 @@ fi
export DEVSTACK_GATE_TOPOLOGY=${DEVSTACK_GATE_TOPOLOGY:-aio}
# Run pre test hook if we have one
if function_exists "pre_test_hook"; then
echo "Running pre_test_hook"
xtrace=$(set +o | grep xtrace)
set -o xtrace
tsfilter pre_test_hook | tee $WORKSPACE/devstack-gate-pre-test-hook.txt
sudo mv $WORKSPACE/devstack-gate-pre-test-hook.txt $BASE/logs/
$xtrace
fi
call_hook_if_defined "pre_test_hook"
# Run the gate function
echo "Running gate_hook"
@ -412,15 +405,10 @@ if [ $GATE_RETVAL -ne 0 ]; then
fi
# Run post test hook if we have one
if [ $GATE_RETVAL -eq 0 ] && function_exists "post_test_hook"; then
echo "Running post_test_hook"
xtrace=$(set +o | grep xtrace)
set -o xtrace -o pipefail
tsfilter post_test_hook | tee $WORKSPACE/devstack-gate-post-test-hook.txt
if [ $GATE_RETVAL -eq 0 ]; then
# Run post_test_hook if we have one
call_hook_if_defined "post_test_hook"
RETVAL=$?
sudo mv $WORKSPACE/devstack-gate-post-test-hook.txt $BASE/logs/
set +o pipefail
$xtrace
fi
if [ $GATE_RETVAL -eq 137 ] && [ -f $WORKSPACE/gate.pid ] ; then

View File

@ -41,6 +41,23 @@ function function_exists {
type $1 2>/dev/null | grep -q 'is a function'
}
function call_hook_if_defined {
local hook_name=$1
local filename=${2-$WORKSPACE/devstack-gate-$hook_name.txt}
local save_dir=${3-$BASE/logs/}
if function_exists $hook_name; then
echo "Running $hook_name"
xtrace=$(set +o | grep xtrace)
set -o xtrace -o pipefail
tsfilter $hook_name | tee $filename
local ret_val=$?
mv $filename $save_dir
set +o pipefail
$xtrace
return $ret_val
fi
}
# awk filter to timestamp the stream, including stderr merging
function tsfilter {
$@ 2>&1 | awk '

View File

@ -401,6 +401,59 @@ function test_workspace_branch_arg {
assert_raises setup_workspace
}
function test_call_hook_if_defined {
local filename=test_call_hook_if_defined.txt
local save_dir=`pwd`/tmp
mkdir -p $save_dir
function demo_script {
local filename=$1
local save_dir=$2
# Clean up any files from previous tests
rm -f $save_dir/$filename
call_hook_if_defined test_hook $filename $save_dir
ret_val=$?
return $ret_val
}
# No hook defined returns success 0 & no file created
demo_script $filename $save_dir
ret_val=$?
assert_equal "$ret_val" "0"
[[ -e $save_dir/$filename ]]
file_exists=$?
assert_equal $file_exists 1
# Hook defined returns its error code and file with output
function test_hook {
echo "hello test_hook"
return 123
}
demo_script $filename $save_dir
ret_val=$?
assert_equal "$ret_val" "123"
[[ -e $save_dir/$filename ]]
file_exists=$?
assert_equal $file_exists 0
# Make sure the expected contents has length > 0
result_expected=`cat $save_dir/$filename | grep "hello test_hook"`
[[ ${#result_expected} -eq "0" ]]
assert_equal $? 1
# Hook defined with invalid file fails
demo_script /invalid/file.txt $save_dir
ret_val=$?
assert_equal "$ret_val" "1"
# Clean up
rm -rf $save_dir
}
# Run tests:
#set -o xtrace
test_branch_override
@ -413,6 +466,7 @@ test_one_on_master
test_periodic
test_two_on_master
test_workspace_branch_arg
test_call_hook_if_defined
if [[ ! -z "$ERROR" ]]; then
echo