"Initial commit to Gerrit"
[profile/ivi/gpsd.git] / regress-driver
1 #!/bin/sh
2 #
3 # The regression-test driver script.  This used to be explicit in the
4 # makefile before we regrouped the regression tests by stable and unstable 
5 # drivers.
6
7 # Requires GNU date extensions
8 # Should return an empty blank string if tose are not present.
9 starttime=`date +"%s" 2>/dev/null`
10
11 # We need to have the build directory in $GPSD_HOME to find the new gpsd
12 if [ "`dirname $0`" = "." ]; then
13     GPSD_HOME=`pwd`
14 else
15     GPSD_HOME=`dirname $0`
16 fi
17
18 # Arrange to call a gpsfake in the source directory without fuss.
19 if [ -z ${PYTHON} ]; then
20     PYTHON="python"
21 fi
22 # For an explanation of what we define here, see the comment on
23 # PYTHON_DISTUTILS_LIBDIR/PYTHON_DISTUTILS_SCRIPTDIR in configure.ac
24 py_libdir=`pwd`/build/`${PYTHON} -c 'import distutils.util; import sys; print("lib.%s-%s" %(distutils.util.get_platform(), sys.version[0:3]))'`
25 py_scriptdir=`pwd`/build/`${PYTHON} -c 'import sys; print("scripts-%s" %(sys.version[0:3], ))'`
26 if [ -d ${py_libdir} ] && [ -d ${py_scriptdir} ]; then
27     PYTHONPATH=${py_libdir}
28     export PYTHONPATH
29
30     PATH=${py_scriptdir}:${PATH}
31 fi
32 export GPSD_HOME PATH
33
34 mode=regress
35 testing=daemon
36 opts=""
37 while getopts cstrbuvo opt
38 do
39     case $opt in
40         c) testing=clientlib ;; # Can be 'daemon'  
41         s) mode=regress ;;      # Run regression tests
42         t) opts="-b $opts" mode=regress ;;      # Run regression tests w/baton
43         r) mode=superraw ;;     # Run superraw regressions (r=2 mode)
44         b) mode=build ;;        # Rebuild regression check files
45         u) opts="$opts -u" ;;   # Force UDP
46         v) mode=view ;;         # View result of generating a check file
47         o) opts="$opts $OPTARG" ;;      # Pass options to gpsfake
48     esac
49 done
50 shift $(($OPTIND - 1))
51
52 # Enables us to turn debugging up high without screwing up the diff checks
53 # First and Second filter out gpsd log messages.
54 # Third filters out gps.py verbose loggging
55 # Fourth filters out WATCH responses
56 # Fifth filters out DEVICE responses
57 # Sixth filters out VERSION responses
58 # Seventh filters out device fields
59 GPSFILTER="sed -e /^gpsd:/d -e /^gpsfake/d -e /GPS-DATA/d -e /WATCH/d -e /DEVICE/d -e /VERSION/d -e s/,\"device\":[^,}]*//"
60
61 # Use ALTFILTER to set up custom filtering when a regression test fails
62 # This example filters out altitude and some fields computed by gpsd's error 
63 # modeling - these are fragile in the prsence of changes to the fix-buffering 
64 # logic. Note that as the last attribute mode needs to be handled differently.
65 #ALTFILTER="-e s/\"alt\":[^,]*,// -e s/\"ep[vhs]\":[-+0-9.]*// -e s/,\"mode\":[^}]*//"
66
67 TMP=/tmp
68
69 # Only twirl the baton on a tty, avoids junk in transcripts. 
70 if [ -t 1 ]
71 then
72     opts="$opts -b"
73 fi
74
75 case $mode in
76     regress)
77         echo "Testing the $testing..." >&2
78         errors=0; total=0; notfound=0;
79         for f in $*; do
80             if [ -r $f.chk ]
81             then
82                 trap 'rm -f ${TMP}/test-$$.chk; exit $errors' EXIT HUP INT TERM
83                 case $testing in
84                 daemon) gpsfake -s 38400 -1 -p $opts ${f} | ${GPSFILTER} ${ALTFILTER} >${TMP}/test-$$.chk ;;
85                 clientlib) $GPSD_HOME/libgps -b <${f} >${TMP}/test-$$.chk ;;
86                 esac
87                 if [ "${ALTFILTER}" ]
88                 then
89                     trap 'rm -f ${TMP}/test-$$.chk ${TMP}/testout-$$.chk ${TMP}/testin-$$.chk ${TMP}/diff-$$; exit $errors' EXIT HUP INT TERM
90                     sed -n <${f}.chk >${TMP}/testin-$$.chk ${ALTFILTER} -e 'p';
91                     sed -n <${TMP}/test-$$.chk >${TMP}/testout-$$.chk ${ALTFILTER} -e 'p';
92                     diff -ub ${TMP}/testin-$$.chk ${TMP}/testout-$$.chk >${TMP}/diff-$$;
93                 else
94                     diff -ub ${f}.chk ${TMP}/test-$$.chk >${TMP}/diff-$$;
95                 fi
96                 if test -s ${TMP}/diff-$$ ; then
97                     errors=`expr $errors + 1`;
98                     cat ${TMP}/diff-$$
99                 fi;
100                 rm -f ${TMP}/test-$$.chk ${TMP}/testout-$$.chk ${TMP}/testin-$$.chk ${TMP}/diff-$$
101             else
102                 echo "*** No check log $f.chk exists"
103                 notfound=`expr $notfound + 1`;
104             fi
105             total=`expr $total + 1`;
106         done; 
107         if test $errors -gt 0; then
108             echo "Regression test FAILED: $errors errors in $total tests total  ($notfound not found).";
109             status=1;
110         else
111             echo "Regression test successful: no errors in $total tests ($notfound not found).";
112             status=0;
113         fi
114         ;;
115     superraw)
116         echo "Testing super-raw mode..." >&2
117         for f in $*; do
118             gpsfake -s 38400 -1 -p $opts -r '{"class":"WATCH","enable":False,"raw":2}' $opts ${f} \
119             | ./devtools/striplog -1 >${TMP}/test1-$$.chk;
120             ./devtools/striplog <${f} >${TMP}/test2-$$.chk;
121             cmp ${TMP}/test[12]-$$.chk;
122         done; rm ${TMP}/test[12]-$$.chk
123         ;;
124     build)
125         echo "Rebuilding $testing regressions..." >&2
126         for f in $*; do
127             case $testing in 
128             daemon) gpsfake -s 38400 -1 -p $opts ${f} | ${GPSFILTER} >${f}.chk;;
129             clientlib) $GPSD_HOME/libgps -b <${f} >${f}.chk ;;
130             esac
131         done
132         status=0
133         ;;
134     view)
135         echo "Viewing..." >&2
136         for f in $*; do
137             case $testing in 
138             daemon) gpsfake -s 38400 -1 -p $opts ${f} | ${GPSFILTER} ;;
139             clientlib) $GPSD_HOME/libgps -b <${f} ;;
140             esac
141         done
142         status=0
143         ;;
144 esac
145
146 # See starttime above
147 endtime=`date +"%s" 2>/dev/null`
148
149 if [ "$starttime" -a "$endtime" ]
150 then
151     echo -n "Elapsed time: " 
152     echo "scale=2; ${endtime} - ${starttime}" | bc
153 fi
154
155 exit $status