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