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