]> git.saurik.com Git - apt.git/blame_incremental - test/integration/framework
use apt-key adv (+ gnupg) instead of gpgv for verify
[apt.git] / test / integration / framework
... / ...
CommitLineData
1#!/bin/sh -- # no runable script, just for vi
2
3EXIT_CODE=0
4
5# we all like colorful messages
6if [ "$MSGCOLOR" != 'NO' ]; then
7 if [ ! -t 1 ]; then # but check that we output to a terminal
8 export MSGCOLOR='NO'
9 fi
10fi
11
12
13if [ "$MSGCOLOR" != 'NO' ]; then
14 CERROR="\033[1;31m" # red
15 CWARNING="\033[1;33m" # yellow
16 CMSG="\033[1;32m" # green
17 CINFO="\033[1;96m" # light blue
18 CDEBUG="\033[1;94m" # blue
19 CNORMAL="\033[0;39m" # default system console color
20 CDONE="\033[1;32m" # green
21 CPASS="\033[1;32m" # green
22 CFAIL="\033[1;31m" # red
23 CCMD="\033[1;35m" # pink
24fi
25
26msgdie() { printf "${CERROR}E: $1${CNORMAL}\n" >&2; exit 1; }
27msgwarn() { printf "${CWARNING}W: $1${CNORMAL}\n" >&2; }
28msgmsg() { printf "${CMSG}$1${CNORMAL}\n"; }
29msginfo() { printf "${CINFO}I: $1${CNORMAL}\n"; }
30msgdebug() { printf "${CDEBUG}D: $1${CNORMAL}\n"; }
31msgdone() { printf "${CDONE}DONE${CNORMAL}\n"; }
32msgnwarn() { printf "${CWARNING}W: $1${CNORMAL}" >&2; }
33msgnmsg() { printf "${CMSG}$1${CNORMAL}"; }
34msgninfo() { printf "${CINFO}I: $1${CNORMAL}"; }
35msgndebug() { printf "${CDEBUG}D: $1${CNORMAL}"; }
36msgtest() {
37 while [ -n "$1" ]; do
38 printf "${CINFO}$1${CCMD} "
39 printf -- "$(echo "$2" | sed -e 's#^apt\([cgfs]\)#apt-\1#')${CINFO} "
40 shift
41 if [ -n "$1" ]; then shift; else break; fi
42 done
43 printf "…${CNORMAL} "
44}
45msgpass() { printf "${CPASS}PASS${CNORMAL}\n"; }
46msgskip() { printf "${CWARNING}SKIP${CNORMAL}\n" >&2; }
47msgfail() {
48 if [ $# -gt 0 ]; then printf "${CFAIL}FAIL: $*${CNORMAL}\n" >&2;
49 else printf "${CFAIL}FAIL${CNORMAL}\n" >&2; fi
50 EXIT_CODE=$((EXIT_CODE+1));
51}
52
53# enable / disable Debugging
54MSGLEVEL=${MSGLEVEL:-3}
55if [ $MSGLEVEL -le 0 ]; then
56 msgdie() { true; }
57fi
58if [ $MSGLEVEL -le 1 ]; then
59 msgwarn() { true; }
60 msgnwarn() { true; }
61fi
62if [ $MSGLEVEL -le 2 ]; then
63 msgmsg() { true; }
64 msgnmsg() { true; }
65 msgtest() { true; }
66 msgpass() { printf " ${CPASS}P${CNORMAL}"; }
67 msgskip() { printf " ${CWARNING}S${CNORMAL}" >&2; }
68 if [ -n "$CFAIL" ]; then
69 msgfail() { printf " ${CFAIL}FAIL${CNORMAL}" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
70 else
71 msgfail() { printf " ###FAILED###" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
72 fi
73fi
74if [ $MSGLEVEL -le 3 ]; then
75 msginfo() { true; }
76 msgninfo() { true; }
77fi
78if [ $MSGLEVEL -le 4 ]; then
79 msgdebug() { true; }
80 msgndebug() { true; }
81fi
82msgdone() {
83 if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
84 [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
85 [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
86 [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
87 [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
88 true;
89 else
90 printf "${CDONE}DONE${CNORMAL}\n";
91 fi
92}
93getaptconfig() {
94 if [ -f ./aptconfig.conf ]; then
95 echo "./aptconfig.conf"
96 elif [ -f ../aptconfig.conf ]; then
97 echo "../aptconfig.conf"
98 fi
99}
100runapt() {
101 msgdebug "Executing: ${CCMD}$*${CDEBUG} "
102 local CMD="$1"
103 shift
104 case $CMD in
105 sh|aptitude|*/*|command) ;;
106 *) CMD="${BUILDDIRECTORY}/$CMD";;
107 esac
108 MALLOC_PERTURB_=21 MALLOC_CHECK_=2 APT_CONFIG="$(getaptconfig)" LD_LIBRARY_PATH=${LIBRARYPATH} $CMD "$@"
109}
110aptconfig() { runapt apt-config "$@"; }
111aptcache() { runapt apt-cache "$@"; }
112aptcdrom() { runapt apt-cdrom "$@"; }
113aptget() { runapt apt-get "$@"; }
114aptftparchive() { runapt apt-ftparchive "$@"; }
115aptkey() { runapt apt-key "$@"; }
116aptmark() { runapt apt-mark "$@"; }
117aptsortpkgs() { runapt apt-sortpkgs "$@"; }
118apt() { runapt apt "$@"; }
119apthelper() { runapt "${APTHELPERBINDIR}/apt-helper" "$@"; }
120aptwebserver() { runapt "${APTWEBSERVERBINDIR}/aptwebserver" "$@"; }
121aptitude() { runapt aptitude "$@"; }
122aptextracttemplates() { runapt apt-extracttemplates "$@"; }
123aptinternalsolver() { runapt "${APTINTERNALSOLVER}" "$@"; }
124
125dpkg() {
126 command dpkg --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log "$@"
127}
128dpkgcheckbuilddeps() {
129 command dpkg-checkbuilddeps --admindir=${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg "$@"
130}
131gdb() {
132 local CMD="$1"
133 shift
134 runapt command gdb --quiet -ex run "${BUILDDIRECTORY}/$CMD" --args "${BUILDDIRECTORY}/$CMD" "$@"
135}
136gpg() {
137 # see apt-key for the whole trickery. Setup is done in setupenvironment
138 command gpg --ignore-time-conflict --no-options --no-default-keyring \
139 --homedir "${TMPWORKINGDIRECTORY}/gnupghome" \
140 --no-auto-check-trustdb --trust-model always \
141 "$@"
142}
143
144exitwithstatus() {
145 # error if we about to overflow, but ...
146 # "255 failures ought to be enough for everybody"
147 if [ $EXIT_CODE -gt 255 ]; then
148 msgdie "Total failure count $EXIT_CODE too big"
149 fi
150 exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
151}
152
153shellsetedetector() {
154 local exit_status=$?
155 if [ "$exit_status" != '0' ]; then
156 printf >&2 "${CERROR}E: Looks like the testcases ended prematurely with exitcode: ${exit_status}${CNORMAL}\n"
157 if [ "$EXIT_CODE" = '0' ]; then
158 EXIT_CODE="$exit_status"
159 fi
160 fi
161}
162
163addtrap() {
164 if [ "$1" = 'prefix' ]; then
165 CURRENTTRAP="$2 $CURRENTTRAP"
166 else
167 CURRENTTRAP="$CURRENTTRAP $1"
168 fi
169 trap "shellsetedetector; $CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
170}
171
172setupenvironment() {
173 TMPWORKINGDIRECTORY=$(mktemp -d)
174 TESTDIRECTORY=$(readlink -f $(dirname $0))
175 msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
176
177 # allow overriding the default BUILDDIR location
178 BUILDDIRECTORY=${APT_INTEGRATION_TESTS_BUILD_DIR:-"${TESTDIRECTORY}/../../build/bin"}
179 LIBRARYPATH=${APT_INTEGRATION_TESTS_LIBRARY_PATH:-"${BUILDDIRECTORY}"}
180 METHODSDIR=${APT_INTEGRATION_TESTS_METHODS_DIR:-"${BUILDDIRECTORY}/methods"}
181 APTHELPERBINDIR=${APT_INTEGRATION_TESTS_LIBEXEC_DIR:-"${BUILDDIRECTORY}"}
182 APTWEBSERVERBINDIR=${APT_INTEGRATION_TESTS_WEBSERVER_BIN_DIR:-"${BUILDDIRECTORY}"}
183 APTINTERNALSOLVER=${APT_INTEGRATION_TESTS_INTERNAL_SOLVER:-"${BUILDDIRECTORY}/apt-internal-solver"}
184 test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
185 # -----
186
187 addtrap "cd /; rm -rf $TMPWORKINGDIRECTORY;"
188 cd $TMPWORKINGDIRECTORY
189 mkdir rootdir aptarchive keys
190 cd rootdir
191 mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
192 mkdir -p var/cache var/lib/apt var/log tmp
193 mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
194 touch var/lib/dpkg/available
195 mkdir -p usr/lib/apt
196 ln -s ${METHODSDIR} usr/lib/apt/methods
197 if [ "$BUILDDIRECTORY" = "$LIBRARYPATH" ]; then
198 mkdir -p usr/lib/apt/solvers
199 ln -s "${BUILDDIRECTORY}/apt-dump-solver" usr/lib/apt/solvers/dump
200 ln -s "${BUILDDIRECTORY}/apt-internal-solver" usr/lib/apt/solvers/apt
201 echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" > etc/apt/apt.conf.d/externalsolver.conf
202 fi
203 # use the autoremove from the BUILDDIRECTORY if its there, otherwise
204 # system
205 if [ -e ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove ]; then
206 ln -s ${BUILDDIRECTORY}/../../debian/apt.conf.autoremove etc/apt/apt.conf.d/01autoremove
207 else
208 ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
209 fi
210 cd ..
211 local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
212 if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
213 cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
214 fi
215 local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
216 if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
217 cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
218 fi
219 cp $(find $TESTDIRECTORY -name '*.pub' -o -name '*.sec') keys/
220 ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
221 echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
222 echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
223 echo "Debug::NoLocking \"true\";" >> aptconfig.conf
224 echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
225 echo "Dir::Bin::Methods \"${METHODSDIR}\";" >> aptconfig.conf
226 echo "Dir::Bin::apt-key \"${BUILDDIRECTORY}/apt-key\";" >> aptconfig.conf
227 echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
228 echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
229 echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
230 echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
231 echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
232 if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
233 echo "DPKG::options:: \"--force-architecture\";" >> aptconfig.conf # Added to test multiarch before dpkg is ready for it…
234 fi
235 echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
236 echo 'quiet::NoUpdate "true";' >> aptconfig.conf
237 echo 'quiet::NoStatistic "true";' >> aptconfig.conf
238 echo "Acquire::https::CaInfo \"${TESTDIR}/apt.pem\";" > rootdir/etc/apt/apt.conf.d/99https
239 echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
240 configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
241
242 # gpg needs a trustdb to function, but it can't be invalid (not even empty)
243 # see also apt-key where this trickery comes from:
244 local TRUSTDBDIR="${TMPWORKINGDIRECTORY}/gnupghome"
245 mkdir "$TRUSTDBDIR"
246 chmod 700 "$TRUSTDBDIR"
247 # We also don't use a secret keyring, of course, but gpg panics and
248 # implodes if there isn't one available - and writeable for imports
249 local SECRETKEYRING="${TRUSTDBDIR}/secring.gpg"
250 touch $SECRETKEYRING
251 # now create the trustdb with an (empty) dummy keyring
252 # newer gpg versions are fine without it, but play it safe for now
253 gpg --quiet --check-trustdb --secret-keyring $SECRETKEYRING --keyring $SECRETKEYRING >/dev/null 2>&1
254
255 # cleanup the environment a bit
256 # prefer our apt binaries over the system apt binaries
257 export PATH="${BUILDDIRECTORY}:${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
258 export LC_ALL=C.UTF-8
259 unset LANGUAGE APT_CONFIG
260 unset GREP_OPTIONS DEB_BUILD_PROFILES
261
262 msgdone "info"
263}
264
265getarchitecture() {
266 if [ "$1" = "native" -o -z "$1" ]; then
267 eval `aptconfig shell ARCH APT::Architecture`
268 if [ -n "$ARCH" ]; then
269 echo $ARCH
270 else
271 dpkg --print-architecture
272 fi
273 else
274 echo $1
275 fi
276}
277
278getarchitectures() {
279 echo "$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
280}
281
282getarchitecturesfromcommalist() {
283 echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
284}
285
286configarchitecture() {
287 {
288 echo "APT::Architecture \"$(getarchitecture $1)\";"
289 while [ -n "$1" ]; do
290 echo "APT::Architectures:: \"$(getarchitecture $1)\";"
291 shift
292 done
293 } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
294 configdpkg
295}
296
297configdpkg() {
298 if [ ! -e rootdir/var/lib/dpkg/status ]; then
299 local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
300 if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
301 cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
302 else
303 echo -n > rootdir/var/lib/dpkg/status
304 fi
305 fi
306 rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
307 if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
308 local ARCHS="$(getarchitectures)"
309 if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
310 DPKGARCH="$(dpkg --print-architecture)"
311 for ARCH in ${ARCHS}; do
312 if [ "${ARCH}" != "${DPKGARCH}" ]; then
313 if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
314 # old-style used e.g. in Ubuntu-P – and as it seems travis
315 echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
316 echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
317 fi
318 fi
319 done
320 if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
321 # dpkg doesn't really check the version as long as it is fully installed,
322 # but just to be sure we choose one above the required version
323 insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
324 fi
325 fi
326 fi
327}
328
329configcompression() {
330 while [ -n "$1" ]; do
331 case "$1" in
332 '.') printf ".\t.\tcat\n";;
333 'gz') printf "gzip\tgz\tgzip\n";;
334 'bz2') printf "bzip2\tbz2\tbzip2\n";;
335 'lzma') printf "lzma\tlzma\txz --format=lzma\n";;
336 'xz') printf "xz\txz\txz\n";;
337 *) printf "$1\t$1\t$1\n";;
338 esac
339 shift
340 done > ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf
341}
342
343setupsimplenativepackage() {
344 local NAME="$1"
345 local ARCH="$2"
346 local VERSION="$3"
347 local RELEASE="${4:-unstable}"
348 local DEPENDENCIES="$5"
349 local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
350 If you find such a package installed on your system,
351 something went horribly wrong! They are autogenerated
352 und used only by testcases and surf no other propose…"}"
353
354 local SECTION="${7:-others}"
355 local DISTSECTION
356 if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
357 DISTSECTION="main"
358 else
359 DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
360 fi
361 local BUILDDIR=incoming/${NAME}-${VERSION}
362 mkdir -p ${BUILDDIR}/debian/source
363 cd ${BUILDDIR}
364 echo "* most suckless software product ever" > FEATURES
365 test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
366 test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
367
368 * Initial release
369
370 -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
371 test -e debian/control || echo "Source: $NAME
372Section: $SECTION
373Priority: optional
374Maintainer: Joe Sixpack <joe@example.org>
375Build-Depends: debhelper (>= 7)
376Standards-Version: 3.9.1
377
378Package: $NAME" > debian/control
379 if [ "$ARCH" = 'all' ]; then
380 echo "Architecture: all" >> debian/control
381 else
382 echo "Architecture: any" >> debian/control
383 fi
384 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
385 echo "Description: $DESCRIPTION" >> debian/control
386
387 test -e debian/compat || echo "7" > debian/compat
388 test -e debian/source/format || echo "3.0 (native)" > debian/source/format
389 test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
390 cd - > /dev/null
391}
392
393buildsimplenativepackage() {
394 local NAME="$1"
395 local ARCH="$2"
396 local VERSION="$3"
397 local RELEASE="${4:-unstable}"
398 local DEPENDENCIES="$5"
399 local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
400 If you find such a package installed on your system,
401 something went horribly wrong! They are autogenerated
402 und used only by testcases and surf no other propose…"}"
403
404 local SECTION="${7:-others}"
405 local PRIORITY="${8:-optional}"
406 local FILE_TREE="$9"
407 local COMPRESS_TYPE="${10:-gzip}"
408 local DISTSECTION
409 if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
410 DISTSECTION="main"
411 else
412 DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
413 fi
414 local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
415
416 msgninfo "Build package ${NAME} in ${VERSION} for ${RELEASE} in ${DISTSECTION}… "
417 mkdir -p $BUILDDIR/debian/source
418 echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
419 echo "#!/bin/sh
420echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
421
422 echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
423 echo "$NAME ($VERSION) $RELEASE; urgency=low
424
425 * Initial release
426
427 -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
428 echo "Source: $NAME
429Section: $SECTION
430Priority: $PRIORITY
431Maintainer: Joe Sixpack <joe@example.org>
432Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
433 local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
434 test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
435 echo "
436Package: $NAME" >> ${BUILDDIR}/debian/control
437
438 if [ "$ARCH" = 'all' ]; then
439 echo "Architecture: all" >> ${BUILDDIR}/debian/control
440 else
441 echo "Architecture: any" >> ${BUILDDIR}/debian/control
442 fi
443 local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
444 test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
445 echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
446
447 echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
448 (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' \
449 | while read SRC; do
450 echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
451# if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
452# gpg --yes --secret-keyring ./keys/joesixpack.sec \
453# --keyring ./keys/joesixpack.pub --default-key 'Joe Sixpack' \
454# --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
455# mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
456# fi
457 done
458
459 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
460 rm -rf ${BUILDDIR}/debian/tmp
461 mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
462 cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
463 cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
464 if [ -n "$FILE_TREE" ]; then
465 cp -ar "$FILE_TREE" ${BUILDDIR}/debian/tmp
466 fi
467
468 (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
469 (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
470 local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
471 # ensure the right permissions as dpkg-deb ensists
472 chmod 755 ${BUILDDIR}/debian/tmp/DEBIAN
473 if ! dpkg-deb -Z${COMPRESS_TYPE} --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. >$LOG 2>&1; then
474 cat $LOG
475 false
476 fi
477 rm $LOG
478 echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
479 done
480
481 mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
482 cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
483 cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
484 rm -rf "${BUILDDIR}"
485 msgdone "info"
486}
487
488buildpackage() {
489 local BUILDDIR=$1
490 local RELEASE=$2
491 local SECTION=$3
492 local ARCH=$(getarchitecture $4)
493 local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
494 local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
495 msgninfo "Build package ${PKGNAME} for ${RELEASE} in ${SECTION}… "
496 cd $BUILDDIR
497 if [ "$ARCH" = "all" ]; then
498 ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
499 fi
500 if ! dpkg-buildpackage -uc -us -a$ARCH >$BUILDLOG 2>&1 ; then
501 cat $BUILDLOG
502 false
503 fi
504 local PKGS="$(grep '^dpkg-deb: building package' $BUILDLOG | cut -d'/' -f 2 | sed -e "s#'\.##")"
505 local SRCS="$(grep '^dpkg-source: info: building' $BUILDLOG | grep -o '[a-z0-9._+~-]*$')"
506 cd - > /dev/null
507 for PKG in $PKGS; do
508 echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
509 done
510 for SRC in $SRCS; do
511 echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
512 done
513 msgdone "info"
514}
515
516buildaptarchive() {
517 if [ -d incoming ]; then
518 buildaptarchivefromincoming "$@"
519 else
520 buildaptarchivefromfiles "$@"
521 fi
522}
523
524createaptftparchiveconfig() {
525 local COMPRESSORS="$(cut -d' ' -f 1 ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | tr '\n' ' ')"
526 COMPRESSORS="${COMPRESSORS%* }"
527 local ARCHS="$(find pool/ -name '*.deb' | grep -oE '_[a-z0-9-]+\.deb$' | sort | uniq | sed -e '/^_all.deb$/ d' -e 's#^_\([a-z0-9-]*\)\.deb$#\1#' | tr '\n' ' ')"
528 if [ -z "$ARCHS" ]; then
529 # the pool is empty, so we will operate on faked packages - let us use the configured archs
530 ARCHS="$(getarchitectures)"
531 fi
532 echo -n 'Dir {
533 ArchiveDir "' >> ftparchive.conf
534 echo -n $(readlink -f .) >> ftparchive.conf
535 echo -n '";
536 CacheDir "' >> ftparchive.conf
537 echo -n $(readlink -f ..) >> ftparchive.conf
538 echo -n '";
539 FileListDir "' >> ftparchive.conf
540 echo -n $(readlink -f pool/) >> ftparchive.conf
541 echo -n '";
542};
543Default {
544 Packages::Compress "'"$COMPRESSORS"'";
545 Sources::Compress "'"$COMPRESSORS"'";
546 Contents::Compress "'"$COMPRESSORS"'";
547 Translation::Compress "'"$COMPRESSORS"'";
548 LongDescription "false";
549};
550TreeDefault {
551 Directory "pool/";
552 SrcDirectory "pool/";
553};
554APT {
555 FTPArchive {
556 Release {
557 Origin "joesixpack";
558 Label "apttestcases";
559 Suite "unstable";
560 Description "repository with dummy packages";
561 Architectures "' >> ftparchive.conf
562 echo -n "$ARCHS" >> ftparchive.conf
563 echo 'source";
564 };
565 };
566};' >> ftparchive.conf
567 for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
568 echo -n 'tree "dists/' >> ftparchive.conf
569 echo -n "$DIST" >> ftparchive.conf
570 echo -n '" {
571 Architectures "' >> ftparchive.conf
572 echo -n "$ARCHS" >> ftparchive.conf
573 echo -n 'source";
574 FileList "' >> ftparchive.conf
575 echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
576 echo -n '";
577 SourceFileList "' >> ftparchive.conf
578 echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
579 echo -n '";
580 Sections "' >> ftparchive.conf
581 echo -n "$(find ./pool/ -maxdepth 1 -name "${DIST}.*.pkglist" -type f | cut -d'/' -f 3 | cut -d'.' -f 2 | sort | uniq | tr '\n' ' ')" >> ftparchive.conf
582 echo '";
583};' >> ftparchive.conf
584 done
585}
586
587buildaptftparchivedirectorystructure() {
588 local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
589 for DIST in $DISTS; do
590 local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
591 for SECTION in $SECTIONS; do
592 local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
593 for ARCH in $ARCHS; do
594 mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
595 done
596 mkdir -p dists/${DIST}/${SECTION}/source
597 mkdir -p dists/${DIST}/${SECTION}/i18n
598 done
599 done
600}
601
602insertpackage() {
603 local RELEASE="$1"
604 local NAME="$2"
605 local ARCH="$3"
606 local VERSION="$4"
607 local DEPENDENCIES="$5"
608 local PRIORITY="${6:-optional}"
609 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
610 If you find such a package installed on your system,
611 something went horribly wrong! They are autogenerated
612 und used only by testcases and surf no other propose…"}"
613 local ARCHS=""
614 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
615 if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
616 ARCHS="$(getarchitectures)"
617 else
618 ARCHS="$arch"
619 fi
620 for BUILDARCH in $ARCHS; do
621 local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
622 mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
623 touch aptarchive/dists/${RELEASE}/main/source/Sources
624 local FILE="${PPATH}/Packages"
625 echo "Package: $NAME
626Priority: $PRIORITY
627Section: other
628Installed-Size: 42
629Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
630 test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
631 echo "Version: $VERSION
632Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
633 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
634 echo "Description: $DESCRIPTION" >> $FILE
635 echo >> $FILE
636 done
637 done
638}
639
640insertsource() {
641 local RELEASE="$1"
642 local NAME="$2"
643 local ARCH="$3"
644 local VERSION="$4"
645 local DEPENDENCIES="$5"
646 local ARCHS=""
647 local SPATH="aptarchive/dists/${RELEASE}/main/source"
648 mkdir -p $SPATH
649 local FILE="${SPATH}/Sources"
650 echo "Package: $NAME
651Binary: $NAME
652Version: $VERSION
653Maintainer: Joe Sixpack <joe@example.org>
654Architecture: $ARCH" >> $FILE
655 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
656 echo "Files:
657 d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
658 d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
659" >> $FILE
660}
661
662insertinstalledpackage() {
663 local NAME="$1"
664 local ARCH="$2"
665 local VERSION="$3"
666 local DEPENDENCIES="$4"
667 local PRIORITY="${5:-optional}"
668 local STATUS="${6:-install ok installed}"
669 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
670 If you find such a package installed on your system,
671 something went horribly wrong! They are autogenerated
672 und used only by testcases and surf no other propose…"}"
673
674 local FILE='rootdir/var/lib/dpkg/status'
675 local INFO='rootdir/var/lib/dpkg/info'
676 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
677 echo "Package: $NAME
678Status: $STATUS
679Priority: $PRIORITY
680Section: other
681Installed-Size: 42
682Maintainer: Joe Sixpack <joe@example.org>
683Version: $VERSION" >> $FILE
684 test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
685 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
686 echo "Description: $DESCRIPTION" >> $FILE
687 echo >> $FILE
688 if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
689 echo -n > ${INFO}/${NAME}:${arch}.list
690 else
691 echo -n > ${INFO}/${NAME}.list
692 fi
693 done
694}
695
696
697buildaptarchivefromincoming() {
698 msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
699 cd aptarchive
700 [ -e pool ] || ln -s ../incoming pool
701 [ -e ftparchive.conf ] || createaptftparchiveconfig
702 [ -e dists ] || buildaptftparchivedirectorystructure
703 msgninfo "\tGenerate Packages, Sources and Contents files… "
704 aptftparchive -qq generate ftparchive.conf
705 cd - > /dev/null
706 msgdone "info"
707 generatereleasefiles
708}
709
710buildaptarchivefromfiles() {
711 msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
712 find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
713 msgninfo "\t${line} file… "
714 compressfile "$line" "$1"
715 msgdone "info"
716 done
717 generatereleasefiles "$@"
718}
719
720compressfile() {
721 cat ${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf | while read compressor extension command; do
722 if [ "$compressor" = '.' ]; then
723 if [ -n "$2" ]; then
724 touch -d "$2" "$1"
725 fi
726 continue
727 fi
728 cat "$1" | $command > "${1}.${extension}"
729 if [ -n "$2" ]; then
730 touch -d "$2" "${1}.${extension}"
731 fi
732 done
733}
734
735# can be overridden by testcases for their pleasure
736getcodenamefromsuite() {
737 case "$1" in
738 unstable) echo 'sid';;
739 *) echo -n "$1";;
740 esac
741}
742getreleaseversionfromsuite() { true; }
743getlabelfromsuite() { true; }
744
745generatereleasefiles() {
746 # $1 is the Date header and $2 is the ValidUntil header to be set
747 # both should be given in notation date/touch can understand
748 msgninfo "\tGenerate Release files… "
749 if [ -e aptarchive/dists ]; then
750 for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
751 local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
752 local CODENAME="$(getcodenamefromsuite $SUITE)"
753 local VERSION="$(getreleaseversionfromsuite $SUITE)"
754 local LABEL="$(getlabelfromsuite $SUITE)"
755 if [ -n "$VERSION" ]; then
756 VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
757 fi
758 if [ -n "$LABEL" ]; then
759 LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
760 fi
761 aptftparchive -qq release $dir \
762 -o APT::FTPArchive::Release::Suite="${SUITE}" \
763 -o APT::FTPArchive::Release::Codename="${CODENAME}" \
764 ${LABEL} \
765 ${VERSION} \
766 | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
767 if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
768 sed -i '/^Date: / a\
769NotAutomatic: yes' $dir/Release
770 fi
771 if [ -n "$1" -a "$1" != "now" ]; then
772 sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
773 fi
774 if [ -n "$2" ]; then
775 sed -i "/^Date: / a\
776Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
777 fi
778 done
779 else
780 aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
781 fi
782 if [ -n "$1" -a "$1" != "now" ]; then
783 for release in $(find ./aptarchive -name 'Release'); do
784 touch -d "$1" $release
785 done
786 fi
787 msgdone "info"
788}
789
790setupdistsaptarchive() {
791 local APTARCHIVE=$(readlink -f ./aptarchive)
792 rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
793 rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
794 for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
795 SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
796 msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
797 echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
798 echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
799 msgdone "info"
800 done
801}
802
803setupflataptarchive() {
804 local APTARCHIVE=$(readlink -f ./aptarchive)
805 if [ -f ${APTARCHIVE}/Packages ]; then
806 msgninfo "\tadd deb sources.list line… "
807 echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
808 msgdone "info"
809 else
810 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
811 fi
812 if [ -f ${APTARCHIVE}/Sources ]; then
813 msgninfo "\tadd deb-src sources.list line… "
814 echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
815 msgdone "info"
816 else
817 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
818 fi
819}
820
821setupaptarchive() {
822 buildaptarchive
823 if [ -e aptarchive/dists ]; then
824 setupdistsaptarchive
825 else
826 setupflataptarchive
827 fi
828 signreleasefiles
829 if [ "$1" != '--no-update' ]; then
830 msgninfo "\tSync APT's cache with the archive… "
831 aptget update -qq
832 msgdone "info"
833 fi
834}
835
836signreleasefiles() {
837 local SIGNER="${1:-Joe Sixpack}"
838 local GPG="gpg --batch --yes"
839 msgninfo "\tSign archive with $SIGNER key… "
840 local REXKEY='keys/rexexpired'
841 local SECEXPIREBAK="${REXKEY}.sec.bak"
842 local PUBEXPIREBAK="${REXKEY}.pub.bak"
843 if [ "${SIGNER}" = 'Rex Expired' ]; then
844 # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
845 # option doesn't exist anymore (and using faketime would add a new obscure dependency)
846 # therefore we 'temporary' make the key not expired and restore a backup after signing
847 cp ${REXKEY}.sec $SECEXPIREBAK
848 cp ${REXKEY}.pub $PUBEXPIREBAK
849 local SECUNEXPIRED="${REXKEY}.sec.unexpired"
850 local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
851 if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
852 cp $SECUNEXPIRED ${REXKEY}.sec
853 cp $PUBUNEXPIRED ${REXKEY}.pub
854 else
855 printf "expire\n1w\nsave\n" | $GPG --keyring ${REXKEY}.pub --secret-keyring ${REXKEY}.sec --command-fd 0 --edit-key "${SIGNER}" >/dev/null 2>&1 || true
856 cp ${REXKEY}.sec $SECUNEXPIRED
857 cp ${REXKEY}.pub $PUBUNEXPIRED
858 fi
859 fi
860 for KEY in $(find keys/ -name '*.sec'); do
861 GPG="$GPG --secret-keyring $KEY"
862 done
863 for KEY in $(find keys/ -name '*.pub'); do
864 GPG="$GPG --keyring $KEY"
865 done
866 for RELEASE in $(find aptarchive/ -name Release); do
867 $GPG --default-key "$SIGNER" --armor --detach-sign --sign --output ${RELEASE}.gpg ${RELEASE}
868 local INRELEASE="$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')"
869 $GPG --default-key "$SIGNER" --clearsign --output $INRELEASE $RELEASE
870 # we might have set a specific date for the Release file, so copy it
871 touch -d "$(stat --format "%y" ${RELEASE})" ${RELEASE}.gpg ${INRELEASE}
872 done
873 if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
874 mv -f $SECEXPIREBAK ${REXKEY}.sec
875 mv -f $PUBEXPIREBAK ${REXKEY}.pub
876 fi
877 msgdone "info"
878}
879
880webserverconfig() {
881 msgtest "Set webserver config option '${1}' to" "$2"
882 local DOWNLOG='rootdir/tmp/download-testfile.log'
883 local STATUS='rootdir/tmp/webserverconfig.status'
884 rm -f "$STATUS" "$DOWNLOG"
885 if downloadfile "http://localhost:8080/_config/set/${1}/${2}" "$STATUS" > "$DOWNLOG"; then
886 msgpass
887 else
888 cat "$DOWNLOG" "$STATUS"
889 msgfail
890 fi
891 testwebserverlaststatuscode '200'
892}
893
894rewritesourceslist() {
895 local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive")"
896 for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
897 sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#http://localhost:8080/#${1}#" -e "s#http://localhost:4433/#${1}#"
898 done
899}
900
901# wait for up to 10s for a pid file to appear to avoid possible race
902# when a helper is started and dosn't write the PID quick enough
903waitforpidfile() {
904 local PIDFILE="$1"
905 for i in $(seq 10); do
906 if test -s "$PIDFILE"; then
907 return 0
908 fi
909 sleep 1
910 done
911 msgdie "waiting for $PIDFILE failed"
912 return 1
913}
914
915changetowebserver() {
916 if [ "$1" != '--no-rewrite' ]; then
917 rewritesourceslist 'http://localhost:8080/'
918 else
919 shift
920 fi
921 if test -x ${APTWEBSERVERBINDIR}/aptwebserver; then
922 cd aptarchive
923 local LOG="webserver.log"
924 if ! aptwebserver -o aptwebserver::fork=1 "$@" >$LOG 2>&1 ; then
925 cat $LOG
926 false
927 fi
928 waitforpidfile aptwebserver.pid
929 local PID="$(cat aptwebserver.pid)"
930 if [ -z "$PID" ]; then
931 msgdie 'Could not fork aptwebserver successfully'
932 fi
933 addtrap "kill $PID;"
934 cd - > /dev/null
935 else
936 msgdie 'You have to build aptwerbserver or install a webserver'
937 fi
938}
939
940changetohttpswebserver() {
941 if ! which stunnel4 >/dev/null; then
942 msgdie 'You need to install stunnel4 for https testcases'
943 fi
944 if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
945 changetowebserver --no-rewrite "$@"
946 fi
947 echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
948cert = ${TESTDIRECTORY}/apt.pem
949output = /dev/null
950
951[https]
952accept = 4433
953connect = 8080
954" > ${TMPWORKINGDIRECTORY}/stunnel.conf
955 stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
956 waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
957 local PID="$(cat ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid)"
958 if [ -z "$PID" ]; then
959 msgdie 'Could not fork stunnel4 successfully'
960 fi
961 addtrap 'prefix' "kill ${PID};"
962 rewritesourceslist 'https://localhost:4433/'
963}
964
965changetocdrom() {
966 mkdir -p rootdir/media/cdrom/.disk
967 local CD="$(readlink -f rootdir/media/cdrom)"
968 echo "acquire::cdrom::mount \"${CD}\";
969acquire::cdrom::${CD}/::mount \"mv ${CD}-unmounted ${CD}\";
970acquire::cdrom::${CD}/::umount \"mv ${CD} ${CD}-unmounted\";
971acquire::cdrom::autodetect 0;" > rootdir/etc/apt/apt.conf.d/00cdrom
972 echo -n "$1" > ${CD}/.disk/info
973 if [ ! -d aptarchive/dists ]; then
974 msgdie 'Flat file archive cdroms can not be created currently'
975 return 1
976 fi
977 mv aptarchive/dists "$CD"
978 ln -s "$(readlink -f ./incoming)" $CD/pool
979 find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
980 # start with an unmounted disk
981 mv "${CD}" "${CD}-unmounted"
982 # we don't want the disk to be modifiable
983 addtrap 'prefix' "chmod -f -R +w $PWD/rootdir/media/cdrom/dists/ $PWD/rootdir/media/cdrom-unmounted/dists/ || true;"
984 chmod -R -w rootdir/media/cdrom-unmounted/dists
985}
986
987downloadfile() {
988 local PROTO="$(echo "$1" | cut -d':' -f 1 )"
989 apthelper -o Debug::Acquire::${PROTO}=1 \
990 download-file "$1" "$2" 2>&1 || true
991 # only if the file exists the download was successful
992 if [ -e "$2" ]; then
993 return 0
994 else
995 return 1
996 fi
997}
998
999checkdiff() {
1000 local DIFFTEXT="$(command diff -u "$@" | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
1001 if [ -n "$DIFFTEXT" ]; then
1002 echo >&2
1003 echo >&2 "$DIFFTEXT"
1004 return 1
1005 else
1006 return 0
1007 fi
1008}
1009
1010testfileequal() {
1011 local FILE="$1"
1012 shift
1013 msgtest "Test for correctness of file" "$FILE"
1014 if [ -z "$*" ]; then
1015 echo -n "" | checkdiff $FILE - && msgpass || msgfail
1016 else
1017 echo "$*" | checkdiff $FILE - && msgpass || msgfail
1018 fi
1019}
1020
1021testempty() {
1022 msgtest "Test for no output of" "$*"
1023 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
1024 if $* >$COMPAREFILE 2>&1 && test ! -s $COMPAREFILE; then
1025 msgpass
1026 else
1027 cat $COMPAREFILE
1028 msgfail
1029 fi
1030}
1031
1032testequal() {
1033 local MSG='Test of equality of'
1034 if [ "$1" = '--nomsg' ]; then
1035 MSG=''
1036 shift
1037 fi
1038
1039 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
1040 echo "$1" > $COMPAREFILE
1041 shift
1042
1043 if [ -n "$MSG" ]; then
1044 msgtest "$MSG" "$*"
1045 fi
1046 $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1047}
1048
1049testequalor2() {
1050 local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
1051 local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
1052 local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
1053 echo "$1" > $COMPAREFILE1
1054 echo "$2" > $COMPAREFILE2
1055 shift 2
1056 msgtest "Test for equality OR of" "$*"
1057 $* >$COMPAREAGAINST 2>&1 || true
1058 if checkdiff $COMPAREFILE1 $COMPAREAGAINST >/dev/null 2>&1 || \
1059 checkdiff $COMPAREFILE2 $COMPAREAGAINST >/dev/null 2>&1
1060 then
1061 msgpass
1062 else
1063 echo -n "\n${CINFO}Diff against OR 1${CNORMAL}"
1064 checkdiff $COMPAREFILE1 $COMPAREAGAINST || true
1065 echo -n "${CINFO}Diff against OR 2${CNORMAL}"
1066 checkdiff $COMPAREFILE2 $COMPAREAGAINST || true
1067 msgfail
1068 fi
1069}
1070
1071testshowvirtual() {
1072 local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
1073 local PACKAGE="$1"
1074 shift
1075 while [ -n "$1" ]; do
1076 VIRTUAL="${VIRTUAL}
1077N: Can't select versions from package '$1' as it is purely virtual"
1078 PACKAGE="${PACKAGE} $1"
1079 shift
1080 done
1081 msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
1082 VIRTUAL="${VIRTUAL}
1083N: No packages found"
1084 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
1085 local ARCH="$(getarchitecture 'native')"
1086 echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
1087 aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1088}
1089
1090testnopackage() {
1091 msgtest "Test for non-existent packages" "apt-cache show $*"
1092 local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
1093 if [ -n "$SHOWPKG" ]; then
1094 echo >&2
1095 echo >&2 "$SHOWPKG"
1096 msgfail
1097 else
1098 msgpass
1099 fi
1100}
1101
1102testdpkginstalled() {
1103 msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
1104 local PKGS="$(dpkg -l "$@" 2>/dev/null | grep '^i' | wc -l)"
1105 if [ "$PKGS" != $# ]; then
1106 echo >&2 $PKGS
1107 dpkg -l "$@" | grep '^[a-z]' >&2
1108 msgfail
1109 else
1110 msgpass
1111 fi
1112}
1113
1114testdpkgnotinstalled() {
1115 msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
1116 local PKGS="$(dpkg -l "$@" 2> /dev/null | grep '^i' | wc -l)"
1117 if [ "$PKGS" != 0 ]; then
1118 echo
1119 dpkg -l "$@" | grep '^[a-z]' >&2
1120 msgfail
1121 else
1122 msgpass
1123 fi
1124}
1125
1126testmarkedauto() {
1127 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
1128 if [ -n "$1" ]; then
1129 msgtest 'Test for correctly marked as auto-installed' "$*"
1130 while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
1131 else
1132 msgtest 'Test for correctly marked as auto-installed' 'no package'
1133 echo -n > $COMPAREFILE
1134 fi
1135 aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
1136}
1137
1138testsuccess() {
1139 if [ "$1" = '--nomsg' ]; then
1140 shift
1141 else
1142 msgtest 'Test for successful execution of' "$*"
1143 fi
1144 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output"
1145 if $@ >${OUTPUT} 2>&1; then
1146 msgpass
1147 else
1148 echo >&2
1149 cat >&2 $OUTPUT
1150 msgfail
1151 fi
1152}
1153
1154testfailure() {
1155 if [ "$1" = '--nomsg' ]; then
1156 shift
1157 else
1158 msgtest 'Test for failure in execution of' "$*"
1159 fi
1160 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
1161 if $@ >${OUTPUT} 2>&1; then
1162 echo >&2
1163 cat >&2 $OUTPUT
1164 msgfail
1165 else
1166 msgpass
1167 fi
1168}
1169
1170testwebserverlaststatuscode() {
1171 local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
1172 local STATUS='rootdir/tmp/webserverstatus-statusfile.log'
1173 rm -f "$DOWNLOG" "$STATUS"
1174 msgtest 'Test last status code from the webserver was' "$1"
1175 downloadfile "http://localhost:8080/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG"
1176 if [ "$(cat "$STATUS")" = "$1" ]; then
1177 msgpass
1178 else
1179 echo >&2
1180 if [ -n "$2" ]; then
1181 shift
1182 echo >&2 '#### Additionally provided output files contain:'
1183 cat >&2 "$@"
1184 fi
1185 echo >&2 '#### Download log of the status code:'
1186 cat >&2 "$DOWNLOG"
1187 msgfail "Status was $(cat "$STATUS")"
1188 fi
1189}
1190
1191pause() {
1192 echo "STOPPED execution. Press enter to continue"
1193 local IGNORE
1194 read IGNORE
1195}