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