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