remove new result if same
[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                 rm $OUT2
109             else
110                 echo "Test $testno: Failed. See $OUT1, $OUT2 and $DIFF"
111                 echo "URL: $f"
112                 code=1
113             fi
114         else
115             echo "Test $testno: Making for the first time"
116             mv $OUT2 $OUT1
117             code=1
118         fi
119         testno=`expr $testno + 1`
120         postfile=
121     elif echo $f | grep '^[0-9]' >/dev/null; then
122         if [ -n "$DEBUG" ] ; then 
123             echo "Sleeping $f"
124         fi
125         sleep $f
126     else
127         if test -f $srcdir/$f; then
128             postfile=$srcdir/$f
129         else
130             echo "File $f does not exist"
131             code=1
132         fi
133     fi
134     if [ -z "$SKIP_PAZPAR2" ] ; then  
135         if ps -p $PP2PID >/dev/null 2>&1; then
136             :
137         else
138             IFS="$oIFS"
139             if test -n "$SLEEP_PID"; then
140                 echo "Test $testno: pazpar2 terminated (timeout, probably)"
141             else
142                 echo "Test $testno: pazpar2 died"
143             fi
144             exit 1
145         fi
146     fi
147 done
148
149 # Kill programs
150
151 if [ -z "$SKIP_PAZPAR2" ] ; then 
152     kill_pazpar2
153     sleep 2
154 fi
155
156 exit $code
157
158 # Local Variables:
159 # mode:shell-script
160 # sh-indentation: 2
161 # sh-basic-offset: 4
162 # End: