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