git: 37357ba28a6d - stable/14 - ctl: add tests for START STOP UNIT

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Thu, 19 Sep 2024 20:24:52 UTC
The branch stable/14 has been updated by asomers:

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

commit 37357ba28a6dd984043a60e16084bd79071d95e2
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2024-08-23 20:51:32 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2024-09-19 20:23:59 +0000

    ctl: add tests for START STOP UNIT
    
    Sponsored by:   Axcient
    Reviewed by:    emaste, markj
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1409
    
    (cherry picked from commit fe1755fa6bb4039c1e00f5226c473a024685005b)
---
 tests/sys/cam/ctl/Makefile           |   5 +-
 tests/sys/cam/ctl/ctl.subr           |  94 ++++++++++++++++++++++
 tests/sys/cam/ctl/read_buffer.sh     |  72 +++--------------
 tests/sys/cam/ctl/start_stop_unit.sh | 150 +++++++++++++++++++++++++++++++++++
 4 files changed, 257 insertions(+), 64 deletions(-)

diff --git a/tests/sys/cam/ctl/Makefile b/tests/sys/cam/ctl/Makefile
index 0e6f39a1a56f..1b857bb0c291 100644
--- a/tests/sys/cam/ctl/Makefile
+++ b/tests/sys/cam/ctl/Makefile
@@ -2,9 +2,12 @@ PACKAGE=	tests
 
 TESTSDIR=	${TESTSBASE}/sys/cam/ctl
 
+${PACKAGE}FILES+=	ctl.subr
+
 ATF_TESTS_SH+=	read_buffer
+ATF_TESTS_SH+=	start_stop_unit
 
 # Must be exclusive because it disables/enables camsim
-TEST_METADATA.read_buffer+=	is_exclusive="true"
+TEST_METADATA+=	is_exclusive="true"
 
 .include <bsd.test.mk>
diff --git a/tests/sys/cam/ctl/ctl.subr b/tests/sys/cam/ctl/ctl.subr
new file mode 100644
index 000000000000..18991e0fa144
--- /dev/null
+++ b/tests/sys/cam/ctl/ctl.subr
@@ -0,0 +1,94 @@
+# vim: filetype=sh
+
+# 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.
+
+load_modules() {
+	if ! kldstat -q -m ctl; then
+		kldload ctl || atf_skip "could not load ctl kernel mod"
+	fi
+	if ! ctladm port -o on -p 0; then
+		atf_skip "could not enable the camsim frontend"
+	fi
+}
+
+find_device() {
+	LUN=$1
+
+	# Rescan camsim
+	# XXX  camsim doesn't update when creating a new device.  Worse, a
+	# rescan won't look for new devices.  So we must disable/re-enable it.
+	# Worse still, enabling it isn't synchronous, so we need a retry loop
+	# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=281000
+	retries=5
+	ctladm port -o off -p 0 >/dev/null
+	ctladm port -o on -p 0 >/dev/null
+	HEXLUN=`printf %x $LUN`
+	while true; do
+		dev=`camcontrol devlist | awk -v lun=$HEXLUN '/FREEBSD CTL/ && $9==lun {split($10, fields, /[,]/); print fields[1];}' | sed 's:[()]::'`
+		if [ -z "$dev" -o ! -c /dev/$dev ]; then
+			retries=$(( $retries - 1 ))
+			if [ $retries -eq 0 ]; then
+				cat lun-create.txt
+				camcontrol devlist
+				atf_fail "Could not find GEOM device"
+			fi
+			sleep 0.1
+			continue
+		fi
+		break
+	done
+	# Ensure that it's actually ready.  camcontrol may report the disk's
+	# ident before it's actually ready to receive commands.  Maybe that's
+	# because all of the GEOM providers must probe it?
+	while true; do
+		dd if=/dev/$dev bs=4096 count=1 of=/dev/null >/dev/null 2>/dev/null && break
+		retries=$(( $retries - 1 ))
+		if [ $retries -eq 0 ]; then
+			atf_fail "Device never became ready"
+		fi
+		sleep 0.1
+	done
+}
+
+# Create a CTL LUN
+create_ramdisk() {
+	EXTRA_ARGS=$*
+
+	atf_check -o save:lun-create.txt ctladm create -b ramdisk -s 1048576 $EXTRA_ARGS
+	atf_check egrep -q "LUN created successfully" lun-create.txt
+	LUN=`awk '/LUN ID:/ {print $NF}' lun-create.txt`
+	if [ -z "$LUN" ]; then
+		atf_fail "Could not find LUN id"
+	fi
+	find_device $LUN
+}
+
+cleanup() {
+	if [ -e "lun-create.txt" ]; then
+		lun_id=`awk '/LUN ID:/ {print $NF}' lun-create.txt`
+		ctladm remove -b ramdisk -l $lun_id > /dev/null
+	fi
+}
diff --git a/tests/sys/cam/ctl/read_buffer.sh b/tests/sys/cam/ctl/read_buffer.sh
index 4a84eb6b9725..e54b0dadc134 100644
--- a/tests/sys/cam/ctl/read_buffer.sh
+++ b/tests/sys/cam/ctl/read_buffer.sh
@@ -28,61 +28,7 @@
 # * Buffer ID other than 0.  We don't support those.
 # * The Mode Specific field.  We don't support it.
 
