chiark / gitweb /
Test suite: Print better info on failures
[dgit.git] / tests / lib
1 #
2
3 exec 2>&1
4 set -x
5 set -o pipefail
6
7 . tests/lib-core
8 . tests/lib-restricts
9
10 t-report-failure () {
11         set +x
12         rc=$1
13         cat <<END >&2
14 TEST FAILED
15 funcs: ${FUNCNAME[*]}
16 lines: ${BASH_LINENO[*]}
17 files: ${BASH_SOURCE[*]}
18 END
19         exit 16
20 }
21
22 trap 'test $? = 0 || t-report-failure' EXIT
23
24 t-set-intree
25
26 : ${DGIT_TEST_DEBUG=-D}
27 export DGIT_TEST_DEBUG
28
29 export GIT_COMMITTER_DATE='1440253867 +0100'
30 export GIT_AUTHOR_DATE='1440253867 +0100'
31
32 root=`pwd`
33 troot=$root/tests
34 testname="${DGIT_TEST_TESTNAME-${0##*/}}"
35
36 tmp=$ADTTMP
37 if [ x"$tmp" = x ]; then
38         mkdir -p tests/tmp
39         tmp=tests/tmp/$testname
40         rm -rf $tmp
41         mkdir $tmp
42 fi
43 cd $tmp
44
45 tmp=`pwd`
46
47 t-set-using-tmp
48
49 ln -f $troot/ssh ssh
50
51 mkdir -p $tmp/gnupg
52 cp $troot/gnupg/* $tmp/gnupg
53 chmod go-rw $tmp/gnupg/*
54
55 export DEBCHANGE_VENDOR=dpkg
56
57 mkdir -p $tmp/incoming
58 cat <<END >$tmp/dput.cf
59 [test-dummy]
60 method                  = local
61 incoming                = $tmp/incoming
62 run_dinstall            = 0
63 END
64
65 t-git-next-date () {
66         GIT_COMMITTER_DATE="$(( ${GIT_COMMITTER_DATE%% *} + 1 )) ${GIT_COMMITTER_DATE#* }"
67         GIT_AUTHOR_DATE="$GIT_COMMITTER_DATE"
68 }
69
70 t-expect-fail () {
71         local mpat="$1"; shift
72
73         set +o pipefail
74         LC_MESSAGES=C "$@" 2>&1 | tee $tmp/t.output
75         local ps="${PIPESTATUS[*]}"
76         set -o pipefail
77
78         case $ps in
79         "0 0")  fail "command unexpectedly succeeded (instead of: $mpat)" ;;
80         *" 0")  ;;
81         *)      fail "tee failed"  ;;
82         esac
83
84         t-grep-mpat "$mpat" $tmp/t.output
85 }
86
87 t-grep-mpat () {
88         local mpat="$1"
89         local file="$2"
90
91         local grepper=fgrep
92         case "$mpat" in
93         [A-Z]:*)
94                 case "$mpat" in
95                 E:*)    grepper=egrep   ;;
96                 F:*)    grepper=fgrep   ;;
97                 *)      fail "bad mpat prefix in $mpat";;
98                 esac
99                 mpat=${mpat#[A-Z]:}
100                 ;;
101         esac
102
103         $grepper -e "$mpat" "$file" ||
104                 fail "message not found"
105 }
106
107 t-expect-push-fail () {
108         local mpat="$1"; shift
109
110         local triedpush=`git rev-parse HEAD`
111
112         t-reporefs pre-push
113         t-expect-fail "$mpat"  "$@"
114         t-reporefs post-push
115         diff $tmp/show-refs.{pre,post}-push
116
117         t-git-objects-not-present '' $triedpush
118
119         eval "$t_expect_push_fail_hook"
120 }
121
122 t-git-objects-not-present () {
123         # t-git-objects-not-present GITDIR|'' OBJID [...]
124         # specifying '' means the repo for package $p
125         local gitdir="${1-$dgitrepo}"
126         local obj
127         if ! [ -e "$gitdir" ]; then return; fi
128         for obj in "$@"; do
129                 GIT_DIR=$gitdir \
130                 t-expect-fail 'unable to find' \
131                 git cat-file -t $obj
132         done
133 }
134
135 t-reporefs () {
136         local whichoutput=$1; shift
137         local whichrepo=${1-$dgitrepo}
138         local outputfile="$tmp/show-refs.$whichoutput"
139         (set -e
140          exec >"$outputfile"
141          if test -d $whichrepo; then
142                 cd $whichrepo
143                 git show-ref |sort
144         fi)
145 }
146
147 t-untar () {
148         local tarfile=$1.tar
149         local edittree=$1.edit
150         if test -d "$edittree"; then
151                 cp -a "$edittree"/* .
152         else
153                 tar xf "$tarfile"
154         fi
155 }
156
157 t-worktree () {
158         rm -rf $p
159         t-untar $troot/worktrees/${p}_$1
160 }
161
162 t-select-package () {
163         p=$1
164         dgitrepo=$tmp/git/$p.git
165 }
166
167 t-git () {
168         t-select-package $1
169         v=$2
170         mkdir -p $tmp/git
171         local gs=$troot/git-srcs/${p}_$v.git
172         (set -e; cd $tmp/git; t-untar $gs)
173 }
174
175 t-git-none () {
176         mkdir -p $tmp/git
177         (set -e; cd $tmp/git; tar xf $troot/git-template.tar)
178 }
179
180 t-git-merge-base () {
181         git merge-base $1 $2 || test $? = 1
182 }
183
184 t-has-ancestor () {
185         local now=`git rev-parse HEAD`
186         local ancestor=`git rev-parse $1^{}`
187         local mbase=`t-git-merge-base $ancestor $now`
188         if [ x$mbase != x$ancestor ]; then
189                 fail "not ff $ancestor..$now, $mbase != $ancestor"
190         fi
191 }
192
193 t-prep-newpackage () {
194         t-select-package $1
195         v=$2
196         t-archive-none $p
197         t-git-none
198         t-worktree $v
199         cd $p
200         if ! git show-ref --verify --quiet refs/heads/master; then
201                 git branch -m dgit/sid master
202                 git remote rm dgit
203         fi
204         cd ..
205 }
206
207 t-archive-none () {
208         t-select-package $1
209         mkdir -p $tmp/aq $tmp/mirror/pool/main
210
211         local suite=sid
212
213         >$tmp/aq/package.$suite.$p
214         t-archive-updated $suite $p
215
216         >$tmp/aq/package.new.$p
217         t-archive-updated new $p
218
219         ln -s sid $tmp/aq/dsc_in_suite/unstable
220         cat <<'END' >$tmp/aq/suites
221 [
222    {
223       "archive" : "ftp-master",
224       "codename" : "sid",
225       "components" : [
226          "main",
227          "contrib",
228          "non-free"
229       ],
230       "name" : "unstable",
231       "dakname" : "unstable"
232    }
233 ]
234 END
235 }
236
237 t-archive-updated () {
238         local suite=$1
239         local p=$2
240         local suitedir=$tmp/aq/dsc_in_suite/$suite
241         mkdir -p $suitedir
242         perl <$tmp/aq/package.$suite.$p >$suitedir/$p -wne '
243                 use JSON;
244                 use strict;
245                 our @v;
246                 m{^(\S+) (\w+) ([^ \t/]+)/(\S+)} or die;
247                 push @v, {
248                         "version" => "$1",
249                         "sha256sum" => "$2",
250                         "component" => "$3",
251                         "filename" => "$4",
252                 };
253                 END {
254                         print to_json \@v or die $!;
255                 }
256         '
257 }
258
259 t-archive-process-incoming () {
260         local suite=$1
261         mv $tmp/incoming/${p}_${v}[._]* $tmp/mirror/pool/main/
262         t-archive-query "$suite"
263 }
264
265 t-archive-query () {
266         local suite=${1-sid}
267         local dscf=main/${p}_${v}.dsc
268         local sha=`sha256sum <$tmp/mirror/pool/$dscf`
269         echo "${v} ${sha%  -} $dscf" >>$tmp/aq/package.$suite.${p}
270         t-archive-updated $suite $p
271 }
272
273 t-archive () {
274         t-archive-none $1
275         v=$2
276         local dscf=${p}_$2.dsc
277         rm -f $tmp/mirror/pool/main/${p}_*
278         ln $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/pool/main/
279         t-archive-query
280         rm -rf $tmp/extract
281         mkdir $tmp/extract
282         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/pool/main/$dscf)
283 }
284
285 t-git-dir-time-passes () {
286         touch -d 'last year' $dgitrepo
287 }
288
289 t-git-dir-check () {
290         local gitdir=$dgitrepo
291         case "$1" in
292         enoent)
293                 if test -e "$gitdir"; then fail "$gitdir exists"; fi
294                 return
295                 ;;
296         public) wantstat='7[75]5' ;;
297         secret) wantstat='7[70]0' ;;
298         *)      fail "$1 t-git-dir-check ?" ;;
299         esac
300         gotstat=`stat -c%a $gitdir`
301         case "$gotstat" in
302         *$wantstat) return ;;
303         *)      fail "$gitdir has mode $gotstat, expected $wantstat" ;;
304         esac
305 }
306
307 t-rm-dput-dropping () {
308         rm -f $tmp/${p}_${v}_*.upload
309 }
310
311 t-dgit () {
312         local dgit=${DGIT_TEST-dgit}
313         pwd
314         : '
315 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
316         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
317                 -dtest-dummy $DGIT_TEST_OPTS $DGIT_TEST_DEBUG \
318                 -k39B13D8A "$@"
319         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
320 '
321 }
322
323 t-diff-nogit () {
324         diff --exclude=.git --exclude=.pc -ruN $*
325 }
326
327 t-files-notexist () {
328         local f
329         for f in "$@"; do
330                 if [ -e $f ]; then
331                         fail "$f exists!"
332                 fi
333         done
334 }
335
336 t-cloned-fetched-good () {
337         t-diff-nogit ../extract/$p-${v%-*} .
338         t-clean-on-branch dgit/sid
339         t-refs-same-start
340         t-refs-same \
341                 refs/heads/dgit/sid \
342                 refs/remotes/dgit/dgit/sid
343         t-refs-notexist refs/dgit/unstable refs/remotes/dgit/dgit/unstable
344 }
345
346 t-output () {
347         printf "%s\n" "$1" >$tmp/t.want
348         shift
349         "$@" >$tmp/t.got
350         diff $tmp/t.want $tmp/t.got
351 }
352
353 t-clean-on-branch () {
354         t-output "## $1" git status -b --porcelain
355 }
356
357 t-git-get-ref-exact () {
358         local ref=$1
359         # does not dereference, unlike t-git-get-ref
360         case "$ref" in
361         refs/*) ;;
362         *) fail "t-git-get-ref-exact bad $ref" ;;
363         esac
364         git for-each-ref --format='%(objectname)' "[r]efs/${ref#refs/}"
365 }
366
367 t-git-get-ref () {
368         local ref=$1
369         case "$ref" in
370         refs/*) ;;
371         *) fail "t-git-get-ref bad $ref" ;;
372         esac
373         (git show-ref -d $1 || test $? = 1) | perl -ne '
374                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
375                 END { print "$x\n" if length $x; }
376         '
377 }
378
379 t-ref-same-exact () {
380         local name="$1"
381         local val=`t-git-get-ref-exact $name`
382         t-ref-same-val "$name" $val
383 }
384
385 t-ref-same () {
386         local name="$1"
387         local val=`t-git-get-ref $name`
388         t-ref-same-val "$name" $val
389 }
390
391 t-ref-head () {
392         local val=`git rev-parse HEAD`
393         t-ref-same-val HEAD $val
394 }
395
396 t-ref-same-val () {
397         local name="$1"
398         local val=$2
399         case "$t_ref_val" in
400         '')             ;;
401         "$val")         ;;
402         *)              fail "ref varies: $name: $val != $t_ref_val" ;;
403         esac
404         t_ref_val="$val"
405 }
406
407 t-refs-same-start () {
408         t_ref_val=''
409 }
410
411 t-refs-same () {
412         local g
413         for g in $*; do
414                 t-ref-same $g
415         done
416 }
417
418 t-refs-notexist () {
419         local val
420         for g in $*; do
421                 val=`t-git-get-ref $g`
422                 if [ "x$val" != x ]; then
423                         fail "ref $g unexpectedly exists ($val)"
424                 fi
425         done
426 }
427
428 t-v-tag () {
429         echo refs/tags/test-dummy/${v//\~/_}
430 }
431
432 t-check-pushed-master () {
433         local master=`t-git-get-ref refs/heads/master`
434         if [ x$master = x$t_ref_val ]; then return; fi
435         if [ x$master = x ]; then fail "failed to push master"; fi
436         # didn't update master, it must be not FF
437         local mbase=`t-git-merge-base $master $t_ref_val`
438         if [ x$mbase = x$master ]; then fail "failed to ff master"; fi
439 }
440
441 t-pushed-good () {
442         local branch=$1
443         t-ref-dsc-dgit
444         t-refs-same \
445                 refs/heads/$branch \
446                 `t-v-tag` \
447                 refs/remotes/dgit/dgit/sid
448         t-refs-notexist \
449                 refs/heads/dgit/unstable \
450                 refs/remotes/dgit/dgit/unstable
451         (set -e; cd $dgitrepo
452          t-refs-same \
453                 refs/dgit/sid \
454                 `t-v-tag`
455          ${t_check_pushed_master:- : NOT-DRS-NO-CHECK-PUSHED-MASTER}
456          t-refs-notexist \
457                 refs/dgit/unstable
458         )
459         git verify-tag `t-v-tag`
460 }
461
462 t-commit-build-push-expect-log () {
463         local msg=$1
464         local mpat=$2
465         t-commit "$msg"
466         t-dgit build
467         LC_MESSAGES=C \
468         t-dgit push --new 2>&1 |tee $tmp/push.log
469         t-grep-mpat "$mpat" $tmp/push.log
470 }
471
472 t-822-field () {
473         local file=$1
474         local field=$2
475         perl -e '
476                 use Dpkg::Control::Hash;
477                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
478                 $h->parse(\*STDIN,"'"$file"'");
479                 my $val = $h->{"'$field'"},"\n";
480                 die "'"$file $field"'" unless defined $val;
481                 print $val,"\n";
482         ' <$file
483 }
484
485 t-stunt-envvar () {
486         local var=$1
487         local tstunt=$2
488         eval '
489                 case "'$var'" in
490                 "$tstunt:"*)    ;;
491                 *":$tstunt:"*)  ;;
492                 *)              '$var'="$tstunt:$'$var'" ;;
493                 esac
494         '
495 }
496
497 t-tstunt () {
498         local tstunt=$tmp/tstunt
499         t-stunt-envvar PATH $tstunt
500         t-stunt-envvar PERLLIB $tstunt
501         local f
502         for f in "$@"; do
503                 f="./$f"
504                 local d="$tstunt/${f%/*}"
505                 mkdir -p $d
506                 ln -sf "$troot/tstunt/$f" "$d"/.
507         done
508 }
509
510 t-tstunt-parsechangelog () {
511         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
512 }
513
514 t-ref-dsc-dgit () {
515         local dsc=${p}_${v}.dsc
516         local val=`t-822-field $tmp/incoming/$dsc Dgit`
517         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
518         t-ref-same-val $dsc "$val"
519 }
520
521 t-apply-diff () {
522         local v1=$1
523         local v2=$2
524         (cd $troot/pkg-srcs;
525          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
526          | patch -p1 -u
527 }
528
529 t-gbp-unapplied-pq2qc () {
530         # does `gbp pq export'
531         # commits the resulting debian/patches on  qc/BRANCH
532         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
533         # qc/BRANCH is not fast-forwarding
534
535         gbp pq export
536
537         branch=`git symbolic-ref HEAD`
538         branch=${branch#refs/heads/}
539
540         case "$branch" in
541         */*) fail "unexpected branch $branch" ;;
542         esac
543
544         git branch -f qc/$branch
545         git checkout qc/$branch
546         git add debian/patches
547         git commit -m 'Commit patch queue'
548 }
549
550 t-gbp-example-prep () {
551         t-archive example 1.0-1
552         t-git-none
553         t-worktree 1.0
554
555         cd example
556
557         t-dgit fetch
558
559         git-checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
560         gbp pq rebase
561
562         echo '/* some comment */' >>src.c
563         git add src.c
564         git commit -m 'Add a comment to an upstream file'
565
566         t-gbp-unapplied-pq2qc
567
568         t-commit 'some updates' 1.0-2
569
570         git merge -s ours \
571                 -m 'Pseudo-merge to make descendant of archive' \
572                 remotes/dgit/dgit/sid
573 }
574
575 t-commit () {
576         local msg=$1
577         v=${2-1.$revision}
578         dch -v$v --distribution unstable "$1"
579         git add debian/changelog
580         debcommit
581         revision=$(( ${revision-0} + 1 ))
582 }
583
584 t-git-config () {
585         git config --global "$@"
586 }
587
588 t-drs () {
589         export DGIT_TEST_TROOT=$troot
590  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
591  t-git-config dgit-distro.test-dummy.git-check true
592  t-git-config dgit-distro.test-dummy.git-create true
593         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
594         cp $troot/suites $tmp/.
595         cp $troot/suites $tmp/suites-master
596
597         export t_check_pushed_master=t-check-pushed-master
598
599         drs_dispatch=$tmp/distro=test-dummy
600         mkdir $drs_dispatch
601
602         if [ "x$DGIT_TEST_INTREE" != x ]; then
603                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
604         fi
605
606         ln -sf $tmp/git $drs_dispatch/repos
607         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
608         mkdir -p $drs_dispatch/keyrings
609         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
610         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
611         ln -sf /bin/true $drs_dispatch/policy-hook
612 }
613
614 t-dsd () {
615         t-drs
616  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
617  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
618  t-git-config dgit-distro.test-dummy.git-create true
619  t-git-config dgit-distro.test-dummy.git-url \
620                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
621
622  t-git-config dgit-distro.test-dummy.diverts.drs /drs
623  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
624  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
625  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
626  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
627
628         echo 'no-such-package* drs' >$drs_dispatch/diverts
629 }
630
631 t-policy-admin () {
632         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
633 }
634
635 t-policy-nonexist () {
636         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
637 }
638
639 t-make-hook-link () {
640         local hook=$1 # in infra/
641         local linkpath=$2
642         hook=${DGIT_INFRA_PFX}$hook
643         case $hook in
644         */*)    ;;
645         *)      hook=`type -P $hook` ;;
646         esac
647         ln -sf "$hook" $linkpath
648 }
649
650 t-policy () {
651         local policyhook=$1
652         t-make-hook-link $policyhook $drs_dispatch/policy-hook
653 }
654
655 t-debpolicy () {
656         t-dsd
657         t-policy dgit-repos-policy-debian
658
659         mkdir $tmp/git
660         t-policy-admin create-db
661 }
662
663 t-policy-periodic () {
664         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
665                 test-dummy $drs_dispatch '' --cron
666 }
667
668 t-restrict () {
669         local restriction=$1
670         (cd $root; t-restriction-$restriction >&2)
671 }
672
673 t-dependencies () {
674         : "Hopefully installed: $*"
675 }
676
677 t-chain-test () {
678         local ct=$1
679         local d=${0%/*}
680         cd $root
681         export DGIT_TEST_TESTNAME="$testname"
682         export ADTTMP=$tmp
683         exec "$d/$ct"
684 }       
685
686 t-alt-test () {
687         local t=${0##*/}
688         t-${t%%-*}
689         t-chain-test "${t#*-}"
690 }