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