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