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