091c8022aef1b9b16b32b99b91c4ac3afc02a26d
[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 `uname` = "Linux"; then
61     sec=0.3
62     maxrounds=20
63 else
64     sec=1
65     maxrounds=10
66 fi
67 LEVELS=loglevel,fatal,warn,log,debug,notime,zoom,zoomdetails
68 if test -n "$PAZPAR2_USE_VALGRIND"; then
69     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 &
70     PP2PID=$!
71     sleep 6
72 elif test -n "$SKIP_PAZPAR2"; then
73     echo "Skipping pazpar2. Must already be running with correct config!!! "
74 else
75     ../src/pazpar2 -v $LEVELS -d -X -l ${PREFIX}_pazpar2.log -f ${srcdir}/${CFG} >${PREFIX}_extra_pazpar2.log 2>&1 &
76     PP2PID=$!
77     sleep 2
78 fi
79
80 if [ -z "$SKIP_PAZPAR2" -a -z "$WAIT_PAZPAR2" ] ; then
81     if ps -p $PP2PID >/dev/null 2>&1; then
82         (sleep $WAIT; kill_pazpar2 >/dev/null) &
83         SLEEP_PID=$!
84         trap kill_pazpar2 INT
85         trap kill_pazpar2 HUP
86     else
87         echo "pazpar2 failed to start"
88         exit 1
89     fi
90 fi
91
92 # Set to success by default.. Will be set to non-zero in case of failure
93 code=0
94
95 # We can start test for real
96 testno=1
97 rounds=1
98 for f in `cat ${srcdir}/${URLS}`; do
99     if echo $f | grep '^http' >/dev/null; then
100         OUT1=${srcdir}/${PREFIX}_${testno}.res
101         OUT2=${PREFIX}_${testno}.log
102         DIFF=${PREFIX}_${testno}.dif
103         rm -f $OUT2 $DIFF
104         if [ -n "$DEBUG" ] ; then
105             echo "test $testno: $f"
106         fi
107         while test $rounds -gt 0; do
108             if test -n "${postfile}"; then
109                 eval $POST
110             else
111                 eval $GET
112             fi
113             if test ! -f $OUT2; then
114                 touch $OUT2
115             fi
116             rounds=`expr $rounds - 1`
117             if test -f $OUT1 -a -z "$PAZPAR2_OVERRIDE_TEST"; then
118                 if diff $OUT1 $OUT2 >$DIFF; then
119                     rm $DIFF
120                     rm $OUT2
121                     rounds=0
122                 else
123                     if test $rounds -eq 0; then
124                         echo "Test $testno: Failed. See $OUT1, $OUT2 and $DIFF"
125                         echo "URL: $f"
126                         code=1
127                     fi
128                 fi
129             else
130                 if test $rounds -eq 0; then
131                     echo "Test $testno: Making for the first time"
132                     mv $OUT2 $OUT1
133                     code=1
134                 fi
135             fi
136             if test $rounds -gt 0; then
137                 sleep $sec
138             fi
139         done
140         testno=`expr $testno + 1`
141         postfile=
142         rounds=1
143     elif echo $f | grep '^[0-9]' >/dev/null; then
144         rounds=$maxrounds
145     else
146         if test -f $srcdir/$f; then
147             postfile=$srcdir/$f
148         else
149             echo "File $f does not exist"
150             code=1
151         fi
152     fi
153     if [ -z "$SKIP_PAZPAR2" ] ; then
154         if ps -p $PP2PID >/dev/null 2>&1; then
155             :
156         else
157             IFS="$oIFS"
158             if test -n "$SLEEP_PID"; then
159                 echo "Test $testno: pazpar2 terminated (timeout, probably)"
160             else
161                 echo "Test $testno: pazpar2 died"
162             fi
163             exit 1
164         fi
165     fi
166 done
167
168 if [ "$WAIT_PAZPAR2" ] ; then
169     i=0
170     while test $i -lt $WAIT_PAZPAR2; do
171         i=`expr $i + 1`
172         echo -n "$i."
173         sleep 60
174     done
175     echo "done"
176 fi
177
178 # Kill programs
179
180 if [ -z "$SKIP_PAZPAR2" ] ; then
181     kill_pazpar2
182     sleep 2
183 fi
184
185 exit $code
186
187 # Local Variables:
188 # mode:shell-script
189 # sh-indentation: 2
190 # sh-basic-offset: 4
191 # End: