chiark / gitweb /
f1ffa8b11563219b2f3082a591fca04ed4d16c9c
[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                         my $json = JSON->new->canonical();
257                         print $json->encode(\@v) or die $!;
258                 }
259         '
260 }
261
262 t-archive-process-incoming () {
263         local suite=$1
264         mv $tmp/incoming/${p}_${v}[._]* $tmp/mirror/pool/main/
265         t-archive-query "$suite"
266 }
267
268 t-archive-query () {
269         local suite=${1-sid}
270         local dscf=main/${p}_${v}.dsc
271         local sha=`sha256sum <$tmp/mirror/pool/$dscf`
272         echo "${v} ${sha%  -} $dscf" >>$tmp/aq/package.$suite.${p}
273         t-archive-updated $suite $p
274 }
275
276 t-archive () {
277         t-archive-none $1
278         v=$2
279         local dscf=${p}_$2.dsc
280         rm -f $tmp/mirror/pool/main/${p}_*
281         ln $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/pool/main/
282         t-archive-query
283         rm -rf $tmp/extract
284         mkdir $tmp/extract
285         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/pool/main/$dscf)
286 }
287
288 t-git-dir-time-passes () {
289         touch -d 'last year' $dgitrepo
290 }
291
292 t-git-dir-check () {
293         local gitdir=$dgitrepo
294         case "$1" in
295         enoent)
296                 if test -e "$gitdir"; then fail "$gitdir exists"; fi
297                 return
298                 ;;
299         public) wantstat='7[75]5' ;;
300         secret) wantstat='7[70]0' ;;
301         *)      fail "$1 t-git-dir-check ?" ;;
302         esac
303         gotstat=`stat -c%a $gitdir`
304         case "$gotstat" in
305         *$wantstat) return ;;
306         *)      fail "$gitdir has mode $gotstat, expected $wantstat" ;;
307         esac
308 }
309
310 t-rm-dput-dropping () {
311         rm -f $tmp/${p}_${v}_*.upload
312 }
313
314 t-dgit () {
315         local dgit=${DGIT_TEST-dgit}
316         pwd
317         : '
318 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
319         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
320                 -dtest-dummy $DGIT_TEST_OPTS $DGIT_TEST_DEBUG \
321                 -k39B13D8A "$@"
322         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
323 '
324 }
325
326 t-diff-nogit () {
327         diff --exclude=.git --exclude=.pc -ruN $*
328 }
329
330 t-files-notexist () {
331         local f
332         for f in "$@"; do
333                 if [ -e $f ]; then
334                         fail "$f exists!"
335                 fi
336         done
337 }
338
339 t-cloned-fetched-good () {
340         t-diff-nogit ../extract/$p-${v%-*} .
341         t-clean-on-branch dgit/sid
342         t-refs-same-start
343         t-refs-same \
344                 refs/heads/dgit/sid \
345                 refs/remotes/dgit/dgit/sid
346         t-refs-notexist refs/dgit/unstable refs/remotes/dgit/dgit/unstable
347 }
348
349 t-output () {
350         printf "%s\n" "$1" >$tmp/t.want
351         shift
352         "$@" >$tmp/t.got
353         diff $tmp/t.want $tmp/t.got
354 }
355
356 t-clean-on-branch () {
357         t-output "## $1" git status -b --porcelain
358 }
359
360 t-git-get-ref-exact () {
361         local ref=$1
362         # does not dereference, unlike t-git-get-ref
363         case "$ref" in
364         refs/*) ;;
365         *) fail "t-git-get-ref-exact bad $ref" ;;
366         esac
367         git for-each-ref --format='%(objectname)' "[r]efs/${ref#refs/}"
368 }
369
370 t-git-get-ref () {
371         local ref=$1
372         case "$ref" in
373         refs/*) ;;
374         *) fail "t-git-get-ref bad $ref" ;;
375         esac
376         (git show-ref -d $1 || test $? = 1) | perl -ne '
377                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
378                 END { print "$x\n" if length $x; }
379         '
380 }
381
382 t-ref-same-exact () {
383         local name="$1"
384         local val=`t-git-get-ref-exact $name`
385         t-ref-same-val "$name" $val
386 }
387
388 t-ref-same () {
389         local name="$1"
390         local val=`t-git-get-ref $name`
391         t-ref-same-val "$name" $val
392 }
393
394 t-ref-head () {
395         local val=`git rev-parse HEAD`
396         t-ref-same-val HEAD $val
397 }
398
399 t-ref-same-val () {
400         local name="$1"
401         local val=$2
402         case "$t_ref_val" in
403         '')             ;;
404         "$val")         ;;
405         *)              fail "ref varies: $name:\
406  ${val:-nothing} != ${t_ref_val:-nothing}" ;;
407         esac
408         t_ref_val="$val"
409 }
410
411 t-refs-same-start () {
412         t_ref_val=''
413 }
414
415 t-refs-same () {
416         local g
417         for g in $*; do
418                 t-ref-same $g
419         done
420 }
421
422 t-refs-notexist () {
423         local val
424         for g in $*; do
425                 val=`t-git-get-ref $g`
426                 if [ "x$val" != x ]; then
427                         fail "ref $g unexpectedly exists ($val)"
428                 fi
429         done
430 }
431
432 t-v-tag () {
433         echo refs/tags/$tagpfx/${v//\~/_}
434 }
435
436 t-check-pushed-master () {
437         local master=`t-git-get-ref refs/heads/master`
438         if [ x$master = x$t_ref_val ]; then return; fi
439         if [ x$master = x ]; then fail "failed to push master"; fi
440         # didn't update master, it must be not FF
441         local mbase=`t-git-merge-base $master $t_ref_val`
442         if [ x$mbase = x$master ]; then fail "failed to ff master"; fi
443 }
444
445 t-pushed-good () {
446         local branch=$1
447         t-ref-dsc-dgit
448         t-refs-same \
449                 refs/heads/$branch \
450                 `t-v-tag` \
451                 refs/remotes/dgit/dgit/sid
452         t-refs-notexist \
453                 refs/heads/dgit/unstable \
454                 refs/remotes/dgit/dgit/unstable
455         (set -e; cd $dgitrepo
456          t-refs-same \
457                 refs/dgit/sid \
458                 `t-v-tag`
459          ${t_check_pushed_master:- : NOT-DRS-NO-CHECK-PUSHED-MASTER}
460          t-refs-notexist \
461                 refs/dgit/unstable
462         )
463         git verify-tag `t-v-tag`
464 }
465
466 t-commit-build-push-expect-log () {
467         local msg=$1
468         local mpat=$2
469         t-commit "$msg"
470         t-dgit build
471         LC_MESSAGES=C \
472         t-dgit push --new 2>&1 |tee $tmp/push.log
473         t-grep-mpat "$mpat" $tmp/push.log
474 }
475
476 t-822-field () {
477         local file=$1
478         local field=$2
479         perl -e '
480                 use Dpkg::Control::Hash;
481                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
482                 $h->parse(\*STDIN,"'"$file"'");
483                 my $val = $h->{"'$field'"},"\n";
484                 die "'"$file $field"'" unless defined $val;
485                 print $val,"\n";
486         ' <$file
487 }
488
489 t-stunt-envvar () {
490         local var=$1
491         local tstunt=$2
492         eval '
493                 case "'$var'" in
494                 "$tstunt:"*)    ;;
495                 *":$tstunt:"*)  ;;
496                 *)              '$var'="$tstunt:$'$var'" ;;
497                 esac
498         '
499 }
500
501 t-tstunt () {
502         local tstunt=$tmp/tstunt
503         t-stunt-envvar PATH $tstunt
504         t-stunt-envvar PERLLIB $tstunt
505         local f
506         for f in "$@"; do
507                 f="./$f"
508                 local d="$tstunt/${f%/*}"
509                 mkdir -p $d
510                 ln -sf "$troot/tstunt/$f" "$d"/.
511         done
512 }
513
514 t-tstunt-parsechangelog () {
515         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
516 }
517
518 t-ref-dsc-dgit () {
519         local dsc=${p}_${v}.dsc
520         local val=`t-822-field $tmp/incoming/$dsc Dgit`
521         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
522         t-ref-same-val $dsc "$val"
523 }
524
525 t-apply-diff () {
526         local v1=$1
527         local v2=$2
528         (cd $troot/pkg-srcs;
529          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
530          | patch -p1 -u
531 }
532
533 t-gbp-unapplied-pq2qc () {
534         # does `gbp pq export'
535         # commits the resulting debian/patches on  qc/BRANCH
536         # leaves us on qc/BRANCH (eg "qc/quilt-tip"))
537         # qc/BRANCH is not fast-forwarding
538
539         gbp pq export
540
541         branch=`git symbolic-ref HEAD`
542         branch=${branch#refs/heads/}
543
544         case "$branch" in
545         */*) fail "unexpected branch $branch" ;;
546         esac
547
548         git branch -f qc/$branch
549         git checkout qc/$branch
550         git add debian/patches
551         git commit -m 'Commit patch queue'
552 }
553
554 t-gbp-example-prep () {
555         t-archive example 1.0-1
556         t-git-none
557         t-worktree 1.0
558
559         cd example
560
561         t-dgit fetch
562
563         git-checkout -b patch-queue/quilt-tip-2 patch-queue/quilt-tip
564         gbp pq rebase
565
566         echo '/* some comment */' >>src.c
567         git add src.c
568         git commit -m 'Add a comment to an upstream file'
569
570         t-gbp-unapplied-pq2qc
571
572         t-commit 'some updates' 1.0-2
573
574         git merge -s ours \
575                 -m 'Pseudo-merge to make descendant of archive' \
576                 remotes/dgit/dgit/sid
577 }
578
579 t-commit () {
580         local msg=$1
581         v=${2-1.$revision}
582         dch -v$v --distribution unstable "$1"
583         git add debian/changelog
584         debcommit
585         revision=$(( ${revision-0} + 1 ))
586 }
587
588 t-git-config () {
589         git config --global "$@"
590 }
591
592 t-drs () {
593         export DGIT_TEST_TROOT=$troot
594  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
595  t-git-config dgit-distro.test-dummy.git-check true
596  t-git-config dgit-distro.test-dummy.git-create true
597         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
598         cp $troot/suites $tmp/.
599         cp $troot/suites $tmp/suites-master
600
601         export t_check_pushed_master=t-check-pushed-master
602
603         drs_dispatch=$tmp/distro=test-dummy
604         mkdir $drs_dispatch
605
606         if [ "x$DGIT_TEST_INTREE" != x ]; then
607                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
608         fi
609
610         ln -sf $tmp/git $drs_dispatch/repos
611         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
612         mkdir -p $drs_dispatch/keyrings
613         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
614         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
615         ln -sf /bin/true $drs_dispatch/policy-hook
616 }
617
618 t-newtag () {
619  export tagpfx=archive/test-dummy
620  t-git-config dgit-distro.test-dummy.dgit-tag-format new
621 }
622 t-oldtag () {
623  t-git-config dgit-distro.test-dummy.dgit-tag-format old
624 }
625
626 t-dsd () {
627         t-drs
628  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
629  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
630  t-git-config dgit-distro.test-dummy.git-create true
631  t-git-config dgit-distro.test-dummy.git-url \
632                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
633
634  t-git-config dgit-distro.test-dummy.diverts.drs /drs
635  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
636  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
637  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
638  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
639
640         echo 'no-such-package* drs' >$drs_dispatch/diverts
641 }
642
643 t-policy-admin () {
644         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
645 }
646
647 t-policy-nonexist () {
648         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
649 }
650
651 t-make-hook-link () {
652         local hook=$1 # in infra/
653         local linkpath=$2
654         hook=${DGIT_INFRA_PFX}$hook
655         case $hook in
656         */*)    ;;
657         *)      hook=`type -P $hook` ;;
658         esac
659         ln -sf "$hook" $linkpath
660 }
661
662 t-policy () {
663         local policyhook=$1
664         t-make-hook-link $policyhook $drs_dispatch/policy-hook
665 }
666
667 t-debpolicy () {
668         t-dsd
669         t-policy dgit-repos-policy-debian
670
671         mkdir $tmp/git
672         t-policy-admin create-db
673 }
674
675 t-policy-periodic () {
676         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
677                 test-dummy $drs_dispatch '' --cron
678 }
679
680 t-restrict () {
681         local restriction=$1
682         (cd $root; t-restriction-$restriction >&2)
683 }
684
685 t-dependencies () {
686         : "Hopefully installed: $*"
687 }
688
689 t-chain-test () {
690         local ct=$1
691         local d=${0%/*}
692         cd $root
693         export DGIT_TEST_TESTNAME="$testname"
694         export ADTTMP=$tmp
695         exec "$d/$ct"
696 }       
697
698 t-alt-test () {
699         local t=${0##*/}
700         t-${t%%-*}
701         t-chain-test "${t#*-}"
702 }