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