chiark / gitweb /
7a5ede52f9f96178d17f513454448ecd4a331482
[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
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 fail () {
45         echo >&2 "failed: $*"
46         exit 1
47 }
48
49 t-expect-fail () {
50         local mpat="$1"; shift
51         LC_MESSAGES=C "$@" 2>&1 | tee $tmp/t.output
52         test ${PIPESTATUS[0]} != 0
53         egrep "$mpat" $tmp/t.output ||false
54 }
55
56 t-untar () {
57         local tarfile=$1.tar
58         local edittree=$1.edit
59         if test -d "$edittree"; then
60                 cp -al "$edittree"/* .
61         else
62                 tar xf "$tarfile"
63         fi
64 }
65
66 t-worktree () {
67         rm -rf $p
68         t-untar $troot/worktrees/${p}_$1
69 }
70
71 t-git () {
72         p=$1
73         v=$2
74         mkdir -p $tmp/git
75         local gs=$troot/git-srcs/${p}_$v.git
76         (set -e; cd $tmp/git; t-untar $gs)
77 }
78
79 t-git-none () {
80         mkdir -p $tmp/git
81         (set -e; cd $tmp/git; tar xf $troot/git-template.tar)
82 }
83
84 t-has-ancestor () {
85         local now=`git rev-parse HEAD`
86         local ancestor=`git rev-parse $1^{}`
87         local mbase=`git merge-base $ancestor $now`
88         if [ x$mbase != x$ancestor ]; then
89                 fail "not ff $ancestor..$now, $mbase != $ancestor"
90         fi
91 }
92
93 t-prep-newpackage () {
94         p=$1
95         v=$2
96         t-archive-none $p
97         t-git-none
98         v=3-1
99         t-worktree $v
100         cd $p
101         git branch -m dgit/sid master
102         git remote rm dgit
103         cd ..
104 }
105
106 t-archive-none () {
107         p=$1
108         mkdir -p $tmp/aq $tmp/mirror/pool/main
109         mkdir -p $tmp/aq/dsc_in_suite $tmp/aq/dsc_in_suite/sid
110
111         local suite=sid
112         >$tmp/aq/package.$suite.$p
113         t-archive-updated $suite $p
114
115         ln -s sid $tmp/aq/dsc_in_suite/unstable
116         cat <<'END' >$tmp/aq/suites
117 [
118    {
119       "archive" : "ftp-master",
120       "codename" : "sid",
121       "components" : [
122          "main",
123          "contrib",
124          "non-free"
125       ],
126       "name" : "unstable",
127       "dakname" : "unstable"
128    }
129 ]
130 END
131 }
132
133 t-archive-updated () {
134         local suite=$1
135         local p=$2
136         perl <$tmp/aq/package.$suite.$p >$tmp/aq/dsc_in_suite/$suite/$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--D} \
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
329 t-dsd () {
330         t-drs
331  t-git-config dgit-distro.test-dummy.ssh "$troot/dsd-ssh"
332  t-git-config dgit-distro.test-dummy.git-check ssh-cmd
333  t-git-config dgit-distro.test-dummy.git-create true
334  t-git-config dgit-distro.test-dummy.git-url \
335                 "ext::$troot/dsd-ssh X %S /dgit/test-dummy/repos"
336
337  t-git-config dgit-distro.test-dummy.diverts.drs /drs
338  t-git-config dgit-distro.test-dummy/drs.ssh "$troot/ssh"
339  t-git-config dgit-distro.test-dummy/drs.git-url $tmp/git
340  t-git-config dgit-distro.test-dummy/drs.git-check ssh-cmd
341  t-git-config dgit-distro.test-dummy/drs.git-create ssh-cmd
342
343         dsd_dispatch=$tmp/dispatch-dir/distro=test-dummy
344         mkdir -p $dsd_dispatch
345         ln -sf $root $dsd_dispatch/dgit-live
346         ln -sf $tmp/git $dsd_dispatch/repos
347         ln -sf $tmp/suites $tmp/dm.txt $dsd_dispatch/
348         mkdir -p $dsd_dispatch/keyrings
349         ln -sf $tmp/dd.gpg $dsd_dispatch/keyrings/debian-keyring.gpg
350         ln -sf $tmp/dm.gpg $dsd_dispatch/keyrings/debian-maintainers.gpg
351         ln -sf /bin/true $dsd_dispatch/policy-hook
352
353         echo 'no-such-package* drs' >$dsd_dispatch/diverts
354 }
355
356 t-chain-test () {
357         local ct=$1
358         local d=${0%/*}
359         cd $root
360         export DGIT_TEST_TESTNAME="$testname"
361         export ADTTMP=$tmp
362         exec "$d/$ct"
363 }       
364
365 t-alt-test () {
366         local t=${0##*/}
367         t-${t%%-*}
368         t-chain-test "${t#*-}"
369 }