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