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