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