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