[PATCH] tests: Do not hardcode sudo tool

Michal Suchanek msuchanek at suse.de
Wed Aug 24 09:48:11 CEST 2022


In some situations it may be needed to pass parameters to sudo or to use
a different tool to gain root access. Add SUDO variable to specify the
sudo tool.

Signed-off-by: Michal Suchanek <msuchanek at suse.de>
---
 test/fs/fat-noncontig-test.sh     |  9 +++++----
 test/fs/fs-test.sh                | 26 ++++++++++++++------------
 test/py/tests/test_fs/conftest.py |  8 +++++---
 test/py/tests/test_ut.py          | 14 ++++++++------
 4 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/test/fs/fat-noncontig-test.sh b/test/fs/fat-noncontig-test.sh
index b02dae765f..7e478c6705 100755
--- a/test/fs/fat-noncontig-test.sh
+++ b/test/fs/fat-noncontig-test.sh
@@ -60,6 +60,7 @@ testfn=noncontig.img
 mnttestfn=${mnt}/${testfn}
 crcaddr=0
 loadaddr=1000
+[ -n "$SUDO" ] || SUDO=sudo
 
 for prereq in fallocate mkfs.fat dd crc32; do
     if [ ! -x "`which $prereq`" ]; then
@@ -87,7 +88,7 @@ if [ ! -f ${img} ]; then
         exit $?
     fi
 
-    sudo mount -o loop,uid=$(id -u) ${img} ${mnt}
+    $SUDO mount -o loop,uid=$(id -u) ${img} ${mnt}
     if [ $? -ne 0 ]; then
         echo Could not mount test filesystem
         exit $?
@@ -106,20 +107,20 @@ if [ ! -f ${img} ]; then
     # sector size (ignoring sizes that are multiples of both).
     dd if=${fill} of=${mnttestfn} bs=511 >/dev/null 2>&1
 
-    sudo umount ${mnt}
+    $SUDO umount ${mnt}
     if [ $? -ne 0 ]; then
         echo Could not unmount test filesystem
         exit $?
     fi
 fi
 
-sudo mount -o ro,loop,uid=$(id -u) ${img} ${mnt}
+$SUDO mount -o ro,loop,uid=$(id -u) ${img} ${mnt}
 if [ $? -ne 0 ]; then
     echo Could not mount test filesystem
     exit $?
 fi
 crc=0x`crc32 ${mnttestfn}`
-sudo umount ${mnt}
+$SUDO umount ${mnt}
 if [ $? -ne 0 ]; then
     echo Could not unmount test filesystem
     exit $?
diff --git a/test/fs/fs-test.sh b/test/fs/fs-test.sh
index b87748106c..bd55ff51b6 100755
--- a/test/fs/fs-test.sh
+++ b/test/fs/fs-test.sh
@@ -55,6 +55,8 @@ OUT="${OUT_DIR}/fs-test"
 MB1="${MOUNT_DIR}/${SMALL_FILE}"
 GB2p5="${MOUNT_DIR}/${BIG_FILE}"
 
+[ -n "$SUDO" ] || SUDO=sudo
+
 # ************************
 # * Functions start here *
 # ************************
