chiark / gitweb /
Test suite: Print better info on t-ref-same failure
[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:\
403  ${val:-nothing} != ${t_ref_val:-nothing}" ;;
404         esac
405         t_ref_val="$val"
406 }
407
408 t-refs-same-start () {
409         t_ref_val=''
410 }
411
412 t-refs-same () {
413         local g
414         for g in $*; do
415                 t-ref-same $g
416         done
417 }
418
419 t-refs-notexist () {
420         local val
421         for g in $*; do
422                 val=`t-git-get-ref $g`
423                 if [ "x$val" != x ]; then
424                         fail "ref $g unexpectedly exists ($val)"
425                 fi
426         done
427 }
428
429 t-v-tag () {
430         echo refs/tags/test-dummy/${v//\~/_}
431 }
432
433 t-check-pushed-master () {
434         local master=`t-git-get-ref refs/heads/master`
435         if [ x$master = x$t_ref_val ]; then return; fi
436         if [ x$master = x ]; then fail "failed to push master"; fi
437         # didn't update master, it must be not FF
438         local mbase=`t-git-merge-base $master $t_ref_val`
439         if [ x$mbase = x$master ]; then fail "failed to ff master"; fi
440 }
441
442 t-pushed-good () {
443         local branch=$1
444         t-ref-dsc-dgit
445         t-refs-same \
446                 refs/heads/$branch \
447                 `t-v-tag` \
448                 refs/remotes/dgit/dgit/sid
449         t-refs-notexist \
450                 refs/heads/dgit/unstable \
451                 refs/remotes/dgit/dgit/unstable
452         (set -e; cd $dgitrepo
453          t-refs-same \
454                 refs/dgit/sid \
455                 `t-v-tag`
456          ${t_check_pushed_master:- : NOT-DRS-NO-CHECK-PUSHED-MASTER}
457          t-refs-notexist \
458                 refs/dgit/unstable
459         )
460         git verify-tag `t-v-tag`
461 }
462
463 t-commit-build-push-expect-log () {
464         local msg=$1
465         local mpat=$2
466         t-commit "$msg"
467         t-dgit build
468         LC_MESSAGES=C \
469         t-dgit push --new 2>&1 |tee $tmp/push.log
470         t-grep-mpat "$mpat" $tmp/push.log
471 }
472
473 t-822-field () {
474         local file=$1
475         local field=$2
476         perl -e '
477                 use Dpkg::Control::Hash;
478                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
479                 $h->parse(\*STDIN,"'"$file"'");
480                 my $val = $h->{"'$field'"},"\n";
481                 die "'"$file $field"'" unless defined $val;
482                 print $val,"\n";
483         ' <$file
484 }
485
486 t-stunt-envvar () {
487         local var=$1
488         local tstunt=$2
489         eval '
490                 case "'$var'" in
491                 "$tstunt:"*)    ;;
492                 *":$tstunt:"*)  ;;
493                 *)              '$var'="$tstunt:$'$var'" ;;
494                 esac
495         '
496 }
497
498 t-tstunt () {
499         local tstunt=$tmp/tstunt
500         t-stunt-envvar PATH $tstunt
501         t-stunt-envvar PERLLIB $tstunt
502         local f
503         for f in "$@"; do
504                 f="./$f"
505                 local d="$tstunt/${f%/*}"
506                 mkdir -p $d
507                 ln -sf "$troot/tstunt/$f" "$d"/.
508         done
509 }
510
511 t-tstunt-parsechangelog () {
512         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
513 }
514
515 t-ref-dsc-dgit () {
516         local dsc=${p}_${v}.dsc
517         local val=`t-822-field $tmp/incoming/$dsc Dgit`
518         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
519         t-ref-same-val $dsc "$val"
520 }
521
522 t-apply-diff () {
523         local v1=$1
524         local v2=$2
525         (cd $troot/pkg-srcs;
526          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
527          | patch -p1 -u
528 }
529
530 t-gbp-unapplied-pq2qc () {
531         # does `gbp pq export'
532         # commits the resulting debian/patches on  qc/BRANCH
533         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
534         # qc/BRANCH is not fast-forwarding
535
536         gbp pq export
537
538         branch=`git symbolic-ref HEAD`
539         branch=${branch#refs/heads/}
540
541         case "$branch" in
542         */*) fail "unexpected branch $branch" ;;
543         esac
544
545         git branch -f qc/$branch
546         git checkout qc/$branch
547         git add debian/patches
548         git commit -m 'Commit patch queue'
549 }
550
551 t-gbp-example-prep () {
552         t-archive example 1.0-1
553         t-git-none
554         t-worktree 1.0
555
556         cd example
557
558         t-dgit fetch
559
560         git-checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
561         gbp pq rebase
562
563         echo '/* some comment */' >>src.c
564         git add src.c
565         git commit -m 'Add a comment to an upstream file'
566
567         t-gbp-unapplied-pq2qc
568
569         t-commit 'some updates' 1.0-2
570
571         git merge -s ours \
572                 -m 'Pseudo-merge to make descendant of archive' \
573                 remotes/dgit/dgit/sid
574 }
575
576 t-commit () {
577         local msg=$1
578         v=${2-1.$revision}
579         dch -v$v --distribution unstable "$1"
580         git add debian/changelog
581         debcommit
582         revision=$(( ${revision-0} + 1 ))
583 }
584
585 t-git-config () {
586         git config --global "$@"
587 }
588
589 t-drs () {
590         export DGIT_TEST_TROOT=$troot
591  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
592  t-git-config dgit-distro.test-dummy.git-check true
593  t-git-config dgit-distro.test-dummy.git-create true
594         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
595         cp $troot/suites $tmp/.
596         cp $troot/suites $tmp/suites-master
597
598         export t_check_pushed_master=t-check-pushed-master
599
600         drs_dispatch=$tmp/distro=test-dummy
601         mkdir $drs_dispatch
602
603         if [ "x$DGIT_TEST_INTREE" != x ]; then
604                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
605         fi
606
607         ln -sf $tmp/git $drs_dispatch/repos
608         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
609         mkdir -p $drs_dispatch/keyrings
610         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
611         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
612         ln -sf /bin/true $drs_dispatch/policy-hook
613 }
614
615 t-dsd () {
616         t-drs
617  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
618  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
619  t-git-config dgit-distro.test-dummy.git-create true
620  t-git-config dgit-distro.test-dummy.git-url \
621                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
622
623  t-git-config dgit-distro.test-dummy.diverts.drs /drs
624  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
625  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
626  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
627  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
628
629         echo 'no-such-package* drs' >$drs_dispatch/diverts
630 }
631
632 t-policy-admin () {
633         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
634 }
635
636 t-policy-nonexist () {
637         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
638 }
639
640 t-make-hook-link () {
641         local hook=$1 # in infra/
642         local linkpath=$2
643         hook=${DGIT_INFRA_PFX}$hook
644         case $hook in
645         */*)    ;;
646         *)      hook=`type -P $hook` ;;
647         esac
648         ln -sf "$hook" $linkpath
649 }
650
651 t-policy () {
652         local policyhook=$1
653         t-make-hook-link $policyhook $drs_dispatch/policy-hook
654 }
655
656 t-debpolicy () {
657         t-dsd
658         t-policy dgit-repos-policy-debian
659
660         mkdir $tmp/git
661         t-policy-admin create-db
662 }
663
664 t-policy-periodic () {
665         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
666                 test-dummy $drs_dispatch '' --cron
667 }
668
669 t-restrict () {
670         local restriction=$1
671         (cd $root; t-restriction-$restriction >&2)
672 }
673
674 t-dependencies () {
675         : "Hopefully installed: $*"
676 }
677
678 t-chain-test () {
679         local ct=$1
680         local d=${0%/*}
681         cd $root
682         export DGIT_TEST_TESTNAME="$testname"
683         export ADTTMP=$tmp
684         exec "$d/$ct"
685 }       
686
687 t-alt-test () {
688         local t=${0##*/}
689         t-${t%%-*}
690         t-chain-test "${t#*-}"
691 }