8ada79a89234fcd61a0efffedcdc5e2af7e79983
[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 "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     WAIT=400
136 elif test -n "$SKIP_PAZPAR2"; then
137     echo "Test ${PREFIX}: not starting Pazpar2 (should be running already)"
138 else
139     ../src/pazpar2 -v $LEVELS -d -X -l ${PREFIX}_pazpar2.log -f ${srcdir}/${CFG} >${PREFIX}_extra_pazpar2.log 2>&1 &
140     PP2PID=$!
141     sleep 2
142 fi
143
144 if [ -z "$SKIP_PAZPAR2" -a -z "$WAIT_PAZPAR2" ] ; then
145     if ps -p $PP2PID >/dev/null 2>&1; then
146         (sleep $WAIT; kill_pazpar2 >/dev/null) &
147         SLEEP_PID=$!
148         trap kill_pazpar2 INT
149         trap kill_pazpar2 HUP
150     else
151         echo "Test ${PREFIX}: pazpar2 failed to start"
152         if test -f ztest.pid; then
153             kill `cat ztest.pid`
154             rm -f ztest.pid
155         fi
156         exit 1
157     fi
158 fi
159
160 GET='$curl --silent --output $OUT2 "$f"'
161 POST='$curl --silent --header "Content-Type: text/xml" --data-binary "@$postfile" --output $OUT2  "$f"'
162
163 # Set to success by default.. Will be set to non-zero in case of failure
164 code=0
165
166 # We can start test for real
167 testno=1
168 rounds=1
169 for f in `cat ${srcdir}/${URLS}`; do
170     if echo $f | grep '^http' >/dev/null; then
171         OUT1=${srcdir}/${PREFIX}_${testno}.res
172         OUT2=${PREFIX}_${testno}.log
173         DIFF=${PREFIX}_${testno}.dif
174         rm -f $OUT2 $DIFF
175         if [ -n "$DEBUG" ] ; then
176             echo "test $testno: $f"
177         fi
178         while test $rounds -gt 0; do
179             if test -n "${postfile}"; then
180                 eval $POST
181             else
182                 eval $GET
183             fi
184             if test ! -f $OUT2; then
185                 touch $OUT2
186             fi
187             rounds=`expr $rounds - 1`
188             if test -f $OUT1 -a -z "$PAZPAR2_OVERRIDE_TEST"; then
189                 if diff $OUT1 $OUT2 >$DIFF 2>/dev/null; then
190                     rm $DIFF
191                     rm $OUT2
192                     rounds=0
193                 else
194                     if test $rounds -eq 0; then
195                         echo "Test $testno: Failed. See $OUT1, $OUT2 and $DIFF"
196                         echo "URL: $f"
197                         code=1
198                     fi
199                 fi
200             else
201                 if test $rounds -eq 0; then
202                     echo "Test $testno: Making for the first time"
203                     mv $OUT2 $OUT1
204                     code=1
205                 fi
206             fi
207             if test $rounds -gt 0; then
208                 sleep $sec
209             fi
210         done
211         testno=`expr $testno + 1`
212         postfile=
213         rounds=1
214     elif echo $f | grep '^[0-9]' >/dev/null; then
215         rounds=$maxrounds
216     else
217         if test -f $srcdir/$f; then
218             postfile=$srcdir/$f
219         else
220             echo "File $f does not exist"
221             code=1
222         fi
223     fi
224     if [ -z "$SKIP_PAZPAR2" ] ; then
225         if ps -p $PP2PID >/dev/null 2>&1; then
226             :
227         else
228             IFS="$oIFS"
229             if test -n "$SLEEP_PID"; then
230                 echo "Test $testno: pazpar2 terminated (timeout, probably)"
231             else
232                 echo "Test $testno: pazpar2 died"
233             fi
234             exit 1
235         fi
236     fi
237 done
238
239 if [ "$WAIT_PAZPAR2" ] ; then
240     i=0
241     while test $i -lt $WAIT_PAZPAR2; do
242         i=`expr $i + 1`
243         echo -n "$i."
244         sleep 60
245     done
246     echo "done"
247 fi
248 # Kill programs
249 if test -f ztest.pid; then
250     kill `cat ztest.pid`
251     rm -f ztest.pid
252 fi
253
254 if [ -z "$SKIP_PAZPAR2" ] ; then
255     kill_pazpar2
256     sleep 2
257 fi
258 exit $code
259
260 # Local Variables:
261 # mode:shell-script
262 # sh-indentation: 2
263 # sh-basic-offset: 4
264 # End: