xref: /freebsd/usr.bin/renice/tests/renice_test.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#
2# Copyright (c) 2022-2023 Klara, Inc.
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7# Name of user to use for -u tests when running as root.  Beware that
8# processes owned by that user will be affected by the test.
9TEST_USER=nobody
10
11_renice() {
12	atf_check -o empty -e ignore -s exit:0 renice "$@"
13}
14
15atf_check_nice_value() {
16	local pid=$1
17	local expected=$2
18	local actual="$(ps -o nice= -p $pid)"
19	atf_check test "$actual" -eq "$expected"
20}
21
22atf_test_case renice_abs_pid
23renice_abs_pid_head() {
24	atf_set "descr" "Set a process's nice number to an absolute value"
25}
26renice_abs_pid_body() {
27	local pid nice incr
28	sleep 60 &
29	pid=$!
30	nice="$(ps -o nice= -p $pid)"
31	incr=3
32	_renice $((nice+incr)) $pid
33	atf_check_nice_value $pid $((nice+incr))
34	kill $pid
35}
36
37atf_test_case renice_rel_pid
38renice_rel_pid_head() {
39	atf_set "descr" "Change a process's nice number by a relative value"
40}
41renice_rel_pid_body() {
42	local pid nice incr
43	sleep 60 &
44	pid=$!
45	nice="$(ps -o nice= -p $pid)"
46	incr=3
47	_renice -n $incr $pid
48	_renice -p -n $incr $pid
49	_renice -n $incr -p $pid
50	atf_check_nice_value $pid $((nice+incr+incr+incr))
51	kill $pid
52}
53
54atf_test_case renice_abs_pgid
55renice_abs_pgid_head() {
56	atf_set "descr" "Set a process group's nice number to an absolute value"
57}
58renice_abs_pgid_body() {
59	local pid pgid nice incr
60	# make sure target runs in a different pgrp than ours
61	pid="$(sh -mc "sleep 60 >/dev/null & echo \$!")"
62	pgid="$(ps -o pgid= -p $pid)"
63	nice="$(ps -o nice= -p $pid)"
64	incr=3
65	_renice $((nice+incr)) -g $pgid
66	atf_check_nice_value $pid $((nice+incr))
67	kill $pid
68}
69
70atf_test_case renice_rel_pgid
71renice_rel_pgid_head() {
72	atf_set "descr" "Change a process group's nice number by a relative value"
73}
74renice_rel_pgid_body() {
75	local pid pgid nice incr
76	# make sure target runs in a different pgrp than ours
77	pid="$(sh -mc "sleep 60 >/dev/null & echo \$!")"
78	pgid="$(ps -o pgid= -p $pid)"
79	nice="$(ps -o nice= -p $pid)"
80	incr=3
81	_renice -g -n $incr $pgid
82	_renice -n $incr -g $pgid
83	atf_check_nice_value $pid $((nice+incr+incr))
84	kill $pid
85}
86
87atf_test_case renice_abs_user
88renice_abs_user_head() {
89	atf_set "descr" "Set a user's processes' nice numbers to an absolute value"
90	atf_set "require.user" "root"
91}
92renice_abs_user_body() {
93	local user pid nice incr
94	pid="$(su -m $TEST_USER -c "/bin/sh -c 'sleep 60 >/dev/null & echo \$!'")"
95	nice="$(ps -o nice= -p $pid)"
96	incr=3
97	_renice $((nice+incr)) -u $TEST_USER
98	atf_check_nice_value $pid $((nice+incr))
99	kill $pid
100}
101
102atf_test_case renice_rel_user
103renice_rel_user_head() {
104	atf_set "descr" "Change a user's processes' nice numbers by a relative value"
105	atf_set "require.user" "root"
106}
107renice_rel_user_body() {
108	local user pid nice incr
109	pid="$(su -m $TEST_USER -c "/bin/sh -c 'sleep 60 >/dev/null & echo \$!'")"
110	nice="$(ps -o nice= -p $pid)"
111	incr=3
112	_renice -u -n $incr $TEST_USER
113	_renice -n $incr -u $TEST_USER
114	atf_check_nice_value $pid $((nice+incr+incr))
115	kill $pid
116}
117
118atf_test_case renice_delim
119renice_delim_head() {
120	atf_set "descr" "Test various delimiter positions"
121}
122renice_delim_body() {
123	local pid nice incr
124	sleep 60 &
125	pid=$!
126	nice="$(ps -o nice= -p $pid)"
127	incr=0
128	# without -p
129	: $((incr=incr+1))
130	_renice -- $((nice+incr)) $pid
131	atf_check_nice_value $pid $((nice+incr))
132	: $((incr=incr+1))
133	_renice $((nice+incr)) -- $pid
134	atf_check_nice_value $pid $((nice+incr))
135	: $((incr=incr+1))
136	_renice $((nice+incr)) $pid --
137	atf_check_nice_value $pid $((nice+incr))
138	# with -p
139	: $((incr=incr+1))
140	_renice -p -- $((nice+incr)) $pid
141	atf_check_nice_value $pid $((nice+incr))
142	: $((incr=incr+1))
143	_renice -p $((nice+incr)) -- $pid
144	atf_check_nice_value $pid $((nice+incr))
145	: $((incr=incr+1))
146	_renice -p $((nice+incr)) $pid --
147	atf_check_nice_value $pid $((nice+incr))
148	: $((incr=incr+1))
149	_renice $((nice+incr)) -p -- $pid
150	atf_check_nice_value $pid $((nice+incr))
151	: $((incr=incr+1))
152	_renice $((nice+incr)) -p $pid --
153	atf_check_nice_value $pid $((nice+incr))
154	kill $pid
155}
156
157atf_test_case renice_incr_noarg
158renice_incr_noarg_head() {
159	atf_set "descr" "Do not segfault if -n is given without an argument"
160}
161renice_incr_noarg_body() {
162	atf_check -o empty -e ignore -s exit:1 renice -n
163}
164
165atf_init_test_cases() {
166	atf_add_test_case renice_abs_pid
167	atf_add_test_case renice_rel_pid
168	atf_add_test_case renice_abs_pgid
169	atf_add_test_case renice_rel_pgid
170	atf_add_test_case renice_abs_user
171	atf_add_test_case renice_rel_user
172	atf_add_test_case renice_delim
173	atf_add_test_case renice_incr_noarg
174}
175