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