Merge branch 'master' into paz-927
[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 -z "$SKIP_PAZPAR2" -a -n "$PP2PID"; then
18         kill $PP2PID
19         PP2PID=""
20         rm -f pazpar2.pid
21     fi
22     if test -f ztest.pid; then
23         kill `cat ztest.pid`
24         rm -f ztest.pid
25     fi
26     if test -n "$SLEEP_PID"; then
27         kill $SLEEP_PID
28         SLEEP_PID=""
29     fi
30 }
31
32 ztest=false
33 icu=false
34 while test $# -gt 0; do
35     case "$1" in
36         -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
37         *) optarg= ;;
38     esac
39     case $1 in
40         --ztest)
41             ztest=true
42             ;;
43         --icu)
44             icu=true
45             ;;
46         -*)
47             echo "Bad option $1"
48             exit 1
49             ;;
50         *)
51             PREFIX=$1
52             ;;
53     esac
54     shift
55 done
56
57 if test "x${PREFIX}" = "x"; then
58     echo Missing prefix for run_pazpar2.sh
59     exit 1
60 fi
61
62 # look for curl in PATH
63 oIFS=$IFS
64 IFS=:
65 curl=''
66 for p in $PATH; do
67     if test -x $p/curl; then
68         curl=$p/curl
69         break
70     fi
71 done
72 IFS=$oIFS
73
74 if test -z $curl; then
75     echo "$PREFIX: curl not found"
76     exit 1
77 fi
78
79 if test "$icu" = "true"; then
80     if ../src/pazpar2 -V |grep icu:enabled >/dev/null; then
81         :
82     else
83         echo "Skipping ${PREFIX}: ICU support unavailable"
84         exit 0
85     fi
86 fi
87
88 if test "$ztest" = "true" ; then
89     YAZ_ZTEST_XML_FETCH=${srcdir}/${PREFIX}.
90     export YAZ_ZTEST_XML_FETCH
91
92     oIFS=$IFS
93     IFS=:
94     F=''
95     for p in $PATH; do
96         if test -x $p/yaz-ztest -a -x $p/yaz-client; then
97             VERSION=`$p/yaz-client -V 2>/dev/null|awk '{print $3;}'|awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
98             if test -n "$VERSION"; then
99                 if test $VERSION -ge 4002052; then
100                     F=$p/yaz-ztest
101                     break
102                 fi
103             fi
104         fi
105     done
106     if test -z "$F"; then
107         for p in ${srcdir}/../../yaz ${srcdir}/../../yaz-*; do
108             if test -x $p/ztest/yaz-ztest; then
109                 VERSION=`$p/ztest/yaz-ztest -V 2>/dev/null|awk '{print $3;}'|awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 1000 + $2) * 1000 + $3;}'`
110                 if test -n "$VERSION"; then
111                     if test "$VERSION" -ge 4002052; then
112                         F=$p/ztest/yaz-ztest
113                         break
114                     fi
115                 fi
116             fi
117         done
118     fi
119     IFS=$oIFS
120     if test -z "$F"; then
121         echo "Skipping ${PREFIX}: recent yaz-ztest not found"
122         exit 0
123     fi
124     rm -f ztest.pid
125     rm -f ${PREFIX}_ztest.log
126     $F -l ${PREFIX}_ztest.log -p ztest.pid -D tcp:localhost:9999
127     sleep 1
128     if test ! -f ztest.pid; then
129         echo "yaz-ztest could not be started"
130         exit 0
131     fi
132 fi
133
134 # remove log if starting pazpar2
135 if [ -z "$SKIP_PAZPAR2" ] ; then
136     rm -f ${PREFIX}_pazpar2.log
137 fi
138
139 CFG=${PREFIX}.cfg
140 URLS=${PREFIX}.urls
141 VALGRINDLOG=${PREFIX}_valgrind.log
142
143 if test `uname` = "Linux"; then
144     sec=0.3
145     maxrounds=20
146 else
147     sec=1
148     maxrounds=10
149 fi
150 LEVELS=loglevel,fatal,warn,log,debug,zoom,zoomdetails
151 if test "$PERF_PROG"; then
152     eval $PERF_PROG ../src/pazpar2 -p pazpar2.pid -X -l ${PREFIX}_pazpar2.log -f ${srcdir}/${CFG} >${PREFIX}_extra_pazpar2.log 2>&1 &
153     PP2PID=$!
154     sleep 5
155     if test -f pazpar2.pid; then
156         PP2PID=`cat pazpar2.pid`
157         echo "Got PID $PP2PID"
158     fi
159 elif test -n "$PAZPAR2_USE_VALGRIND"; then
160     valgrind --num-callers=30 --show-reachable=yes --leak-check=full --log-file=$VALGRINDLOG ../src/pazpar2 -v $LEVELS -X -l ${PREFIX}_pazpar2.log -f ${CFG} >${PREFIX}_extra_pazpar2.log 2>&1 &
161     PP2PID=$!
162     sleep 0.01
163     WAIT=400
164 elif test -n "$SKIP_PAZPAR2"; then
165     echo "${PREFIX}: not starting Pazpar2 (should be running already)"
166 else
167     ../src/pazpar2 -v $LEVELS -d -X -l ${PREFIX}_pazpar2.log -f ${srcdir}/${CFG} >${PREFIX}_extra_pazpar2.log 2>&1 &
168     PP2PID=$!
169 fi
170
171 if [ -z "$SKIP_PAZPAR2" -a -z "$WAIT_PAZPAR2" ] ; then
172     if ps -p $PP2PID >/dev/null 2>&1; then
173         (sleep $WAIT; kill_pazpar2 >/dev/null) &
174         SLEEP_PID=$!
175         trap kill_pazpar2 INT
176         trap kill_pazpar2 HUP
177     else
178         echo "${PREFIX}: pazpar2 failed to start"
179         if test -f ztest.pid; then
180             kill `cat ztest.pid`
181             rm -f ztest.pid
182         fi
183         exit 1
184     fi
185 fi
186
187 GET='$curl --silent --output $OUT2 "$f"'
188 POST='$curl --silent --header "Content-Type: text/xml" --data-binary "@$postfile" --output $OUT2  "$f"'
189
190 # Set to success by default.. Will be set to non-zero in case of failure
191 code=0
192
193 # We can start test for real
194 testno=1
195 # set rounds to "wait" because pazpar2 might not be ready
196 rounds=$maxrounds
197 for f in `cat ${srcdir}/${URLS}`; do
198     if echo $f | grep '^http' >/dev/null; then
199         OUT1=${srcdir}/${PREFIX}_${testno}.res
200         OUT2=${PREFIX}_${testno}.log
201         DIFF=${PREFIX}_${testno}.dif
202         rm -f $OUT2 $DIFF
203         if [ -n "$DEBUG" ] ; then
204             echo "test $testno: $f"
205         fi
206         while test $rounds -gt 0; do
207             if test -n "${postfile}"; then
208                 eval $POST
209             else
210                 eval $GET
211             fi
212             if test ! -f $OUT2; then
213                 touch $OUT2
214             fi
215             rounds=`expr $rounds - 1`
216             if test -f $OUT1 -a -z "$PAZPAR2_OVERRIDE_TEST"; then
217                 if diff $OUT1 $OUT2 >$DIFF 2>/dev/null; then
218                     rm $DIFF
219                     rm $OUT2
220                     rounds=0
221                 else
222                     if test $rounds -eq 0; then
223                         echo "${PREFIX} $testno: Failed. See $OUT1, $OUT2 and $DIFF"
224                         echo "URL: $f"
225                         code=1
226                     fi
227                 fi
228             else
229                 if test $testno -eq 1 -o $rounds -eq 0; then
230                     echo "${PREFIX} $testno: Making for the first time"
231                     mv $OUT2 $OUT1
232                     code=1
233                     rounds=0
234                 fi
235             fi
236             if test $rounds -gt 0; then
237                 sleep $sec
238             fi
239         done
240         testno=`expr $testno + 1`
241         postfile=
242         rounds=1
243     elif echo $f | grep '^w' >/dev/null; then
244         rounds=$maxrounds
245     elif echo $f | grep '^[0-9]' >/dev/null; then
246         sleep $f
247         rounds=1
248     else
249         if test -f $srcdir/$f; then
250             postfile=$srcdir/$f
251         else
252             echo "File $f does not exist"
253             code=1
254         fi
255     fi
256     if [ -z "$SKIP_PAZPAR2" ] ; then
257         if ps -p $PP2PID >/dev/null 2>&1; then
258             :
259         else
260             IFS="$oIFS"
261             if test -n "$SLEEP_PID"; then
262                 echo "${PREFIX} $testno: pazpar2 terminated (timeout, probably)"
263             else
264                 echo "${PREFIX} $testno: pazpar2 died"
265             fi
266             PP2PID=""
267             code=1
268             break
269         fi
270     fi
271 done
272
273 if [ "$WAIT_PAZPAR2" ] ; then
274     i=0
275     while test $i -lt $WAIT_PAZPAR2; do
276         i=`expr $i + 1`
277         echo -n "$i."
278         sleep 60
279     done
280     echo "done"
281 fi
282 kill_pazpar2
283 sleep 2
284 if test "$PERF_PROG"; then
285     tail -3 ${PREFIX}_extra_pazpar2.log
286 fi
287 exit $code
288
289 # Local Variables:
290 # mode:shell-script
291 # sh-indentation: 2
292 # sh-basic-offset: 4
293 # End: