chiark / gitweb /
Test suite: start work on test of policy machinery
[dgit.git] / tests / lib
1 #
2
3 exec 2>&1
4 set -x
5
6 if [ "x$DGIT_TEST_INTREE" != x ]; then
7         : ${DGIT_TEST:=$DGIT_TEST_INTREE/dgit}
8         : ${DGIT_REPOS_SERVER_TEST:=$DGIT_TEST_INTREE/infra/dgit-repos-server}
9         : ${DGIT_SSH_DISPATCH_TEST:=$DGIT_TEST_INTREE/infra/dgit-ssh-dispatch}
10         : ${DGIT_INFRA_PFX:=$DGIT_TEST_INTREE${DGIT_TEST_INTREE:+/infra/}}
11         export DGIT_TEST DGIT_REPOS_SERVER_TEST DGIT_SSH_DISPATCH_TEST
12 fi
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 export HOME=$tmp
29 export DGIT_TEST_DUMMY_DIR=$tmp
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 export GNUPGHOME=$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 fail () {
46         echo >&2 "failed: $*"
47         exit 1
48 }
49
50 t-expect-fail () {
51         local mpat="$1"; shift
52         LC_MESSAGES=C "$@" 2>&1 | tee $tmp/t.output
53         test ${PIPESTATUS[0]} != 0
54         egrep "$mpat" $tmp/t.output ||false
55 }
56
57 t-untar () {
58         local tarfile=$1.tar
59         local edittree=$1.edit
60         if test -d "$edittree"; then
61                 cp -al "$edittree"/* .
62         else
63                 tar xf "$tarfile"
64         fi
65 }
66
67 t-worktree () {
68         rm -rf $p
69         t-untar $troot/worktrees/${p}_$1
70 }
71
72 t-git () {
73         p=$1
74         v=$2
75         mkdir -p $tmp/git
76         local gs=$troot/git-srcs/${p}_$v.git
77         (set -e; cd $tmp/git; t-untar $gs)
78 }
79
80 t-git-none () {
81         mkdir -p $tmp/git
82         (set -e; cd $tmp/git; tar xf $troot/git-template.tar)
83 }
84
85 t-has-ancestor () {
86         local now=`git rev-parse HEAD`
87         local ancestor=`git rev-parse $1^{}`
88         local mbase=`git merge-base $ancestor $now`
89         if [ x$mbase != x$ancestor ]; then
90                 fail "not ff $ancestor..$now, $mbase != $ancestor"
91         fi
92 }
93
94 t-prep-newpackage () {
95         p=$1
96         v=$2
97         t-archive-none $p
98         t-git-none
99         v=3-1
100         t-worktree $v
101         cd $p
102         git branch -m dgit/sid master
103         git remote rm dgit
104         cd ..
105 }
106
107 t-archive-none () {
108         p=$1
109         mkdir -p $tmp/aq $tmp/mirror/pool/main
110         mkdir -p $tmp/aq/dsc_in_suite $tmp/aq/dsc_in_suite/sid
111
112         local suite=sid
113         >$tmp/aq/package.$suite.$p
114         t-archive-updated $suite $p
115
116         ln -s sid $tmp/aq/dsc_in_suite/unstable
117         cat <<'END' >$tmp/aq/suites
118 [
119    {
120       "archive" : "ftp-master",
121       "codename" : "sid",
122       "components" : [
123          "main",
124          "contrib",
125          "non-free"
126       ],
127       "name" : "unstable",
128       "dakname" : "unstable"
129    }
130 ]
131 END
132 }
133
134 t-archive-updated () {
135         local suite=$1
136         local p=$2
137         perl <$tmp/aq/package.$suite.$p >$tmp/aq/dsc_in_suite/$suite/$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         mv incoming/${p}_${v}[._]* mirror/pool/main/
156         t-archive-query
157 }
158
159 t-archive-query () {
160         local dscf=main/${p}_${v}.dsc
161         local sha=`sha256sum <mirror/pool/$dscf`
162         echo "${v} ${sha%  -} $dscf" >>$tmp/aq/package.sid.${p}
163         t-archive-updated sid $p
164 }
165
166 t-archive () {
167         t-archive-none $1
168         v=$2
169         local dscf=${p}_$2.dsc
170         rm -f $tmp/mirror/pool/main/${p}_*
171         ln $troot/pkg-srcs/${p}_${2%-*}* $tmp/mirror/pool/main/
172         t-archive-query
173         rm -rf $tmp/extract
174         mkdir $tmp/extract
175         (set -e; cd $tmp/extract; dpkg-source -x ../mirror/pool/main/$dscf)
176 }
177
178 t-dgit () {
179         local dgit=${DGIT_TEST-dgit}
180         : '
181 {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{'
182         $dgit --dgit=$dgit --dget:-u --dput:--config=$tmp/dput.cf \
183                 -dtest-dummy $DGIT_TEST_OPTS ${DGIT_TEST_DEBUG--D} \
184                 -k39B13D8A "$@"
185         : '}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
186 '
187 }
188
189 t-diff-nogit () {
190         diff --exclude=.git --exclude=.pc -ruN $*
191 }
192
193 t-cloned-fetched-good () {
194         t-diff-nogit ../extract/$p-${v%-*} .
195         t-clean-on-branch dgit/sid
196         t-refs-same-start
197         t-refs-same \
198                 refs/heads/dgit/sid \
199                 refs/remotes/dgit/dgit/sid
200         t-refs-notexist dgit/unstable remotes/dgit/dgit/unstable
201 }
202
203 t-output () {
204         printf "%s\n" "$1" >$tmp/t.want
205         shift
206         "$@" >$tmp/t.got
207         diff $tmp/t.want $tmp/t.got
208 }
209
210 t-clean-on-branch () {
211         t-output "## $1" git status -b --porcelain
212 }
213
214 t-git-get-ref () {
215         local ref=$1
216         git show-ref -d $1 | perl -ne '
217                 $x = $1 if m#^(\w+) \Q'$1'\E(?:\^\{\})?$#;
218                 END { print "$x\n" if length $x; }
219         '
220 }
221
222 t-ref-same () {
223         local name="$1"
224         local val=`t-git-get-ref $name`
225         t-ref-same-val "$name" $val
226 }
227
228 t-ref-head () {
229         local val=`git rev-parse HEAD`
230         t-ref-same-val HEAD $val
231 }
232
233 t-ref-same-val () {
234         local name="$1"
235         local val=$2
236         case "$t_ref_val" in
237         '')             ;;
238         "$val")         ;;
239         *)              fail "ref varies: $name: $val != $t_ref_val" ;;
240         esac
241         t_ref_val="$val"
242 }
243
244 t-refs-same-start () {
245         t_ref_val=''
246 }
247
248 t-refs-same () {
249         local g
250         for g in $*; do
251                 t-ref-same $g
252         done
253 }
254
255 t-refs-notexist () {
256         local val
257         for g in $*; do
258                 val=`t-git-get-ref $g >$tmp/t.refx`
259                 if [ "x$val" != x ]; then
260                         fail "ref $g unexpectedly exists ($val)"
261                 fi
262         done
263 }
264
265 t-v-tag () {
266         echo refs/tags/debian/${v//\~/_}
267 }
268
269 t-pushed-good () {
270         local branch=$1
271         t-ref-dsc-dgit
272         t-refs-same \
273                 refs/heads/$branch \
274                 `t-v-tag` \
275                 refs/remotes/dgit/dgit/sid
276         t-refs-notexist \
277                 refs/heads/dgit/unstable \
278                 refs/remotes/dgit/dgit/unstable
279         (set -e; cd $tmp/git/$p.git
280          t-refs-same \
281                 refs/dgit/sid \
282                 `t-v-tag`
283          t-refs-notexist \
284                 refs/dgit/unstable
285         )
286         git verify-tag `t-v-tag`
287 }
288
289 t-822-field () {
290         local file=$1
291         local field=$2
292         perl -e '
293                 use Dpkg::Control::Hash;
294                 my $h = new Dpkg::Control::Hash allow_pgp=>1;
295                 $h->parse(\*STDIN,"'"$file"'");
296                 my $val = $h->{"'$field'"},"\n";
297                 die "'"$file $field"'" unless defined $val;
298                 print $val,"\n";
299         ' <$file
300 }
301
302 t-ref-dsc-dgit () {
303         local dsc=${p}_${v}.dsc
304         local val=`t-822-field $tmp/incoming/$dsc Dgit`
305         perl -e '$_=shift @ARGV; die "$dsc Dgit $_ ?" unless m/^\w+\b/;' "$val"
306         t-ref-same-val $dsc "$val"
307 }
308
309 t-apply-diff () {
310         local v1=$1
311         local v2=$2
312         (cd $troot/pkg-srcs; debdiff ${p}_${v1}.dsc ${p}_${v2}.dsc) \
313                 | patch -p1 -u
314 }
315
316 t-git-config () {
317         git config --global "$@"
318 }
319
320 t-drs () {
321         export DGIT_TEST_TROOT=$troot
322         export DGIT_TEST_TMP=$tmp
323  t-git-config dgit-distro.test-dummy.git-url "ext::$troot/drs-git-ext %S "
324  t-git-config dgit-distro.test-dummy.git-check true
325  t-git-config dgit-distro.test-dummy.git-create true
326         cp $root/tests/gnupg/{dd.gpg,dm.gpg,dm.txt} $tmp/.
327         cp $root/tests/suites $tmp/.
328 }
329
330 t-dsd () {
331         t-drs
332  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
333  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
334  t-git-config dgit-distro.test-dummy.git-create true
335  t-git-config dgit-distro.test-dummy.git-url \
336                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
337
338  t-git-config dgit-distro.test-dummy.diverts.drs /drs
339  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
340  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
341  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
342  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
343
344         dsd_dispatch=$tmp/dispatch-dir/distro=test-dummy
345         mkdir -p $dsd_dispatch
346         ln -sf $root $dsd_dispatch/dgit-live
347         ln -sf $tmp/git $dsd_dispatch/repos
348         ln -sf $tmp/suites $tmp/dm.txt $dsd_dispatch/
349         mkdir -p $dsd_dispatch/keyrings
350         ln -sf $tmp/dd.gpg $dsd_dispatch/keyrings/debian-keyring.gpg
351         ln -sf $tmp/dm.gpg $dsd_dispatch/keyrings/debian-maintainers.gpg
352         ln -sf /bin/true $dsd_dispatch/policy-hook
353
354         echo 'no-such-package* drs' >$dsd_dispatch/diverts
355 }
356
357 t-policy-admin () {
358         ${DGIT_INFRA_PFX}dgit-repos-admin-debian --repos $tmp/git "$@"
359 }
360
361 t-debpolicy () {
362         t-dsd
363
364         ln -sf ${DGIT_INFRA_PFX}dgit-repos-policy-debian \
365                 $dsd_dispatch/policy-hook
366
367         mkdir $tmp/git
368         t-policy-admin create-db
369 }
370
371 t-chain-test () {
372         local ct=$1
373         local d=${0%/*}
374         cd $root
375         export DGIT_TEST_TESTNAME="$testname"
376         export ADTTMP=$tmp
377         exec "$d/$ct"
378 }       
379
380 t-alt-test () {
381         local t=${0##*/}
382         t-${t%%-*}
383         t-chain-test "${t#*-}"
384 }