chiark / gitweb /
dgit-repos-policy-debian: Fix check-list printing
[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 export HOME=$tmp
28 export DGIT_TEST_DUMMY_DIR=$tmp
29 ln -f $troot/ssh ssh
30
31 mkdir -p $tmp/gnupg
32 cp $troot/gnupg/* $tmp/gnupg
33 chmod go-rw $tmp/gnupg/*
34 export GNUPGHOME=$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         v=3-1
94         t-worktree $v
95         cd $p
96         git branch -m dgit/sid master
97         git remote rm dgit
98         cd ..
99 }
100
101 t-archive-none () {
102         p=$1
103         mkdir -p $tmp/aq $tmp/mirror/pool/main
104         mkdir -p $tmp/aq/dsc_in_suite $tmp/aq/dsc_in_suite/sid
105
106         local suite=sid
107         >$tmp/aq/package.$suite.$p
108         t-archive-updated $suite $p
109
110         ln -s sid $tmp/aq/dsc_in_suite/unstable
111         cat <<'END' >$tmp/aq/suites
112 [
113    {
114       "archive" : "ftp-master",
115       "codename" : "sid",
116       "components" : [
117          "main",
118          "contrib",
119          "non-free"
120       ],
121       "name" : "unstable",
122       "dakname" : "unstable"
123    }
124 ]
125 END
126 }
127
128 t-archive-updated () {
129         local suite=$1
130         local p=$2
131         perl <$tmp/aq/package.$suite.$p >$tmp/aq/dsc_in_suite/$suite/$p -wne '
132                 use JSON;
133                 use strict;
134                 our @v;
135                 m{^(\S+) (\w+) ([^ \t/]+)/(\S+)} or die;
136                 push @v, {
137                         "version" => "$1",
138                         "sha256sum" => "$2",
139                         "component" => "$3",
140                         "filename" => "$4",
141                 };
142                 END {
143                         print to_json \@v or die $!;
144                 }
145         '
146 }
147
148 t-archive-process-incoming () {
149         mv incoming/${p}_${v}[._]* mirror/pool/main/
150         t-archive-query
151 }
152
153 t-archive-query () {
154         local dscf=main/${p}_${v}.dsc
155         local sha=`sha256sum <mirror/pool/$dscf`
156         echo "${v} ${sha%  -} $dscf" >>$tmp/aq/package.sid.${p}
157         t-archive-updated sid $p
158 }
159
160 t-archive () {
161         t-archive-none $1
162         v=$2
163         local dscf=${p}_$2.dsc
164         rm -f $tmp/mirror/pool/main/${p}_*
165         ln $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/pool/main/
166         t-archive-query
167         rm -rf $tmp/extract
168         mkdir $tmp/extract
169         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/pool/main/$dscf)
170 }
171
172 t-dgit () {
173         local dgit=${DGIT_TEST-dgit}
174         : '
175 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
176         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
177                 -dtest-dummy $DGIT_TEST_OPTS $DGIT_TEST_DEBUG \
178                 -k39B13D8A "$@"
179         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
180 '
181 }
182
183 t-diff-nogit () {
184         diff --exclude=.git --exclude=.pc -ruN $*
185 }
186
187 t-cloned-fetched-good () {
188         t-diff-nogit ../extract/$p-${v%-*} .
189         t-clean-on-branch dgit/sid
190         t-refs-same-start
191         t-refs-same \
192                 refs/heads/dgit/sid \
193                 refs/remotes/dgit/dgit/sid
194         t-refs-notexist dgit/unstable remotes/dgit/dgit/unstable
195 }
196
197 t-output () {
198         printf "%s\n" "$1" >$tmp/t.want
199         shift
200         "$@" >$tmp/t.got
201         diff $tmp/t.want $tmp/t.got
202 }
203
204 t-clean-on-branch () {
205         t-output "## $1" git status -b --porcelain
206 }
207
208 t-git-get-ref () {
209         local ref=$1
210         git show-ref -d $1 | perl -ne '
211                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
212                 END { print "$x\n" if length $x; }
213         '
214 }
215
216 t-ref-same () {
217         local name="$1"
218         local val=`t-git-get-ref $name`
219         t-ref-same-val "$name" $val
220 }
221
222 t-ref-head () {
223         local val=`git rev-parse HEAD`
224         t-ref-same-val HEAD $val
225 }
226
227 t-ref-same-val () {
228         local name="$1"
229         local val=$2
230         case "$t_ref_val" in
231         '')             ;;
232         "$val")         ;;
233         *)              fail "ref varies: $name: $val != $t_ref_val" ;;
234         esac
235         t_ref_val="$val"
236 }
237
238 t-refs-same-start () {
239         t_ref_val=''
240 }
241
242 t-refs-same () {
243         local g
244         for g in $*; do
245                 t-ref-same $g
246         done
247 }
248
249 t-refs-notexist () {
250         local val
251         for g in $*; do
252                 val=`t-git-get-ref $g >$tmp/t.refx`
253                 if [ "x$val" != x ]; then
254                         fail "ref $g unexpectedly exists ($val)"
255                 fi
256         done
257 }
258
259 t-v-tag () {
260         echo refs/tags/debian/${v//\~/_}
261 }
262
263 t-pushed-good () {
264         local branch=$1
265         t-ref-dsc-dgit
266         t-refs-same \
267                 refs/heads/$branch \
268                 `t-v-tag` \
269                 refs/remotes/dgit/dgit/sid
270         t-refs-notexist \
271                 refs/heads/dgit/unstable \
272                 refs/remotes/dgit/dgit/unstable
273         (set -e; cd $tmp/git/$p.git
274          t-refs-same \
275                 refs/dgit/sid \
276                 `t-v-tag`
277          t-refs-notexist \
278                 refs/dgit/unstable
279         )
280         git verify-tag `t-v-tag`
281 }
282
283 t-822-field () {
284         local file=$1
285         local field=$2
286         perl -e '
287                 use Dpkg::Control::Hash;
288                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
289                 $h->parse(\*STDIN,"'"$file"'");
290                 my $val = $h->{"'$field'"},"\n";
291                 die "'"$file $field"'" unless defined $val;
292                 print $val,"\n";
293         ' <$file
294 }
295
296 t-ref-dsc-dgit () {
297         local dsc=${p}_${v}.dsc
298         local val=`t-822-field $tmp/incoming/$dsc Dgit`
299         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
300         t-ref-same-val $dsc "$val"
301 }
302
303 t-apply-diff () {
304         local v1=$1
305         local v2=$2
306         (cd $troot/pkg-srcs; debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc) \
307                 | patch -p1 -u
308 }
309
310 t-git-config () {
311         git config --global "$@"
312 }
313
314 t-drs () {
315         export DGIT_TEST_TROOT=$troot
316         export DGIT_TEST_TMP=$tmp
317  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
318  t-git-config dgit-distro.test-dummy.git-check true
319  t-git-config dgit-distro.test-dummy.git-create true
320         cp $root/tests/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
321         cp $root/tests/suites $tmp/.
322
323         drs_dispatch=$tmp/distro=test-dummy
324         mkdir $drs_dispatch
325         ln -sf $root $drs_dispatch/dgit-live
326         ln -sf $tmp/git $drs_dispatch/repos
327         ln -sf $tmp/suites $tmp/dm.txt $drs_dispatch/
328         mkdir -p $drs_dispatch/keyrings
329         ln -sf $tmp/dd.gpg $drs_dispatch/keyrings/debian-keyring.gpg
330         ln -sf $tmp/dm.gpg $drs_dispatch/keyrings/debian-maintainers.gpg
331         ln -sf /bin/true $drs_dispatch/policy-hook
332 }
333
334 t-dsd () {
335         t-drs
336  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
337  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
338  t-git-config dgit-distro.test-dummy.git-create true
339  t-git-config dgit-distro.test-dummy.git-url \
340                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
341
342  t-git-config dgit-distro.test-dummy.diverts.drs /drs
343  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
344  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
345  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
346  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
347
348         echo 'no-such-package* drs' >$drs_dispatch/diverts
349 }
350
351 t-policy-admin () {
352         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
353 }
354
355 t-debpolicy () {
356         t-dsd
357
358         ln -sf ${DGIT_INFRA_PFX}dgit-repos-policy-debian \
359                 $drs_dispatch/policy-hook
360
361         mkdir $tmp/git
362         t-policy-admin create-db
363 }
364
365 t-chain-test () {
366         local ct=$1
367         local d=${0%/*}
368         cd $root
369         export DGIT_TEST_TESTNAME="$testname"
370         export ADTTMP=$tmp
371         exec "$d/$ct"
372 }       
373
374 t-alt-test () {
375         local t=${0##*/}
376         t-${t%%-*}
377         t-chain-test "${t#*-}"
378 }