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