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