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