diff options
author | ngie <ngie@FreeBSD.org> | 2017-07-17 20:57:38 +0000 |
---|---|---|
committer | ngie <ngie@FreeBSD.org> | 2017-07-17 20:57:38 +0000 |
commit | 4854558895ec60f2ca616ab0139d666cf89b6314 (patch) | |
tree | 7334220ad236a4a0f10b1e5cd0291d12c99cb6c3 | |
parent | 45815ddd427314488bc69a326e9368089812ba1b (diff) | |
download | FreeBSD-src-4854558895ec60f2ca616ab0139d666cf89b6314.zip FreeBSD-src-4854558895ec60f2ca616ab0139d666cf89b6314.tar.gz |
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 `<prog>.core`
(`kern.corefile`).
-rw-r--r-- | etc/mtree/BSD.tests.dist | 2 | ||||
-rw-r--r-- | usr.bin/procstat/Makefile | 6 | ||||
-rw-r--r-- | usr.bin/procstat/tests/Makefile | 9 | ||||
-rwxr-xr-x | usr.bin/procstat/tests/procstat_test.sh | 140 | ||||
-rw-r--r-- | usr.bin/procstat/tests/while1.c | 40 |
5 files changed, 197 insertions, 0 deletions
diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index 9e74933..d565510 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 dc6eba9..a881921 100644 --- a/usr.bin/procstat/Makefile +++ b/usr.bin/procstat/Makefile @@ -1,5 +1,7 @@ # $FreeBSD$ +.include <bsd.own.mk> + 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 <bsd.prog.mk> diff --git a/usr.bin/procstat/tests/Makefile b/usr.bin/procstat/tests/Makefile new file mode 100644 index 0000000..b885742 --- /dev/null +++ b/usr.bin/procstat/tests/Makefile @@ -0,0 +1,9 @@ +# $FreeBSD$ + +ATF_TESTS_SH+= procstat_test + +PROGS+= while1 + +BINDIR= ${TESTSDIR} + +.include <bsd.test.mk> diff --git a/usr.bin/procstat/tests/procstat_test.sh b/usr.bin/procstat/tests/procstat_test.sh new file mode 100755 index 0000000..a939f4f --- /dev/null +++ b/usr.bin/procstat/tests/procstat_test.sh @@ -0,0 +1,140 @@ +# +# Copyright (c) 2017 Ngie Cooper <ngie@FreeBSD.org> +# 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 0000000..6f70602 --- /dev/null +++ b/usr.bin/procstat/tests/while1.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 Ngie Cooper <ngie@FreeBSD.org> + * 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 <sys/cdefs.h> +__RCSID("$FreeBSD$"); + +#include <stdlib.h> +#include <unistd.h> + +int +main(void) +{ + + for (;;) + usleep(100); + exit(1); +} |