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