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