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