Wed Jun 24 09:32:41 2020 UTC ()
Add few basic tests for cpuctl(8). These cover PR kern/45117 and PR bin/54220.
Though, the former is not explicitly tested as it hangs the system.


(jruoho)
diff -r1.845 -r1.846 src/distrib/sets/lists/tests/mi
diff -r1.163 -r1.164 src/etc/mtree/NetBSD.dist.tests
diff -r0 -r1.1 src/tests/usr.sbin/cpuctl/Makefile
diff -r0 -r1.1 src/tests/usr.sbin/cpuctl/t_cpuctl.sh

cvs diff -r1.845 -r1.846 src/distrib/sets/lists/tests/mi (expand / switch to unified diff)

--- src/distrib/sets/lists/tests/mi 2020/06/24 09:21:43 1.845
+++ src/distrib/sets/lists/tests/mi 2020/06/24 09:32:41 1.846
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: mi,v 1.845 2020/06/24 09:21:43 jruoho Exp $ 1# $NetBSD: mi,v 1.846 2020/06/24 09:32:41 jruoho Exp $
2# 2#
3# Note: don't delete entries from here - mark them as "obsolete" instead. 3# Note: don't delete entries from here - mark them as "obsolete" instead.
4# 4#
5./etc/mtree/set.tests tests-sys-root 5./etc/mtree/set.tests tests-sys-root
6./usr/libdata/debug/usr/tests tests-base-debug compattestdir 6./usr/libdata/debug/usr/tests tests-base-debug compattestdir
7./usr/libdata/debug/usr/tests/atf tests-atf-debug compattestfile,atf 7./usr/libdata/debug/usr/tests/atf tests-atf-debug compattestfile,atf
8./usr/libdata/debug/usr/tests/atf/atf-c tests-atf-debug compattestfile,atf 8./usr/libdata/debug/usr/tests/atf/atf-c tests-atf-debug compattestfile,atf
9./usr/libdata/debug/usr/tests/atf/atf-c++ tests-atf-debug compattestfile,atf 9./usr/libdata/debug/usr/tests/atf/atf-c++ tests-atf-debug compattestfile,atf
10./usr/libdata/debug/usr/tests/atf/atf-c++/detail tests-atf-debug compattestfile,atf 10./usr/libdata/debug/usr/tests/atf/atf-c++/detail tests-atf-debug compattestfile,atf
11./usr/libdata/debug/usr/tests/atf/atf-c/detail tests-atf-debug compattestfile,atf 11./usr/libdata/debug/usr/tests/atf/atf-c/detail tests-atf-debug compattestfile,atf
12./usr/libdata/debug/usr/tests/atf/atf-compile tests-obsolete obsolete 12./usr/libdata/debug/usr/tests/atf/atf-compile tests-obsolete obsolete
13./usr/libdata/debug/usr/tests/atf/atf-report tests-obsolete obsolete 13./usr/libdata/debug/usr/tests/atf/atf-report tests-obsolete obsolete
14./usr/libdata/debug/usr/tests/atf/atf-run tests-obsolete obsolete 14./usr/libdata/debug/usr/tests/atf/atf-run tests-obsolete obsolete
@@ -4743,26 +4743,30 @@ @@ -4743,26 +4743,30 @@
4743./usr/tests/usr.bin/xlint/lint1/d_packed_structs.c tests-usr.bin-tests compattestfile,atf 4743./usr/tests/usr.bin/xlint/lint1/d_packed_structs.c tests-usr.bin-tests compattestfile,atf
4744./usr/tests/usr.bin/xlint/lint1/d_shift_to_narrower_type.c tests-usr.bin-tests compattestfile,atf 4744./usr/tests/usr.bin/xlint/lint1/d_shift_to_narrower_type.c tests-usr.bin-tests compattestfile,atf
4745./usr/tests/usr.bin/xlint/lint1/d_type_conv1.c tests-usr.bin-tests compattestfile,atf 4745./usr/tests/usr.bin/xlint/lint1/d_type_conv1.c tests-usr.bin-tests compattestfile,atf
4746./usr/tests/usr.bin/xlint/lint1/d_type_conv2.c tests-usr.bin-tests compattestfile,atf 4746./usr/tests/usr.bin/xlint/lint1/d_type_conv2.c tests-usr.bin-tests compattestfile,atf
4747./usr/tests/usr.bin/xlint/lint1/d_type_conv3.c tests-usr.bin-tests compattestfile,atf 4747./usr/tests/usr.bin/xlint/lint1/d_type_conv3.c tests-usr.bin-tests compattestfile,atf
4748./usr/tests/usr.bin/xlint/lint1/d_type_question_colon.c tests-usr.bin-tests compattestfile,atf 4748./usr/tests/usr.bin/xlint/lint1/d_type_question_colon.c tests-usr.bin-tests compattestfile,atf
4749./usr/tests/usr.bin/xlint/lint1/d_typefun.c tests-usr.bin-tests compattestfile,atf 4749./usr/tests/usr.bin/xlint/lint1/d_typefun.c tests-usr.bin-tests compattestfile,atf
4750./usr/tests/usr.bin/xlint/lint1/d_typename_as_var.c tests-usr.bin-tests compattestfile,atf 4750./usr/tests/usr.bin/xlint/lint1/d_typename_as_var.c tests-usr.bin-tests compattestfile,atf
4751./usr/tests/usr.bin/xlint/lint1/d_zero_sized_arrays.c tests-usr.bin-tests compattestfile,atf 4751./usr/tests/usr.bin/xlint/lint1/d_zero_sized_arrays.c tests-usr.bin-tests compattestfile,atf
4752./usr/tests/usr.bin/xlint/lint1/t_integration tests-usr.bin-tests compattestfile,atf 4752./usr/tests/usr.bin/xlint/lint1/t_integration tests-usr.bin-tests compattestfile,atf
4753./usr/tests/usr.sbin tests-usr.sbin-tests compattestfile,atf 4753./usr/tests/usr.sbin tests-usr.sbin-tests compattestfile,atf
4754./usr/tests/usr.sbin/Atffile tests-usr.sbin-tests compattestfile,atf 4754./usr/tests/usr.sbin/Atffile tests-usr.sbin-tests compattestfile,atf
4755./usr/tests/usr.sbin/Kyuafile tests-usr.sbin-tests compattestfile,atf,kyua 4755./usr/tests/usr.sbin/Kyuafile tests-usr.sbin-tests compattestfile,atf,kyua
 4756./usr/tests/usr.sbin/cpuctl tests-usr.sbin-tests compattestfile,atf
 4757./usr/tests/usr.sbin/cpuctl/Atffile tests-usr.sbin-tests compattestfile,atf
 4758./usr/tests/usr.sbin/cpuctl/Kyuafile tests-usr.sbin-tests compattestfile,atf,kyua
 4759./usr/tests/usr.sbin/cpuctl/t_cpuctl tests-usr.sbin-tests compattestfile,atf
