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