From f0990e3238faadf402c7262dfb90987d1eee192d Mon Sep 17 00:00:00 2001 From: ngie Date: Mon, 17 Jul 2017 20:57:38 +0000 Subject: [PATCH] MFC r318325: Start writing up some basic feature tests for procstat These tests query a running process for information related to the -b, -c, -e, and -f flags; the -f testcase is largely stubbed out, pending additional work to determine a good, deterministic descriptor. Core file test support is coming soon--it requires a bit more effort due to the fact that: - coredumps can be disabled (kern.coredump=0). - corefiles can be put in different directories than the current directory, or be named something other than `.core` (`kern.corefile`). git-svn-id: svn://svn.freebsd.org/base/stable/10@321084 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- etc/mtree/BSD.tests.dist | 2 + usr.bin/procstat/Makefile | 6 + usr.bin/procstat/tests/Makefile | 9 ++ usr.bin/procstat/tests/procstat_test.sh | 140 ++++++++++++++++++++++++ usr.bin/procstat/tests/while1.c | 40 +++++++ 5 files changed, 197 insertions(+) create mode 100644 usr.bin/procstat/tests/Makefile create mode 100755 usr.bin/procstat/tests/procstat_test.sh create mode 100644 usr.bin/procstat/tests/while1.c diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index 9e7493326..d56551042 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -616,6 +616,8 @@ .. printf .. + procstat + .. sed regress.multitest.out .. diff --git a/usr.bin/procstat/Makefile b/usr.bin/procstat/Makefile index dc6eba9ba..a881921fe 100644 --- a/usr.bin/procstat/Makefile +++ b/usr.bin/procstat/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include + PROG= procstat MAN= procstat.1 SRCS= procstat.c \ @@ -20,4 +22,8 @@ SRCS= procstat.c \ LDADD+= -lutil -lprocstat -lkvm DPADD+= ${LIBUTIL} ${LIBPROCSTAT} ${LIBKVM} +.if ${MK_TESTS} != "no" +SUBDIR+= tests +.endif + .include diff --git a/usr.bin/procstat/tests/Makefile b/usr.bin/procstat/tests/Makefile new file mode 100644 index 000000000..b8857426e --- /dev/null +++ b/usr.bin/procstat/tests/Makefile @@ -0,0 +1,9 @@ +# $FreeBSD$ + +ATF_TESTS_SH+= procstat_test + +PROGS+= while1 + +BINDIR= ${TESTSDIR} + +.include diff --git a/usr.bin/procstat/tests/procstat_test.sh b/usr.bin/procstat/tests/procstat_test.sh new file mode 100755 index 000000000..a939f4f26 --- /dev/null +++ b/usr.bin/procstat/tests/procstat_test.sh @@ -0,0 +1,140 @@ +# +# Copyright (c) 2017 Ngie Cooper +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# $FreeBSD$ +# + +MAX_TRIES=20 +PROG_PID= +PROG_PATH=$(atf_get_srcdir)/while1 + +SP='[[:space:]]' + +start_program() +{ + echo "Starting program in background" + PROG_COMM=while1 + PROG_PATH=$(atf_get_srcdir)/$PROG_COMM + + $PROG_PATH $* & + PROG_PID=$! + try=0 + while [ $try -lt $MAX_TRIES ] && ! kill -0 $PROG_PID; do + sleep 0.5 + : $(( try += 1 )) + done + if [ $try -ge $MAX_TRIES ]; then + atf_fail "Polled for program start $MAX_TRIES tries and failed" + fi +} + +atf_test_case binary_info +binary_info_head() +{ + atf_set "descr" "Checks -b support" +} +binary_info_body() +{ + start_program bogus-arg + + line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP*" + header_re=$(printf "$line_format" "PID" "COMM" "OSREL" "PATH") + line_re=$(printf "$line_format" $PROG_PID $PROG_COMM "[[:digit:]]+" "$PROG_PATH") + + atf_check -o save:procstat.out procstat -b $PROG_PID + + atf_check -o match:"$header_re" head -n 1 procstat.out + atf_check -o match:"$line_re" tail -n 1 procstat.out +} + +atf_test_case command_line_arguments +command_line_arguments_head() +{ + atf_set "descr" "Checks -c support" +} +command_line_arguments_body() +{ + arguments="my arguments" + + start_program $arguments + + line_format="$SP*%s$SP+%s$SP+%s$SP*" + header_re=$(printf "$line_format" "PID" "COMM" "ARGS") + line_re=$(printf "$line_format" $PROG_PID "$PROG_COMM" "$PROG_PATH $arguments") + + atf_check -o save:procstat.out procstat -c $PROG_PID + atf_check -o match:"$header_re" head -n 1 procstat.out + atf_check -o match:"$line_re" tail -n 1 procstat.out +} + +atf_test_case environment +environment_head() +{ + atf_set "descr" "Checks -e support" +} +environment_body() +{ + var="MY_VARIABLE=foo" + eval "export $var" + + start_program my arguments + + line_format="$SP*%s$SP+%s$SP+%s$SP*" + header_re=$(printf "$line_format" "PID" "COMM" "ENVIRONMENT") + line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".*$var.*") + + atf_check -o save:procstat.out procstat -e $PROG_PID + + atf_check -o match:"$header_re" head -n 1 procstat.out + atf_check -o match:"$line_re" tail -n 1 procstat.out +} + +atf_test_case file_descriptor +file_descriptor_head() +{ + atf_set "descr" "Checks -f support" +} +file_descriptor_body() +{ + start_program my arguments + + line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP%s$SP*" + header_re=$(printf "$line_format" "PID" "COMM" "FD" "T" "V" "FLAGS" "REF" "OFFSET" "PRO" "NAME") + # XXX: write a more sensible feature test + line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".+" ".+" ".+" ".+" ".+" ".+" ".+" ".+") + + atf_check -o save:procstat.out procstat -f $PROG_PID + + atf_check -o match:"$header_re" head -n 1 procstat.out + atf_check -o match:"$line_re" awk 'NR > 1' procstat.out +} + +atf_init_test_cases() +{ + atf_add_test_case binary_info + atf_add_test_case command_line_arguments + atf_add_test_case environment + atf_add_test_case file_descriptor +} diff --git a/usr.bin/procstat/tests/while1.c b/usr.bin/procstat/tests/while1.c new file mode 100644 index 000000000..6f706026e --- /dev/null +++ b/usr.bin/procstat/tests/while1.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 Ngie Cooper + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +__RCSID("$FreeBSD$"); + +#include +#include + +int +main(void) +{ + + for (;;) + usleep(100); + exit(1); +} -- 2.42.0