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