[25978] | 1 | #!/bin/sh
|
---|
| 2 | ### Copyright 2010 Manuel Carrasco Moñino. (manolo at apache.org)
|
---|
| 3 | ###
|
---|
| 4 | ### Licensed under the Apache License, Version 2.0.
|
---|
| 5 | ### You may obtain a copy of it at
|
---|
| 6 | ### http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 7 |
|
---|
| 8 | ###
|
---|
| 9 | ### A library for shell scripts which creates reports in jUnit format.
|
---|
| 10 | ### These reports can be used in Hudson, or any other CI.
|
---|
| 11 | ###
|
---|
| 12 | ### Usage:
|
---|
| 13 | ### - Include this file in your shell script
|
---|
| 14 | ### - Use juLog to call your command any time you want to produce a new report
|
---|
| 15 | ### Usage: juLog <options> command arguments
|
---|
| 16 | ### options:
|
---|
| 17 | ### -name="TestName" : the test name which will be shown in the junit report
|
---|
| 18 | ### -error="RegExp" : a regexp which sets the test as failure when the output matches it
|
---|
| 19 | ### -ierror="RegExp" : same as -error but case insensitive
|
---|
| 20 | ### - Junit reports are left in the folder 'result' under the directory where the script is executed.
|
---|
| 21 | ### - Configure hudson to parse junit files from the generated folder
|
---|
| 22 | ###
|
---|
| 23 |
|
---|
| 24 | asserts=00; errors=0; total=0; content=""
|
---|
| 25 |
|
---|
| 26 | # create output folder
|
---|
| 27 | juDIR=`pwd`/results
|
---|
| 28 | mkdir -p $juDIR || exit
|
---|
| 29 |
|
---|
| 30 | # The name of the suite is calculated based in your script name
|
---|
| 31 | suite=`basename $0 | sed -e 's/.sh$//' | tr "." "_"`
|
---|
| 32 |
|
---|
| 33 | # A wrapper for the eval method witch allows catching seg-faults and use tee
|
---|
| 34 | errfile=/tmp/evErr.$$.log
|
---|
| 35 | eVal() {
|
---|
| 36 | eval "$1"
|
---|
| 37 | echo $? | tr -d "\n" >$errfile
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | # Method to clean old tests
|
---|
| 41 | juLogClean() {
|
---|
| 42 | echo "+++ Removing old junit reports from: $juDIR "
|
---|
| 43 | rm -f $juDIR/TEST-*
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | # Execute a command and record its results
|
---|
| 47 | juLog() {
|
---|
| 48 |
|
---|
| 49 | # parse arguments
|
---|
| 50 | ya=""; icase=""; testname="";
|
---|
| 51 | while [ -z "$ya" ]; do
|
---|
| 52 | case "$1" in
|
---|
| 53 | -name=*) name=$asserts-`echo "$1" | sed -e 's/-name=//'`; shift;;
|
---|
| 54 | -ierror=*) ereg=`echo "$1" | sed -e 's/-ierror=//'`; icase="-i"; shift;;
|
---|
| 55 | -error=*) ereg=`echo "$1" | sed -e 's/-error=//'`; shift;;
|
---|
| 56 | -test=*) testname=`echo "$1" | sed -e 's/-test=//'`; shift;;
|
---|
| 57 | *) ya=1;;
|
---|
| 58 | esac
|
---|
| 59 | done
|
---|
| 60 |
|
---|
| 61 | # use first arg as name if it was not given
|
---|
| 62 | if [ -z "$name" ]; then
|
---|
| 63 | name="$asserts-$1"
|
---|
| 64 | shift
|
---|
| 65 | fi
|
---|
| 66 |
|
---|
| 67 | # calculate command to eval
|
---|
| 68 | [ -z "$1" ] && return
|
---|
| 69 | cmd="$1"; shift
|
---|
| 70 | while [ -n "$1" ]
|
---|
| 71 | do
|
---|
| 72 | cmd="$cmd \"$1\""
|
---|
| 73 | shift
|
---|
| 74 | done
|
---|
| 75 |
|
---|
| 76 | # eval the command sending output to a file
|
---|
| 77 | outf=/var/tmp/ju$$.txt
|
---|
| 78 | >$outf
|
---|
| 79 | echo "" | tee -a $outf
|
---|
| 80 | echo "+++ Running case: $testname " | tee -a $outf
|
---|
| 81 | echo "+++ working dir: "`pwd` | tee -a $outf
|
---|
| 82 | #echo "+++ command: $cmd" | tee -a $outf
|
---|
| 83 | ini=`date +%s`
|
---|
| 84 | eVal "$cmd" 2>&1 | tee -a $outf
|
---|
| 85 | evErr=`cat $errfile`
|
---|
| 86 | rm -f $errfile
|
---|
| 87 | end=`date +%s`
|
---|
| 88 | echo "+++ exit code: $evErr" | tee -a $outf
|
---|
| 89 |
|
---|
| 90 | # set the appropriate error, based in the exit code and the regex
|
---|
| 91 | [ $evErr != 0 ] && err=1 || err=0
|
---|
| 92 | out=`cat $outf | sed -e 's/^\([^+]\)/| \1/g'`
|
---|
| 93 | if [ $err = 0 -a -n "$ereg" ]; then
|
---|
| 94 | H=`echo "$out" | egrep $icase "$ereg"`
|
---|
| 95 | [ -n "$H" ] && err=1
|
---|
| 96 | fi
|
---|
| 97 | echo "+++ error: $err" | tee -a $outf
|
---|
| 98 | rm -f $outf
|
---|
| 99 |
|
---|
| 100 | # calculate vars
|
---|
| 101 | asserts=`expr $asserts + 1`
|
---|
| 102 | asserts=`printf "%.2d" $asserts`
|
---|
| 103 | errors=`expr $errors + $err`
|
---|
| 104 | time=`expr $end - $ini`
|
---|
| 105 | total=`expr $total + $time`
|
---|
| 106 |
|
---|
| 107 | # write the junit xml report
|
---|
| 108 | ## failure tag
|
---|
| 109 | [ $err = 0 ] && failure="" || failure="
|
---|
| 110 | <failure type=\"ScriptError\" message=\"Script Error\"></failure>
|
---|
| 111 | "
|
---|
| 112 | ## testcase tag
|
---|
| 113 | content="$content
|
---|
| 114 | <testcase assertions=\"1\" name=\"$testname $ereg\" time=\"$time\">
|
---|
| 115 | $failure
|
---|
| 116 | <system-out>
|
---|
| 117 | <![CDATA[
|
---|
| 118 | $out
|
---|
| 119 | ]]>
|
---|
| 120 | </system-out>
|
---|
| 121 | </testcase>
|
---|
| 122 | "
|
---|
| 123 | ## testsuite block
|
---|
| 124 | cat <<EOF >$juDIR/TEST-$suite.xml
|
---|
| 125 | <testsuite failures="0" assertions="$assertions" name="ISSM Test Suite" tests="1" errors="$errors" time="$total">
|
---|
| 126 | $content
|
---|
| 127 | </testsuite>
|
---|
| 128 | EOF
|
---|
| 129 |
|
---|
| 130 | }
|
---|
| 131 |
|
---|