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