xref: /freebsd/usr.bin/renice/tests/renice_test.sh (revision afdce0837014fb7a41fced959c9457f89e878052)
17ccdad90SDag-Erling Smørgrav#
2d59dd30cSDag-Erling Smørgrav# Copyright (c) 2022-2023 Klara, Inc.
37ccdad90SDag-Erling Smørgrav#
4d59dd30cSDag-Erling Smørgrav# SPDX-License-Identifier: BSD-2-Clause
57ccdad90SDag-Erling Smørgrav#
67ccdad90SDag-Erling Smørgrav
77ccdad90SDag-Erling Smørgrav# Name of user to use for -u tests when running as root.  Beware that
87ccdad90SDag-Erling Smørgrav# processes owned by that user will be affected by the test.
97ccdad90SDag-Erling SmørgravTEST_USER=nobody
107ccdad90SDag-Erling Smørgrav
117ccdad90SDag-Erling Smørgrav_renice() {
127ccdad90SDag-Erling Smørgrav	atf_check -o empty -e ignore -s exit:0 renice "$@"
137ccdad90SDag-Erling Smørgrav}
147ccdad90SDag-Erling Smørgrav
151bc6dcc2SDag-Erling Smørgravatf_check_nice_value() {
161bc6dcc2SDag-Erling Smørgrav	local pid=$1
17*afdce083SDag-Erling Smørgrav	local expected=$2
18*afdce083SDag-Erling Smørgrav	local actual="$(ps -o nice= -p $pid)"
19*afdce083SDag-Erling Smørgrav	atf_check test "$actual" -eq "$expected"
201bc6dcc2SDag-Erling Smørgrav}
211bc6dcc2SDag-Erling Smørgrav
227ccdad90SDag-Erling Smørgravatf_test_case renice_abs_pid
23d59dd30cSDag-Erling Smørgravrenice_abs_pid_head() {
24d59dd30cSDag-Erling Smørgrav	atf_set "descr" "Set a process's nice number to an absolute value"
25d59dd30cSDag-Erling Smørgrav}
267ccdad90SDag-Erling Smørgravrenice_abs_pid_body() {
277ccdad90SDag-Erling Smørgrav	local pid nice incr
287ccdad90SDag-Erling Smørgrav	sleep 60 &
297ccdad90SDag-Erling Smørgrav	pid=$!
307ccdad90SDag-Erling Smørgrav	nice="$(ps -o nice= -p $pid)"
317ccdad90SDag-Erling Smørgrav	incr=3
327ccdad90SDag-Erling Smørgrav	_renice $((nice+incr)) $pid
331bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
347ccdad90SDag-Erling Smørgrav	kill $pid
357ccdad90SDag-Erling Smørgrav}
367ccdad90SDag-Erling Smørgrav
377ccdad90SDag-Erling Smørgravatf_test_case renice_rel_pid
38d59dd30cSDag-Erling Smørgravrenice_rel_pid_head() {
39d59dd30cSDag-Erling Smørgrav	atf_set "descr" "Change a process's nice number by a relative value"
40d59dd30cSDag-Erling Smørgrav}
417ccdad90SDag-Erling Smørgravrenice_rel_pid_body() {
427ccdad90SDag-Erling Smørgrav	local pid nice incr
437ccdad90SDag-Erling Smørgrav	sleep 60 &
447ccdad90SDag-Erling Smørgrav	pid=$!
457ccdad90SDag-Erling Smørgrav	nice="$(ps -o nice= -p $pid)"
467ccdad90SDag-Erling Smørgrav	incr=3
477ccdad90SDag-Erling Smørgrav	_renice -n $incr $pid
487ccdad90SDag-Erling Smørgrav	_renice -p -n $incr $pid
497ccdad90SDag-Erling Smørgrav	_renice -n $incr -p $pid
501bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr+incr+incr))
517ccdad90SDag-Erling Smørgrav	kill $pid
527ccdad90SDag-Erling Smørgrav}
537ccdad90SDag-Erling Smørgrav
547ccdad90SDag-Erling Smørgravatf_test_case renice_abs_pgid
55d59dd30cSDag-Erling Smørgravrenice_abs_pgid_head() {
56d59dd30cSDag-Erling Smørgrav	atf_set "descr" "Set a process group's nice number to an absolute value"
57d59dd30cSDag-Erling Smørgrav}
587ccdad90SDag-Erling Smørgravrenice_abs_pgid_body() {
597ccdad90SDag-Erling Smørgrav	local pid pgid nice incr
607ccdad90SDag-Erling Smørgrav	# make sure target runs in a different pgrp than ours
61*afdce083SDag-Erling Smørgrav	pid="$(sh -mc "sleep 60 >/dev/null & echo \$!")"
627ccdad90SDag-Erling Smørgrav	pgid="$(ps -o pgid= -p $pid)"
637ccdad90SDag-Erling Smørgrav	nice="$(ps -o nice= -p $pid)"
647ccdad90SDag-Erling Smørgrav	incr=3
657ccdad90SDag-Erling Smørgrav	_renice $((nice+incr)) -g $pgid
661bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
677ccdad90SDag-Erling Smørgrav	kill $pid
687ccdad90SDag-Erling Smørgrav}
697ccdad90SDag-Erling Smørgrav
707ccdad90SDag-Erling Smørgravatf_test_case renice_rel_pgid
71d59dd30cSDag-Erling Smørgravrenice_rel_pgid_head() {
72d59dd30cSDag-Erling Smørgrav	atf_set "descr" "Change a process group's nice number by a relative value"
73d59dd30cSDag-Erling Smørgrav}
747ccdad90SDag-Erling Smørgravrenice_rel_pgid_body() {
757ccdad90SDag-Erling Smørgrav	local pid pgid nice incr
767ccdad90SDag-Erling Smørgrav	# make sure target runs in a different pgrp than ours
77*afdce083SDag-Erling Smørgrav	pid="$(sh -mc "sleep 60 >/dev/null & echo \$!")"
787ccdad90SDag-Erling Smørgrav	pgid="$(ps -o pgid= -p $pid)"
797ccdad90SDag-Erling Smørgrav	nice="$(ps -o nice= -p $pid)"
807ccdad90SDag-Erling Smørgrav	incr=3
817ccdad90SDag-Erling Smørgrav	_renice -g -n $incr $pgid
827ccdad90SDag-Erling Smørgrav	_renice -n $incr -g $pgid
831bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr+incr))
847ccdad90SDag-Erling Smørgrav	kill $pid
857ccdad90SDag-Erling Smørgrav}
867ccdad90SDag-Erling Smørgrav
877ccdad90SDag-Erling Smørgravatf_test_case renice_abs_user
887ccdad90SDag-Erling Smørgravrenice_abs_user_head() {
89d59dd30cSDag-Erling Smørgrav	atf_set "descr" "Set a user's processes' nice numbers to an absolute value"
907ccdad90SDag-Erling Smørgrav	atf_set "require.user" "root"
917ccdad90SDag-Erling Smørgrav}
927ccdad90SDag-Erling Smørgravrenice_abs_user_body() {
937ccdad90SDag-Erling Smørgrav	local user pid nice incr
94*afdce083SDag-Erling Smørgrav	pid="$(su -m $TEST_USER -c "/bin/sh -c 'sleep 60 >/dev/null & echo \$!'")"
957ccdad90SDag-Erling Smørgrav	nice="$(ps -o nice= -p $pid)"
967ccdad90SDag-Erling Smørgrav	incr=3
977ccdad90SDag-Erling Smørgrav	_renice $((nice+incr)) -u $TEST_USER
981bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
997ccdad90SDag-Erling Smørgrav	kill $pid
1007ccdad90SDag-Erling Smørgrav}
1017ccdad90SDag-Erling Smørgrav
1027ccdad90SDag-Erling Smørgravatf_test_case renice_rel_user
1037ccdad90SDag-Erling Smørgravrenice_rel_user_head() {
104d59dd30cSDag-Erling Smørgrav	atf_set "descr" "Change a user's processes' nice numbers by a relative value"
1057ccdad90SDag-Erling Smørgrav	atf_set "require.user" "root"
1067ccdad90SDag-Erling Smørgrav}
1077ccdad90SDag-Erling Smørgravrenice_rel_user_body() {
1087ccdad90SDag-Erling Smørgrav	local user pid nice incr
109*afdce083SDag-Erling Smørgrav	pid="$(su -m $TEST_USER -c "/bin/sh -c 'sleep 60 >/dev/null & echo \$!'")"
1107ccdad90SDag-Erling Smørgrav	nice="$(ps -o nice= -p $pid)"
1117ccdad90SDag-Erling Smørgrav	incr=3
1127ccdad90SDag-Erling Smørgrav	_renice -u -n $incr $TEST_USER
1137ccdad90SDag-Erling Smørgrav	_renice -n $incr -u $TEST_USER
1141bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr+incr))
1157ccdad90SDag-Erling Smørgrav	kill $pid
1167ccdad90SDag-Erling Smørgrav}
1177ccdad90SDag-Erling Smørgrav
1187ccdad90SDag-Erling Smørgravatf_test_case renice_delim
119d59dd30cSDag-Erling Smørgravrenice_delim_head() {
120d59dd30cSDag-Erling Smørgrav	atf_set "descr" "Test various delimiter positions"
121d59dd30cSDag-Erling Smørgrav}
1227ccdad90SDag-Erling Smørgravrenice_delim_body() {
1237ccdad90SDag-Erling Smørgrav	local pid nice incr
1247ccdad90SDag-Erling Smørgrav	sleep 60 &
1257ccdad90SDag-Erling Smørgrav	pid=$!
1267ccdad90SDag-Erling Smørgrav	nice="$(ps -o nice= -p $pid)"
1277ccdad90SDag-Erling Smørgrav	incr=0
1287ccdad90SDag-Erling Smørgrav	# without -p
1297ccdad90SDag-Erling Smørgrav	: $((incr=incr+1))
1307ccdad90SDag-Erling Smørgrav	_renice -- $((nice+incr)) $pid
1311bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
1327ccdad90SDag-Erling Smørgrav	: $((incr=incr+1))
1337ccdad90SDag-Erling Smørgrav	_renice $((nice+incr)) -- $pid
1341bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
1357ccdad90SDag-Erling Smørgrav	: $((incr=incr+1))
1367ccdad90SDag-Erling Smørgrav	_renice $((nice+incr)) $pid --
1371bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
1387ccdad90SDag-Erling Smørgrav	# with -p
1397ccdad90SDag-Erling Smørgrav	: $((incr=incr+1))
1407ccdad90SDag-Erling Smørgrav	_renice -p -- $((nice+incr)) $pid
1411bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
1427ccdad90SDag-Erling Smørgrav	: $((incr=incr+1))
1437ccdad90SDag-Erling Smørgrav	_renice -p $((nice+incr)) -- $pid
1441bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
1457ccdad90SDag-Erling Smørgrav	: $((incr=incr+1))
1467ccdad90SDag-Erling Smørgrav	_renice -p $((nice+incr)) $pid --
1471bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
1487ccdad90SDag-Erling Smørgrav	: $((incr=incr+1))
1497ccdad90SDag-Erling Smørgrav	_renice $((nice+incr)) -p -- $pid
1501bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
1517ccdad90SDag-Erling Smørgrav	: $((incr=incr+1))
1527ccdad90SDag-Erling Smørgrav	_renice $((nice+incr)) -p $pid --
1531bc6dcc2SDag-Erling Smørgrav	atf_check_nice_value $pid $((nice+incr))
1547ccdad90SDag-Erling Smørgrav	kill $pid
1557ccdad90SDag-Erling Smørgrav}
1567ccdad90SDag-Erling Smørgrav
157b03311caSDag-Erling Smørgravatf_test_case renice_incr_noarg
158d59dd30cSDag-Erling Smørgravrenice_incr_noarg_head() {
159d59dd30cSDag-Erling Smørgrav	atf_set "descr" "Do not segfault if -n is given without an argument"
160d59dd30cSDag-Erling Smørgrav}
161b03311caSDag-Erling Smørgravrenice_incr_noarg_body() {
162b03311caSDag-Erling Smørgrav	atf_check -o empty -e ignore -s exit:1 renice -n
163b03311caSDag-Erling Smørgrav}
164b03311caSDag-Erling Smørgrav
1657ccdad90SDag-Erling Smørgravatf_init_test_cases() {
1667ccdad90SDag-Erling Smørgrav	atf_add_test_case renice_abs_pid
1677ccdad90SDag-Erling Smørgrav	atf_add_test_case renice_rel_pid
1687ccdad90SDag-Erling Smørgrav	atf_add_test_case renice_abs_pgid
1697ccdad90SDag-Erling Smørgrav	atf_add_test_case renice_rel_pgid
1707ccdad90SDag-Erling Smørgrav	atf_add_test_case renice_abs_user
1717ccdad90SDag-Erling Smørgrav	atf_add_test_case renice_rel_user
1727ccdad90SDag-Erling Smørgrav	atf_add_test_case renice_delim
173b03311caSDag-Erling Smørgrav	atf_add_test_case renice_incr_noarg
1747ccdad90SDag-Erling Smørgrav}
175