summaryrefslogtreecommitdiff
path: root/tests/common
diff options
context:
space:
mode:
authorQu Wenruo <quwenruo@cn.fujitsu.com>2015-03-02 11:41:50 +0800
committerDavid Sterba <dsterba@suse.cz>2015-03-09 12:08:56 +0100
commita624680b0e45e0f7ea1c847f8ccebe04aa52ba75 (patch)
tree51d6859eaff8328eb8d3f5d7ed668ad7ae519063 /tests/common
parent0c13bf7936a0388c0bdc99dc80218a0179c88f31 (diff)
btrfs-progs: fsck-test: Add check_sudo to check valid root/sudo privilege
Although fsck-test/012 uses sudo, it uses 'sudo -n', which won't prompt user to input password and will return 1 if no valid credential is found. And this makes test result quite annoying since it fails to mount and still continue, which will always fail. This patch will check 'sudo -v -n' and 'sudo -n true' to determine whether sudo works fine in different version/settings, since in some setting/version, 'sudo -v -n' will fail even the user is set NOPASSWD. Also, remove the 'have_root_helper' variant, since there is a possibility that sudo credential will timeout during the test and 'have_root_helper' won't help to detect such problem. New '_sudo' command will do credential check if needed to avoid such problem. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz>
Diffstat (limited to 'tests/common')
-rw-r--r--tests/common50
1 files changed, 43 insertions, 7 deletions
diff --git a/tests/common b/tests/common
index d7d2e9be..ccdadd5f 100644
--- a/tests/common
+++ b/tests/common
@@ -9,6 +9,12 @@ _fail()
exit 1
}
+_not_run()
+{
+ echo " [NOTRUN] $*"
+ exit 0
+}
+
run_check()
{
echo "############### $@" >> $RESULT 2>&1
@@ -51,14 +57,44 @@ check_all_images()
# 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=
-have_root_helper=0
-export sudo
-export have_root_helper
+_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 != 0 ]; then
- sudo="sudo --non-interactive"
+ 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
- have_root_helper=1
+ _sudo=root_helper
}