tests: poll for stable results
[pazpar2-moved-to-github.git] / test / run_pazpar2.sh
1 #!/bin/sh
2 #
3 # Regression test using pazpar2 against z3950.indexdata.com/marc or gils
4 # Reads Pazpar2 URLs from $1
5 #            Outputs to $1_<no>.log
6 #            Matches against results in $1_<no>.res
7 # Requires curl
8
9 # srcdir might be set by make
10 srcdir=${srcdir:-"."}
11
12 # terminate pazpar2 if test takes more than this (in seconds)
13 WAIT=120
14
15 kill_pazpar2()
16 {
17     if test -n "$PP2PID"; then
18         kill $PP2PID
19     fi
20     if test -n "$SLEEP_PID"; then
21         kill $SLEEP_PID
22         SLEEP_PID=""
23     fi
24 }
25
26 PREFIX=$1
27 if test "x${PREFIX}" = "x"; then
28     echo Missing prefix for run_pazpar2.sh
29     exit 1
30 fi
31
32 # look for curl in PATH
33 oIFS=$IFS
34 IFS=:
35 curl=''
36 for p in $PATH; do
37     if test -x $p/curl; then
38         curl=$p/curl
39         break
40     fi
41 done
42 IFS=$oIFS
43
44 if test -z $curl; then
45     echo "curl not found. $PREFIX can not be tested"
46     exit 1
47 fi
48 GET='$curl --silent --output $OUT2 "$f"'
49 POST='$curl --silent --header "Content-Type: text/xml" --data-binary "@$postfile" --output $OUT2  "$f"'
50
51 if [ -z "$SKIP_PAZPAR2" ] ; then
52 # remove log if starting pazpar2
53     rm -f ${PREFIX}_pazpar2.log
54 fi
55
56 CFG=${PREFIX}.cfg
57 URLS=${PREFIX}.urls
58 VALGRINDLOG=${PREFIX}_valgrind.log
59
60 if test `uname` = "Linux"; then
61     sec=0.3
62     maxrounds=20
63 else
64     sec=1
65     maxrounds=10
66 fi
67
68 if test -n "$PAZPAR2_USE_VALGRIND"; then
69     valgrind --num-callers=30 --show-reachable=yes --leak-check=full --log-file=$VALGRINDLOG ../src/pazpar2 -X -l ${PREFIX}_pazpar2.log -f ${CFG} >${PREFIX}_extra_pazpar2.log 2>&1 &
70 elif test -n "$SKIP_PAZPAR2"; then
71     echo "Skipping pazpar2. Must already be running with correct config!!! "
72 else
73     YAZ_LOG=zoom,zoomdetails,debug,log,fatal ../src/pazpar2 -v loglevel,fatal,warn,log,debug,notime,zoom,zoomdetails -d -X -l ${PREFIX}_pazpar2.log -f ${srcdir}/${CFG} >${PREFIX}_extra_pazpar2.log 2>&1 &
74 fi
75
76 PP2PID=$!
77
78 if [ -z "$SKIP_PAZPAR2" ] ; then
79     if ps -p $PP2PID >/dev/null 2>&1; then
80         (sleep $WAIT; kill_pazpar2 >/dev/null) &
81         SLEEP_PID=$!
82         trap kill_pazpar2 INT
83         trap kill_pazpar2 HUP
84         sleep 3
85     else
86         echo "pazpar2 failed to start"
87         exit 1
88     fi
89 fi
90
91 # Set to success by default.. Will be set to non-zero in case of failure
92 code=0
93
94 # We can start test for real
95 testno=1
96 rounds=1
97 for f in `cat ${srcdir}/${URLS}`; do
98     if echo $f | grep '^http' >/dev/null; then
99         OUT1=${srcdir}/${PREFIX}_${testno}.res
100         OUT2=${PREFIX}_${testno}.log
101         DIFF=${PREFIX}_${testno}.dif
102         rm -f $OUT2 $DIFF
103         if [ -n "$DEBUG" ] ; then
104             echo "test $testno: $f"
105         fi
106         while test $rounds -gt 0; do
107             if test -n "${postfile}"; then
108                 eval $POST
109             else
110                 eval $GET
111             fi
112             if test ! -f $OUT2; then
113                 touch $OUT2
114             fi
115             rounds=`expr $rounds - 1`
116             if test -f $OUT1 -a -z "$PAZPAR2_OVERRIDE_TEST"; then
117                 if diff $OUT1 $OUT2 >$DIFF; then
118                     rm $DIFF
119                     rm $OUT2
120                     rounds=0
121                 else
122                     if test $rounds -eq 0; then
123                         echo "Test $testno: Failed. See $OUT1, $OUT2 and $DIFF"
124                         echo "URL: $f"
125                         code=1
126                     fi
127                 fi
128             else
129                 if test $rounds -eq 0; then
130                     echo "Test $testno: Making for the first time"
131                     mv $OUT2 $OUT1
132                     code=1
133                 fi
134             fi
135             if test $rounds -gt 0; then
136                 sleep $sec
137             fi
138         done
139         testno=`expr $testno + 1`
140         postfile=
141         rounds=1
142     elif echo $f | grep '^[0-9]' >/dev/null; then
143         rounds=$maxrounds
144     else
145         if test -f $srcdir/$f; then
146             postfile=$srcdir/$f
147         else
148             echo "File $f does not exist"
149             code=1
150         fi
151     fi
152     if [ -z "$SKIP_PAZPAR2" ] ; then
153         if ps -p $PP2PID >/dev/null 2>&1; then
154             :
155         else
156             IFS="$oIFS"
157             if test -n "$SLEEP_PID"; then
158                 echo "Test $testno: pazpar2 terminated (timeout, probably)"
159             else
160                 echo "Test $testno: pazpar2 died"
161             fi
162             exit 1
163         fi
164     fi
165 done
166
167 # Kill programs
168
169 if [ -z "$SKIP_PAZPAR2" ] ; then
170     kill_pazpar2
171     sleep 2
172 fi
173
174 exit $code
175
176 # Local Variables:
177 # mode:shell-script
178 # sh-indentation: 2
179 # sh-basic-offset: 4
180 # End: