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