procstat_test.sh revision 321083
1290001Sglebius#
2132451Sroberto# Copyright (c) 2017 Ngie Cooper <ngie@FreeBSD.org>
354359Sroberto# All rights reserved.
4290001Sglebius#
5290001Sglebius# Redistribution and use in source and binary forms, with or without
654359Sroberto# modification, are permitted provided that the following conditions
754359Sroberto# are met:
854359Sroberto# 1. Redistributions of source code must retain the above copyright
954359Sroberto#    notice, this list of conditions and the following disclaimer.
1054359Sroberto# 2. Redistributions in binary form must reproduce the above copyright
1154359Sroberto#    notice, this list of conditions and the following disclaimer in the
1254359Sroberto#    documentation and/or other materials provided with the distribution.
1354359Sroberto#
1454359Sroberto# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15106163Sroberto# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16106163Sroberto# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1754359Sroberto# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18290001Sglebius# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19290001Sglebius# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20290001Sglebius# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21290001Sglebius# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22290001Sglebius# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23290001Sglebius# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24290001Sglebius# POSSIBILITY OF SUCH DAMAGE.
25290001Sglebius#
26290001Sglebius# $FreeBSD: stable/11/usr.bin/procstat/tests/procstat_test.sh 318325 2017-05-15 22:52:25Z ngie $
27290001Sglebius#
28290001Sglebius
29290001SglebiusMAX_TRIES=20
30290001SglebiusPROG_PID=
31290001SglebiusPROG_PATH=$(atf_get_srcdir)/while1
32290001Sglebius
33290001SglebiusSP='[[:space:]]'
34290001Sglebius
35290001Sglebiusstart_program()
36290001Sglebius{
37290001Sglebius	echo "Starting program in background"
38290001Sglebius	PROG_COMM=while1
39290001Sglebius	PROG_PATH=$(atf_get_srcdir)/$PROG_COMM
40290001Sglebius
41290001Sglebius	$PROG_PATH $* &
42290001Sglebius	PROG_PID=$!
43290001Sglebius	try=0
44290001Sglebius	while [ $try -lt $MAX_TRIES ] && ! kill -0 $PROG_PID; do
45290001Sglebius		sleep 0.5
46290001Sglebius		: $(( try += 1 ))
47290001Sglebius	done
48290001Sglebius	if [ $try -ge $MAX_TRIES ]; then
49290001Sglebius		atf_fail "Polled for program start $MAX_TRIES tries and failed"
50290001Sglebius	fi
51290001Sglebius}
52290001Sglebius
53290001Sglebiusatf_test_case binary_info
54290001Sglebiusbinary_info_head()
55290001Sglebius{
56290001Sglebius	atf_set "descr" "Checks -b support"
57290001Sglebius}
58290001Sglebiusbinary_info_body()
59290001Sglebius{
60290001Sglebius	start_program bogus-arg
61290001Sglebius
62290001Sglebius	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP*"
63290001Sglebius	header_re=$(printf "$line_format" "PID" "COMM" "OSREL" "PATH")
64290001Sglebius	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM "[[:digit:]]+" "$PROG_PATH")
65290001Sglebius
66290001Sglebius	atf_check -o save:procstat.out procstat -b $PROG_PID
67290001Sglebius
68290001Sglebius	atf_check -o match:"$header_re" head -n 1 procstat.out
69290001Sglebius	atf_check -o match:"$line_re" tail -n 1 procstat.out
70290001Sglebius}
71290001Sglebius
72290001Sglebiusatf_test_case command_line_arguments
7354359Srobertocommand_line_arguments_head()
74200576Sroberto{
7554359Sroberto	atf_set "descr" "Checks -c support"
76200576Sroberto}
77132451Srobertocommand_line_arguments_body()
78132451Sroberto{
79132451Sroberto	arguments="my arguments"
80132451Sroberto
8182498Sroberto	start_program $arguments
82132451Sroberto
8354359Sroberto	line_format="$SP*%s$SP+%s$SP+%s$SP*"
8454359Sroberto	header_re=$(printf "$line_format" "PID" "COMM" "ARGS")
8554359Sroberto	line_re=$(printf "$line_format" $PROG_PID "$PROG_COMM" "$PROG_PATH $arguments")
8654359Sroberto
8754359Sroberto	atf_check -o save:procstat.out procstat -c $PROG_PID
8854359Sroberto	atf_check -o match:"$header_re" head -n 1 procstat.out
8954359Sroberto	atf_check -o match:"$line_re" tail -n 1 procstat.out
9054359Sroberto}
91290001Sglebius
92182007Srobertoatf_test_case environment
93182007Srobertoenvironment_head()
94290001Sglebius{
95290001Sglebius	atf_set "descr" "Checks -e support"
96290001Sglebius}
97290001Sglebiusenvironment_body()
98290001Sglebius{
99290001Sglebius	var="MY_VARIABLE=foo"
100290001Sglebius	eval "export $var"
101290001Sglebius
102290001Sglebius	start_program my arguments
103290001Sglebius
104290001Sglebius	line_format="$SP*%s$SP+%s$SP+%s$SP*"
105290001Sglebius	header_re=$(printf "$line_format" "PID" "COMM" "ENVIRONMENT")
106290001Sglebius	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".*$var.*")
107290001Sglebius
108290001Sglebius	atf_check -o save:procstat.out procstat -e $PROG_PID
109290001Sglebius
110290001Sglebius	atf_check -o match:"$header_re" head -n 1 procstat.out
111290001Sglebius	atf_check -o match:"$line_re" tail -n 1 procstat.out
112290001Sglebius}
113290001Sglebius
114290001Sglebiusatf_test_case file_descriptor
115290001Sglebiusfile_descriptor_head()
116290001Sglebius{
117290001Sglebius	atf_set "descr" "Checks -f support"
118290001Sglebius}
119290001Sglebiusfile_descriptor_body()
120290001Sglebius{
121290001Sglebius	start_program my arguments
122290001Sglebius
123290001Sglebius	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP%s$SP*"
124290001Sglebius	header_re=$(printf "$line_format" "PID" "COMM" "FD" "T" "V" "FLAGS" "REF" "OFFSET" "PRO" "NAME")
125290001Sglebius	# XXX: write a more sensible feature test
126290001Sglebius	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".+" ".+" ".+" ".+" ".+" ".+" ".+" ".+")
127290001Sglebius
128290001Sglebius	atf_check -o save:procstat.out procstat -f $PROG_PID
129290001Sglebius
130290001Sglebius	atf_check -o match:"$header_re" head -n 1 procstat.out
131290001Sglebius	atf_check -o match:"$line_re" awk 'NR > 1' procstat.out
132182007Sroberto}
133182007Sroberto
134290001Sglebiusatf_init_test_cases()
135182007Sroberto{
136182007Sroberto	atf_add_test_case binary_info
137182007Sroberto	atf_add_test_case command_line_arguments
138200576Sroberto	atf_add_test_case environment
139182007Sroberto	atf_add_test_case file_descriptor
140182007Sroberto}
141290001Sglebius