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