4756./usr/tests/usr.sbin/mtree tests-usr.sbin-tests compattestfile,atf 4760./usr/tests/usr.sbin/mtree tests-usr.sbin-tests compattestfile,atf
4757./usr/tests/usr.sbin/mtree/Atffile tests-usr.sbin-tests compattestfile,atf 4761./usr/tests/usr.sbin/mtree/Atffile tests-usr.sbin-tests compattestfile,atf
4758./usr/tests/usr.sbin/mtree/Kyuafile tests-usr.sbin-tests compattestfile,atf,kyua 4762./usr/tests/usr.sbin/mtree/Kyuafile tests-usr.sbin-tests compattestfile,atf,kyua
4759./usr/tests/usr.sbin/mtree/d_convert.in tests-usr.sbin-tests compattestfile,atf 4763./usr/tests/usr.sbin/mtree/d_convert.in tests-usr.sbin-tests compattestfile,atf
4760./usr/tests/usr.sbin/mtree/d_convert_C.out tests-usr.sbin-tests compattestfile,atf 4764./usr/tests/usr.sbin/mtree/d_convert_C.out tests-usr.sbin-tests compattestfile,atf
4761./usr/tests/usr.sbin/mtree/d_convert_C_S.out tests-usr.sbin-tests compattestfile,atf 4765./usr/tests/usr.sbin/mtree/d_convert_C_S.out tests-usr.sbin-tests compattestfile,atf
4762./usr/tests/usr.sbin/mtree/d_convert_D.out tests-usr.sbin-tests compattestfile,atf 4766./usr/tests/usr.sbin/mtree/d_convert_D.out tests-usr.sbin-tests compattestfile,atf
4763./usr/tests/usr.sbin/mtree/d_convert_D_S.out tests-usr.sbin-tests compattestfile,atf 4767./usr/tests/usr.sbin/mtree/d_convert_D_S.out tests-usr.sbin-tests compattestfile,atf
4764./usr/tests/usr.sbin/mtree/d_create.out tests-obsolete obsolete 4768./usr/tests/usr.sbin/mtree/d_create.out tests-obsolete obsolete
4765./usr/tests/usr.sbin/mtree/d_merge.in tests-usr.sbin-tests compattestfile,atf 4769./usr/tests/usr.sbin/mtree/d_merge.in tests-usr.sbin-tests compattestfile,atf
4766./usr/tests/usr.sbin/mtree/d_merge_C_M.out tests-usr.sbin-tests compattestfile,atf 4770./usr/tests/usr.sbin/mtree/d_merge_C_M.out tests-usr.sbin-tests compattestfile,atf
4767./usr/tests/usr.sbin/mtree/d_merge_C_M_S.out tests-usr.sbin-tests compattestfile,atf 4771./usr/tests/usr.sbin/mtree/d_merge_C_M_S.out tests-usr.sbin-tests compattestfile,atf
4768./usr/tests/usr.sbin/mtree/mtree_d_create.out tests-usr.sbin-tests compattestfile,atf 4772./usr/tests/usr.sbin/mtree/mtree_d_create.out tests-usr.sbin-tests compattestfile,atf

