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