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