From owner-svn-src-head@freebsd.org Tue Apr 7 20:26:44 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id A0E9127CBC8; Tue, 7 Apr 2020 20:26:44 +0000 (UTC) (envelope-from dab@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48xf84254Tz4By4; Tue, 7 Apr 2020 20:26:44 +0000 (UTC) (envelope-from dab@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 42D39F931; Tue, 7 Apr 2020 20:26:44 +0000 (UTC) (envelope-from dab@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 037KQiXw087008; Tue, 7 Apr 2020 20:26:44 GMT (envelope-from dab@FreeBSD.org) Received: (from dab@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 037KQhZF087003; Tue, 7 Apr 2020 20:26:43 GMT (envelope-from dab@FreeBSD.org) Message-Id: <202004072026.037KQhZF087003@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: dab set sender to dab@FreeBSD.org using -f From: David Bright Date: Tue, 7 Apr 2020 20:26:43 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r359717 - in head: etc/mtree sbin/nvmecontrol sbin/nvmecontrol/tests X-SVN-Group: head X-SVN-Commit-Author: dab X-SVN-Commit-Paths: in head: etc/mtree sbin/nvmecontrol sbin/nvmecontrol/tests X-SVN-Commit-Revision: 359717 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Apr 2020 20:26:44 -0000 Author: dab Date: Tue Apr 7 20:26:42 2020 New Revision: 359717 URL: https://svnweb.freebsd.org/changeset/base/359717 Log: Add a basic test for nvmecontrol I recently made some bug fixes in nvmecontrol. It occurred to me that since nvmecontrol lacks any kyua tests, I should convert the informal testing I did into a more formal automated test. The test in this change should be considered just a starting point; it is neither complete nor thorough. While converting the test to ATF/kyua, I discovered a small bug in nvmecontrol; the nvmecontrol devlist command would always exit with an unsuccessful status. So I included the fix for that, too, so that the test won't fail. Reviewed by: imp@ MFC after: 3 days Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D24269 Added: head/sbin/nvmecontrol/tests/ head/sbin/nvmecontrol/tests/Makefile (contents, props changed) head/sbin/nvmecontrol/tests/basic.sh (contents, props changed) Modified: head/etc/mtree/BSD.tests.dist head/sbin/nvmecontrol/Makefile head/sbin/nvmecontrol/devlist.c Modified: head/etc/mtree/BSD.tests.dist ============================================================================== --- head/etc/mtree/BSD.tests.dist Tue Apr 7 19:46:00 2020 (r359716) +++ head/etc/mtree/BSD.tests.dist Tue Apr 7 20:26:42 2020 (r359717) @@ -438,6 +438,8 @@ .. mdconfig .. + nvmecontrol + .. pfctl files .. Modified: head/sbin/nvmecontrol/Makefile ============================================================================== --- head/sbin/nvmecontrol/Makefile Tue Apr 7 19:46:00 2020 (r359716) +++ head/sbin/nvmecontrol/Makefile Tue Apr 7 20:26:42 2020 (r359717) @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + PACKAGE=runtime PROG= nvmecontrol SRCS= comnd.c nvmecontrol.c @@ -11,6 +13,8 @@ MAN= nvmecontrol.8 LDFLAGS+= -rdynamic LIBADD+= util SUBDIR= modules +HAS_TESTS= +SUBDIR.${MK_TESTS}+= tests .PATH: ${SRCTOP}/sys/dev/nvme Modified: head/sbin/nvmecontrol/devlist.c ============================================================================== --- head/sbin/nvmecontrol/devlist.c Tue Apr 7 19:46:00 2020 (r359716) +++ head/sbin/nvmecontrol/devlist.c Tue Apr 7 20:26:42 2020 (r359717) @@ -122,8 +122,10 @@ devlist(const struct cmd *f, int argc, char *argv[]) close(fd); } - if (found == 0) + if (found == 0) { printf("No NVMe controllers found.\n"); + exit(1); + } - exit(1); + exit(0); } Added: head/sbin/nvmecontrol/tests/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sbin/nvmecontrol/tests/Makefile Tue Apr 7 20:26:42 2020 (r359717) @@ -0,0 +1,6 @@ +# $FreeBSD$ + +PACKAGE= tests +ATF_TESTS_SH+= basic + +.include Added: head/sbin/nvmecontrol/tests/basic.sh ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sbin/nvmecontrol/tests/basic.sh Tue Apr 7 20:26:42 2020 (r359717) @@ -0,0 +1,236 @@ +# SPDX-License-Identifier: BSD-2-Clause-FreeBSD +# +# Copyright (c) 2020 David A. Bright +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. +# +# $FreeBSD$ + +# +# A basic test for nvmecontrol. This isn't a thorough or complete test +# of nvmecontrol functionality; it is more of a sanity check that +# nvmecontrol basically works. +# + +DANGEROUS=false # Set to true to run "dangerous" tests +# Select a nvme device to use for testing. If none exist, use nvme0. +TEST_DEV=$(cd /dev/;ls nvme[0-9]* | grep 'nvme[0-9][0-9]*$' | head -1) 2>/dev/null +TEST_DEV=${TEST_DEV:-nvme0} +TEST_DEV_PATH=/dev/${TEST_DEV} +INV_OPT="-z" +INV_OPT_MSG="invalid option -- z" + + +atf_test_case fake_lib cleanup +fake_lib_head() +{ + atf_set "descr" "check loading of a library from /lib" + atf_set "require.user" "root" +} +fake_lib_body() +{ + local libdir="/lib/nvmecontrol" + local fakelib="${libdir}/fake.so" + if [ -d ${libdir} ] ; then + touch ${fakelib} + atf_check -s not-exit:0 -o ignore -e match:"Can't load ${fakelib}" nvmecontrol + rm -f ${fakelib} + else + atf_skip "Skipping; directory ${libdir} does not exist" + fi +} +fake_lib_cleanup() +{ + rm -f /lib/nvmecontrol/fake.so +} + +atf_test_case fake_local_lib cleanup +fake_local_lib_head() +{ + atf_set "descr" "check loading of a library from /usr/local/lib" + atf_set "require.user" "root" +} +fake_local_lib_body() +{ + local libdir="/usr/local/lib/nvmecontrol" + local fakelib="${libdir}/fake.so" + if [ -d ${libdir} ] ; then + touch ${fakelib} + atf_check -s not-exit:0 -o ignore -e match:"Can't load ${fakelib}" nvmecontrol + rm -f ${fakelib} + else + atf_skip "Skipping; directory ${libdir} does not exist" + fi +} +fake_local_lib_cleanup() +{ + rm -f /usr/local/lib/nvmecontrol/fake.so +} + +atf_test_case admin_passthru +admin_passthru_head() +{ + atf_set "descr" "check the admin-passthru command" + atf_set "require.user" "root" +} +admin_passthru_body() +{ + if [ -c "${TEST_DEV_PATH}" ] ; then + atf_check -o not-empty -e empty nvmecontrol admin-passthru --opcode=06 --data-len=4096 --cdw10=1 -r 0 ${TEST_DEV} + else + atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol admin-passthru --opcode=06 --data-len=4096 --cdw10=1 -r 0 ${TEST_DEV} + fi + atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol admin-passthru ${INV_OPT} --opcode=06 --data-len=4096 --cdw10=1 -r 0 ${TEST_DEV} +} + +atf_test_case devlist +devlist_head() +{ + atf_set "descr" "check the devlist command" + atf_set "require.user" "root" +} +devlist_body() +{ + if [ -c "${TEST_DEV_PATH}" ] ; then + atf_check -o not-empty -e empty nvmecontrol devlist + else + atf_check -s not-exit:0 -o ignore -e ignore nvmecontrol devlist + fi + atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol devlist ${INV_OPT} +} + +atf_test_case identify +identify_head() +{ + atf_set "descr" "check the identify command" + atf_set "require.user" "root" +} +identify_body() +{ + if [ -c "${TEST_DEV_PATH}" ] ; then + atf_check -o not-empty -e empty nvmecontrol identify ${TEST_DEV} + else + atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol identify ${TEST_DEV} + fi + atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol identify ${INV_OPT} ${TEST_DEV} +} + +atf_test_case io_passthru +io_passthru_head() +{ + atf_set "descr" "check the io-passthru command" + atf_set "require.user" "root" +} +io_passthru_body() +{ + if [ -c "${TEST_DEV_PATH}" ] ; then + atf_check -o not-empty -e empty nvmecontrol io-passthru --opcode=02 --data-len=4096 --cdw10=0 --cdw11=0 --cdw12=0x70000 -r 0 nvme0 ${TEST_DEV} + else + atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol io-passthru --opcode=02 --data-len=4096 --cdw10=0 --cdw11=0 --cdw12=0x70000 -r 0 nvme0 ${TEST_DEV} + fi + atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol io-passthru ${INV_OPT} --opcode=02 --data-len=4096 --cdw10=0 --cdw11=0 --cdw12=0x70000 -r 0 nvme0 ${TEST_DEV} +} + +atf_test_case logpage +logpage_head() +{ + atf_set "descr" "check the logpage command" + atf_set "require.user" "root" +} +logpage_body() +{ + if [ -c "${TEST_DEV_PATH}" ] ; then + atf_check -o not-empty -e empty nvmecontrol logpage -p 1 ${TEST_DEV} + else + atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol logpage -p 1 ${TEST_DEV} + fi + atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol logpage -p 1 ${INV_OPT} ${TEST_DEV} +} + +atf_test_case nsid +nsid_head() +{ + atf_set "descr" "check the nsid command" + atf_set "require.user" "root" +} +nsid_body() +{ + if [ -c "${TEST_DEV_PATH}" ] ; then + atf_check -o not-empty -e empty nvmecontrol nsid ${TEST_DEV} + else + atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol nsid ${TEST_DEV} + fi + atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol nsid ${INV_OPT} ${TEST_DEV} +} + +atf_test_case power +power_head() +{ + atf_set "descr" "check the power command" + atf_set "require.user" "root" +} +power_body() +{ + if [ -c "${TEST_DEV_PATH}" ] ; then + atf_check -o not-empty -e empty nvmecontrol power ${TEST_DEV} + else + atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol power ${TEST_DEV} + fi + atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol power ${INV_OPT} ${TEST_DEV} +} + +atf_test_case reset +reset_head() +{ + atf_set "descr" "check the reset command" + atf_set "require.user" "root" +} +reset_body() +{ + atf_check -s not-exit:0 -o ignore -e match:"${INV_OPT_MSG}" nvmecontrol reset ${INV_OPT} ${TEST_DEV} + if [ -c "${TEST_DEV_PATH}" ] ; then + # Reset of an active device seems a little dangerous, + # therefore, this is normally disabled. + if ${DANGEROUS} ; then + atf_check -o not-empty -e empty nvmecontrol reset ${TEST_DEV} + else + atf_skip "Skipping reset test" + fi + else + atf_check -s not-exit:0 -o empty -e not-empty nvmecontrol reset ${TEST_DEV} + fi +} + + +atf_init_test_cases() +{ + atf_add_test_case fake_lib + atf_add_test_case fake_local_lib + atf_add_test_case admin_passthru + atf_add_test_case devlist + atf_add_test_case identify + atf_add_test_case io_passthru + atf_add_test_case logpage + atf_add_test_case nsid + atf_add_test_case power + atf_add_test_case reset +}