xref: /freebsd/usr.bin/procstat/tests/procstat_test.sh (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1#
2# Copyright (c) 2017 Enji Cooper <ngie@FreeBSD.org>
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
17# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23# POSSIBILITY OF SUCH DAMAGE.
24#
25# $FreeBSD$
26#
27
28PROG_PID=
29PROG_PATH=$(atf_get_srcdir)/while1
30
31SP='[[:space:]]'
32
33start_program()
34{
35	echo "Starting program in background"
36	PROG_COMM=while1
37	PROG_PATH=$(atf_get_srcdir)/$PROG_COMM
38
39	mkfifo wait_for_start || atf_fail "mkfifo"
40	$PROG_PATH $* >wait_for_start &
41	PROG_PID=$!
42	if ! read dummy <wait_for_start; then
43		atf_fail "Program did not start properly"
44	fi
45	rm wait_for_start
46}
47
48atf_test_case binary_info
49binary_info_head()
50{
51	atf_set "descr" "Checks -b support"
52}
53binary_info_body()
54{
55	start_program bogus-arg
56
57	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP*"
58	header_re=$(printf "$line_format" "PID" "COMM" "OSREL" "PATH")
59	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM "[[:digit:]]+" "$PROG_PATH")
60
61	atf_check -o save:procstat.out procstat binary $PROG_PID
62	atf_check -o match:"$header_re" head -n 1 procstat.out
63	atf_check -o match:"$line_re" tail -n 1 procstat.out
64
65	atf_check -o save:procstat.out procstat -b $PROG_PID
66	atf_check -o match:"$header_re" head -n 1 procstat.out
67	atf_check -o match:"$line_re" tail -n 1 procstat.out
68}
69
70atf_test_case command_line_arguments
71command_line_arguments_head()
72{
73	atf_set "descr" "Checks -c support"
74}
75command_line_arguments_body()
76{
77	arguments="my arguments"
78
79	start_program $arguments
80
81	line_format="$SP*%s$SP+%s$SP+%s$SP*"
82	header_re=$(printf "$line_format" "PID" "COMM" "ARGS")
83	line_re=$(printf "$line_format" $PROG_PID "$PROG_COMM" "$PROG_PATH $arguments")
84
85	atf_check -o save:procstat.out procstat arguments $PROG_PID
86	atf_check -o match:"$header_re" head -n 1 procstat.out
87	atf_check -o match:"$line_re" tail -n 1 procstat.out
88
89	atf_check -o save:procstat.out procstat -c $PROG_PID
90	atf_check -o match:"$header_re" head -n 1 procstat.out
91	atf_check -o match:"$line_re" tail -n 1 procstat.out
92}
93
94atf_test_case environment
95environment_head()
96{
97	atf_set "descr" "Checks -e support"
98}
99environment_body()
100{
101	var="MY_VARIABLE=foo"
102	eval "export $var"
103
104	start_program my arguments
105
106	line_format="$SP*%s$SP+%s$SP+%s$SP*"
107	header_re=$(printf "$line_format" "PID" "COMM" "ENVIRONMENT")
108	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".*$var.*")
109
110	atf_check -o save:procstat.out procstat environment $PROG_PID
111	atf_check -o match:"$header_re" head -n 1 procstat.out
112	atf_check -o match:"$line_re" tail -n 1 procstat.out
113
114	atf_check -o save:procstat.out procstat -e $PROG_PID
115	atf_check -o match:"$header_re" head -n 1 procstat.out
116	atf_check -o match:"$line_re" tail -n 1 procstat.out
117}
118
119atf_test_case file_descriptor
120file_descriptor_head()
121{
122	atf_set "descr" "Checks -f support"
123}
124file_descriptor_body()
125{
126	start_program my arguments
127
128	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP%s$SP*"
129	header_re=$(printf "$line_format" "PID" "COMM" "FD" "T" "V" "FLAGS" "REF" "OFFSET" "PRO" "NAME")
130	# XXX: write a more sensible feature test
131	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".+" ".+" ".+" ".+" ".+" ".+" ".+" ".+")
132
133	atf_check -o save:procstat.out procstat files $PROG_PID
134	atf_check -o match:"$header_re" head -n 1 procstat.out
135	atf_check -o match:"$line_re" awk 'NR > 1' procstat.out
136
137	atf_check -o save:procstat.out procstat -f $PROG_PID
138	atf_check -o match:"$header_re" head -n 1 procstat.out
139	atf_check -o match:"$line_re" awk 'NR > 1' procstat.out
140}
141
142atf_test_case kernel_stacks
143kernel_stacks_head()
144{
145	atf_set "descr" "Captures kernel stacks for all visible threads"
146}
147kernel_stacks_body()
148{
149	atf_check -o save:procstat.out procstat -a kstack
150	atf_check -o not-empty awk '{if ($3 == "procstat") print}' procstat.out
151
152	atf_check -o save:procstat.out procstat -kka
153	atf_check -o not-empty awk '{if ($3 == "procstat") print}' procstat.out
154}
155
156atf_init_test_cases()
157{
158	atf_add_test_case binary_info
159	atf_add_test_case command_line_arguments
160	atf_add_test_case environment
161	atf_add_test_case file_descriptor
162	atf_add_test_case kernel_stacks
163}
164