From patchwork Mon Apr 23 09:17:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Vorel X-Patchwork-Id: 902846 X-Patchwork-Delegate: chrubis@suse.cz Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40V17w41GQz9ryr for ; Mon, 23 Apr 2018 19:17:29 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 0571A3E754D for ; Mon, 23 Apr 2018 11:17:26 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [IPv6:2001:4b78:1:20::2]) by picard.linux.it (Postfix) with ESMTP id 97F3D3E68D9 for ; Mon, 23 Apr 2018 11:17:24 +0200 (CEST) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id B8D9060085F for ; Mon, 23 Apr 2018 11:17:20 +0200 (CEST) Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 0DC65ACC8 for ; Mon, 23 Apr 2018 09:17:20 +0000 (UTC) From: Petr Vorel To: ltp@lists.linux.it Date: Mon, 23 Apr 2018 11:17:06 +0200 Message-Id: <20180423091706.24154-1-pvorel@suse.cz> X-Mailer: git-send-email 2.16.3 X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=-0.0 required=7.0 tests=SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH v2 1/1] tst_test.sh: Add test cmd helper tst_test_cmds() X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" + tst_cmd_available() tst_test_cmds() is meant to be a check just for a particular test. Works like tst_check_cmds(), but instead of tst_brk() calls tst_res(). tst_cmd_available() helper can handle cases when command shell builtin is not available (e.g. Busybox). Signed-off-by: Petr Vorel --- Hi, if you don't like using which or testing with 127 exit code in tst_cmd_available() (or if you don't like tst_cmd_available()), I can remove it. Kind regards, Petr --- doc/test-writing-guidelines.txt | 17 +++++++++++++++++ testcases/lib/tst_test.sh | 29 +++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/doc/test-writing-guidelines.txt b/doc/test-writing-guidelines.txt index cbbfe6c0f..320b42bf8 100644 --- a/doc/test-writing-guidelines.txt +++ b/doc/test-writing-guidelines.txt @@ -1519,6 +1519,23 @@ existence each of them and exits the test with 'TCONF' on first misssing. Alternatively the 'tst_check_cmds()' function can be used to do the same on runtime, since sometimes we need to the check at runtime too. +'tst_test_cmds()' can be used for requirements just for a particular test +as it doesn't exit (it issues 'tst_res TCONF'). Expected usage is: +... + +TST_TESTFUNC=do_test +. tst_test.sh + +do_test() +{ + tst_test_cmds cmd || return + cmd --foo + ... +} + +tst_run +... + Locating kernel modules +++++++++++++++++++++++ diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh index 8d49d34b6..b3e803e05 100644 --- a/testcases/lib/tst_test.sh +++ b/testcases/lib/tst_test.sh @@ -201,12 +201,37 @@ tst_mkfs() ROD_SILENT mkfs.$fs_type $fs_opts $device } +tst_cmd_available() +{ + if type command > /dev/null 2>&1; then + command -v $1 > /dev/null 2>&1 || return 1 + else + which $1 > /dev/null 2>&1 + if [ $? -eq 0 ]; then + return 0 + elif [ $? -eq 127 ]; then + tst_brk TCONF "missing which command" + else + return 1 + fi + fi +} + tst_check_cmds() { local cmd for cmd in $*; do - if ! command -v $cmd > /dev/null 2>&1; then - tst_brk TCONF "'$cmd' not found" + tst_cmd_available $cmd || tst_brk TCONF "'$cmd' not found" + done +} + +tst_test_cmds() +{ + local cmd + for cmd in $*; do + if ! tst_cmd_available $cmd; then + tst_res TCONF "'$cmd' not found" + return 1 fi done }