git: ab9e5b86596c - stable/13 - Add some ATF tests for ctladm

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Fri, 28 Jun 2024 19:25:13 UTC
The branch stable/13 has been updated by asomers:

URL: https://cgit.FreeBSD.org/src/commit/?id=ab9e5b86596c998b2001b87a2a6c67f564769075

commit ab9e5b86596c998b2001b87a2a6c67f564769075
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2024-06-05 20:16:25 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2024-06-28 19:04:11 +0000

    Add some ATF tests for ctladm
    
    So far only "ctladm port -c" and "ctladm port -r" are covered.
    
    Sponsored by:   Axcient
    Reviewed by:    mav
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1279
    
    (cherry picked from commit 9747d11d91642cb9b81602d88e8aebeb388543c7)
    
    Fix mtree entry for ctladm tests
    
    Sponsored by:   Axcient
    
    (cherry picked from commit 81ef0a89fcc055bdf3d0f1affbadb643412ebce9)
    
    ctladm: add a copyright header to the port test
    
    [skip ci]
    
    Reported by:    markj
    Sponsored by:   Axcient
    
    (cherry picked from commit a1608e8854da7f3b5734c2fcb85dd740733a08e6)
---
 etc/mtree/BSD.tests.dist       |   2 +
 usr.sbin/ctladm/Makefile       |   3 +
 usr.sbin/ctladm/tests/Makefile |  10 +++
 usr.sbin/ctladm/tests/port.sh  | 180 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 195 insertions(+)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index 5d31c374166d..91e02c4ae7e5 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -1141,6 +1141,8 @@
     usr.sbin
         chown
         ..
+        ctladm
+        ..
         daemon
         ..
         etcupdate
diff --git a/usr.sbin/ctladm/Makefile b/usr.sbin/ctladm/Makefile
index f9ddd251f96c..69dc727deaf4 100644
--- a/usr.sbin/ctladm/Makefile
+++ b/usr.sbin/ctladm/Makefile
@@ -23,4 +23,7 @@ MAN=		ctladm.8
 CFLAGS+=	-DWANT_ISCSI
 .endif
 
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+=	tests
+
 .include <bsd.prog.mk>