@@ -351,34 +353,34 @@ EOF
 function create_files() {
 	# Mount the image so we can populate it.
 	mkdir -p "$MOUNT_DIR"
-	sudo mount -o loop,rw "$1" "$MOUNT_DIR"
+	$SUDO mount -o loop,rw "$1" "$MOUNT_DIR"
 
 	# Create a subdirectory.
-	sudo mkdir -p "$MOUNT_DIR/SUBDIR"
+	$SUDO mkdir -p "$MOUNT_DIR/SUBDIR"
 
 	# Create big file in this image.
 	# Note that we work only on the start 1MB, couple MBs in the 2GB range
 	# and the last 1 MB of the huge 2.5GB file.
 	# So, just put random values only in those areas.
 	if [ ! -f "${GB2p5}" ]; then
-		sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 \
+		$SUDO dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 \
 			&> /dev/null
-		sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=2 seek=2047 \
+		$SUDO dd if=/dev/urandom of="${GB2p5}" bs=1M count=2 seek=2047 \
 			&> /dev/null
-		sudo dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 seek=2499 \
+		$SUDO dd if=/dev/urandom of="${GB2p5}" bs=1M count=1 seek=2499 \
 			&> /dev/null
 	fi
 
 	# Create a small file in this image.
 	if [ ! -f "${MB1}" ]; then
-		sudo dd if=/dev/urandom of="${MB1}" bs=1M count=1 \
+		$SUDO dd if=/dev/urandom of="${MB1}" bs=1M count=1 \
 			&> /dev/null
 	fi
 
 	# Delete the small file copies which possibly are written as part of a
 	# previous test.
-	sudo rm -f "${MB1}.w"
-	sudo rm -f "${MB1}.w2"
+	$SUDO rm -f "${MB1}.w"
+	$SUDO rm -f "${MB1}.w2"
 
 	# Generate the md5sums of reads that we will test against small file
 	dd if="${MB1}" bs=1M skip=0 count=1 2> /dev/null | md5sum > "$2"
@@ -405,7 +407,7 @@ function create_files() {
 		2> /dev/null | md5sum >> "$2"
 
 	sync
-	sudo umount "$MOUNT_DIR"
+	$SUDO umount "$MOUNT_DIR"
 	rmdir "$MOUNT_DIR"
 }
 
@@ -597,13 +599,13 @@ for fs in ext4 fat16 fat32; do
 		uid=""
 		;;
 	esac
-	sudo mount -o loop,rw,$uid "$IMAGE" "$MOUNT_DIR"
-	sudo chmod 777 "$MOUNT_DIR"
+	$SUDO mount -o loop,rw,$uid "$IMAGE" "$MOUNT_DIR"
+	$SUDO chmod 777 "$MOUNT_DIR"
 
 	OUT_FILE="${OUT}.sb.${fs}.out"
 	test_image $IMAGE $fs $SMALL_FILE $BIG_FILE sb `pwd`/$MOUNT_DIR \
 		> ${OUT_FILE} 2>&1
-	sudo umount "$MOUNT_DIR"
+	$SUDO umount "$MOUNT_DIR"
 	rmdir "$MOUNT_DIR"
 
 	check_results $OUT_FILE $MD5_FILE_FS $SMALL_FILE $BIG_FILE
diff --git a/test/py/tests/test_fs/conftest.py b/test/py/tests/test_fs/conftest.py
index b638284e07..21fc8ce9e2 100644
--- a/test/py/tests/test_fs/conftest.py
+++ b/test/py/tests/test_fs/conftest.py
@@ -10,6 +10,8 @@ from subprocess import call, check_call, check_output, CalledProcessError
 from fstest_defs import *
 import u_boot_utils as util
 
+sudo = os.environ.get('SUDO', 'sudo')
+
 supported_fs_basic = ['fat16', 'fat32', 'ext4']
 supported_fs_ext = ['fat16', 'fat32']
 supported_fs_mkdir = ['fat16', 'fat32']
@@ -222,11 +224,11 @@ def mount_fs(fs_type, device, mount_point):
     if re.match('fat', fs_type):
         mount_opt += ',umask=0000'
 
-    check_call('sudo mount -o %s %s %s'
+    check_call(sudo + ' mount -o %s %s %s'
         % (mount_opt, device, mount_point), shell=True)
 
     # may not be effective for some file systems
-    check_call('sudo chmod a+rw %s' % mount_point, shell=True)
+    check_call(sudo + ' chmod a+rw %s' % mount_point, shell=True)
 
 def umount_fs(mount_point):
     """Unmount a volume.
@@ -251,7 +253,7 @@ def umount_fs(mount_point):
             pass
 
     else:
-        call('sudo umount %s' % mount_point, shell=True)
+        call(sudo + ' umount %s' % mount_point, shell=True)
 
 #
 # Fixture for basic fs test
diff --git a/test/py/tests/test_ut.py b/test/py/tests/test_ut.py
index 35fb393c1f..698c3db448 100644
--- a/test/py/tests/test_ut.py
+++ b/test/py/tests/test_ut.py
@@ -8,6 +8,8 @@ import pytest
 
 import u_boot_utils
 
+sudo = os.environ.get('SUDO', 'sudo')
+
 def mkdir_cond(dirname):
     """Create a directory if it doesn't already exist
 
@@ -25,7 +27,7 @@ def setup_bootflow_image(u_boot_console):
     mkdir_cond(mnt)
 
     u_boot_utils.run_and_log(cons, 'qemu-img create %s 20M' % fname)
-    u_boot_utils.run_and_log(cons, 'sudo sfdisk %s' % fname,
+    u_boot_utils.run_and_log(cons, sudo + ' sfdisk %s' % fname,
                              stdin=b'type=c')
 
     loop = None
@@ -33,12 +35,12 @@ def setup_bootflow_image(u_boot_console):
     complete = False
     try:
         out = u_boot_utils.run_and_log(cons,
-                                       'sudo losetup --show -f -P %s' % fname)
+                                       sudo + ' losetup --show -f -P %s' % fname)
         loop = out.strip()
         fatpart = '%sp1' % loop
-        u_boot_utils.run_and_log(cons, 'sudo mkfs.vfat %s' % fatpart)
+        u_boot_utils.run_and_log(cons, sudo + ' mkfs.vfat %s' % fatpart)
         u_boot_utils.run_and_log(
-            cons, 'sudo mount -o loop %s %s -o uid=%d,gid=%d' %
+            cons, sudo + ' mount -o loop %s %s -o uid=%d,gid=%d' %
             (fatpart, mnt, os.getuid(), os.getgid()))
         mounted = True
 
@@ -84,9 +86,9 @@ label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
               str(exc))
     finally:
         if mounted:
-            u_boot_utils.run_and_log(cons, 'sudo umount %s' % mnt)
+            u_boot_utils.run_and_log(cons, sudo + ' umount %s' % mnt)
         if loop:
-            u_boot_utils.run_and_log(cons, 'sudo losetup -d %s' % loop)
+            u_boot_utils.run_and_log(cons, sudo + ' losetup -d %s' % loop)
 
     if not complete:
         # Use a prepared image since we cannot create one
-- 
2.37.1



More information about the U-Boot mailing list