From patchwork Wed Aug 24 07:48:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Michal_Such=C3=A1nek?= X-Patchwork-Id: 1669614 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=k+qJenmI; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=so+nW5hb; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MCJB31M0Qz1ygF for ; Wed, 24 Aug 2022 17:48:27 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 481058300F; Wed, 24 Aug 2022 09:48:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.b="k+qJenmI"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="so+nW5hb"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6D72284187; Wed, 24 Aug 2022 09:48:20 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7996A801A9 for ; Wed, 24 Aug 2022 09:48:17 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=msuchanek@suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 42B7C1FFD5; Wed, 24 Aug 2022 07:48:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1661327297; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=q4NfXJ+SY7gqXXiU9EQMsC6ZaUMi6UI7jPyjBVh5P3Y=; b=k+qJenmIAHsHsx4ynqVN1uqcdFzC1dWWLanjDb5v/XQnPZOKHZQpBmRRKRVy9RKpEyzqhJ BmEoafNPjTox2S8t9hrKvpmmBzBqWd1PiQhqWiSqek/7FYMYgtqY8DZOgUGqjJyAea3yqq UxScbHFqsgn/tedTuSYQVwxLDiqjiyo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1661327297; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=q4NfXJ+SY7gqXXiU9EQMsC6ZaUMi6UI7jPyjBVh5P3Y=; b=so+nW5hbnyCCY2oFCFGKVZYTWEjom60UvFm68MSOQSTZrcBPs/o51MNBSOnOxVhaR4bNyB BYsAez1HusxX+rAQ== Received: from naga.suse.cz (unknown [10.100.224.114]) by relay2.suse.de (Postfix) with ESMTP id 27BE12C141; Wed, 24 Aug 2022 07:48:17 +0000 (UTC) From: Michal Suchanek To: u-boot@lists.denx.de Cc: Michal Suchanek , Simon Glass Subject: [PATCH] tests: Do not hardcode sudo tool Date: Wed, 24 Aug 2022 09:48:11 +0200 Message-Id: <20220824074811.29617-1-msuchanek@suse.de> X-Mailer: git-send-email 2.37.1 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean 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 Reviewed-by: Simon Glass --- 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