diff --git a/usr.sbin/ctladm/tests/Makefile b/usr.sbin/ctladm/tests/Makefile
new file mode 100644
index 000000000000..73ac94d77d21
--- /dev/null
+++ b/usr.sbin/ctladm/tests/Makefile
@@ -0,0 +1,10 @@
+
+PACKAGE=	tests
+
+ATF_TESTS_SH=	port
+
+# "ctladm port" does not report the name of the port just created, so we can't
+# cleanup unless we assume that no other test created a port too.
+TEST_METADATA+= is_exclusive="true"
+
+.include <bsd.test.mk>
diff --git a/usr.sbin/ctladm/tests/port.sh b/usr.sbin/ctladm/tests/port.sh
new file mode 100644
index 000000000000..110b89bd965b
--- /dev/null
+++ b/usr.sbin/ctladm/tests/port.sh
@@ -0,0 +1,180 @@
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Axcient
+# All rights reserved.
+#
+# 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 DOCUMENTATION IS PROVIDED BY THE AUTHOR ``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 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.
+
+# Things that aren't tested due to lack of kernel support:
+# * Creating camsim ports
+# * Creating tpc ports
+# * Creating camtgt ports
+# * Creating umass ports
+
+# TODO
+# * Creating iscsi ports
+# * Creating nvmf ports
+# * Creating ha ports
+# * Creating fc ports
+
+skip_if_ctld() {
+	if service ctld onestatus > /dev/null; then
+		# If ctld is running on this server, let's not interfere.
+		atf_skip "Cannot run this test while ctld is running"
+	fi
+}
+
+cleanup() {
+	driver=$1
+
+	if [ -e after-ports ]; then
+		diff before-ports after-ports | awk "/$driver/ {print \$2}" | xargs -n1 ctladm port -r -d ioctl -p
+	fi
+}
+
+atf_test_case create_ioctl cleanup
+create_ioctl_head()
+{
+	atf_set "descr" "ctladm can create a new ioctl port"
+	atf_set "require.user" "root"
+}
+create_ioctl_body()
+{
+	skip_if_ctld
+
+	atf_check -o save:before-ports ctladm portlist -qf ioctl
+	atf_check ctladm port -c -d "ioctl"
+	atf_check -o save:after-ports ctladm portlist -qf ioctl
+	if test `wc -l before-ports | cut -w -f2` -ge `wc -l after-ports | cut -w -f2`; then
+		atf_fail "Did not create a new ioctl port"
+	fi
+}
+create_ioctl_cleanup()
+{
+	cleanup ioctl
+}
+
+atf_test_case create_ioctl_options cleanup
+create_ioctl_options_head()
+{
+	atf_set "descr" "ctladm can set options when creating a new ioctl port"
+	atf_set "require.user" "root"
+}
+create_ioctl_options_body()
+{
+	skip_if_ctld
+
+	atf_check -o save:before-ports ctladm portlist -qf ioctl
+	atf_check ctladm port -c -d "ioctl" -O pp=101 -O vp=102
+	atf_check -o save:after-ports ctladm portlist -qf ioctl
+	if test `wc -l before-ports | cut -w -f2` -ge `wc -l after-ports | cut -w -f2`; then
+		atf_fail "Did not create a new ioctl port"
+	fi
+	if ! egrep -q '101[[:space:]]+102' after-ports; then
+		ctladm portlist
+		atf_fail "Did not create the port with the specified options"
+	fi
+}
+create_ioctl_options_cleanup()
+{
+	cleanup ioctl
+}
+
+
+atf_test_case disable_ioctl cleanup
+disable_ioctl_head()
+{
+	atf_set "descr" "ctladm can disable an ioctl port"
+	atf_set "require.user" "root"
+}
+disable_ioctl_body()
+{
+	skip_if_ctld
+
+	atf_check -o save:before-ports ctladm portlist -qf ioctl
+	atf_check ctladm port -c -d "ioctl"
+	atf_check -o save:after-ports ctladm portlist -qf ioctl
+	if test `wc -l before-ports | cut -w -f2` -ge `wc -l after-ports | cut -w -f2`; then
+		atf_fail "Did not create a new ioctl port"
+	fi
+	portnum=`diff before-ports after-ports | awk '/ioctl/ {print $2}'`;
+	atf_check -o ignore ctladm port -o off -p $portnum
+	atf_check -o match:"^$portnum *NO" ctladm portlist -qf ioctl
+}
+disable_ioctl_cleanup()
+{
+	cleanup ioctl
+}
+
+atf_test_case enable_ioctl cleanup
+enable_ioctl_head()
+{
+	atf_set "descr" "ctladm can enable an ioctl port"
+	atf_set "require.user" "root"
+}
+enable_ioctl_body()
+{
+	skip_if_ctld
+
+	atf_check -o save:before-ports ctladm portlist -qf ioctl
+	atf_check ctladm port -c -d "ioctl"
+	atf_check -o save:after-ports ctladm portlist -qf ioctl
+	if test `wc -l before-ports | cut -w -f2` -ge `wc -l after-ports | cut -w -f2`; then
+		atf_fail "Did not create a new ioctl port"
+	fi
+	portnum=`diff before-ports after-ports | awk '/ioctl/ {print $2}'`;
+	atf_check -o ignore ctladm port -o off -p $portnum
+	atf_check -o ignore ctladm port -o on -p $portnum
+	atf_check -o match:"^$portnum *YES" ctladm portlist -qf ioctl
+}
+enable_ioctl_cleanup()
+{
+	cleanup ioctl
+}
+
+atf_test_case remove_ioctl
+remove_ioctl_head()
+{
+	atf_set "descr" "ctladm can remove an ioctl port"
+	atf_set "require.user" "root"
+}
+remove_ioctl_body()
+{
+	skip_if_ctld
+
+	atf_check -o save:before-ports ctladm portlist -qf ioctl
+	atf_check ctladm port -c -d "ioctl"
+	atf_check -o save:after-ports ctladm portlist -qf ioctl
+	if test `wc -l before-ports | cut -w -f2` -ge `wc -l after-ports | cut -w -f2`; then
+		atf_fail "Did not create a new ioctl port"
+	fi
+	portnum=`diff before-ports after-ports | awk '/ioctl/ {print $2}'`;
+	atf_check ctladm port -r -d ioctl -p $portnum
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case create_ioctl
+	atf_add_test_case create_ioctl_options
+	atf_add_test_case disable_ioctl
+	atf_add_test_case enable_ioctl
+	atf_add_test_case remove_ioctl
+}