chiark / gitweb /
merge new features and fixes from development branch
[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 extras=''
239
240 case "$rc" in
241 0)      summary='all OK';                       email=''
242                                         upload=$upload_if_ok            ;;
243 2)      summary='OK (some skipped)';            email=''
244                                         upload=$upload_if_ok            ;;
245 8)      summary='package declares no tests';    email=''
246                                         upload=false; extras='nt'       ;;
247 4|6)    summary='test(s) failed!';      email="$maintainer_email"       ;;
248 12)     summary='erroneous package!';   email="$maintainer_email"       ;;
249 16)     summary='testbed failed!';      email="administrator_email"     ;;
250 *)      summary='unexpected failure!';  email="administrator_email"; ourx=20;;
251 esac
252
253 progress "RESULTS $summary"
254
255 if $upload; then
256         progress "bundling"
257         ln "$tmp"/_summary "$tp"/summary
258
259         for odir in tmp out; do
260                 if test -d "$tp"/$odir; then
261                         GZIP=-2 tar -f "$tp"/$odir.tar.gz -C "$tp" -zc $odir
262                         rm -r "$tp"/$odir
263                 fi
264         done
265
266         progress "uploading"
267         printf >&3 "uploading"
268         $rsync -rltH --safe-links --delete "$tp" "$destrsynchead/$destdirfin/"
269         printf >&3 " "
270 fi
271
272 if [ "x$email" != x ]; then
273         progress "contacting $email"
274         eval "email_addr=\$$email"
275         printf >&3 "email \"%s\" " "$email_addr"
276         cat >"$tmp"/_email <<END
277 From: $from
278 To: $email_addr
279 Subject: autopkgtest $distro $desc: $summary
280
281  Test executed for:  $distro  $target  $pkg
282  Outcome: $summary
283 END
284         sed -e 's/^/  /' "$tmp"/_summary >>"$tmp"/_email
285         cat >>"$tmp"/_email <<END
286
287 This message is automatically generated by the autopkgtest package
288 testing system.  You are receiving it because:
289 END
290         case "$email" in
291                 pMaintainer)
292                         cat >>"$tmp"/_email <<END
293  You are listed in the Maintainer field of the $pkg package in $distro
294   and the test results appear to indicate a problem with the package.
295 END
296                         ;;
297                 maintainer_email_override)
298                         cat >>"$tmp"/_email <<END
299  The test results appear to indicate a problem with the package
300   and reports for package maintainers for $distro are being directed to
301   $maintainer_email_override
302 END
303                         ;;
304                 administrator_email)
305                         cat >>"$tmp"/_email <<END
306  You are the administrator for the autopkgtest installation.
307 END
308                         ;;
309                 *)
310                         echo >&2 "huh email $email is what why?"
311                         exit 1
312                         ;;
313         esac
314         cat >>"$tmp/_email" <<END
315
316 The test log, which is intended to be sufficient to diagnose most
317 failures, can be found below.  However, in case this is not
318 sufficient, another copy can be found along with output files, saved
319 temporary files, and so on, at:
320  $desthttphead/$destdirfin/
321
322 If you have any questions about this service please contact me at:
323  $from
324
325 Regards,
326 $salutation
327
328 -8<-
329 END
330 fi
331
332 printf >>"$var"/log "%s=%s rc=%s emailed='%s'\n" \
333         "$target" "$pkg" $rc "$email_addr"
334
335 if [ "x$ourx" = x0 ]; then
336         sed -e "/^$pkg /d" <"$lastinfo" >"$lastinfo".new
337         printf "%s %s %s %s\n" "$pkg" "$pVersion" "$now" "$extras" \
338                 >>"$lastinfo".new
339         mv "$lastinfo".new "$lastinfo"
340         progress "tested."
341 else
342         progress "fault ($ourx)."
343 fi
344
345 if [ "x$email" = x ]; then
346         if $interactive; then
347                 cat "$tmp"/_log >&2
348         fi
349 else
350         cat >>"$tmp"/_email 2>&1 "$tmp"/_log ||:
351         if $interactive; then
352                 cat "$tmp"/_email >&2
353         else
354                 sendmail -odi -oem -t -oi <"$tmp"/_email
355         fi
356 fi
357
358 printf >&3 "done %s.\n" $ourx
359 exit $ourx