-load_modules() {
-	if ! kldstat -q -m ctl; then
-		kldload ctl || atf_skip "could not load ctl kernel mod"
-	fi
-	if ! ctladm port -o on -p 0; then
-		atf_skip "could not enable the camsim frontend"
-	fi
-}
-
-find_da_device() {
-	SERIAL=$1
-
-	# Rescan camsim
-	# XXX  camsim doesn't update when creating a new device.  Worse, a
-	# rescan won't look for new devices.  So we must disable/re-enable it.
-	# Worse still, enabling it isn't synchronous, so we need a retry loop
-	# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=281000
-	retries=5
-	ctladm port -o off -p 0 >/dev/null
-	ctladm port -o on -p 0 >/dev/null
-	while true; do
-
-		# Find the corresponding da device
-		da=`geom disk list | awk -v serial=$SERIAL ' /Geom name:/ { devname=$NF } /ident:/ && $NF ~ serial { print devname; exit } '`
-		if [ -z "$da" ]; then
-			retries=$(( $retries - 1 ))
-			if [ $retries -eq 0 ]; then
-				cat lun-create.txt
-				geom disk list
-				atf_fail "Could not find da device"
-			fi
-			sleep 0.1
-			continue
-		fi
-		break
-	done
-}
-
-# Create a CTL LUN
-create_ramdisk() {
-	atf_check -o save:lun-create.txt ctladm create -b ramdisk -s 1048576
-	atf_check egrep -q "LUN created successfully" lun-create.txt
-	SERIAL=`awk '/Serial Number:/ {print $NF}' lun-create.txt`
-	if [ -z "$SERIAL" ]; then
-		atf_fail "Could not find serial number"
-	fi
-	find_da_device $SERIAL
-}
-
-cleanup() {
-	if [ -e "lun-create.txt" ]; then
-		lun_id=`awk '/LUN ID:/ {print $NF}' lun-create.txt`
-		ctladm remove -b ramdisk -l $lun_id > /dev/null
-	fi
-}
+. $(atf_get_srcdir)/ctl.subr
 
 atf_test_case basic cleanup
 basic_head()
@@ -98,10 +44,10 @@ basic_body()
 	# Write to its buffer
 	cp /etc/passwd input
 	len=`wc -c input | cut -wf 2`
-	atf_check -o ignore sg_write_buffer --mode data --in=input /dev/$da
+	atf_check -o ignore sg_write_buffer --mode data --in=input /dev/$dev
 
 	# Read it back
-	atf_check -o save:output sg_read_buffer --mode data -l $len --raw /dev/$da
+	atf_check -o save:output sg_read_buffer --mode data -l $len --raw /dev/$dev
 
 	# And verify
 	if ! diff -q input output; then
