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