From c7d813a93eeb447470734c9bc0c140d90a54c271 Mon Sep 17 00:00:00 2001 From: Enji Cooper Date: Mon, 15 May 2017 22:52:25 +0000 Subject: 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`). MFC after: 2 months Sponsored by: Dell EMC Isilon --- 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 +++++++++ 4 files changed, 195 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 (limited to 'usr.bin/procstat') diff --git a/usr.bin/procstat/Makefile b/usr.bin/procstat/Makefile index 7c951966e414..10b9040372ea 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 \ LIBADD+= procstat xo util sbuf +.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 000000000000..b8857426ee8d --- /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 000000000000..a939f4f26cd7 --- /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 000000000000..6f706026ee9b --- /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); +} -- cgit v1.2.3