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