cvs diff -r1.163 -r1.164 src/etc/mtree/NetBSD.dist.tests (expand / switch to unified diff)

--- src/etc/mtree/NetBSD.dist.tests 2020/06/24 09:21:43 1.163
+++ src/etc/mtree/NetBSD.dist.tests 2020/06/24 09:32:41 1.164
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1# $NetBSD: NetBSD.dist.tests,v 1.163 2020/06/24 09:21:43 jruoho Exp $ 1# $NetBSD: NetBSD.dist.tests,v 1.164 2020/06/24 09:32:41 jruoho Exp $
2 2
3./usr/libdata/debug/usr/tests 3./usr/libdata/debug/usr/tests
4./usr/libdata/debug/usr/tests/atf 4./usr/libdata/debug/usr/tests/atf
5./usr/libdata/debug/usr/tests/atf/atf-c 5./usr/libdata/debug/usr/tests/atf/atf-c
6./usr/libdata/debug/usr/tests/atf/atf-c/detail 6./usr/libdata/debug/usr/tests/atf/atf-c/detail
7./usr/libdata/debug/usr/tests/atf/atf-c++ 7./usr/libdata/debug/usr/tests/atf/atf-c++
8./usr/libdata/debug/usr/tests/atf/atf-c++/detail 8./usr/libdata/debug/usr/tests/atf/atf-c++/detail
9./usr/libdata/debug/usr/tests/atf/formats 9./usr/libdata/debug/usr/tests/atf/formats
10./usr/libdata/debug/usr/tests/atf/test-programs 10./usr/libdata/debug/usr/tests/atf/test-programs
11./usr/libdata/debug/usr/tests/atf/tools 11./usr/libdata/debug/usr/tests/atf/tools
12./usr/libdata/debug/usr/tests/bin 12./usr/libdata/debug/usr/tests/bin
13./usr/libdata/debug/usr/tests/bin/df 13./usr/libdata/debug/usr/tests/bin/df
14./usr/libdata/debug/usr/tests/crypto 14./usr/libdata/debug/usr/tests/crypto
@@ -431,17 +431,18 @@ @@ -431,17 +431,18 @@
431./usr/tests/usr.bin/sdiff 431./usr/tests/usr.bin/sdiff
432./usr/tests/usr.bin/sed 432./usr/tests/usr.bin/sed
433./usr/tests/usr.bin/shmif_dumpbus 433./usr/tests/usr.bin/shmif_dumpbus
434./usr/tests/usr.bin/sort 434./usr/tests/usr.bin/sort
435./usr/tests/usr.bin/tar 435./usr/tests/usr.bin/tar
436./usr/tests/usr.bin/tmux 436./usr/tests/usr.bin/tmux
437./usr/tests/usr.bin/tr 437./usr/tests/usr.bin/tr
438./usr/tests/usr.bin/unifdef 438./usr/tests/usr.bin/unifdef
439./usr/tests/usr.bin/uniq 439./usr/tests/usr.bin/uniq
440./usr/tests/usr.bin/vmstat 440./usr/tests/usr.bin/vmstat
441./usr/tests/usr.bin/xlint 441./usr/tests/usr.bin/xlint
442./usr/tests/usr.bin/xlint/lint1 442./usr/tests/usr.bin/xlint/lint1
443./usr/tests/usr.sbin 443./usr/tests/usr.sbin
 444./usr/tests/usr.sbin/cpuctl
