chiark / gitweb /
9f1de64290f86f6a7fbc743eb2133d7d28406a7f
[autopkgtest.git] / runner / adt-testreport-onepackage
1 #!/bin/bash
2
3 set -e
4
5 mirror=http://mirror.relativity.greenend.org.uk/mirror/ubuntu.good
6 distro=feisty
7 suite=main
8 salt=''
9 tmp=tmp
10 var=var
11 administrator_email=ian@davenant.greenend.org.uk
12 maintainer_email_override="$administrator_email"
13 salutation="Ian"
14 from="$salutation Jackson <ian@davenant.greenend.org.uk>"
15 rsync=rsync
16 disable=true
17 interactive=true
18 target=source
19 arch=`dpkg --print-architecture`
20
21 for config in "$@"; do
22         case "$config" in
23         *=*)    eval "$config"  ;;
24         *)      . "$config"     ;;
25         esac
26 done
27
28 if $disable; then
29         echo >&2 'disabled because config inadequate (no disable=false)'
30         exit 1
31 fi
32
33 : ${destdirtail:=$distro-$target}
34 : ${destdirfin:="$destdircommon$destdirtail"}
35
36 case $target in
37 source)
38         sources=Sources
39         descx=''
40         ;;
41 binary-*)
42         sources=Packages
43         descx="${target#binary-}"
44         ;;
45 *)
46         echo >&2 'target must be source or binary-*'
47         exit 1
48 esac
49
50 exec 3>&1
51 printf >&3 "starting "
52
53 rm -rf "$tmp"
54 mkdir "$tmp"
55
56 >"$tmp"/_log
57
58 if $interactive; then
59         echo '(log diverted to stdout)' >>"$tmp"/_log
60 else
61         exec >>"$tmp"/_log
62 fi
63 exec 4>&1
64
65 progress () {
66         echo "++++++ $1 ++++++"
67 }
68
69 gurl () {
70         progress "fetching $1"
71         curl -fsS "$1" >"$2"
72 }
73
74 gurl "$mirror/dists/$distro/$suite/$target/$sources.gz" "$tmp"/_$sources.gz
75 zcat "$tmp"/_$sources.gz >"$tmp"/_$sources-in
76
77 lastinfo="$var"/lastinfo-$target
78
79 now=`date +%s`
80 >>"$lastinfo"
81
82 progress selecting
83
84 if [ "x$pkg" = x ]; then
85  pkg="`perl -e '
86         use IO::Handle;
87
88         $pre= "[-+.0-9a-z]+";
89         $vre= "[-+.0-9a-zA-Z:~]+";
90
91         sub f1() { $fn=shift @ARGV; open F, $fn or die "$fn $!"; }
92         sub f2() { F->error and die "$fn $!"; close F or die "$fn $!"; }
93
94         f1();
95         while (<F>) {
96                 die unless m/^($pre) ($vre) (\d+)( .*)?$/;
97                 $lastver{$1}= $2;
98                 $lasttime{$1}= $3;
99                 $extras{$1}= $4." ";
100         }
101         f2();
102         f1();
103         $best_score= -1;
104         sub scorepackage () {
105                 return if $skip;
106                 return if $score < $best_score
107                      or ($score==$best_score and \
108                          $package gt $best_package);
109                 $best_score= $score;
110                 $best_package= $package;
111         }
112         sub endpackage () {
113                 return unless (defined $package
114                                 or defined $version
115                                 or defined $skip);
116                 die unless defined $package;
117                 die unless defined $version;
118                 scorepackage();
119                 undef $package;
120                 undef $version;
121                 undef $skip;
122         }
123         while (<F>) {
124                 if (m/^Package: ($pre)$/) {
125                         die if defined $package;
126                         $package= $1;
127                 } elsif (m/^Version: ($vre)$/) {
128                         die if defined $version;
129                         $version= $1;
130                         $score= '$now' - $lasttime{$package};
131                         $score= 1e7 if $score>1e7;
132                         $score *= 5 if $lastver{$package} ne $version;
133                         $score *= 10 unless $extras{$package} =~ m/ nt /;
134                 } elsif (m/^Architecture:.*/ &&
135                          !m/\s'$arch'\s/) {
136                         $skip= 1;
137                 } elsif (m/^$/) {
138                         endpackage();
139                 }
140         }
141         f2();
142         endpackage();
143         die unless length $best_package;
144         open L, ">&4" or die $!;
145         printf L "selected %s (age %s, score %d)\n",
146                 $best_package,
147                 exists($lastime{$best_package})
148                 ? '$now' - $lasttime{$best_package}
149                 : "<never-yet>",
150                 $best_score;
151         print "$best_package\n" or die $!;
152  ' "$lastinfo" "$tmp"/_$sources-in`"
153 else
154         printf >&4 "package forced: %s\n" "$pkg"
155 fi
156
157 sed -n "/^Package: $pkg\$/,/^\$/p" \
158  <"$tmp"/_$sources-in >"$tmp"/_this-stanza
159
160 echo
161 cat "$tmp"/_this-stanza
162
163 getfield () {
164         eval 'p'$1'="`
165                 sed -n '\''s/^'$1': //p'\'' \
166                  <"$tmp"/_this-stanza
167         `"'
168 }
169
170 printf >&3 "selected \"%s\" " $pkg
171
172 tp="$tmp/$pkg"
173 mkdir "$tp" "$tp/src" "$tp/tmp" "$tp/out"
174
175 getfield Version
176
177 if test $target = source; then
178         getfield Directory
179         leafnames="`
180                 sed -n '/^Files:/,/^([^ ].*)?$/{ /^ /{
181                         s/^ [0-9a-z][0-9a-z]*  *[0-9][0-9]* //; p
182                         }}' \
183                  <"$tmp"/_this-stanza
184         `"
185         for leafname in $leafnames; do
186                 df="$tp/src/$leafname"
187                 case "$leafname" in
188                 */*|.*) echo >&2 "bad leafname: $leafname"; exit 1;;
189                 *.dsc) fot="$df";;
190                 esac
191                 gurl "$mirror/$pDirectory/$leafname" "$df"
192         done
193         testmode=--source
194         testmode2=''
195         desc="$pkg"
196         : ${upload_if_ok:=true}
197 else
198         getfield Filename
199         fot="$tp/src/$pkg.deb"
200         gurl "$mirror/$pFilename" "$fot"
201         testmode='--binaries=install --binary'
202         testmode2=--instantiate
203         desc="$pkg $descx"
204         : ${upload_if_ok:=false}
205 fi
206
207 if [ "x$maintainer_email_override" = x ]; then
208         getfield Maintainer
209         maintainer_email=pMaintainer
210 else
211         maintainer_email=maintainer_email_override
212 fi
213
214 printf >&3 "adt-run "
215
216 progress "starting test"
217
218 xrc () {
219         printf "+ %s\n" "$*"
220         set +e
221         "$@"
222         rc=$?
223         set -e
224 }
225
226 echo 'fatal: adt-run did not start properly' >"$tmp"/_summary
227
228 xrc adt-run --tmp-dir "$tp"/tmp                         \
229         --output-dir "$tp"/out                          \
230         --log-file "$tp"/log                            \
231         --summary "$tmp"/_summary                       \
232         $adtrun_extra_opts                              \
233         $testmode "$fot" $testmode2                     \
234  ---                                                    \
235  adt-virt-xenlvm                                        \
236         $adtvirt_extra_opts                             \
237  --                                                     \
238         --distro="$distro"                              \
239  2>&1 3>&- 4>&-
240
241 printf >&3 "%s " $rc
242
243 ourx=0
244 upload=true
245 extras=''
246
247 case "$rc" in
248 0)      summary='all OK';                       email=''
249                                         upload=$upload_if_ok            ;;
250 2)      summary='OK (some skipped)';            email=''
251                                         upload=$upload_if_ok            ;;
252 8)      summary='package declares no tests';    email=''
253                                         upload=false; extras='nt'       ;;
254 4|6)    summary='test(s) failed!';      email="$maintainer_email"       ;;
255 12)     summary='erroneous package!';   email="$maintainer_email"       ;;
256 16)     summary='testbed failed!';      email="administrator_email"     ;;
257 *)      summary='unexpected failure!';  email="administrator_email"; ourx=20;;
258 esac
259
260 progress "RESULTS $summary"
261
262 if $upload; then
263         progress "bundling"
264         ln "$tmp"/_summary "$tp"/summary
265
266         for odir in tmp out; do
267                 if test -d "$tp"/$odir; then
268                         GZIP=-2 tar -f "$tp"/$odir.tar.gz -C "$tp" -zc $odir
269                         rm -r "$tp"/$odir
270                 fi
271         done
272
273         progress "uploading"
274         printf >&3 "uploading"
275         $rsync -rltH --safe-links --delete "$tp" "$destrsynchead/$destdirfin/"
276         printf >&3 " "
277 fi
278
279 if [ "x$email" != x ]; then
280         progress "contacting $email"
281         eval "email_addr=\$$email"
282         printf >&3 "email \"%s\" " "$email_addr"
283         cat >"$tmp"/_email <<END
284 From: $from
285 To: $email_addr
286 Subject: autopkgtest $distro $desc: $summary
287
288  Test executed for:  $distro  $target  $pkg
289  Outcome: $summary
290 END
291         sed -e 's/^/  /' "$tmp"/_summary >>"$tmp"/_email
292         cat >>"$tmp"/_email <<END
293
294 This message is automatically generated by the autopkgtest package
295 testing system.  You are receiving it because:
296 END
297         case "$email" in
298                 pMaintainer)
299                         cat >>"$tmp"/_email <<END
300  You are listed in the Maintainer field of the $pkg package in $distro
301   and the test results appear to indicate a problem with the package.
302 END
303                         ;;
304                 maintainer_email_override)
305                         cat >>"$tmp"/_email <<END
306  The test results appear to indicate a problem with the package
307   and reports for package maintainers for $distro are being directed to
308   $maintainer_email_override
309 END
310                         ;;
311                 administrator_email)
312                         cat >>"$tmp"/_email <<END
313  You are the administrator for the autopkgtest installation.
314 END
315                         ;;
316                 *)
317                         echo >&2 "huh email $email is what why?"
318                         exit 1
319                         ;;
320         esac
321         cat >>"$tmp/_email" <<END
322
323 The test log, which is intended to be sufficient to diagnose most
324 failures, can be found below.  However, in case this is not
325 sufficient, another copy can be found along with output files, saved
326 temporary files, and so on, at:
327  $desthttphead/$destdirfin/
328
329 If you have any questions about this service please contact me at:
330  $from
331
332 Regards,
333 $salutation
334
335 -8<-
336 END
337 fi
338
339 printf >>"$var"/log "%s=%s rc=%s emailed='%s'\n" \
340         "$target" "$pkg" $rc "$email_addr"
341
342 if [ "x$ourx" = x0 ]; then
343         sed -e "/^$pkg /d" <"$lastinfo" >"$lastinfo".new
344         printf "%s %s %s %s\n" "$pkg" "$pVersion" "$now" "$extras" \
345                 >>"$lastinfo".new
346         mv "$lastinfo".new "$lastinfo"
347         progress "tested."
348 else
349         progress "fault ($ourx)."
350 fi
351
352 if [ "x$email" = x ]; then
353         if $interactive; then
354                 cat "$tmp"/_log >&2
355         fi
356 else
357         cat >>"$tmp"/_email 2>&1 "$tmp"/_log ||:
358         if $interactive; then
359                 cat "$tmp"/_email >&2
360         else
361                 sendmail -odi -oem -t -oi <"$tmp"/_email
362         fi
363 fi
364
365 printf >&3 "done %s.\n" $ourx
366 exit $ourx