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