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