git: 9747d11d9164 - main - Add some ATF tests for ctladm

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Mon, 10 Jun 2024 16:01:41 UTC
The branch main has been updated by asomers:

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

commit 9747d11d91642cb9b81602d88e8aebeb388543c7
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2024-06-05 20:16:25 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2024-06-10 16:01:25 +0000

    Add some ATF tests for ctladm
    
    So far only "ctladm port -c" and "ctladm port -r" are covered.
    
    MFC after:      2 weeks
    Sponsored by:   Axcient
    Reviewed by:    mav
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1279
---
 etc/mtree/BSD.tests.dist       |   2 +
 usr.sbin/ctladm/Makefile       |   3 +
 usr.sbin/ctladm/tests/Makefile |  10 +++
 usr.sbin/ctladm/tests/port.sh  | 155 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 170 insertions(+)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index 3f447f9ec25e..86db4304b932 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -90,6 +90,8 @@
             ..
         ..
         usr.sbin
+            ctladm
+            ..
             dtrace
                 common
                     aggs
diff --git a/usr.sbin/ctladm/Makefile b/usr.sbin/ctladm/Makefile
index 5e0df8065cce..b563891672be 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..b2cdea6f1de2
--- /dev/null
+++ b/usr.sbin/ctladm/tests/port.sh
@@ -0,0 +1,155 @@
+# 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
+}