chiark / gitweb /
77c15d4e7e8ababab621b481a17133482a3f1be8
[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-git () {
132         p=$1
133         v=$2
134         mkdir -p $tmp/git
135         local gs=$troot/git-srcs/${p}_$v.git
136         (set -e; cd $tmp/git; t-untar $gs)
137 }
138
139 t-git-none () {
140         mkdir -p $tmp/git
141         (set -e; cd $tmp/git; tar xf $troot/git-template.tar)
142 }
143
144 t-has-ancestor () {
145         local now=`git rev-parse HEAD`
146         local ancestor=`git rev-parse $1^{}`
147         local mbase=`git merge-base $ancestor $now`
148         if [ x$mbase != x$ancestor ]; then
149                 fail "not ff $ancestor..$now, $mbase != $ancestor"
150         fi
151 }
152
153 t-prep-newpackage () {
154         p=$1
155         v=$2
156         t-archive-none $p
157         t-git-none
158         t-worktree $v
159         cd $p
160         if ! git show-ref --verify --quiet refs/heads/master; then
161                 git branch -m dgit/sid master
162                 git remote rm dgit
163         fi
164         cd ..
165 }
166
167 t-archive-none () {
168         p=$1
169         mkdir -p $tmp/aq $tmp/mirror/pool/main
170
171         local suite=sid
172
173         >$tmp/aq/package.$suite.$p
174         t-archive-updated $suite $p
175
176         >$tmp/aq/package.new.$p
177         t-archive-updated new $p
178
179         ln -s sid $tmp/aq/dsc_in_suite/unstable
180         cat <<'END' >$tmp/aq/suites
181 [
182    {
183       "archive" : "ftp-master",
184       "codename" : "sid",
185       "components" : [
186          "main",
187          "contrib",
188          "non-free"
189       ],
190       "name" : "unstable",
191       "dakname" : "unstable"
192    }
193 ]
194 END
195 }
196
197 t-archive-updated () {
198         local suite=$1
199         local p=$2
200         local suitedir=$tmp/aq/dsc_in_suite/$suite
201         mkdir -p $suitedir
202         perl <$tmp/aq/package.$suite.$p >$suitedir/$p -wne '
203                 use JSON;
204                 use strict;
205                 our @v;
206                 m{^(\S+) (\w+) ([^ \t/]+)/(\S+)} or die;
207                 push @v, {
208                         "version" => "$1",
209                         "sha256sum" => "$2",
210                         "component" => "$3",
211                         "filename" => "$4",
212                 };
213                 END {
214                         print to_json \@v or die $!;
215                 }
216         '
217 }
218
219 t-archive-process-incoming () {
220         local suite=$1
221         mv $tmp/incoming/${p}_${v}[._]* $tmp/mirror/pool/main/
222         t-archive-query "$suite"
223 }
224
225 t-archive-query () {
226         local suite=${1-sid}
227         local dscf=main/${p}_${v}.dsc
228         local sha=`sha256sum <$tmp/mirror/pool/$dscf`
229         echo "${v} ${sha%  -} $dscf" >>$tmp/aq/package.$suite.${p}
230         t-archive-updated $suite $p
231 }
232
233 t-archive () {
234         t-archive-none $1
235         v=$2
236         local dscf=${p}_$2.dsc
237         rm -f $tmp/mirror/pool/main/${p}_*
238         ln $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/pool/main/
239         t-archive-query
240         rm -rf $tmp/extract
241         mkdir $tmp/extract
242         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/pool/main/$dscf)
243 }
244
245 t-git-dir-time-passes () {
246         touch -d 'last year' $tmp/git/$p.git
247 }
248
249 t-git-dir-check () {
250         local gitdir=$tmp/git/$p.git
251         case "$1" in
252         enoent)
253                 if test -e "$gitdir"; then fail "$gitdir exists"; fi
254                 return
255                 ;;
256         public) wantstat='7[75]5' ;;
257         secret) wantstat='7[70]0' ;;
258         *)      fail "$1 t-git-dir-check ?" ;;
259         esac
260         gotstat=`stat -c%a $gitdir`
261         case "$gotstat" in
262         *$wantstat) return ;;
263         *)      fail "$gitdir has mode $gotstat, expected $wantstat" ;;
264         esac
265 }
266
267 t-rm-dput-dropping () {
268         rm -f $tmp/${p}_${v}_*.upload
269 }
270
271 t-dgit () {
272         local dgit=${DGIT_TEST-dgit}
273         pwd
274         : '
275 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
276         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
277                 -dtest-dummy $DGIT_TEST_OPTS $DGIT_TEST_DEBUG \
278                 -k39B13D8A "$@"
279         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
280 '
281 }
282
283 t-diff-nogit () {
284         diff --exclude=.git --exclude=.pc -ruN $*
285 }
286
287 t-cloned-fetched-good () {
288         t-diff-nogit ../extract/$p-${v%-*} .
289         t-clean-on-branch dgit/sid
290         t-refs-same-start
291         t-refs-same \
292                 refs/heads/dgit/sid \
293                 refs/remotes/dgit/dgit/sid
294         t-refs-notexist refs/dgit/unstable refs/remotes/dgit/dgit/unstable
295 }
296
297 t-output () {
298         printf "%s\n" "$1" >$tmp/t.want
299         shift
300         "$@" >$tmp/t.got
301         diff $tmp/t.want $tmp/t.got
302 }
303
304 t-clean-on-branch () {
305         t-output "## $1" git status -b --porcelain
306 }
307
308 t-git-get-ref-exact () {
309         local ref=$1
310         # does not dereference, unlike t-git-get-ref
311         case "$ref" in
312         refs/*) ;;
313         *) fail "t-git-get-ref-exact bad $ref" ;;
314         esac
315         git for-each-ref --format='%(objectname)' "[r]efs/${ref#refs/}"
316 }
317
318 t-git-get-ref () {
319         local ref=$1
320         case "$ref" in
321         refs/*) ;;
322         *) fail "t-git-get-ref bad $ref" ;;
323         esac
324         (git show-ref -d $1 || test $? = 1) | perl -ne '
325                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
326                 END { print "$x\n" if length $x; }
327         '
328 }
329
330 t-ref-same-exact () {
331         local name="$1"
332         local val=`t-git-get-ref-exact $name`
333         t-ref-same-val "$name" $val
334 }
335
336 t-ref-same () {
337         local name="$1"
338         local val=`t-git-get-ref $name`
339         t-ref-same-val "$name" $val
340 }
341
342 t-ref-head () {
343         local val=`git rev-parse HEAD`
344         t-ref-same-val HEAD $val
345 }
346
347 t-ref-same-val () {
348         local name="$1"
349         local val=$2
350         case "$t_ref_val" in
351         '')             ;;
352         "$val")         ;;
353         *)              fail "ref varies: $name: $val != $t_ref_val" ;;
354         esac
355         t_ref_val="$val"
356 }
357
358 t-refs-same-start () {
359         t_ref_val=''
360 }
361
362 t-refs-same () {
363         local g
364         for g in $*; do
365                 t-ref-same $g
366         done
367 }
368
369 t-refs-notexist () {
370         local val
371         for g in $*; do
372                 val=`t-git-get-ref $g`
373                 if [ "x$val" != x ]; then
374                         fail "ref $g unexpectedly exists ($val)"
375                 fi
376         done
377 }
378
379 t-v-tag () {
380         echo refs/tags/test-dummy/${v//\~/_}
381 }
382
383 t-pushed-good () {
384         local branch=$1
385         t-ref-dsc-dgit
386         t-refs-same \
387                 refs/heads/$branch \
388                 `t-v-tag` \
389                 refs/remotes/dgit/dgit/sid
390         t-refs-notexist \
391                 refs/heads/dgit/unstable \
392                 refs/remotes/dgit/dgit/unstable
393         (set -e; cd $tmp/git/$p.git
394          t-refs-same \
395                 refs/dgit/sid \
396                 `t-v-tag`
397          t-refs-notexist \
398                 refs/dgit/unstable
399         )
400         git verify-tag `t-v-tag`
401 }
402
403 t-822-field () {
404         local file=$1
405         local field=$2
406         perl -e '
407                 use Dpkg::Control::Hash;
408                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
409                 $h->parse(\*STDIN,"'"$file"'");
410                 my $val = $h->{"'$field'"},"\n";
411                 die "'"$file $field"'" unless defined $val;
412                 print $val,"\n";
413         ' <$file
414 }
415
416 t-stunt-envvar () {
417         local var=$1
418         local tstunt=$2
419         eval '
420                 case "'$var'" in
421                 "$tstunt:"*)    ;;
422                 *":$tstunt:"*)  ;;
423                 *)              '$var'="$tstunt:$'$var'" ;;
424                 esac
425         '
426 }
427
428 t-tstunt () {
429         local tstunt=$tmp/tstunt
430         t-stunt-envvar PATH $tstunt
431         t-stunt-envvar PERLLIB $tstunt
432         local f
433         for f in "$@"; do
434                 f="./$f"
435                 local d="$tstunt/${f%/*}"
436                 mkdir -p $d
437                 ln -sf "$troot/tstunt/$f" "$d"/.
438         done
439 }
440
441 t-tstunt-parsechangelog () {
442         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
443 }
444
445 t-prep-mergechangelogs () {
446         local b=merge.dpkg-mergechangelogs
447         git config $b.name 'debian/changelog merge driver'
448         git config $b.driver 'dpkg-mergechangelogs -m %O %A %B %A'
449         mkdir -p .git/info
450         echo >>.git/info/attributes \
451                 'debian/changelog merge=dpkg-mergechangelogs'
452 }
453
454 t-ref-dsc-dgit () {
455         local dsc=${p}_${v}.dsc
456         local val=`t-822-field $tmp/incoming/$dsc Dgit`
457         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
458         t-ref-same-val $dsc "$val"
459 }
460
461 t-apply-diff () {
462         local v1=$1
463         local v2=$2
464         (cd $troot/pkg-srcs;
465          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
466          | patch -p1 -u
467 }
468
469 t-commit () {
470         local msg=$1
471         v=${2-1.$revision}
472         dch -v$v --distribution unstable "$1"
473         git add debian/changelog
474         debcommit
475         revision=$(( ${revision-0} + 1 ))
476 }
477
478 t-git-config () {
479         git config --global "$@"
480 }
481
482 t-drs () {
483         export DGIT_TEST_TROOT=$troot
484  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
485  t-git-config dgit-distro.test-dummy.git-check true
486  t-git-config dgit-distro.test-dummy.git-create true
487         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
488         cp $troot/suites $tmp/.
489
490         drs_dispatch=$tmp/distro=test-dummy
491         mkdir $drs_dispatch
492
493         if [ "x$DGIT_TEST_INTREE" != x ]; then
494                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
495         fi
496
497         ln -sf $tmp/git $drs_dispatch/repos
498         ln -sf $tmp/suites $tmp/dm.txt $drs_dispatch/
499         mkdir -p $drs_dispatch/keyrings
500         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
501         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
502         ln -sf /bin/true $drs_dispatch/policy-hook
503 }
504
505 t-dsd () {
506         t-drs
507  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
508  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
509  t-git-config dgit-distro.test-dummy.git-create true
510  t-git-config dgit-distro.test-dummy.git-url \
511                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
512
513  t-git-config dgit-distro.test-dummy.diverts.drs /drs
514  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
515  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
516  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
517  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
518
519         echo 'no-such-package* drs' >$drs_dispatch/diverts
520 }
521
522 t-policy-admin () {
523         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
524 }
525
526 t-policy-nonexist () {
527         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
528 }
529
530 t-policy () {
531         local policyhook=$1
532         policyhook=${DGIT_INFRA_PFX}$policyhook
533         case $policyhook in
534         */*)    ;;
535         *)      policyhook=`type -P $policyhook` ;;
536         esac
537         ln -sf "$policyhook" $drs_dispatch/policy-hook
538 }
539
540 t-debpolicy () {
541         t-dsd
542         t-policy dgit-repos-policy-debian
543
544         mkdir $tmp/git
545         t-policy-admin create-db
546 }
547
548 t-policy-periodic () {
549         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
550                 test-dummy $drs_dispatch '' --cron
551 }
552
553 t-chain-test () {
554         local ct=$1
555         local d=${0%/*}
556         cd $root
557         export DGIT_TEST_TESTNAME="$testname"
558         export ADTTMP=$tmp
559         exec "$d/$ct"
560 }       
561
562 t-alt-test () {
563         local t=${0##*/}
564         t-${t%%-*}
565         t-chain-test "${t#*-}"
566 }