chiark / gitweb /
469b8fc2cf97f15e2f92ee37b5da816f91b22af5
[dgit.git] / 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-files-notexist () {
305         local f
306         for f in "$@"; do
307                 if [ -e $f ]; then
308                         fail "$f exists!"
309                 fi
310         done
311 }
312
313 t-cloned-fetched-good () {
314         t-diff-nogit ../extract/$p-${v%-*} .
315         t-clean-on-branch dgit/sid
316         t-refs-same-start
317         t-refs-same \
318                 refs/heads/dgit/sid \
319                 refs/remotes/dgit/dgit/sid
320         t-refs-notexist refs/dgit/unstable refs/remotes/dgit/dgit/unstable
321 }
322
323 t-output () {
324         printf "%s\n" "$1" >$tmp/t.want
325         shift
326         "$@" >$tmp/t.got
327         diff $tmp/t.want $tmp/t.got
328 }
329
330 t-clean-on-branch () {
331         t-output "## $1" git status -b --porcelain
332 }
333
334 t-git-get-ref-exact () {
335         local ref=$1
336         # does not dereference, unlike t-git-get-ref
337         case "$ref" in
338         refs/*) ;;
339         *) fail "t-git-get-ref-exact bad $ref" ;;
340         esac
341         git for-each-ref --format='%(objectname)' "[r]efs/${ref#refs/}"
342 }
343
344 t-git-get-ref () {
345         local ref=$1
346         case "$ref" in
347         refs/*) ;;
348         *) fail "t-git-get-ref bad $ref" ;;
349         esac
350         (git show-ref -d $1 || test $? = 1) | perl -ne '
351                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
352                 END { print "$x\n" if length $x; }
353         '
354 }
355
356 t-ref-same-exact () {
357         local name="$1"
358         local val=`t-git-get-ref-exact $name`
359         t-ref-same-val "$name" $val
360 }
361
362 t-ref-same () {
363         local name="$1"
364         local val=`t-git-get-ref $name`
365         t-ref-same-val "$name" $val
366 }
367
368 t-ref-head () {
369         local val=`git rev-parse HEAD`
370         t-ref-same-val HEAD $val
371 }
372
373 t-ref-same-val () {
374         local name="$1"
375         local val=$2
376         case "$t_ref_val" in
377         '')             ;;
378         "$val")         ;;
379         *)              fail "ref varies: $name: $val != $t_ref_val" ;;
380         esac
381         t_ref_val="$val"
382 }
383
384 t-refs-same-start () {
385         t_ref_val=''
386 }
387
388 t-refs-same () {
389         local g
390         for g in $*; do
391                 t-ref-same $g
392         done
393 }
394
395 t-refs-notexist () {
396         local val
397         for g in $*; do
398                 val=`t-git-get-ref $g`
399                 if [ "x$val" != x ]; then
400                         fail "ref $g unexpectedly exists ($val)"
401                 fi
402         done
403 }
404
405 t-v-tag () {
406         echo refs/tags/test-dummy/${v//\~/_}
407 }
408
409 t-check-pushed-master () {
410         local master=`t-git-get-ref refs/heads/master`
411         if [ x$master = x$t_ref_val ]; then return; fi
412         if [ x$master = x ]; then fail "failed to push master"; fi
413         # didn't update master, it must be not FF
414         local mbase=`t-git-merge-base $master $t_ref_val`
415         if [ x$mbase = x$master ]; then fail "failed to ff master"; fi
416 }
417
418 t-pushed-good () {
419         local branch=$1
420         t-ref-dsc-dgit
421         t-refs-same \
422                 refs/heads/$branch \
423                 `t-v-tag` \
424                 refs/remotes/dgit/dgit/sid
425         t-refs-notexist \
426                 refs/heads/dgit/unstable \
427                 refs/remotes/dgit/dgit/unstable
428         (set -e; cd $dgitrepo
429          t-refs-same \
430                 refs/dgit/sid \
431                 `t-v-tag`
432          ${t_check_pushed_master:- : NOT-DRS-NO-CHECK-PUSHED-MASTER}
433          t-refs-notexist \
434                 refs/dgit/unstable
435         )
436         git verify-tag `t-v-tag`
437 }
438
439 t-822-field () {
440         local file=$1
441         local field=$2
442         perl -e '
443                 use Dpkg::Control::Hash;
444                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
445                 $h->parse(\*STDIN,"'"$file"'");
446                 my $val = $h->{"'$field'"},"\n";
447                 die "'"$file $field"'" unless defined $val;
448                 print $val,"\n";
449         ' <$file
450 }
451
452 t-stunt-envvar () {
453         local var=$1
454         local tstunt=$2
455         eval '
456                 case "'$var'" in
457                 "$tstunt:"*)    ;;
458                 *":$tstunt:"*)  ;;
459                 *)              '$var'="$tstunt:$'$var'" ;;
460                 esac
461         '
462 }
463
464 t-tstunt () {
465         local tstunt=$tmp/tstunt
466         t-stunt-envvar PATH $tstunt
467         t-stunt-envvar PERLLIB $tstunt
468         local f
469         for f in "$@"; do
470                 f="./$f"
471                 local d="$tstunt/${f%/*}"
472                 mkdir -p $d
473                 ln -sf "$troot/tstunt/$f" "$d"/.
474         done
475 }
476
477 t-tstunt-parsechangelog () {
478         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
479 }
480
481 t-ref-dsc-dgit () {
482         local dsc=${p}_${v}.dsc
483         local val=`t-822-field $tmp/incoming/$dsc Dgit`
484         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
485         t-ref-same-val $dsc "$val"
486 }
487
488 t-apply-diff () {
489         local v1=$1
490         local v2=$2
491         (cd $troot/pkg-srcs;
492          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
493          | patch -p1 -u
494 }
495
496 t-commit () {
497         local msg=$1
498         v=${2-1.$revision}
499         dch -v$v --distribution unstable "$1"
500         git add debian/changelog
501         debcommit
502         revision=$(( ${revision-0} + 1 ))
503 }
504
505 t-git-config () {
506         git config --global "$@"
507 }
508
509 t-drs () {
510         export DGIT_TEST_TROOT=$troot
511  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
512  t-git-config dgit-distro.test-dummy.git-check true
513  t-git-config dgit-distro.test-dummy.git-create true
514         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
515         cp $troot/suites $tmp/.
516         cp $troot/suites $tmp/suites-master
517
518         export t_check_pushed_master=t-check-pushed-master
519
520         drs_dispatch=$tmp/distro=test-dummy
521         mkdir $drs_dispatch
522
523         if [ "x$DGIT_TEST_INTREE" != x ]; then
524                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
525         fi
526
527         ln -sf $tmp/git $drs_dispatch/repos
528         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
529         mkdir -p $drs_dispatch/keyrings
530         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
531         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
532         ln -sf /bin/true $drs_dispatch/policy-hook
533 }
534
535 t-dsd () {
536         t-drs
537  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
538  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
539  t-git-config dgit-distro.test-dummy.git-create true
540  t-git-config dgit-distro.test-dummy.git-url \
541                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
542
543  t-git-config dgit-distro.test-dummy.diverts.drs /drs
544  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
545  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
546  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
547  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
548
549         echo 'no-such-package* drs' >$drs_dispatch/diverts
550 }
551
552 t-policy-admin () {
553         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
554 }
555
556 t-policy-nonexist () {
557         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
558 }
559
560 t-policy () {
561         local policyhook=$1
562         policyhook=${DGIT_INFRA_PFX}$policyhook
563         case $policyhook in
564         */*)    ;;
565         *)      policyhook=`type -P $policyhook` ;;
566         esac
567         ln -sf "$policyhook" $drs_dispatch/policy-hook
568 }
569
570 t-debpolicy () {
571         t-dsd
572         t-policy dgit-repos-policy-debian
573
574         mkdir $tmp/git
575         t-policy-admin create-db
576 }
577
578 t-policy-periodic () {
579         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
580                 test-dummy $drs_dispatch '' --cron
581 }
582
583 t-chain-test () {
584         local ct=$1
585         local d=${0%/*}
586         cd $root
587         export DGIT_TEST_TESTNAME="$testname"
588         export ADTTMP=$tmp
589         exec "$d/$ct"
590 }       
591
592 t-alt-test () {
593         local t=${0##*/}
594         t-${t%%-*}
595         t-chain-test "${t#*-}"
596 }