chiark / gitweb /
Mirroring: Tests: Break out various functions (nfc overall)
[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-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-commit-build-push-expect-log () {
440         local msg=$1
441         local mpat=$2
442         t-commit "$msg"
443         t-dgit build
444         LC_MESSAGES=C \
445         t-dgit push --new 2>&1 |tee $tmp/push.log
446         t-grep-mpat "$mpat" $tmp/push.log
447 }
448
449 t-822-field () {
450         local file=$1
451         local field=$2
452         perl -e '
453                 use Dpkg::Control::Hash;
454                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
455                 $h->parse(\*STDIN,"'"$file"'");
456                 my $val = $h->{"'$field'"},"\n";
457                 die "'"$file $field"'" unless defined $val;
458                 print $val,"\n";
459         ' <$file
460 }
461
462 t-stunt-envvar () {
463         local var=$1
464         local tstunt=$2
465         eval '
466                 case "'$var'" in
467                 "$tstunt:"*)    ;;
468                 *":$tstunt:"*)  ;;
469                 *)              '$var'="$tstunt:$'$var'" ;;
470                 esac
471         '
472 }
473
474 t-tstunt () {
475         local tstunt=$tmp/tstunt
476         t-stunt-envvar PATH $tstunt
477         t-stunt-envvar PERLLIB $tstunt
478         local f
479         for f in "$@"; do
480                 f="./$f"
481                 local d="$tstunt/${f%/*}"
482                 mkdir -p $d
483                 ln -sf "$troot/tstunt/$f" "$d"/.
484         done
485 }
486
487 t-tstunt-parsechangelog () {
488         t-tstunt dpkg-parsechangelog Dpkg/Changelog/Parse.pm
489 }
490
491 t-ref-dsc-dgit () {
492         local dsc=${p}_${v}.dsc
493         local val=`t-822-field $tmp/incoming/$dsc Dgit`
494         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
495         t-ref-same-val $dsc "$val"
496 }
497
498 t-apply-diff () {
499         local v1=$1
500         local v2=$2
501         (cd $troot/pkg-srcs;
502          debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc || test $? = 1) \
503          | patch -p1 -u
504 }
505
506 t-commit () {
507         local msg=$1
508         v=${2-1.$revision}
509         dch -v$v --distribution unstable "$1"
510         git add debian/changelog
511         debcommit
512         revision=$(( ${revision-0} + 1 ))
513 }
514
515 t-git-config () {
516         git config --global "$@"
517 }
518
519 t-drs () {
520         export DGIT_TEST_TROOT=$troot
521  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
522  t-git-config dgit-distro.test-dummy.git-check true
523  t-git-config dgit-distro.test-dummy.git-create true
524         cp $troot/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
525         cp $troot/suites $tmp/.
526         cp $troot/suites $tmp/suites-master
527
528         export t_check_pushed_master=t-check-pushed-master
529
530         drs_dispatch=$tmp/distro=test-dummy
531         mkdir $drs_dispatch
532
533         if [ "x$DGIT_TEST_INTREE" != x ]; then
534                 ln -sf "$DGIT_TEST_INTREE" $drs_dispatch/dgit-live
535         fi
536
537         ln -sf $tmp/git $drs_dispatch/repos
538         ln -sf $tmp/suites $tmp/suites-master $tmp/dm.txt $drs_dispatch/
539         mkdir -p $drs_dispatch/keyrings
540         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
541         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
542         ln -sf /bin/true $drs_dispatch/policy-hook
543 }
544
545 t-dsd () {
546         t-drs
547  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
548  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
549  t-git-config dgit-distro.test-dummy.git-create true
550  t-git-config dgit-distro.test-dummy.git-url \
551                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
552
553  t-git-config dgit-distro.test-dummy.diverts.drs /drs
554  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
555  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
556  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
557  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
558
559         echo 'no-such-package* drs' >$drs_dispatch/diverts
560 }
561
562 t-policy-admin () {
563         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
564 }
565
566 t-policy-nonexist () {
567         ln -sf no-such-file-or-directory $drs_dispatch/policy-hook
568 }
569
570 t-policy () {
571         local policyhook=$1
572         policyhook=${DGIT_INFRA_PFX}$policyhook
573         case $policyhook in
574         */*)    ;;
575         *)      policyhook=`type -P $policyhook` ;;
576         esac
577         ln -sf "$policyhook" $drs_dispatch/policy-hook
578 }
579
580 t-debpolicy () {
581         t-dsd
582         t-policy dgit-repos-policy-debian
583
584         mkdir $tmp/git
585         t-policy-admin create-db
586 }
587
588 t-policy-periodic () {
589         ${DGIT_REPOS_SERVER_TEST-dgit-repos-server} \
590                 test-dummy $drs_dispatch '' --cron
591 }
592
593 t-chain-test () {
594         local ct=$1
595         local d=${0%/*}
596         cd $root
597         export DGIT_TEST_TESTNAME="$testname"
598         export ADTTMP=$tmp
599         exec "$d/$ct"
600 }       
601
602 t-alt-test () {
603         local t=${0##*/}
604         t-${t%%-*}
605         t-chain-test "${t#*-}"
606 }