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