PAZPAR2_OVERRIDE_TEST=true makes test override .res-files
[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 PREFIX=$1
27 if test "x${PREFIX}" = "x"; then
28     echo Missing prefix for run_pazpar2.sh
29     exit 1
30 fi
31
32 # look for curl in PATH
33 oIFS=$IFS
34 IFS=:
35 curl=''
36 for p in $PATH; do
37     if test -x $p/curl; then
38         curl=$p/curl
39         break
40     fi
41 done
42 IFS=$oIFS
43
44 if test -z $curl; then
45     echo "curl not found. $PREFIX can not be tested"
46     exit 1
47 fi
48 GET='$curl --silent --output $OUT2 "$f"'
49 POST='$curl --silent --header "Content-Type: text/xml" --data-binary "@$postfile" --output $OUT2  "$f"'
50
51 if [ -z "$SKIP_PAZPAR2" ] ; then
52 # remove log if starting pazpar2
53     rm -f ${PREFIX}_pazpar2.log
54 fi
55
56 CFG=${PREFIX}.cfg
57 URLS=${PREFIX}.urls
58 VALGRINDLOG=${PREFIX}_valgrind.log
59
60 if test -n "$PAZPAR2_USE_VALGRIND"; then
61     valgrind --num-callers=30 --show-reachable=yes --leak-check=full --log-file=$VALGRINDLOG ../src/pazpar2 -X -l ${PREFIX}_pazpar2.log -f ${CFG} >${PREFIX}_extra_pazpar2.log 2>&1 &
62 elif test -n "$SKIP_PAZPAR2"; then 
63     echo "Skipping pazpar2. Must already be running with correct config!!! " 
64 else
65     YAZ_LOG=zoom,zoomdetails,debug,log,fatal ../src/pazpar2 -v loglevel,fatal,warn,log,debug,notime,zoom,zoomdetails -d -X -l ${PREFIX}_pazpar2.log -f ${srcdir}/${CFG} >${PREFIX}_extra_pazpar2.log 2>&1 &
66 fi
67
68 PP2PID=$!
69
70 if [ -z "$SKIP_PAZPAR2" ] ; then 
71     if ps -p $PP2PID >/dev/null 2>&1; then
72         (sleep $WAIT; kill_pazpar2 >/dev/null) &
73         SLEEP_PID=$!
74         trap kill_pazpar2 INT
75         trap kill_pazpar2 HUP
76         sleep 3
77     else
78         echo "pazpar2 failed to start"
79         exit 1
80     fi
81 fi
82
83 # Set to success by default.. Will be set to non-zero in case of failure
84 code=0
85
86 # We can start test for real
87 testno=1
88 for f in `cat ${srcdir}/${URLS}`; do
89     if echo $f | grep '^http' >/dev/null; then
90         OUT1=${srcdir}/${PREFIX}_${testno}.res
91         OUT2=${PREFIX}_${testno}.log
92         DIFF=${PREFIX}_${testno}.dif
93         rm -f $OUT2 $DIFF
94         if [ -n "$DEBUG" ] ; then 
95             echo "test $testno: $f" 
96         fi
97         if test -n "${postfile}"; then
98             eval $POST
99         else
100             eval $GET
101         fi
102         if test ! -f $OUT2; then
103             touch $OUT2
104         fi
105         if test -f $OUT1 -a -z "$PAZPAR2_OVERRIDE_TEST"; then
106             if diff $OUT1 $OUT2 >$DIFF; then
107                 rm $DIFF
108             else
109                 echo "Test $testno: Failed. See $OUT1, $OUT2 and $DIFF"
110                 echo "URL: $f"
111                 code=1
112             fi
113         else
114             echo "Test $testno: Making for the first time"
115             mv $OUT2 $OUT1
116             code=1
117         fi
118         testno=`expr $testno + 1`
119         postfile=
120     elif echo $f | grep '^[0-9]' >/dev/null; then
121         if [ -n "$DEBUG" ] ; then 
122             echo "Sleeping $f"
123         fi
124         sleep $f
125     else
126         if test -f $srcdir/$f; then
127             postfile=$srcdir/$f
128         else
129             echo "File $f does not exist"
130             code=1
131         fi
132     fi
133     if [ -z "$SKIP_PAZPAR2" ] ; then  
134         if ps -p $PP2PID >/dev/null 2>&1; then
135             :
136         else
137             IFS="$oIFS"
138             if test -n "$SLEEP_PID"; then
139                 echo "Test $testno: pazpar2 terminated (timeout, probably)"
140             else
141                 echo "Test $testno: pazpar2 died"
142             fi
143             exit 1
144         fi
145     fi
146 done
147
148 # Kill programs
149
150 if [ -z "$SKIP_PAZPAR2" ] ; then 
151     kill_pazpar2
152     sleep 2
153 fi
154
155 exit $code
156
157 # Local Variables:
158 # mode:shell-script
159 # sh-indentation: 2
160 # sh-basic-offset: 4
161 # End: