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