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