444./usr/tests/usr.sbin/mtree 445./usr/tests/usr.sbin/mtree
445./usr/tests/usr.sbin/tcpdump 446./usr/tests/usr.sbin/tcpdump
446./usr/tests/usr.sbin/traceroute 447./usr/tests/usr.sbin/traceroute
447./usr/tests/usr.sbin/useradd 448./usr/tests/usr.sbin/useradd

File Added: src/tests/usr.sbin/cpuctl/Makefile
# $NetBSD: Makefile,v 1.1 2020/06/24 09:32:41 jruoho Exp $

.include <bsd.own.mk>

TESTSDIR=	${TESTSBASE}/usr.sbin/cpuctl
TESTS_SH=	t_cpuctl

.include <bsd.test.mk>

File Added: src/tests/usr.sbin/cpuctl/t_cpuctl.sh
# $NetBSD: t_cpuctl.sh,v 1.1 2020/06/24 09:32:41 jruoho Exp $
#
# Copyright (c) 2020 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Jukka Ruohonen.
#
# 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
#
tmp="/tmp/cpuctl.txt"

setcpu() {

	ncpu=$(sysctl -n hw.ncpu)

	if [ $ncpu -eq 1 ]; then
		atf_pass
	fi

	# Skip the boot processor. Disabling interrupts
	# on it will hang the system (PR kern/45117).
	#
	while [ $ncpu -gt 1 ]; do

		cpuid=$(expr $ncpu - 1)
		cpuctl $1 $cpuid >/dev/null 2>&1

		if [ ! $? -eq 0 ]; then
			$2 $3
		fi

		ncpu=$(expr $ncpu - 1)
	done
}

clean() {

	i=0

	while read line; do

		i=$(expr $i + 1)

		if [ $i -lt 3 ]; then
			continue
		fi

		cpuid=$(echo $line | awk '{print $1}')
		online=$(echo $line | awk '{print $3}')
		intr=$(echo $line | awk '{print $4}')

		cpuctl $online $cpuid
		cpuctl $intr $cpuid

	done < $tmp

	rm $tmp
}

