]> git.pond.sub.org Git - empserver/blob - tests/test-common.sh
tests: Simplify running cmp_out more than once
[empserver] / tests / test-common.sh
1 export EMPIREHOST=127.0.0.1 LOGNAME=tester
2
3 if [ -x ./config.status ] && ./config.status --version | grep -q '^Wolfpack Empire'
4 then :
5 else echo "$0: Must be run in root of build tree" >&2; exit 1
6 fi
7
8 test=${0##*/}
9 test=${test%-test}
10
11 failed=
12 trap 'if [ "$pid" ]; then kill -9 "$pid" 2>/dev/null || true; fi; [ "$failed" ] && exit 1' EXIT
13
14 # Abbreviations
15 testdir="$srcdir/tests/$test"
16 econfig=sandbox/etc/empire/econfig
17 schedule=sandbox/etc/empire/schedule
18 data=sandbox/var/empire
19 empdump="src/util/empdump -e $econfig"
20 files="src/util/files -e $econfig"
21 fairland="src/util/fairland -e $econfig"
22
23 empthread=`sed -n 's/empthread *:= *\(.*\)/\1/p' <GNUmakefile`
24 warn_empthread=y
25
26 check_empthread()
27 {
28     if [ "$warn_empthread" ] && [ "$empthread" != "LWP" ]
29     then
30         echo "Warning: test not expected to work with thread package $empthread!" >&2
31         warn_empthread=
32     fi
33 }
34
35 create_sandbox()
36 {
37     rm -rf sandbox
38     mkdir -p sandbox/etc/empire sandbox/share/empire/builtin $data
39     touch $schedule
40     cat >$econfig <<EOF
41 data "../../var/empire"
42 info "../../../../info.nr"
43 builtin "../../share/empire/builtin"
44 listen_addr "$EMPIREHOST"
45 keep_journal 2
46 GODNEWS 0
47 running_test_suite 1
48 EOF
49     cp `git ls-files "$srcdir"/src/lib/global | uniq | grep '\.config$'` sandbox/share/empire/builtin
50 }
51
52 now()
53 {
54     # date +%s isn't portable...
55     perl -e 'printf "%s\n", time'
56 }
57
58 start_server()
59 {
60     local pidfile=$data/server.pid
61     local timeout
62
63     #
64     # Currently expected to work only with thread package LWP,
65     # because:
66     #
67     # - Thread scheduling is reliably deterministic only with LWP
68     # - Shell builtin kill appears not to do the job in MinGW
69     # - The Windows server tries to run as service when -d isn't
70     #   specified
71     # - The Windows server does not implement -E crash-dump
72     #
73     # TODO address these shortcomings.
74     #
75     check_empthread
76
77     pid=
78     src/server/emp_server -e $econfig -R 1 -s -E crash-dump
79     timeout=$((`now`+5))
80     until pid=`cat $pidfile 2>/dev/null` && [ -n "$pid" ]
81     do
82         if [ `now` -gt $timeout ]
83         then
84             echo "Timed out waiting for server to create $pidfile" >&2
85             exit 1
86         fi
87     done
88     while src/client/empire red herring 2>&1 | grep -q "Connection refused"
89     do
90         if [ `now` -gt $timeout ]
91         then
92             echo "Timed out waiting for server to accept connections" >&2
93             exit 1
94         fi
95     done
96 }
97
98 stop_server()
99 {
100     local timeout
101     kill "$pid"
102     timeout=$((`now`+5))
103     while kill -0 "$pid" 2>/dev/null
104     do
105         if [ `now` -gt $timeout ]
106         then
107             echo "Timed out waiting for server to terminate" >&2
108             exit 1
109         fi
110     done
111 }
112
113 feed_input()
114 {
115     c=$1
116     r=$2
117     shift 2
118     sed '/^|/d' "$@" | src/client/empire "$c" "$r" >/dev/null
119 }
120
121 feed_files()
122 {
123     for i
124     do
125         c="${i##*/*-}"
126         r=`echo $c | sed 's/^POGO$/peter/'`
127         feed_input "$c" "$r" "$i"
128     done
129 }
130
131 feed_dir()
132 {
133     feed_files `git ls-files "$@" | uniq | grep '/[0-9][0-9]-[^/]*$'`
134 }
135
136 begin_test()
137 {
138     $files -f >/dev/null
139     local xd=
140     case "$1" in
141     *.xdump)
142         xd="$1"
143         $empdump -i "$xd"
144         shift
145         ;;
146     esac
147     if [ -z "$xd" ] || [ "$#" -ne 0 ]
148     then
149         cp -r $data/tel $data/empty.tel
150         start_server
151         if [ "$#" -eq 0 ]
152         then feed_input POGO peter
153         else feed_files "$@"
154         fi
155         echo 'edit c * ?tgms>0 t 0' | feed_input POGO peter
156         stop_server
157         mv $data/tel $data/setup.tel
158         mv $data/empty.tel $data/tel
159         mv $data/news $data/setup.news
160         >$data/news
161         mv $data/lostitems $data/setup.lostitems
162         >$data/lostitems
163         mv $data/journal.log $data/setup.journal.log
164         mv $data/server.log $data/setup.server.log
165     fi
166     start_server
167 }
168
169 end_test ()
170 {
171     stop_server
172     $empdump -x >sandbox/final.xdump
173 }
174
175 cmp_logs_xdump()
176 {
177     cmp_out var/empire/server.log var/empire/journal.log final.xdump
178 }
179
180 cmp_out()
181 {
182     local exp act nrm
183     for i
184     do
185         exp="$testdir/${i##*/}"
186         act="sandbox/$i"
187         nrm="sandbox/normalized-${i##*/}"
188         case "$i" in
189         */journal.log)
190             perl "$srcdir"/tests/normalize.pl -j "$act" ;;
191         */server.log)
192             perl "$srcdir"/tests/normalize.pl -s "$act" ;;
193         *.xdump)
194             perl "$srcdir"/tests/normalize.pl "$act" ;;
195         *)
196             perl -pe 's/\s+$/\n/;' "$act" ;;
197         esac >"$nrm"
198         if diff -u "$exp" "$nrm" >"$nrm.diff"
199         then
200             echo "$i OK"
201         elif [ "$EMPIRE_CHECK_ACCEPT" ]
202         then
203             echo "$i CHANGED"
204             cp "$nrm" "$exp"
205         else
206             failed=y
207             echo "$i FAIL"
208         fi
209     done
210 }