procstat_test.sh revision 318325
1318325Sngie#
2318325Sngie# Copyright (c) 2017 Ngie Cooper <ngie@FreeBSD.org>
3318325Sngie# All rights reserved.
4318325Sngie#
5318325Sngie# Redistribution and use in source and binary forms, with or without
6318325Sngie# modification, are permitted provided that the following conditions
7318325Sngie# are met:
8318325Sngie# 1. Redistributions of source code must retain the above copyright
9318325Sngie#    notice, this list of conditions and the following disclaimer.
10318325Sngie# 2. Redistributions in binary form must reproduce the above copyright
11318325Sngie#    notice, this list of conditions and the following disclaimer in the
12318325Sngie#    documentation and/or other materials provided with the distribution.
13318325Sngie#
14318325Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15318325Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16318325Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17318325Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18318325Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19318325Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20318325Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21318325Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22318325Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23318325Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24318325Sngie# POSSIBILITY OF SUCH DAMAGE.
25318325Sngie#
26318325Sngie# $FreeBSD: head/usr.bin/procstat/tests/procstat_test.sh 318325 2017-05-15 22:52:25Z ngie $
27318325Sngie#
28318325Sngie
29318325SngieMAX_TRIES=20
30318325SngiePROG_PID=
31318325SngiePROG_PATH=$(atf_get_srcdir)/while1
32318325Sngie
33318325SngieSP='[[:space:]]'
34318325Sngie
35318325Sngiestart_program()
36318325Sngie{
37318325Sngie	echo "Starting program in background"
38318325Sngie	PROG_COMM=while1
39318325Sngie	PROG_PATH=$(atf_get_srcdir)/$PROG_COMM
40318325Sngie
41318325Sngie	$PROG_PATH $* &
42318325Sngie	PROG_PID=$!
43318325Sngie	try=0
44318325Sngie	while [ $try -lt $MAX_TRIES ] && ! kill -0 $PROG_PID; do
45318325Sngie		sleep 0.5
46318325Sngie		: $(( try += 1 ))
47318325Sngie	done
48318325Sngie	if [ $try -ge $MAX_TRIES ]; then
49318325Sngie		atf_fail "Polled for program start $MAX_TRIES tries and failed"
50318325Sngie	fi
51318325Sngie}
52318325Sngie
53318325Sngieatf_test_case binary_info
54318325Sngiebinary_info_head()
55318325Sngie{
56318325Sngie	atf_set "descr" "Checks -b support"
57318325Sngie}
58318325Sngiebinary_info_body()
59318325Sngie{
60318325Sngie	start_program bogus-arg
61318325Sngie
62318325Sngie	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP*"
63318325Sngie	header_re=$(printf "$line_format" "PID" "COMM" "OSREL" "PATH")
64318325Sngie	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM "[[:digit:]]+" "$PROG_PATH")
65318325Sngie
66318325Sngie	atf_check -o save:procstat.out procstat -b $PROG_PID
67318325Sngie
68318325Sngie	atf_check -o match:"$header_re" head -n 1 procstat.out
69318325Sngie	atf_check -o match:"$line_re" tail -n 1 procstat.out
70318325Sngie}
71318325Sngie
72318325Sngieatf_test_case command_line_arguments
73318325Sngiecommand_line_arguments_head()
74318325Sngie{
75318325Sngie	atf_set "descr" "Checks -c support"
76318325Sngie}
77318325Sngiecommand_line_arguments_body()
78318325Sngie{
79318325Sngie	arguments="my arguments"
80318325Sngie
81318325Sngie	start_program $arguments
82318325Sngie
83318325Sngie	line_format="$SP*%s$SP+%s$SP+%s$SP*"
84318325Sngie	header_re=$(printf "$line_format" "PID" "COMM" "ARGS")
85318325Sngie	line_re=$(printf "$line_format" $PROG_PID "$PROG_COMM" "$PROG_PATH $arguments")
86318325Sngie
87318325Sngie	atf_check -o save:procstat.out procstat -c $PROG_PID
88318325Sngie	atf_check -o match:"$header_re" head -n 1 procstat.out
89318325Sngie	atf_check -o match:"$line_re" tail -n 1 procstat.out
90318325Sngie}
91318325Sngie
92318325Sngieatf_test_case environment
93318325Sngieenvironment_head()
94318325Sngie{
95318325Sngie	atf_set "descr" "Checks -e support"
96318325Sngie}
97318325Sngieenvironment_body()
98318325Sngie{
99318325Sngie	var="MY_VARIABLE=foo"
100318325Sngie	eval "export $var"
101318325Sngie
102318325Sngie	start_program my arguments
103318325Sngie
104318325Sngie	line_format="$SP*%s$SP+%s$SP+%s$SP*"
105318325Sngie	header_re=$(printf "$line_format" "PID" "COMM" "ENVIRONMENT")
106318325Sngie	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".*$var.*")
107318325Sngie
108318325Sngie	atf_check -o save:procstat.out procstat -e $PROG_PID
109318325Sngie
110318325Sngie	atf_check -o match:"$header_re" head -n 1 procstat.out
111318325Sngie	atf_check -o match:"$line_re" tail -n 1 procstat.out
112318325Sngie}
113318325Sngie
114318325Sngieatf_test_case file_descriptor
115318325Sngiefile_descriptor_head()
116318325Sngie{
117318325Sngie	atf_set "descr" "Checks -f support"
118318325Sngie}
119318325Sngiefile_descriptor_body()
120318325Sngie{
121318325Sngie	start_program my arguments
122318325Sngie
123318325Sngie	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP%s$SP*"
124318325Sngie	header_re=$(printf "$line_format" "PID" "COMM" "FD" "T" "V" "FLAGS" "REF" "OFFSET" "PRO" "NAME")
125318325Sngie	# XXX: write a more sensible feature test
126318325Sngie	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".+" ".+" ".+" ".+" ".+" ".+" ".+" ".+")
127318325Sngie
128318325Sngie	atf_check -o save:procstat.out procstat -f $PROG_PID
129318325Sngie
130318325Sngie	atf_check -o match:"$header_re" head -n 1 procstat.out
131318325Sngie	atf_check -o match:"$line_re" awk 'NR > 1' procstat.out
132318325Sngie}
133318325Sngie
134318325Sngieatf_init_test_cases()
135318325Sngie{
136318325Sngie	atf_add_test_case binary_info
137318325Sngie	atf_add_test_case command_line_arguments
138318325Sngie	atf_add_test_case environment
139318325Sngie	atf_add_test_case file_descriptor
140318325Sngie}
141