# ncpu.
#
atf_test_case ncpu
ncpu_head() {
	atf_require_prog cpuctl
	atf_set "descr" "Test that cpuctl(8) returns the " \
			"same number of CPUs as sysctl(8)"
}

ncpu_body() {

	lst=$(cpuctl list | wc -l)
	ncpu=$(expr $lst - 2)

	if [ $ncpu -eq 1 ]; then
		atf_pass
	fi

	if [ $(sysctl -n hw.ncpu) -eq $ncpu ]; then
		atf_pass
	fi

	atf_fail "Different number of CPUs"
}

# err
#
atf_test_case err cleanup
err_head() {
	atf_require_prog cpuctl
	atf_set "require.user" "root"
	atf_set "descr" "Test invalid parameters to cpuctl(8)"
}

err_body() {

	cpuctl list > $tmp
	ncpu=$(sysctl -n hw.ncpu)

	atf_check -s exit:1 -e ignore \
		-o empty -x cpuctl identify -1

	atf_check -s exit:1 -e ignore \
		-o empty -x cpuctl offline -1

	atf_check -s exit:1 -e ignore \
		-o empty -x cpuctl nointr -1

	atf_check -s exit:1 -e ignore \
		-o empty -x cpuctl identify $(exp ncpu + 1)

	atf_check -s exit:1 -e ignore \
		-o empty -x cpuctl offline $(exp ncpu + 1)

	atf_check -s exit:1 -e ignore \
		-o empty -x cpuctl nointr $(exp ncpu + 1)
}

err_cleanup() {
	clean
}

# identify
#
atf_test_case identify
identify_head() {
	atf_require_prog cpuctl
	atf_set "descr" "Test that cpuctl(8) identifies at least " \
			"something without segfaulting (PR bin/54220)"
}

identify_body() {

	ncpu=$(sysctl -n hw.ncpu)

	while [ $ncpu -gt 0 ]; do
		cpuid=$(expr $ncpu - 1)
		atf_check -s exit:0 -o not-empty -x cpuctl identify $cpuid
		ncpu=$(expr $ncpu - 1)
	done

	atf_pass
}

# offline
#
atf_test_case offline cleanup
offline_head() {
	atf_require_prog cpuctl
	atf_set "require.user" "root"
	atf_set "descr" "Test setting CPUs offline"
}

offline_body() {

	cpuctl list > $tmp
	setcpu "offline" atf_fail "error in setting a CPU offline"

	# Additional check that the boot processor cannot be
	# set offline, as noted in the cpuctl(9) manual page.
	#
	cpuctl offline 0 >/dev/null 2>&1

	if [ $? -eq 0 ]; then
		$2 $3
	fi
}

offline_cleanup() {
	clean
}

atf_test_case offline_perm
offline_perm_head() {
	atf_require_prog cpuctl
	atf_set "require.user" "unprivileged"
	atf_set "descr" "Test setting CPUs offline as a user"
}

offline_perm_body() {
	setcpu "offline" atf_pass
}

# nointr
#
atf_test_case nointr cleanup
nointr_head() {
	atf_require_prog cpuctl
	atf_set "require.user" "root"
	atf_set "descr" "Test disabling interrupts for CPUs"
}

nointr_body() {
	cpuctl list > $tmp
	setcpu "nointr" atf_fail "error in disabling interrupts"
}

nointr_cleanup() {
	clean
}

atf_test_case nointr_perm
nointr_perm_head() {
	atf_require_prog cpuctl
	atf_set "require.user" "unprivileged"
	atf_set "descr" "Test disabling interrupts as a user"
}

nointr_perm_body() {
	setcpu "nointr" atf_pass
}

atf_init_test_cases() {
	atf_add_test_case ncpu
	atf_add_test_case err
	atf_add_test_case identify
	atf_add_test_case offline
	atf_add_test_case offline_perm
	atf_add_test_case nointr
	atf_add_test_case nointr_perm
}