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