"Fossies" - the Fresh Open Source Software Archive

Member "cfengine-3.15.4/tests/acceptance/testall" (7 Jun 2021, 48727 Bytes) of package /linux/misc/cfengine-3.15.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "testall": 3.15.3_vs_3.15.4.

    1 #!/bin/sh
    2 #
    3 #  Copyright 2019 Northern.tech AS
    4 #
    5 #  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
    6 #
    7 #  This program is free software; you can redistribute it and/or modify it
    8 #  under the terms of the GNU General Public License as published by the
    9 #  Free Software Foundation; version 3.
   10 #
   11 #  This program is distributed in the hope that it will be useful,
   12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 #  GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
   19 #
   20 # To the extent this program is licensed as part of the Enterprise
   21 # versions of CFEngine, the applicable Commercial Open Source License
   22 # (COSL) may apply to this file if you as a licensee so wish it. See
   23 # included file COSL.txt.
   24 #
   25 
   26 #
   27 # Detect and replace non-POSIX shell
   28 #
   29 try_exec() {
   30     type "$1" > /dev/null 2>&1 && exec "$@"
   31 }
   32 
   33 broken_posix_shell()
   34 {
   35     unset foo
   36     local foo=1
   37     test "$foo" != "1"
   38 }
   39 
   40 if broken_posix_shell >/dev/null 2>&1
   41 then
   42     try_exec /usr/xpg4/bin/sh "$0" "$@"
   43     echo "No compatible shell script interpreter found."
   44     echo "Please find a POSIX shell for your system."
   45     exit 42
   46 fi
   47 
   48 #
   49 # Explicitly use POSIX tools if needed
   50 #
   51 if [ -f /usr/xpg4/bin/grep ]
   52 then
   53     PATH=/usr/xpg4/bin:$PATH
   54     export PATH
   55 fi
   56 
   57 #
   58 # Unset environment variables which might break runinng acceptance tests
   59 #
   60 GREP_OPTIONS=
   61 export GREP_OPTIONS
   62 
   63 #
   64 # Defaults (overridden by command-line arguments)
   65 #
   66 LOG=test.log
   67 INOTIFYWATCH_LOG=inotifywatch.log
   68 INOTIFYWAIT_LOG=inotifywait.log
   69 INOTIFYWATCH_OPTS="-r -e modify -e attrib -e moved_to -e create -e delete -e delete_self"
   70 INOTIFYWAIT_OPTS="-r -m -e modify -e attrib -e moved_to -e create -e delete -e delete_self"
   71 SUMMARY=summary.log
   72 XML=test.xml
   73 WHOAMI=/c/Windows/System32/whoami.exe
   74 BASE_WORKDIR=${BASE_WORKDIR:-$(pwd)/workdir}
   75 QUIET=
   76 
   77 # Default test types to run if --tests= is not passed
   78 
   79 # DEFAULT ON
   80 COMMON_TESTS=${COMMON_TESTS:-1}       ; export COMMON_TESTS
   81 TIMED_TESTS=${TIMED_TESTS:-1}         ; export TIMED_TESTS
   82 SLOW_TESTS=${SLOW_TESTS:-1}           ; export SLOW_TESTS
   83 ERROREXIT_TESTS=${ERROREXIT_TESTS:-1} ; export ERROREXIT_TESTS
   84 ERRORLOG_TESTS=${ERRORLOG_TESTS:-1}   ; export ERRORLOG_TESTS
   85 SERIAL_TESTS=${SERIAL_TESTS:-1}       ; export SERIAL_TESTS
   86 NETWORK_TESTS=${NETWORK_TESTS:-1}     ; export NETWORK_TESTS
   87 LIBXML2_TESTS=${LIBXML2_TESTS:-1}     ; export LIBXML2_TESTS
   88 LIBCURL_TESTS=${LIBCURL_TESTS:-1}     ; export LIBCURL_TESTS
   89 # DEFAULT OFF
   90 UNSAFE_TESTS=${UNSAFE_TESTS:-0}       ; export UNSAFE_TESTS
   91 STAGING_TESTS=${STAGING_TESTS:-0}     ; export STAGING_TESTS
   92 
   93 
   94 BINDIR=${BINDIR:-}                  ; export BINDIR
   95 NO_CLEAN=${NO_CLEAN:-0}             ; export NO_CLEAN
   96 BASECLASSES=${BASECLASSES:-AUTO}    ; export BASECLASSES
   97 EXTRACLASSES=${EXTRACLASSES:-DEBUG} ; export EXTRACLASSES
   98 AGENT=${AGENT:-}                    ; export AGENT
   99 CF_PROMISES=${CF_PROMISES:-}        ; export CF_PROMISES
  100 CF_SERVERD=${CF_SERVERD:-}          ; export CF_SERVERD
  101 CF_EXECD=${CF_EXECD:-}              ; export CF_EXECD
  102 CF_KEY=${CF_KEY:-}                  ; export CF_KEY
  103 CF_SECRET=${CF_SECRET:-}            ; export CF_SECRET
  104 CF_NET=${CF_NET:-}                  ; export CF_NET
  105 CF_CHECK=${CF_CHECK:-}              ; export CF_CHECK
  106 CF_RUNAGENT=${CF_RUNAGENT:-}        ; export CF_RUNAGENT
  107 RPMVERCMP=${RPMVERCMP:-}            ; export RPMVERCMP
  108 LIBTOOL=${LIBTOOL:-}                ; export LIBTOOL
  109 INCLUDE_IN_WORKDIR=${INCLUDE_IN_WORKDIR:-} ; export INCLUDE_IN_WORKDIR
  110 
  111 VALGRIND_OPTS="${VALGRIND_OPTS:---leak-check=full --show-reachable=yes --suppressions=valgrind-suppressions}"
  112 export VALGRIND_OPTS
  113 
  114 export MAKEFLAGS
  115 export GAINROOT
  116 
  117 # Use TEST_INDEX for indexing a poor man's array. Basically the subscript is
  118 # just appended to the variable name.
  119 TEST_INDEX=0
  120 TEST_TIMED_INDEX=0
  121 #TESTS_TIMED_<index>=0
  122 #TESTS_TIMEOUT_<index>=0
  123 #TESTS_PASSES_<index>=0
  124 TESTS_COUNT=0
  125 TESTS_NORMAL_COUNT=0
  126 TESTS_TIMED_COUNT=0
  127 TESTS_TIMED_REMAINING=0
  128 
  129 case "$OSTYPE" in
  130     msys)
  131         if "$WHOAMI" -priv | grep SeTakeOwnershipPrivilege > /dev/null
  132         then
  133             # Don't use elevate if we already have the privileges. It is slower and
  134             # pops up a flashing window for every single test.
  135             DEFAULT_GAINROOT=
  136         else
  137             DEFAULT_GAINROOT="`dirname $0`/tool_wrappers/elevate.sh"
  138         fi
  139         # Use hardlinks on Windows. Using symbolic links will work, but Msys creates a
  140         # real copy, which eats disk space very quickly when you multiply with the number
  141         # of tests.
  142         LN_CMD="ln -f"
  143         ;;
  144     *)
  145         DEFAULT_GAINROOT=fakeroot
  146         LN_CMD="ln -sf"
  147         ;;
  148 esac
  149 
  150 GAINROOT=${GAINROOT:-$DEFAULT_GAINROOT}
  151 
  152 PASSED_TESTS=0
  153 FAILED_TESTS=0
  154 SUPPRESSED_FAILURES=0
  155 SOFT_FAILURES=0
  156 SKIPPED_TESTS=0
  157 FLAKEY_FAILURES=0
  158 
  159 #
  160 # Many older platforms don't support date +%s, so check for compatibility
  161 # and find Perl for the unix_seconds() routine below. (Mantis #1254)
  162 #
  163 HAVE_DATE_PCT_S=
  164 date +%s | grep %s >/dev/null 2>&1
  165 if [ $? -ne 0 ]
  166 then
  167     HAVE_DATE_PCT_S=1
  168 fi
  169 PERL=`which perl 2>/dev/null`
  170 
  171 # color!
  172 if [ "${CFENGINE_COLOR}" = "1" ]
  173 then
  174     COLOR_SUCCESS="\\033[1;32m"
  175     COLOR_FAILURE="\\033[1;31m"
  176     COLOR_WARNING="\\033[1;33m"
  177     COLOR_NORMAL="\\033[0;39m"
  178 else
  179     COLOR_SUCCESS=
  180     COLOR_FAILURE=
  181     COLOR_WARNING=
  182     COLOR_NORMAL=
  183 fi
  184 
  185 #
  186 # Obtain UNIX time(), using date +%s, Perl, or POSIX-compatible approach.
  187 #
  188 unix_seconds() {
  189     if [ "$HAVE_DATE_PCT_S" ]
  190     then
  191         date +%s
  192         return 0
  193     fi
  194 
  195     if [ "$PERL" ]
  196     then
  197         $PERL -e 'print time() . "\n"' 2>/dev/null
  198         if [ $? -eq 0 ]
  199         then
  200             return 0
  201         fi
  202     fi
  203 
  204     # Last resort if Perl fails - the extended cpio interchange format has
  205     # the file modification timestamp in columns 48-59, in octal.
  206     : > $BASE_WORKDIR/x
  207     echo "ibase=8;$(pax -wx cpio $BASE_WORKDIR/$$.seconds | cut -c 48-59)" | bc 2>/dev/null
  208     rm $BASE_WORKDIR/x
  209 }
  210 
  211 usage() {
  212     echo "testall [-h|--help] [-q|--quiet] [--gainroot=<command>] [--agent=<agent>] [--cfpromises=<cf-promises>] [--cfserverd=<cf-serverd>] [--cfexecd=<cf-execd>] [--cfkey=<cf-key>] [--cfkeycrypt=<cf-secret>] [--cfnet=<cf-net>] [--cfcheck=<cf-check>] [--bindir=<bindir>] [--tests=...] [--gdb] [--printlog] [<test> <test>...]"
  213     echo
  214     echo "If no test is given, all standard tests are run:"
  215     echo "  Tests with names of form <file>.cf are expected to run successfully"
  216     echo "  Tests with names of form <file>.x.cf are expected to exit with error"
  217     echo "Set ${COLOR_SUCCESS}CFENGINE_COLOR=1${COLOR_NORMAL} to get ANSI color markers where appropriate."
  218     echo
  219     echo "If arguments are given, those are executed as tests"
  220     echo
  221     echo " -h"
  222     echo " --help    prints usage"
  223     echo " -q"
  224     echo " --quiet   makes script much quieter"
  225     echo " --gainroot=<command>  forces use of command to gain root privileges,"
  226     echo "           otherwise fakeroot is used.  Use --gainroot=env to make this"
  227     echo "           option a no-op (e.g. you're running inside fakeroot already)"
  228     echo " --agent   provides a way to specify non-default cf-agent location,"
  229     echo "           and defaults to $DEFAGENT."
  230     echo " --baseclasses  provides a way to override the default cf-agent classes,"
  231     echo "           and defaults to ${BASECLASSES}.  Also can use --bc"
  232     echo " --extraclasses  provides a way to append to the default cf-agent classes,"
  233     echo "           and defaults to ${EXTRACLASSES}.  Also can use --ec"
  234     echo " --cfpromises  provides a way to specify non-default cf-promises location,"
  235     echo "           and defaults to $DEFCF_PROMISES."
  236     echo " --cfserverd  provides a way to specify non-default cf-serverd location,"
  237     echo "           and defaults to $DEFCF_SERVERD."
  238     echo " --cfexecd provides a way to specify non-default cf-execd location,"
  239     echo "           and defaults to $DEFCF_EXECD."
  240     echo " --cfkey   provides a way to specify non-default cf-key location,"
  241     echo "           and defaults to $DEFCF_KEY."
  242     echo " --cfkeycrypt  provides a way to specify non-default cf-secret location,"
  243     echo "           and defaults to $DEFCF_SECRET."
  244     echo " --cfnet   provides a way to specify non-default cf-net location,"
  245     echo "           and defaults to $DEFCF_NET."
  246     echo " --cfcheck provides a way to specify non-default cf-check location,"
  247     echo "           and defaults to $DEFCF_CHECK."
  248     echo " --cfrunagent  provides a way to specify non-default cf-runagent location,"
  249     echo "           and defaults to $DEFCF_RUNAGENT."
  250     echo " --rpmvercmp  provides a way to specify non-default rpmvercmp location,"
  251     echo "           and defaults to $DEFRPMVERCMP."
  252     echo " --bindir  specifies the directory containing all the binaries."
  253     echo "           Mutually exclusive with --agent and --cf* arguments."
  254     echo " --libtool specify non-default libtool location (only needed for --gdb)."
  255     echo "               defaults to $DEFLIBTOOL."
  256     echo " --include Include the file or directory given in the workdir before the test"
  257     echo "           starts. This option may be given several times."
  258     echo " --tests=common,network,serial,timed,slow,errorexit,errorlog,libxml2,libcurl"
  259     echo "           This is the default value, you can also add from the following:"
  260     echo "           unsafe,staging"
  261     echo "           NOTE: 'staging' tests are not expected to pass"
  262     echo "           WARN: 'unsafe' tests modify the system they are running on and can cause DAMAGE!"
  263     echo "                          If you use this option you should also use --gainroot=sudo."
  264     echo " --printlog   print the full test.log output immediately.  Override with $PRINTLOG"
  265     echo " --gdb          Run test under GDB"
  266     echo " --valgrind     Run test under Valgrind"
  267     echo " --callgrind    Run test under valgrind --tool=callgrind"
  268     echo " --inotifywatch Run tests and log filesystem statistics"
  269     echo " --inotifywait  Run tests and log filesystem events"
  270     echo " --no-clean does not clean workdir after test finishes"
  271     echo "            (by default it gets cleaned only if test passed)."
  272     echo " --base-workdir Specify a local directory for all working files"
  273     echo " -j[n]"
  274     echo " -jobs=[n] Run tests in parallel, works like make -j option. Note that some"
  275     echo "           tests will always run one by one."
  276     echo " --verbose  Run tests with verbose logging"
  277     echo " --debug    Run tests with debug logging"
  278 }
  279 
  280 workdir() {
  281     echo "$BASE_WORKDIR/$(echo "$1" | sed 's,[./],_,g')"
  282 }
  283 
  284 # Example:    fgrepvar 'word'  VARNAME
  285 # Silent fgrep for variables: search for "word" in $VARNAME
  286 fgrepvar() {
  287     eval echo \$$2  |  fgrep "$1"  >  /dev/null
  288 }
  289 
  290 # Same but instead of word search for a regex
  291 grepvar() {
  292     eval echo \$$2  |  grep "$1"  >  /dev/null
  293 }
  294 
  295 # Takes the following arguments:
  296 # 1. Agent - The agent to execute.
  297 # 2. Test - The test to execute.
  298 # 3. Pass number - [Optional] The current pass number. Used by timed tests.
  299 # 4. Timeout variable - [Optional] The name of the variable to put the next
  300 #        timeout into. Used by timed tests.
  301 runtest() {
  302     # Clear/Reset local variables
  303     unset AGENT TEST TEST_TYPE EXTRATEXT SKIP SKIPREASON RESULT RESULT_MSG TEST_START_TIME FLATNAME WORKDIR OUTFILE TEST_DESCRIPTION TEST_STORY TEST_COVERS
  304 
  305     AGENT="$1"
  306     TEST="$2"
  307     PASS_NUM="$3"
  308     NEXT_TIMEOUT_VAR="$4"
  309     # With STAY_IN_WORKDIR we may be running alongside others, and need to print everything at once
  310     # on one line, so that we don't risk mixing lines together.
  311     if [ -z "$QUIET" -a -z "$STAY_IN_WORKDIR" ]
  312     then
  313         printf "$TEST "
  314     fi
  315 
  316     case "$TEST" in
  317         *.x.cf)       TEST_TYPE=errorexit &&
  318                       EXTRATEXT='(should exit with non-zero, but not crash)' ;;
  319         *.error.cf)   TEST_TYPE=errorlog &&
  320                       EXTRATEXT='(should log error(s), but exit with 0)' ;;
  321         */staging/*)  TEST_TYPE=staging   ;;
  322         */unsafe/*)   TEST_TYPE=unsafe    ;;
  323         */network/*)  TEST_TYPE=network   ;;
  324         */timed/*)    TEST_TYPE=timed     ;;
  325         */slow/*)     TEST_TYPE=slow      ;;
  326         *[/_]serial[/_.]*)  TEST_TYPE=serial  ;;
  327         */11_xml_edits/*)   TEST_TYPE=libxml2 ;;
  328         */01_vars/02_functions/network/url_get.cf)    TEST_TYPE=libcurl ;;
  329         *)            TEST_TYPE=common    ;;
  330     esac
  331 
  332     SKIP=0                          # by default the test is not skipped
  333     SKIPREASON=
  334     if [ $ERROREXIT_TESTS = 0  -a  $TEST_TYPE = errorexit ]
  335     then
  336         SKIP=1
  337         SKIPREASON="${COLOR_WARNING}'errorexit' tests are disabled${COLOR_NORMAL}"
  338     elif [ $ERRORLOG_TESTS = 0  -a  $TEST_TYPE = errorlog ]
  339     then
  340         SKIP=1
  341         SKIPREASON="${COLOR_WARNING}'errorlog' tests are disabled${COLOR_NORMAL}"
  342     elif [ $STAGING_TESTS = 0  -a  $TEST_TYPE = staging ]
  343     then
  344         SKIP=1
  345         SKIPREASON="${COLOR_WARNING}Staging tests are disabled${COLOR_NORMAL}"
  346     elif [ $UNSAFE_TESTS = 0  -a  $TEST_TYPE = unsafe ]
  347     then
  348         SKIP=1
  349         SKIPREASON="${COLOR_WARNING}Unsafe tests are disabled${COLOR_NORMAL}"
  350     elif [ $NETWORK_TESTS = 0  -a  $TEST_TYPE = network ]
  351     then
  352         SKIP=1
  353         SKIPREASON="${COLOR_WARNING}Network-dependent tests are disabled${COLOR_NORMAL}"
  354     elif [ $TIMED_TESTS = 0  -a  $TEST_TYPE = timed ]
  355     then
  356         SKIP=1
  357         SKIPREASON="${COLOR_WARNING}Timed tests are disabled${COLOR_NORMAL}"
  358     elif [ $SLOW_TESTS = 0  -a  $TEST_TYPE = slow ]
  359     then
  360         SKIP=1
  361         SKIPREASON="${COLOR_WARNING}Slow tests are disabled${COLOR_NORMAL}"
  362     elif [ $SERIAL_TESTS = 0  -a  $TEST_TYPE = serial ]
  363     then
  364         SKIP=1
  365         SKIPREASON="${COLOR_WARNING}Serial tests are disabled${COLOR_NORMAL}"
  366     elif [ $LIBXML2_TESTS = 0  -a  $TEST_TYPE = libxml2 ]
  367     then
  368         SKIP=1
  369         SKIPREASON="${COLOR_WARNING}XML file editing tests are disabled${COLOR_NORMAL}"
  370     elif [ $LIBCURL_TESTS = 0  -a  $TEST_TYPE = libcurl ]
  371     then
  372         SKIP=1
  373         SKIPREASON="${COLOR_WARNING}libcurl tests are disabled${COLOR_NORMAL}"
  374     elif [ $COMMON_TESTS = 0  -a  $TEST_TYPE = common ]
  375     then
  376         SKIP=1
  377         SKIPREASON="${COLOR_WARNING}Common tests are disabled${COLOR_NORMAL}"
  378     fi
  379 
  380     TEST_START_TIME=$(unix_seconds)
  381 
  382     # Create workdir
  383     WORKDIR="$(workdir "$TEST")"
  384     OUTFILE="$WORKDIR/output.log"
  385 
  386     if [ -z "$PASS_NUM" ] || [ "$PASS_NUM" -eq 1 ]
  387     then
  388         # Don't reset workdir if this is a subsequent pass.
  389         $GAINROOT rm -rf "$WORKDIR"
  390         mkdir -p "$WORKDIR/bin" "$WORKDIR/tmp"
  391         chmod ugo+rwxt "$WORKDIR/tmp"
  392     fi
  393 
  394     # For unknown reason on the second pass, this file is root-owned, so we need GAINROOT here
  395     $GAINROOT rm -f "$OUTFILE"
  396 
  397     # Make sure these files are owned by the script, for some reason
  398     # each agent execution or each pass, messes this up.
  399     $GAINROOT touch        "$OUTFILE" "$WORKDIR/$LOG" "$WORKDIR/$SUMMARY" "$WORKDIR/$XML"
  400     $GAINROOT chown $USER  "$OUTFILE" "$WORKDIR/$LOG" "$WORKDIR/$SUMMARY" "$WORKDIR/$XML"
  401 
  402     if [ -n "$NEXT_TIMEOUT_VAR" ]
  403     then
  404         eval $NEXT_TIMEOUT_VAR=
  405     fi
  406 
  407     if [ "$SKIP" = 1 ]                                 # Skip
  408     then
  409         TEST_END_TIME=$TEST_START_TIME
  410         RESULT=Skip
  411         RESULT_MSG="${COLOR_WARNING}Skipped ($SKIPREASON)${COLOR_NORMAL}"
  412 
  413     else                                               # Do not skip
  414         # Prepare workdir
  415 
  416         # Don't copy into workdir if this is a subsequent pass.
  417         if [ -z "$PASS_NUM" ] || [ "$PASS_NUM" -eq 1 ]
  418         then
  419             if [ -n "$BINDIR" ]
  420             then
  421                 # Copy everything, because Windows depends on DLLs.
  422                 $LN_CMD "$BINDIR"/* "$WORKDIR/bin"
  423             else
  424                 $LN_CMD "$AGENT" "$WORKDIR/bin"
  425                 $LN_CMD "$CF_PROMISES" "$WORKDIR/bin"
  426                 $LN_CMD "$CF_SERVERD" "$WORKDIR/bin"
  427                 $LN_CMD "$CF_EXECD" "$WORKDIR/bin"
  428                 $LN_CMD "$CF_KEY" "$WORKDIR/bin"
  429                 $LN_CMD "$CF_SECRET" "$WORKDIR/bin"
  430                 $LN_CMD "$CF_NET" "$WORKDIR/bin"
  431                 $LN_CMD "$CF_CHECK" "$WORKDIR/bin"
  432                 $LN_CMD "$CF_RUNAGENT" "$WORKDIR/bin"
  433                 $LN_CMD "$RPMVERCMP" "$WORKDIR/bin"
  434             fi
  435             for inc in $INCLUDE_IN_WORKDIR
  436             do (
  437                 # Copy directory structure, but make links inside. This allows tests to
  438                 # add additional files to the directory.
  439                 base=$(basename $inc)
  440                 mkdir -p "$WORKDIR/$base"
  441                 cd $inc || exit 2
  442                 for dir in $(find . -type d)
  443                 do
  444                     mkdir -p "$WORKDIR/$base/$dir"
  445                 done
  446                 for file in $(find . \! -type d)
  447                 do
  448                     $LN_CMD "$inc/$file" "$WORKDIR/$base/$file"
  449                 done
  450             )
  451             done
  452         fi
  453         if uname | grep MINGW > /dev/null
  454         then
  455             PLATFORM_WORKDIR="$(echo $WORKDIR | sed -e 's%^/\([a-cA-Z]\)/%\1:/%' | sed -e 's%/%\\%g')"
  456             DS="\\"
  457         else
  458             PLATFORM_WORKDIR="$WORKDIR"
  459             DS="/"
  460         fi
  461 
  462         ( echo ----------------------------------------------------------------------
  463           echo "$TEST" "$EXTRATEXT"
  464           echo ----------------------------------------------------------------------
  465         ) >> "$WORKDIR/$LOG"
  466 
  467         echo "#!/bin/sh
  468 CFENGINE_TEST_OVERRIDE_WORKDIR=\"$PLATFORM_WORKDIR\"
  469 TEMP=\"$PLATFORM_WORKDIR${DS}tmp\"
  470 CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR=\"$CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR\"
  471 export CFENGINE_TEST_OVERRIDE_WORKDIR TEMP CFENGINE_TEST_OVERRIDE_EXTENSION_LIBRARY_DIR
  472 
  473 " > "$WORKDIR/runtest"
  474 
  475         if [ "$GDB" = 1 ]
  476         then
  477             if grep libtool < "$AGENT" > /dev/null
  478             then
  479                 printf "\"$LIBTOOL\" --mode=execute " >> "$WORKDIR/runtest"
  480             fi
  481             printf "gdb --args " >> "$WORKDIR/runtest"
  482         fi
  483 
  484         if [ -n "$USE_VALGRIND"  -a  $TEST_TYPE = errorexit ]
  485         then
  486             if grep libtool < "$AGENT" > /dev/null
  487             then
  488                 printf "\"$LIBTOOL\" --mode=execute " >> "$WORKDIR/runtest"
  489             fi
  490             printf "valgrind ${VALGRIND_OPTS} \"$AGENT\" $VERBOSE $DEBUG -Klf \"$TEST\" -D ${PASS_NUM:+test_pass_$PASS_NUM,}${BASECLASSES},${EXTRACLASSES} 2>&1\n" >> "$WORKDIR/runtest"
  491         elif [ x"$PRELOAD_ASAN" != x ]
  492         then
  493             printf "LD_PRELOAD=$PRELOAD_ASAN \"$AGENT\" $VERBOSE $DEBUG -Klf \"$TEST\" -D ${PASS_NUM:+test_pass_$PASS_NUM,}${BASECLASSES},${EXTRACLASSES}\n" >> "$WORKDIR/runtest"
  494         else
  495             printf "\"$AGENT\" $VERBOSE $DEBUG -Klf \"$TEST\" -D ${PASS_NUM:+test_pass_$PASS_NUM,}${BASECLASSES},${EXTRACLASSES}\n" >> "$WORKDIR/runtest"
  496         fi
  497 
  498         chmod +x "$WORKDIR/runtest"
  499 
  500         if [ "$GDB" = 1 ]
  501         then
  502             $GAINROOT "$WORKDIR/runtest"
  503         else
  504             eval $GAINROOT "$WORKDIR/runtest" >>$OUTFILE 2>&1
  505         fi
  506 
  507         RETVAL=$?
  508         TEST_END_TIME=$(unix_seconds)
  509 
  510         cat $OUTFILE >> "$WORKDIR/$LOG"
  511         echo >> "$WORKDIR/$LOG"
  512         echo "Return code is $RETVAL." >> "$WORKDIR/$LOG"
  513 
  514         # Try to collect test metadata if any
  515         if egrep "R: test description: " $OUTFILE > /dev/null
  516         then
  517             TEST_DESCRIPTION="$(egrep "R: test description" $OUTFILE | sed -e "s,.*test description: \([A-Za-z0-9_]*\),\1,")"
  518         fi
  519         if egrep -e "R: test story_id: " $OUTFILE > /dev/null
  520         then
  521             TEST_STORY="$(egrep "R: test story_id" $OUTFILE | sed -e "s,.*test story_id: \([0-9][0-9]*\),\1,")"
  522         fi
  523         if egrep -e "R: test covers: " $OUTFILE > /dev/null
  524         then
  525             TEST_COVERS="$(egrep "R: test covers" $OUTFILE | sed -e "s,.*test covers: \([A-Za-z0-9_]*\),\1,")"
  526         fi
  527 
  528         if [ $TEST_TYPE = errorexit ]
  529         then
  530             if [ $RETVAL -gt 0 ] && [ $RETVAL -lt 128 ]
  531             then
  532                 RESULT=Pass
  533                 RESULT_MSG="${COLOR_SUCCESS}Pass${COLOR_NORMAL}"
  534             elif [ $RETVAL -ge 128 ]
  535             then
  536                 RESULT=FAIL
  537                 RESULT_MSG="${COLOR_FAILURE}FAIL (CRASH!!! THIS IS A BUG!)${COLOR_NORMAL}"
  538             else
  539                 RESULT=FAIL
  540                 RESULT_MSG="${COLOR_FAILURE}FAIL (Failed to exit with error!)${COLOR_NORMAL}"
  541             fi
  542         elif [ $TEST_TYPE = errorlog ]
  543         then
  544             if [ $RETVAL != 0 ]
  545             then
  546                 RESULT=FAIL
  547                 RESULT_MSG="${COLOR_FAILURE}FAIL (NON-ZERO EXIT CODE!)${COLOR_NORMAL}"
  548             elif fgrep 'error: ' "$OUTFILE" > /dev/null
  549             then
  550                 RESULT=Pass
  551                 RESULT_MSG="${COLOR_SUCCESS}Pass${COLOR_NORMAL}"
  552             else
  553                 RESULT=FAIL
  554                 RESULT_MSG="${COLOR_FAILURE}FAIL (No errors printed)${COLOR_NORMAL}"
  555             fi
  556         else # TEST_TYPE is not errorlog or errorexit
  557             # We need to be careful when matching test outcomes. Because of convergence
  558             # passes, a test may output FAIL before it outputs Pass; in this case the
  559             # latter trumps the former. Also be careful when matching an [XS]FAIL; the
  560             # test should not be allowed to output any other outcome in the same run,
  561             # except for FAIL.
  562 
  563             # Some states are output by dcs.cf.sub, therefore check for both TEST
  564             # prefix and dcs.cf.sub prefix.
  565             ESCAPED_TEST="$(echo "($TEST|dcs.cf.sub)" | sed -e 's/\./\\./g')"
  566             if egrep "R: .*$ESCAPED_TEST [XS]FAIL" $OUTFILE > /dev/null && ! egrep "R: .*$ESCAPED_TEST Wait/" $OUTFILE > /dev/null
  567             then
  568                 # Check for passed outcome, which should not happen.
  569                 if egrep "R: .*$ESCAPED_TEST " $OUTFILE | egrep "R: .*$ESCAPED_TEST Pass" > /dev/null
  570                 then
  571                     RESULT=FAIL
  572                     RESULT_MSG="${COLOR_FAILURE}FAIL (The test Passed, but failure was expected)${COLOR_NORMAL}"
  573                 elif egrep "R: .*$ESCAPED_TEST " $OUTFILE | egrep -v "R: .*$ESCAPED_TEST [XS]?FAIL" > /dev/null
  574                 then
  575                     # Other test case outcomes than fail. Should not happen.
  576                     RESULT=FAIL
  577                     RESULT_MSG="${COLOR_FAILURE}FAIL (Failure was expected, but the test had an unexpected test outcome, check test output, Pass/FAIL need to match exactly)${COLOR_NORMAL}"
  578                 else
  579                     TICKET="$(egrep "R: .*$ESCAPED_TEST [XS]FAIL" $OUTFILE | sed -e "s,.*[XS]FAIL/\(.*\),\1,")"
  580                     RESULT="$(egrep "R: .*$ESCAPED_TEST [XS]FAIL" $OUTFILE | sed -e "s,.*\([XS]FAIL\).*,\1,")"
  581                     if [ "$RESULT" = "XFAIL" ]
  582                     then
  583                         RESULT_MSG="${COLOR_WARNING}FAIL (Suppressed, $TICKET)${COLOR_NORMAL}"
  584                     else
  585                         RESULT_MSG="${COLOR_WARNING}Soft fail ($TICKET)${COLOR_NORMAL}"
  586                     fi
  587                 fi
  588             elif [ $RETVAL -ne 0 ]
  589             then
  590                 RESULT=FAIL
  591             elif egrep "R: .*$ESCAPED_TEST FAIL/no_ticket_number" $OUTFILE > /dev/null
  592             then
  593                 RESULT=FAIL
  594                 RESULT_MSG="${COLOR_FAILURE}FAIL (Tried to suppress failure, but no issue number is provided)${COLOR_NORMAL}"
  595             elif egrep "R: .*$ESCAPED_TEST Wait/[0-9]+" $OUTFILE > /dev/null
  596             then
  597                 if [ -z "$NEXT_TIMEOUT_VAR" ]
  598                 then
  599                     RESULT=FAIL
  600                     RESULT_MSG="${COLOR_FAILURE}FAIL (Test tried to wait but is not in \"timed\" directory)${COLOR_NORMAL}"
  601                 else
  602                     WAIT_TIME=$(egrep "R: .*$ESCAPED_TEST Wait/[0-9]+" $OUTFILE | sed -e 's,.*Wait/\([0-9][0-9]*\).*,\1,')
  603                     eval $NEXT_TIMEOUT_VAR=$(($TEST_END_TIME+$WAIT_TIME))
  604                     RESULT=Wait
  605                     RESULT_MSG="Awaiting ($WAIT_TIME seconds)..."
  606                 fi
  607             elif egrep "R: .*$ESCAPED_TEST Pass" $OUTFILE > /dev/null
  608             then
  609                 RESULT=Pass
  610                 RESULT_MSG="${COLOR_SUCCESS}Pass${COLOR_NORMAL}"
  611             elif egrep "R: .*$ESCAPED_TEST Skip/unsupported" $OUTFILE > /dev/null
  612             then
  613                 RESULT=Skip
  614                 RESULT_MSG="${COLOR_WARNING}Skipped (No platform support)${COLOR_NORMAL}"
  615             elif egrep "R: .*$ESCAPED_TEST Skip/needs_work" $OUTFILE > /dev/null
  616             then
  617                 RESULT=Skip
  618                 RESULT_MSG="${COLOR_WARNING}Skipped (Test needs work)${COLOR_NORMAL}"
  619             elif egrep "R: .*$ESCAPED_TEST FLAKEY" $OUTFILE > /dev/null
  620             then
  621                 RESULT=FLAKEY
  622                 TICKET="$(egrep "R: .*$ESCAPED_TEST FLAKEY" $OUTFILE | sed -e "s,.*FLAKEY/\(.*\),\1,")"
  623                 RESULT_MSG="${COLOR_WARNING}Flakey fail ($TICKET)${COLOR_NORMAL}"
  624 
  625             else
  626                 RESULT=FAIL
  627                 RESULT_MSG="${COLOR_FAILURE}FAIL${COLOR_NORMAL}"
  628             fi
  629         fi
  630 
  631         if [ "x$RESULT_MSG" = "x" ]
  632         then
  633             RESULT_MSG=$RESULT
  634         fi
  635 
  636         if [ "$RESULT" = "FAIL" ] && [ -e .succeeded/"$FLATNAME" ]
  637         then
  638             RESULT_MSG="${COLOR_FAILURE}$RESULT_MSG (UNEXPECTED FAILURE)${COLOR_NORMAL}"
  639         fi
  640     fi
  641 
  642     if [ "$RESULT" = "XFAIL" -o "$RESULT" = "SFAIL" ]
  643     then
  644         echo "    <testcase name=\"$(basename $TEST)\""
  645         echo "              classname=\"$TEST $RESULT_MSG\""
  646         echo "              time=\"$(($TEST_END_TIME - $TEST_START_TIME)) seconds\">"
  647     elif [ "$RESULT" != Wait ]
  648     then
  649         echo "    <testcase name=\"$(basename $TEST)\""
  650         echo "              classname=\"$TEST\""
  651         echo "              time=\"$(($TEST_END_TIME - $TEST_START_TIME)) seconds\">"
  652     fi >> "$WORKDIR/$XML"
  653 
  654 
  655     # Fill test metadata if any
  656     if [ ! -z "$TEST_DESCRIPTION" ] || [ ! -z "$TEST_STORY" ] || [ ! -z "$TEST_COVERS" ]
  657     then
  658         if [ ! -z "$TEST_DESCRIPTION" ]
  659         then
  660             TEST_DESCRIPTION_METATAG="name=\"$TEST_DESCRIPTION\""
  661         fi
  662         if [ ! -z "$TEST_STORY" ]
  663         then
  664             TEST_STORY_METATAG="story_id=\"$TEST_STORY\""
  665         fi
  666         if [ ! -z "$TEST_COVERS" ]
  667         then
  668             TEST_COVERS_METATAG="covers=\"$TEST_COVERS\""
  669         fi
  670         echo "        <system-out>$TEST_DESCRIPTION_METATAG $TEST_STORY_METATAG $TEST_COVERS_METATAG</system-out>"
  671     fi >> "$WORKDIR/$XML"
  672 
  673 
  674     echo $RESULT $TEST >> "$WORKDIR/$SUMMARY"
  675     case "$RESULT" in
  676         Pass)
  677             PASSED_TESTS=$(($PASSED_TESTS + 1))
  678 
  679             mkdir -p '.succeeded'
  680             touch .succeeded/"$FLATNAME"
  681             ;;
  682 
  683         FAIL|XFAIL)
  684             ( echo "        <failure type=\"$RESULT\""
  685               echo "                 message=\"$RESULT_MSG $TEST\">"
  686               cat $OUTFILE | sed -e "s/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/'/\&quot;/g"
  687               echo "        </failure>"
  688             ) >> "$WORKDIR/$XML"
  689             FAILED_TESTS=$(($FAILED_TESTS + 1))
  690             if [ "$RESULT" = "XFAIL" ]
  691             then
  692                 SUPPRESSED_FAILURES=$(($SUPPRESSED_FAILURES + 1))
  693             fi
  694             ;;
  695 
  696         SFAIL)
  697             ( echo "        <skipped type=\"$RESULT_MSG\">"
  698               echo "                 message=\"$RESULT_MSG $TEST\">"
  699               cat $OUTFILE | sed -e "s/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/'/\&quot;/g"
  700               echo "        </skipped>"
  701             ) >> "$WORKDIR/$XML"
  702             SOFT_FAILURES=$(($SOFT_FAILURES + 1))
  703             ;;
  704 
  705         Skip)
  706             ( echo "        <skipped type=\"$RESULT_MSG\">"
  707               echo "        </skipped>"
  708             ) >> "$WORKDIR/$XML"
  709             SKIPPED_TESTS=$(($SKIPPED_TESTS + 1))
  710             ;;
  711     esac
  712 
  713     if [ "$RESULT" != Wait ]
  714     then
  715         echo "    </testcase>" >> "$WORKDIR/$XML"
  716     fi
  717 
  718     if [ -e "$WORKDIR" -a "$RESULT" != "FAIL" -a "$RESULT" != "Wait" -a "$NO_CLEAN" = "0" ]
  719     then
  720         # Delete everything except the logs from the workdir.
  721         ls -1 "$WORKDIR" | while read s
  722         do
  723             case "$s" in
  724                 "$LOG"|"$SUMMARY"|"$XML")
  725                 ;;
  726                 *)
  727                     $GAINROOT rm -fr "$WORKDIR/$s"
  728                     ;;
  729             esac
  730         done
  731     fi
  732 
  733     if [ -z "$QUIET" ]
  734     then
  735         if [ -n "$STAY_IN_WORKDIR" ]
  736         then
  737             # See comment about STAY_IN_WORKDIR near start of runtest.
  738             echo "$TEST $RESULT_MSG"
  739         else
  740             echo $RESULT_MSG
  741         fi
  742     else
  743         if [ "$RESULT" = Pass ]
  744         then
  745             printf '.'
  746         elif [ "$RESULT" = Skip ]
  747         then
  748             printf '-'
  749         elif [ "$RESULT" = FAIL ]
  750         then
  751             if [ $TEST_TYPE = errorexit ]
  752             then
  753                 printf '!'
  754             else
  755                 printf 'x'
  756             fi
  757         fi
  758     fi
  759 
  760     (
  761         echo
  762         echo '  ==>' "$RESULT_MSG"
  763         echo
  764     ) >> "$WORKDIR/$LOG"
  765 }
  766 
  767 
  768 ORIG_ARGS=
  769 while true
  770 do
  771     case "$1" in
  772         -h|--help)
  773             usage
  774             exit;;
  775         -q|--quiet)
  776             QUIET=1;;
  777         --gainroot=*)
  778             GAINROOT=${1#--gainroot=};;
  779         --valgrind)
  780             USE_VALGRIND=1;;
  781         --callgrind)
  782             USE_VALGRIND=1
  783             VALGRIND_OPTS="--suppressions=valgrind-suppressions --tool=callgrind";;
  784         --inotifywatch)
  785             USE_INOTIFYWATCH=1;;
  786         --inotifywait)
  787             USE_INOTIFYWAIT=1;;
  788         --tests=*)
  789 
  790             # Deselect all test types, in order to have only the ones
  791             # given as arguments
  792             COMMON_TESTS=0
  793             TIMED_TESTS=0
  794             SLOW_TESTS=0
  795             ERROREXIT_TESTS=0
  796             ERRORLOG_TESTS=0
  797             SERIAL_TESTS=0
  798             NETWORK_TESTS=0
  799             LIBXML2_TESTS=0
  800             LIBCURL_TESTS=0
  801             UNSAFE_TESTS=0
  802             STAGING_TESTS=0
  803 
  804             TESTS_SELECTED=${1#--tests=}
  805             for testtype in `echo $TESTS_SELECTED | tr , ' '`
  806             do
  807                 case "$testtype" in
  808                     common)      COMMON_TESTS=1;;
  809                     timed)       TIMED_TESTS=1;;
  810                     slow)        SLOW_TESTS=1;;
  811                     errorexit)   ERROREXIT_TESTS=1;;
  812                     errorlog)    ERRORLOG_TESTS=1;;
  813                     serial)      SERIAL_TESTS=1;;
  814                     network)     NETWORK_TESTS=1;;
  815                     libxml2)     LIBXML2_TESTS=1;;
  816                     libcurl)     LIBCURL_TESTS=1;;
  817                     unsafe)      UNSAFE_TESTS=1;;
  818                     staging)     STAGING_TESTS=1;;
  819                 esac
  820             done
  821             ;;
  822 
  823         --agent=*)
  824             AGENT=${1#--agent=};;
  825         --baseclasses=*)
  826             BASECLASSES=${1#--baseclasses=};;
  827         --bc=*)
  828             BASECLASSES=${1#--bc=};;
  829         --extraclasses=*)
  830             EXTRACLASSES=${1#--extraclasses=};;
  831         --ec=*)
  832             EXTRACLASSES=${1#--ec=};;
  833         --cfpromises=*)
  834             CF_PROMISES=${1#--cfpromises=};;
  835         --cfserverd=*)
  836             CF_SERVERD=${1#--cfserverd=};;
  837         --cfexecd=*)
  838             CF_EXECD=${1#--cfexecd=};;
  839         --cfkey=*)
  840             CF_KEY=${1#--cfkey=};;
  841         --cfkeycrypt=*)
  842             CF_SECRET=${1#--cfkeycrypt=};;
  843         --cfnet=*)
  844             CF_NET=${1#--cfnet=};;
  845         --cfcheck=*)
  846             CF_CHECK=${1#--cfcheck=};;
  847         --cfrunagent=*)
  848             CF_RUNAGENT=${1#--cfrunagent=};;
  849         --rpmvercmp=*)
  850             RPMVERCMP=${1#--rpmvercmp=};;
  851         --bindir=*)
  852             BINDIR=${1#--bindir=};;
  853         --libtool=*)
  854             LIBTOOL=${1#--libtool=};;
  855         --include=*)
  856             INCLUDE_IN_WORKDIR="$INCLUDE_IN_WORKDIR${INCLUDE_IN_WORKDIR:+ }${1#--include=}";;
  857         -j*|--jobs*)
  858             MAKEFLAGS="$MAKEFLAGS $1";;
  859         --printlog)
  860             PRINTLOG=1;;
  861         --gdb)
  862             GDB=1;;
  863         --no-clean)
  864             NO_CLEAN=1;;
  865         --verbose)
  866             VERBOSE="-v";;
  867         --debug)
  868            DEBUG="--debug";;
  869         --stay-in-workdir)
  870             # Internal option. Meant to keep sub invocations from interfering by
  871             # writing files only into the workdir.
  872             STAY_IN_WORKDIR=1;;
  873         --base-workdir=*)
  874             BASE_WORKDIR="${1#--base-workdir=}";;
  875         -*)
  876             echo "Unknown option: $1"
  877             exit 1;;
  878         *)
  879             break;;
  880     esac
  881     # Make sure spaces are preserved by escaping them.
  882     ORIG_ARGS="$ORIG_ARGS $(echo "$1" | sed -e 's/ /\\ /g')"
  883     shift
  884 done
  885 
  886 #
  887 # Close stdin file descriptor to avoid tty_interactive check in cf-agent being success.
  888 # Do this iff GDB AND Valgrind are not in use.
  889 #
  890 if [ "$GDB" != 1 ] && [ "$USE_VALGRIND" != 1 ]
  891 then
  892     exec </dev/null
  893 fi
  894 
  895 if [ "$OSTYPE" = "msys" ] && [ "$TESTALL_DO_NOT_RECURSE" != 1 ] &&
  896        ! "$WHOAMI" -priv | grep SeTakeOwnershipPrivilege > /dev/null
  897 then
  898     # On Windows we run the entire test run under GAINROOT, because doing it for
  899     # each test is horribly slow.
  900     echo "export GAINROOT=" > runtests.sh
  901     echo "export TESTALL_DO_NOT_RECURSE=1" >> runtests.sh
  902     echo "export DEBUG='$DEBUG'" >> runtests.sh
  903     echo "export VERBOSE='$VERBOSE'" >> runtests.sh
  904     echo "$0 $ORIG_ARGS $@ &" >> runtests.sh
  905     # Note quote change. We want to keep below variables unexpanded.
  906     echo 'trap "kill $!" INT' >> runtests.sh
  907     echo 'trap "kill $!" TERM' >> runtests.sh
  908     # Traps do not fire during commands, but *do* fire during wait.
  909     echo 'wait $!' >> runtests.sh
  910     $GAINROOT "./runtests.sh"
  911     exit $?
  912 fi
  913 
  914 # Check last -j flag, and check if it is -j1.
  915 for arg in $MAKEFLAGS
  916 do
  917     case "$arg" in
  918         -j|--jobs) PARALLEL=1 ;;
  919         -j*) if [ 1 -eq "${arg#-j}" ]
  920              then PARALLEL=
  921              else PARALLEL=1
  922              fi ;;
  923         --jobs=*) if [ 1 -eq "${arg#--jobs=}" ]
  924                   then PARALLEL=
  925                   else PARALLEL=1
  926                   fi ;;
  927     esac
  928 done
  929 
  930 if [ -n "$AGENT" -o -n "$CF_PROMISES" -o -n "$CF_SERVERD" -o -n "$CF_EXECD" -o -n "$CF_KEY" -o -n "$CF_SECRET" -o -n "$CF_NET" -o -n "$CF_CHECK" -o -n "$CF_RUNAGENT" -o -n "$RPMVERCMP" ]
  931 then
  932     if [ -n "$BINDIR" ]
  933     then
  934         echo "--bindir is mutually exclusive with specifying individual binaries."
  935         exit 2
  936     fi
  937 fi
  938 
  939 # We assume we're running this script from one of the following
  940 #   $objdir
  941 #   $objdir/tests/acceptance
  942 #   /var/cfengine/tests/acceptance or from
  943 #   $core_objdir/../{enterprise,masterfiles}/tests/acceptance
  944 find_default_binary()
  945 {
  946     [ -x "`pwd`/../../../core/$2/$2" ] && eval $1=\""`pwd`/../../../core/$2/$2"\"
  947     [ -x "`pwd`/../../../core/ext/$2" ] && eval $1=\""`pwd`/../../../core/ext/$2"\"
  948     [ -x "`pwd`/../../ext/$2" ] && eval $1=\""`pwd`/../../ext/$2"\"
  949     [ -x "`pwd`/../../bin/$2" ] && eval $1=\""`pwd`/../../bin/$2"\"
  950     [ -x "`pwd`/../../$2/$2" ] && eval $1=\""`pwd`/../../$2/$2"\"
  951     [ -x "`pwd`/$2/$2" ] && eval $1=\""`pwd`/$2/$2"\"
  952     [ -n "$BINDIR" -a -x "$BINDIR/$2" ] && eval $1=\""$BINDIR/$2"\"
  953 }
  954 find_default_binary DEFAGENT cf-agent
  955 find_default_binary DEFCF_PROMISES cf-promises
  956 find_default_binary DEFCF_SERVERD cf-serverd
  957 find_default_binary DEFCF_EXECD cf-execd
  958 find_default_binary DEFCF_KEY cf-key
  959 find_default_binary DEFCF_SECRET cf-secret
  960 find_default_binary DEFCF_NET cf-net
  961 find_default_binary DEFCF_CHECK cf-check
  962 find_default_binary DEFCF_RUNAGENT cf-runagent
  963 find_default_binary DEFRPMVERCMP rpmvercmp
  964 
  965 [ -x "`pwd`/libtool" ] && DEFLIBTOOL="`pwd`/libtool"
  966 [ -x "`pwd`/../../libtool" ] && DEFLIBTOOL="`pwd`/../../libtool"
  967 
  968 AGENT=${AGENT:-${DEFAGENT}}
  969 CF_PROMISES=${CF_PROMISES:-${DEFCF_PROMISES}}
  970 CF_SERVERD=${CF_SERVERD:-${DEFCF_SERVERD}}
  971 CF_EXECD=${CF_EXECD:-${DEFCF_EXECD}}
  972 CF_KEY=${CF_KEY:-${DEFCF_KEY}}
  973 CF_SECRET=${CF_SECRET:-${DEFCF_SECRET}}
  974 CF_NET=${CF_NET:-${DEFCF_NET}}
  975 CF_CHECK=${CF_CHECK:-${DEFCF_CHECK}}
  976 CF_RUNAGENT=${CF_RUNAGENT:-${DEFCF_RUNAGENT}}
  977 RPMVERCMP=${RPMVERCMP:-${DEFRPMVERCMP}}
  978 LIBTOOL=${LIBTOOL:-${DEFLIBTOOL}}
  979 
  980 if [ ! -x "$AGENT" -o ! -x "$CF_PROMISES" -o ! -x "$CF_SERVERD" -o ! -x "$CF_EXECD" -o ! -x "$CF_KEY" -o ! -x "$CF_SECRET" -o ! -x "$CF_NET" -o ! -x "$CF_CHECK" -o ! -x "$CF_RUNAGENT" -o ! -x "$RPMVERCMP" ]
  981 then
  982     echo "ERROR: can't find cf-agent or other binary;"     \
  983          " Are you sure you're running this from OBJDIR"   \
  984          " or {core,masterfiles,enterprise}_OBJDIR/tests/acceptance ?"
  985     echo '$AGENT        =' "$AGENT"
  986     echo '$CF_PROMISES  =' "$CF_PROMISES"
  987     echo '$CF_SERVERD   =' "$CF_SERVERD"
  988     echo '$CF_EXECD     =' "$CF_EXECD"
  989     echo '$CF_KEY       =' "$CF_KEY"
  990     echo '$CF_SECRET    =' "$CF_SECRET"
  991     echo '$CF_RUNAGENT  =' "$CF_RUNAGENT"
  992     echo '$CF_NET       =' "$CF_NET"
  993     echo '$CF_CHECK     =' "$CF_CHECK"
  994     echo '$RPMVERCMP    =' "$RPMVERCMP"
  995     exit 1
  996 fi
  997 
  998 if [ "$UNSAFE_TESTS" = "1" ]
  999 then
 1000     if [ "$GAINROOT" = "fakeroot" ]
 1001     then
 1002         echo "Unsafe tests do not play well together with fakeroot. Please use a different"
 1003         echo "--gainroot (like \"sudo\"), or you will get incorrect results."
 1004         exit 1
 1005     fi
 1006 
 1007     # Make sure test dir is accessible to everyone, because unsafe tests may
 1008     # switch user during the test (users promises do this).
 1009     DIR="$(cd "$(dirname "$0")"; pwd)"
 1010     while [ "$DIR" != "/" -a "$DIR" != "" ]
 1011     do
 1012         $GAINROOT chmod go+rx "$DIR"
 1013         DIR="$(dirname "$DIR")"
 1014     done
 1015 fi
 1016 
 1017 if [ $# -gt 0 ]
 1018 then
 1019     # We run all specified tests according to the defaults (no unsafe ones).
 1020     for test in "$@"
 1021     do
 1022         if ! expr "$test" : '[/.]' >/dev/null
 1023         then
 1024             test="./$test"
 1025         fi
 1026 
 1027         if [ -f $test ]
 1028         then
 1029             ALL_TESTS="$ALL_TESTS${ALL_TESTS:+ }$test"
 1030         elif [ -d $test ]
 1031         then
 1032             ALL_TESTS="$ALL_TESTS${ALL_TESTS:+ }$(find "$test" -name workdir -prune -o -name '*.cf' -print | sort)"
 1033         else
 1034             echo "Unable to open test file/directory: $test"
 1035         fi
 1036     done
 1037 else
 1038     ALL_TESTS="$ALL_TESTS${ALL_TESTS:+ }$(find . \( -name selftest -o -name workdir \) -prune -o -name '*.cf' -print | sort)"
 1039 fi
 1040 
 1041 for addtest in $ALL_TESTS
 1042 do
 1043     if echo "$addtest" | fgrep "/timed/" > /dev/null
 1044     then
 1045         if [ "$TIMED_TESTS" = 1 ]
 1046         then
 1047             eval TESTS_TIMED_$TEST_TIMED_INDEX="$addtest"
 1048             eval TESTS_TIMEOUT_$TEST_TIMED_INDEX=0
 1049             eval TESTS_PASSES_$TEST_TIMED_INDEX=0
 1050             TEST_TIMED_INDEX=$(($TEST_TIMED_INDEX+1))
 1051         fi
 1052     else
 1053         eval TESTS_$TEST_INDEX="$addtest"
 1054         TEST_INDEX=$(($TEST_INDEX+1))
 1055     fi
 1056 done
 1057 
 1058 TESTS_NORMAL_COUNT=$TEST_INDEX
 1059 TESTS_TIMED_COUNT=$TEST_TIMED_INDEX
 1060 TESTS_COUNT=$(($TESTS_NORMAL_COUNT + $TESTS_TIMED_COUNT))
 1061 TESTS_TIMED_REMAINING=$TEST_TIMED_INDEX
 1062 
 1063 #
 1064 # fd 7 is a /dev/null for quiet execution and stdout for default one
 1065 #
 1066 if [ -z "$QUIET" ]
 1067 then
 1068     exec 7>&1
 1069 else
 1070     exec 7>/dev/null
 1071 fi
 1072 
 1073 print_header() {
 1074     ( echo ======================================================================
 1075       echo Testsuite started at $(date "+%Y-%m-%d %T")
 1076       echo ----------------------------------------------------------------------
 1077       echo Total tests: $TESTS_COUNT
 1078       echo
 1079       for feature in COMMON_TESTS TIMED_TESTS SLOW_TESTS ERROREXIT_TESTS SERIAL_TESTS NETWORK_TESTS LIBXML2_TESTS LIBCURL_TESTS UNSAFE_TESTS STAGING_TESTS
 1080       do
 1081           if eval "[ \${$feature} != 0 ]"
 1082           then
 1083               printf '%20s: %s\n' $feature enabled
 1084           else
 1085               printf '%20s: %s\n' $feature disabled
 1086           fi
 1087       done
 1088       echo
 1089       if [ x$PARALLEL = x1 ]
 1090       then
 1091           echo "Test run is PARALLEL with MAKEFLAGS=$MAKEFLAGS"
 1092       else
 1093           echo "Test run is not parallel ${MAKEFLAGS:+(MAKEFLAGS=$MAKEFLAGS)}"
 1094       fi
 1095       echo
 1096     ) | tee "$LOG" | tee "$SUMMARY" >&7
 1097 }
 1098 
 1099 print_footer() {
 1100     # Recalculate results since sub invocations may not have been recorded.
 1101     PASSED_TESTS=`egrep '^Pass ' $SUMMARY | wc -l | tr -d ' '`
 1102     FAILED_TESTS=`egrep '^(FAIL|XFAIL) ' $SUMMARY | wc -l | tr -d ' '`
 1103     SUPPRESSED_FAILURES=`egrep '^XFAIL ' $SUMMARY | wc -l | tr -d ' '`
 1104     SOFT_FAILURES=`egrep '^SFAIL ' $SUMMARY | wc -l | tr -d ' '`
 1105     FLAKEY_FAILURES=`egrep '^FLAKEY ' $SUMMARY | wc -l | tr -d ' '`
 1106     SKIPPED_TESTS=`egrep '^Skip ' $SUMMARY | wc -l | tr -d ' '`
 1107 
 1108     ( echo
 1109       echo ======================================================================
 1110       echo "Testsuite finished at $(date  "+%F %T") ($(($END_TIME - $START_TIME)) seconds)"
 1111     ) | tee -a "$LOG" | tee -a "$SUMMARY" >&7
 1112 
 1113     ( echo
 1114       echo   "Passed tests:    $PASSED_TESTS"
 1115       printf "Failed tests:    $FAILED_TESTS"
 1116       if [ $SUPPRESSED_FAILURES -gt 0 ]
 1117       then
 1118           echo " ($SUPPRESSED_FAILURES are known and suppressed)"
 1119       else
 1120           echo
 1121       fi
 1122       echo   "Skipped tests:   $SKIPPED_TESTS"
 1123       echo   "Soft failures:   $SOFT_FAILURES"
 1124       echo   "Flakey failures: $FLAKEY_FAILURES"
 1125       echo   "Total tests:     $TESTS_COUNT"
 1126     ) | tee -a "$LOG" | tee -a "$SUMMARY"
 1127 
 1128     if [ -n "$PRINTLOG" ]
 1129     then
 1130         cat "$LOG"
 1131     fi
 1132 }
 1133 
 1134 finish_xml() {
 1135     mv "$XML" xml.tmp
 1136     (
 1137         cat <<EOF
 1138 <?xml version="1.0" encoding="UTF-8"?>
 1139 <testsuite name="$(pwd)"
 1140            timestamp="$(date "+%F %T")"
 1141            hostname="localhost"
 1142            tests="$TESTS_COUNT"
 1143            failures="$FAILED_TESTS"
 1144            skipped="$SKIPPED_TESTS"
 1145            time="$(($END_TIME - $START_TIME)) seconds">
 1146 EOF
 1147         cat xml.tmp
 1148         cat <<EOF
 1149 </testsuite>
 1150 EOF
 1151     ) > "$XML"
 1152     rm -f xml.tmp
 1153 }
 1154 
 1155 collect_results()
 1156 {
 1157     if [ -n "$STAY_IN_WORKDIR" ]
 1158     then
 1159         return 0
 1160     fi
 1161 
 1162     WORKDIR="$(workdir "$1")"
 1163     for file in "$LOG" "$SUMMARY" "$XML"
 1164     do
 1165         if [ -e "$WORKDIR/$file" ]
 1166         then
 1167             cat "$WORKDIR/$file" >> "$file"
 1168             if [ "$NO_CLEAN" = "0" ]
 1169             then
 1170                 # For unknown reason after the second pass, these file are root-owned, so we need GAINROOT here
 1171                 $GAINROOT rm -f "$WORKDIR/$file"
 1172             fi
 1173         fi
 1174     done
 1175     rmdir "$WORKDIR" >/dev/null 2>&1 || true
 1176 }
 1177 
 1178 check_and_run_timed_tests() {
 1179     TEST_TIMED_INDEX=0
 1180     time=$(unix_seconds)
 1181     # Run timed tests if any deadlines have expired.
 1182     while [ $TEST_TIMED_INDEX -lt $TESTS_TIMED_COUNT ]
 1183     do
 1184         eval test=\$TESTS_TIMED_$TEST_TIMED_INDEX
 1185         eval timeout=\$TESTS_TIMEOUT_$TEST_TIMED_INDEX
 1186         if [ -n "$timeout" ] && [ "$time" -ge "$timeout" ]
 1187         then
 1188             eval TESTS_PASSES_$TEST_TIMED_INDEX="\$((\$TESTS_PASSES_$TEST_TIMED_INDEX+1))"
 1189             eval pass=\$TESTS_PASSES_$TEST_TIMED_INDEX
 1190             runtest "$AGENT" "$test" "$pass" "TESTS_TIMEOUT_$TEST_TIMED_INDEX"
 1191             collect_results "$test"
 1192             eval timeout=\$TESTS_TIMEOUT_$TEST_TIMED_INDEX
 1193             if [ -z "$timeout" ]
 1194             then
 1195                 TESTS_TIMED_REMAINING=$(($TESTS_TIMED_REMAINING - 1))
 1196             fi
 1197         fi
 1198         TEST_TIMED_INDEX=$(($TEST_TIMED_INDEX+1))
 1199     done
 1200 }
 1201 
 1202 run_all_tests() {
 1203     TEST_INDEX=0
 1204     while [ $TEST_INDEX -lt $TESTS_NORMAL_COUNT -o $TESTS_TIMED_REMAINING -gt 0 ]
 1205     do
 1206         check_and_run_timed_tests
 1207 
 1208         # Run normal test.
 1209         if [ $TEST_INDEX -lt $TESTS_NORMAL_COUNT ]
 1210         then
 1211             eval test=\$TESTS_$TEST_INDEX
 1212             runtest "$AGENT" "$test"
 1213             collect_results "$test"
 1214             TEST_INDEX=$(($TEST_INDEX+1))
 1215         elif [ $TESTS_TIMED_REMAINING -gt 0 ]
 1216         then
 1217             sleep 1
 1218         fi
 1219     done
 1220 }
 1221 
 1222 
 1223 # TODO this function is currently creating a Makefile with all possible
 1224 #      tests, either they will run or be skipped. The proper way is to
 1225 #      do all the checks that now are in runtest(), and add the test
 1226 #      here only if it will run. This should be done higher up, where
 1227 #      ALL_TESTS is populated.
 1228 #
 1229 #      When this is done, there won't be a need to pass --tests=... down
 1230 #      the makefile subexecutions.
 1231 
 1232 run_all_tests_using_make() {
 1233     MAKEFILE=Makefile.testall
 1234 
 1235     TEST_BLOCKS=1
 1236     LAST_WAS_SERIAL=0
 1237 
 1238     for curr_test in $ALL_TESTS
 1239     do
 1240         # Keep serial tests after preceding tests and before following tests.
 1241         case "$curr_test" in
 1242             */serial_*|*_serial_*|*_serial.*|*/serial/*|*/unsafe/*)
 1243                 if [ $LAST_WAS_SERIAL = 0 ]
 1244                 then
 1245                     TEST_BLOCKS=$(($TEST_BLOCKS + 1))
 1246                     LAST_WAS_SERIAL=1
 1247                 fi
 1248                 ;;
 1249             *)
 1250                 if [ $LAST_WAS_SERIAL = 1 ]
 1251                 then
 1252                     TEST_BLOCKS=$(($TEST_BLOCKS + 1))
 1253                     LAST_WAS_SERIAL=0
 1254                 fi
 1255                 ;;
 1256         esac
 1257         case "$curr_test" in
 1258             */serial_*|*_serial_*|*_serial.*|*/serial/*|*/unsafe/*|*/timed/*|*/slow/*)
 1259                 eval MAKE_RECIPE_LIST$TEST_BLOCKS='$MAKE_RECIPE_LIST'$TEST_BLOCKS'${MAKE_RECIPE_LIST'$TEST_BLOCKS':+ }$curr_test'
 1260                 ;;
 1261             *)
 1262                 # Separate make target list (notice the "_rule"), because we do not want the target to be a
 1263                 # file name, since make will skip it if it exists (which of course it does).
 1264                 eval MAKE_TARGET_LIST$TEST_BLOCKS='$MAKE_TARGET_LIST'$TEST_BLOCKS'${MAKE_TARGET_LIST'$TEST_BLOCKS':+ }${curr_test}_rule'
 1265                 ;;
 1266         esac
 1267     done
 1268 
 1269     # Redirect to makefile all at once.
 1270     (
 1271         printf "tests:\n\n"
 1272         printf "parallel_block0:\n\n"
 1273         printf "serial_block0:\n\n"
 1274         printf "tests: serial_block0 parallel_block0\n\n"
 1275 
 1276         i=1
 1277         while [ $i -le $TEST_BLOCKS ]
 1278         do
 1279             if eval test -n '"$MAKE_RECIPE_LIST'$i'"'
 1280             then
 1281                 eval printf '"serial_block$i:\n\t@-$0 --stay-in-workdir -j1 ${TESTS_SELECTED:+--tests=$TESTS_SELECTED} $MAKE_RECIPE_LIST'$i'\n\n"'
 1282             fi
 1283             printf "serial_block$i: serial_block$(($i-1)) parallel_block$(($i-1))\n\n"
 1284             eval printf '"parallel_block$i: $MAKE_TARGET_LIST'$i'\n\n"'
 1285             if eval test -n '"$MAKE_TARGET_LIST'$i'"'
 1286             then
 1287                 eval printf '"$MAKE_TARGET_LIST'$i': serial_block$(($i-1)) parallel_block$(($i-1))\n\n"'
 1288             fi
 1289             printf "tests: serial_block$i parallel_block$i\n\n"
 1290 
 1291             i=$(($i + 1))
 1292         done
 1293 
 1294         printf "%%_rule: %%\n\t@-$0 --stay-in-workdir -j1 ${TESTS_SELECTED:+--tests=$TESTS_SELECTED} \$<\n\n"
 1295     ) > $MAKEFILE
 1296 
 1297     ${MAKE:-make} -k -f $MAKEFILE
 1298 
 1299     for curr_test in $ALL_TESTS
 1300     do
 1301         collect_results "$curr_test"
 1302     done
 1303 }
 1304 
 1305 
 1306 
 1307 #
 1308 # ========== RUN THE TESTS ==========
 1309 #
 1310 
 1311 # isEnabled? USE_INOTIFYWATCH
 1312 if [ -n "$USE_INOTIFYWATCH" ]
 1313 then
 1314     if which inotifywatch 2>&1 > /dev/null
 1315     then
 1316         mkdir -p "$BASE_WORKDIR" 2> /dev/null
 1317         inotifywatch ${INOTIFYWATCH_OPTS} $BASE_WORKDIR < /dev/null 2>&1 > ${INOTIFYWATCH_LOG} &
 1318         INOTIFYWATCH_PID=$!
 1319     else
 1320         echo "info: inotifywatch not detected."
 1321         exit 0
 1322     fi
 1323 fi
 1324 
 1325 # isEnabled? USE_INOTIFYWAIT
 1326 if [ -n "$USE_INOTIFYWAIT" ]
 1327 then
 1328     if which inotifywait 2>&1 > /dev/null
 1329     then
 1330         mkdir -p "$BASE_WORKDIR" 2> /dev/null
 1331         inotifywait ${INOTIFYWAIT_OPTS} $BASE_WORKDIR < /dev/null 2>&1 > ${INOTIFYWAIT_LOG} &
 1332         INOTIFYWAIT_PID=$!
 1333     else
 1334         echo "info: inotifywait not detected."
 1335         exit 0
 1336     fi
 1337 fi
 1338 
 1339 trap_handler() {
 1340     [ -n "$INOTIFYWATCH_PID" ] && kill -9 ${INOTIFYWATCH_PID} 2>&1 > /dev/null
 1341     [ -n "$INOTIFYWAIT_PID"  ] && kill -9 ${INOTIFYWAIT_PID}  2>&1 > /dev/null
 1342     exit 0
 1343 }
 1344 if [ -n "$USE_INOTIFYWATCH" -o -n "$USE_INOTIFYWAIT"  ]
 1345 then
 1346     trap trap_handler EXIT INT TERM
 1347 fi
 1348 
 1349 START_TIME=$(unix_seconds)
 1350 if [ -z "$STAY_IN_WORKDIR" ]
 1351 then
 1352     # This is top level invocation.
 1353     print_header
 1354     rm -f "$XML"
 1355 fi
 1356 if [ -z "$PARALLEL" -o "$TESTS_COUNT" -eq 1 ]
 1357 then
 1358     run_all_tests
 1359 else
 1360     run_all_tests_using_make
 1361 fi
 1362 END_TIME=$(unix_seconds)
 1363 if [ -z "$STAY_IN_WORKDIR" ]
 1364 then
 1365     # This is top level invocation.
 1366     print_footer
 1367     finish_xml
 1368 fi
 1369 
 1370 if [ $(($PASSED_TESTS+$FAILED_TESTS+$SOFT_FAILURES+$SKIPPED_TESTS+$FLAKEY_FAILURES)) -ne $TESTS_COUNT ]
 1371 then
 1372     echo "WARNING: Number of test results does not match number of tests!"
 1373     echo "Did something go wrong during execution?"
 1374     exit 2
 1375 fi
 1376 if [ "$FAILED_TESTS" -gt "$SUPPRESSED_FAILURES" ]
 1377 then
 1378     exit 1
 1379 elif [ "$FLAKEY_FAILURES" -gt "0" ] && [ -n "$FLAKEY_IS_FAIL" ]
 1380 then
 1381     exit 4
 1382 else
 1383     exit 0
 1384 fi