@@ -126,7 +72,7 @@ desc_body()
 {
 	create_ramdisk
 
-	atf_check -o inline:" 00     00 04 00 00\n" sg_read_buffer --hex --mode desc /dev/$da
+	atf_check -o inline:" 00     00 04 00 00\n" sg_read_buffer --hex --mode desc /dev/$dev
 }
 desc_cleanup()
 {
@@ -147,10 +93,10 @@ length_body()
 	# Write to its buffer
 	atf_check -o ignore -e ignore dd if=/dev/random of=input bs=4096 count=1
 	atf_check -o ignore -e ignore dd if=input bs=2048 count=1 of=expected
-	atf_check -o ignore sg_write_buffer --mode data --in=input /dev/$da
+	atf_check -o ignore sg_write_buffer --mode data --in=input /dev/$dev
 
 	# Read it back
-	atf_check -o save:output sg_read_buffer --mode data -l 2048 --raw /dev/$da
+	atf_check -o save:output sg_read_buffer --mode data -l 2048 --raw /dev/$dev
 
 	# And verify
 	if ! diff -q expected output; then
@@ -176,10 +122,10 @@ offset_body()
 	# Write to its buffer
 	atf_check -o ignore -e ignore dd if=/dev/random of=input bs=4096 count=1
 	atf_check -o ignore -e ignore dd if=input iseek=2 bs=512 count=1 of=expected
-	atf_check -o ignore sg_write_buffer --mode data --in=input /dev/$da
+	atf_check -o ignore sg_write_buffer --mode data --in=input /dev/$dev
 
 	# Read it back
-	atf_check -o save:output sg_read_buffer --mode data -l 512 -o 1024 --raw /dev/$da
+	atf_check -o save:output sg_read_buffer --mode data -l 512 -o 1024 --raw /dev/$dev
 
 	# And verify
 	if ! diff -q expected output; then
@@ -203,7 +149,7 @@ uninitialized_body()
 	create_ramdisk
 
 	# Read an uninitialized buffer
-	atf_check -o save:output sg_read_buffer --mode data -l 262144 --raw /dev/$da
+	atf_check -o save:output sg_read_buffer --mode data -l 262144 --raw /dev/$dev
 
 	# And verify
 	atf_check -o ignore -e ignore dd if=/dev/zero bs=262144 count=1 of=expected
diff --git a/tests/sys/cam/ctl/start_stop_unit.sh b/tests/sys/cam/ctl/start_stop_unit.sh
new file mode 100644
index 000000000000..163011c8f574
--- /dev/null
+++ b/tests/sys/cam/ctl/start_stop_unit.sh
@@ -0,0 +1,150 @@
+# 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.
+
+. $(atf_get_srcdir)/ctl.subr
+
+# TODO:
+# * format layer
+# * IMM bit
+# * LOEJ
+# * noflush
+# * power conditions
+
+# Not Tested
+# * Power Condition Modifier (not implemented in CTL)
+
+atf_test_case eject cleanup
+eject_head()
+{
+	atf_set "descr" "START STOP UNIT can eject a CDROM device"
+	atf_set "require.user" "root"
+	atf_set "require.progs" sg_start sg_readcap
+}
+eject_body()
+{
+	# -t 5 for CD/DVD device type
+	create_ramdisk -t 5
+
+	# Verify that the device is online
+	# Too bad I don't know of any other way to check that it's stopped but
+	# by using sg_readcap.
+	atf_check -o ignore -e not-match:"Device not ready" sg_readcap /dev/$dev
+
+	# eject the device
+	atf_check sg_start --eject /dev/$dev
+
+	# Ejected, it should now return ENXIO
+	atf_check -s exit:1 -o ignore -e match:"Device not configured" dd if=/dev/$dev bs=4096 count=1 of=/dev/null
+}
+eject_cleanup()
+{
+	cleanup
+}
+
+atf_test_case load cleanup
+load_head()
+{
+	atf_set "descr" "START STOP UNIT can load a CDROM device"
+	atf_set "require.user" "root"
+	atf_set "require.progs" sg_start sg_readcap
+}
+load_body()
+{
+	# -t 5 for CD/DVD device type
+	create_ramdisk -t 5
+
+	# eject the device
+	atf_check sg_start --eject /dev/$dev
+
+	# Verify that it's offline it should now return ENXIO
+	atf_check -s exit:1 -o ignore -e match:"Device not configured" dd if=/dev/$dev bs=4096 count=1 of=/dev/null
+
+	# Load it again
+	atf_check sg_start --load /dev/$dev
+
+	atf_check -o ignore -e ignore dd if=/dev/$dev bs=4096 count=1 of=/dev/null
+	atf_check -o ignore -e not-match:"Device not ready" sg_readcap /dev/$dev
+}
+load_cleanup()
+{
+	cleanup
+}
+
+atf_test_case start cleanup
+start_head()
+{
+	atf_set "descr" "START STOP UNIT can start a device"
+	atf_set "require.user" "root"
+	atf_set "require.progs" sg_start sg_readcap
+}
+start_body()
+{
+	create_ramdisk
+
+	# stop the device
+	atf_check sg_start --stop /dev/$dev
+
+	# And start it again
+	atf_check sg_start /dev/$dev
+
+	# Now sg_readcap should succeed.  Too bad I don't know of any other way
+	# to check that it's stopped.
+	atf_check -o ignore -e not-match:"Device not ready" sg_readcap /dev/$dev
+}
+start_cleanup()
+{
+	cleanup
+}
+
+atf_test_case stop cleanup
+stop_head()
+{
+	atf_set "descr" "START STOP UNIT can stop a device"
+	atf_set "require.user" "root"
+	atf_set "require.progs" sg_start sg_readcap
+}
+stop_body()
+{
+	create_ramdisk
+
+	# Stop the device
+	atf_check sg_start --stop /dev/$dev
+
+	# Now sg_readcap should fail.  Too bad I don't know of any other way to
+	# check that it's stopped.
+	atf_check -s exit:2 -e match:"Device not ready" sg_readcap /dev/$dev
+}
+stop_cleanup()
+{
+	cleanup
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case eject
+	atf_add_test_case load
+	atf_add_test_case start
+	atf_add_test_case stop
+}