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