btrfs-progs/tests/common

101 lines
1.9 KiB
Plaintext
Raw Normal View History

#!/bin/bash
#
# Common routines for all tests
#
_fail()
{
echo "$*" | tee -a $RESULT
exit 1
}
_not_run()
{
echo " [NOTRUN] $*"
exit 0
}
run_check()
{
echo "############### $@" >> $RESULT 2>&1
"$@" >> $RESULT 2>&1 || _fail "failed: $@"
}
check_prereq()
{
if ! [ -f $top/$1 ]; then
_fail "Failed prerequisities: $1";
fi
}
check_image()
{
image=$1
echo "testing image $(basename $image)" >> $RESULT
$top/btrfs check $image >> $RESULT 2>&1
[ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
run_check $top/btrfs check --repair $image
run_check $top/btrfs check $image
}
check_all_images()
{
dir=$1
for i in $(find $dir -iname '*.img')
do
echo "extracting image $(basename $i)" >> $RESULT
$top/btrfs-image -r $i $i.restored || \
_fail "failed to extract image $i"
check_image $i.restored
rm $i.restored
done
}
# some tests need to mount the recovered image and do verifications call
# 'setup_root_helper' and then check for have_root_helper == 1 if the test
# needs to fail otherwise; using sudo by default for now
_sudo=
need_validate=-1
export _sudo
export need_validate
root_helper()
{
if [ $UID -eq 0 ]; then
$*
else
if [ $need_validate -eq 1 ]; then
sudo -v -n &> /dev/null || \
_not_run "Need validate sudo credential"
sudo -n $*
elif [ $need_validate -eq 0 ]; then
sudo -n true &> /dev/null || \
_not_run "Need validate sudo user setting"
sudo -n $*
else
# should not happen
_not_run "Need validate root privilege"
fi
fi
}
setup_root_helper()
{
if [ $UID -eq 0 ]; then
return
fi
# Test for old sudo or special setting, which makes sudo -v fails even
# user is set NOPASSWD
sudo -n true &> /dev/null && need_validate=0
# Newer sudo or default sudo setting
sudo -v -n &> /dev/null && need_validate=1
if [ $need_validate -eq -1 ]; then
_not_run "Need validate root privilege"
fi
_sudo=root_helper
}