Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/usr.sbin/cpuctl Add few basic tests for cpuctl(8). The...



details:   https://anonhg.NetBSD.org/src/rev/65bdfc9ec8af
branches:  trunk
changeset: 935078:65bdfc9ec8af
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Wed Jun 24 09:32:41 2020 +0000

description:
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.

diffstat:

 distrib/sets/lists/tests/mi       |    6 +-
 etc/mtree/NetBSD.dist.tests       |    3 +-
 tests/usr.sbin/cpuctl/Makefile    |    8 +
 tests/usr.sbin/cpuctl/t_cpuctl.sh |  240 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 255 insertions(+), 2 deletions(-)

diffs (293 lines):

diff -r bbea3124d1c3 -r 65bdfc9ec8af distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Wed Jun 24 09:21:43 2020 +0000
+++ b/distrib/sets/lists/tests/mi       Wed Jun 24 09:32:41 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.845 2020/06/24 09:21:43 jruoho Exp $
+# $NetBSD: mi,v 1.846 2020/06/24 09:32:41 jruoho Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -4753,6 +4753,10 @@
 ./usr/tests/usr.sbin                           tests-usr.sbin-tests    compattestfile,atf
 ./usr/tests/usr.sbin/Atffile                   tests-usr.sbin-tests    compattestfile,atf
 ./usr/tests/usr.sbin/Kyuafile                  tests-usr.sbin-tests    compattestfile,atf,kyua
+./usr/tests/usr.sbin/cpuctl                    tests-usr.sbin-tests    compattestfile,atf
+./usr/tests/usr.sbin/cpuctl/Atffile            tests-usr.sbin-tests    compattestfile,atf
+./usr/tests/usr.sbin/cpuctl/Kyuafile           tests-usr.sbin-tests    compattestfile,atf,kyua
+./usr/tests/usr.sbin/cpuctl/t_cpuctl           tests-usr.sbin-tests    compattestfile,atf
 ./usr/tests/usr.sbin/mtree                     tests-usr.sbin-tests    compattestfile,atf
 ./usr/tests/usr.sbin/mtree/Atffile             tests-usr.sbin-tests    compattestfile,atf
 ./usr/tests/usr.sbin/mtree/Kyuafile            tests-usr.sbin-tests    compattestfile,atf,kyua
diff -r bbea3124d1c3 -r 65bdfc9ec8af etc/mtree/NetBSD.dist.tests
--- a/etc/mtree/NetBSD.dist.tests       Wed Jun 24 09:21:43 2020 +0000
+++ b/etc/mtree/NetBSD.dist.tests       Wed Jun 24 09:32:41 2020 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: NetBSD.dist.tests,v 1.163 2020/06/24 09:21:43 jruoho Exp $
+#      $NetBSD: NetBSD.dist.tests,v 1.164 2020/06/24 09:32:41 jruoho Exp $
 
 ./usr/libdata/debug/usr/tests
 ./usr/libdata/debug/usr/tests/atf
@@ -441,6 +441,7 @@
 ./usr/tests/usr.bin/xlint
 ./usr/tests/usr.bin/xlint/lint1
 ./usr/tests/usr.sbin
+./usr/tests/usr.sbin/cpuctl
 ./usr/tests/usr.sbin/mtree
 ./usr/tests/usr.sbin/tcpdump
 ./usr/tests/usr.sbin/traceroute
diff -r bbea3124d1c3 -r 65bdfc9ec8af tests/usr.sbin/cpuctl/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.sbin/cpuctl/Makefile    Wed Jun 24 09:32:41 2020 +0000
@@ -0,0 +1,8 @@
+# $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>
diff -r bbea3124d1c3 -r 65bdfc9ec8af tests/usr.sbin/cpuctl/t_cpuctl.sh
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.sbin/cpuctl/t_cpuctl.sh Wed Jun 24 09:32:41 2020 +0000
@@ -0,0 +1,240 @@
+# $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
+}



Home | Main Index | Thread Index | Old Index