From patchwork Thu Mar 29 12:25:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Vorel X-Patchwork-Id: 892779 X-Patchwork-Delegate: petr.vorel@gmail.com 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 40BkVm5Z75z9rx7 for ; Thu, 29 Mar 2018 23:25:51 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 8BBF93E6198 for ; Thu, 29 Mar 2018 14:25:48 +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 [217.194.8.2]) by picard.linux.it (Postfix) with ESMTP id 899803E610D for ; Thu, 29 Mar 2018 14:25:47 +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 505EB601DAA for ; Thu, 29 Mar 2018 14:25:45 +0200 (CEST) Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 89EBDAF95 for ; Thu, 29 Mar 2018 12:25:45 +0000 (UTC) From: Petr Vorel To: ltp@lists.linux.it Date: Thu, 29 Mar 2018 14:25:38 +0200 Message-Id: <20180329122538.7088-1-pvorel@suse.cz> X-Mailer: git-send-email 2.16.2 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] [RFC PATCH 1/1] lib, commands: Add tst_verify_cmd() and use it 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" Signed-off-by: Petr Vorel --- Hi, draft of function somehow similar to EXPECT_PASS()/ EXPECT_FAIL() or tst_check_cmds() More tests using very similar code: testcases/commands/which/which01.sh testcases/commands/wc/wc01.sh testcases/commands/unshare/unshare01.sh testcases/commands/mkswap/mkswap01.sh testcases/commands/mkfs/mkfs01.sh testcases/commands/du/du01.sh The question is how to add reqexp pattern (env. variable or getopts?) and what would be the best default regexp. Kind regards, Petr --- testcases/commands/df/df01.sh | 51 ++++++------------------------------------- testcases/lib/tst_test.sh | 32 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/testcases/commands/df/df01.sh b/testcases/commands/df/df01.sh index 139230396..2444a110c 100755 --- a/testcases/commands/df/df01.sh +++ b/testcases/commands/df/df01.sh @@ -79,10 +79,7 @@ df_test() { cmd="$1 -P" - df_verify $cmd - if [ $? -ne 0 ]; then - return - fi + tst_verify_cmd -o output -n $cmd || return df_check $cmd if [ $? -ne 0 ]; then @@ -92,10 +89,7 @@ df_test() ROD_SILENT dd if=/dev/zero of=mntpoint/testimg bs=1024 count=1024 - df_verify $cmd - if [ $? -ne 0 ]; then - return - fi + tst_verify_cmd -o output -n $cmd || return df_check $cmd if [ $? -eq 0 ]; then @@ -110,22 +104,6 @@ df_test() sync } -df_verify() -{ - $@ >output 2>&1 - if [ $? -ne 0 ]; then - grep -q -E "unrecognized option | invalid option" output - if [ $? -eq 0 ]; then - tst_res TCONF "'$1' not supported." - return 32 - else - tst_res TFAIL "'$1' failed." - cat output - return 1 - fi - fi -} - df_check() { if [ "$(echo $@)" = "df -i -P" ]; then @@ -184,44 +162,29 @@ test7() test8() { - df_verify "df -h" - if [ $? -eq 0 ]; then - tst_res TPASS "'df -h' passed." - fi + tst_verify_cmd "df -h" || return } test9() { - df_verify "df -H" - if [ $? -eq 0 ]; then - tst_res TPASS "'df -H' passed." - fi + tst_verify_cmd "df -H" || return } test10() { - df_verify "df -m" - if [ $? -eq 0 ]; then - tst_res TPASS "'df -m' passed." - fi + tst_verify_cmd "df -m" || return } test11() { - df_verify "df --version" - if [ $? -eq 0 ]; then - tst_res TPASS "'df --version' passed." - fi + tst_verify_cmd "df --version" || return } test12() { cmd="df -x ${DF_FS_TYPE} -P" - df_verify $cmd - if [ $? -ne 0 ]; then - return - fi + tst_verify_cmd -o output -n $cmd || return grep ${TST_DEVICE} output | grep -q mntpoint if [ $? -ne 0 ]; then diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh index 48afb9cc4..6a14a43c8 100644 --- a/testcases/lib/tst_test.sh +++ b/testcases/lib/tst_test.sh @@ -211,6 +211,38 @@ tst_check_cmds() done } +tst_verify_cmd() +{ + local pattern="${TST_VERIFY_CMD_PATTERN:-unrecognized option|invalid option|unknown option|invalid option|Usage}" + local opt OPTIND dont_pass output + + while getopts "no:" opt; do + case $opt in + 'n') dont_pass=1;; + 'o') output="$OPTARG";; + *);; + esac + done + [ ! "$output" ] && output=$(mktemp "./LTP_$TST_ID.XXXXXXXXXX") + + shift $((OPTIND - 1)) + + $@ >$output 2>&1 + if [ $? -ne 0 ]; then + grep -q -E "$pattern" $output + if [ $? -eq 0 ]; then + tst_res TCONF "'$1' not supported." + return 32 + fi + tst_res TFAIL "'$1' failed." + cat $output + return 1 + fi + if [ ! "$dont_pass" ]; then + tst_res TPASS "'$1' passed." + fi +} + tst_is_int() { [ "$1" -eq "$1" ] 2>/dev/null