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