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