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