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