tests: accept an explaination for msgfail
[ntk/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 expr match "$(readlink -f /proc/$$/fd/1)" '/dev/pts/[0-9]\+' > /dev/null && \
7 expr match "$(readlink -f /proc/$$/fd/2)" '/dev/pts/[0-9]\+' > /dev/null; then
8 CERROR="\e[1;31m" # red
9 CWARNING="\e[1;33m" # yellow
10 CMSG="\e[1;32m" # green
11 CINFO="\e[1;96m" # light blue
12 CDEBUG="\e[1;94m" # blue
13 CNORMAL="\e[0;39m" # default system console color
14 CDONE="\e[1;32m" # green
15 CPASS="\e[1;32m" # green
16 CFAIL="\e[1;31m" # red
17 CCMD="\e[1;35m" # pink
18 fi
19
20 msgdie() { echo "${CERROR}E: $1${CNORMAL}" >&2; exit 1; }
21 msgwarn() { echo "${CWARNING}W: $1${CNORMAL}" >&2; }
22 msgmsg() { echo "${CMSG}$1${CNORMAL}" >&2; }
23 msginfo() { echo "${CINFO}I: $1${CNORMAL}" >&2; }
24 msgdebug() { echo "${CDEBUG}D: $1${CNORMAL}" >&2; }
25 msgdone() { echo "${CDONE}DONE${CNORMAL}" >&2; }
26 msgnwarn() { echo -n "${CWARNING}W: $1${CNORMAL}" >&2; }
27 msgnmsg() { echo -n "${CMSG}$1${CNORMAL}" >&2; }
28 msgninfo() { echo -n "${CINFO}I: $1${CNORMAL}" >&2; }
29 msgndebug() { echo -n "${CDEBUG}D: $1${CNORMAL}" >&2; }
30 msgtest() {
31 while [ -n "$1" ]; do
32 echo -n "${CINFO}$1${CCMD} " >&2;
33 echo -n "$(echo "$2" | sed -e 's/^aptc/apt-c/' -e 's/^aptg/apt-g/' -e 's/^aptf/apt-f/')${CINFO} " >&2;
34 shift
35 if [ -n "$1" ]; then shift; else break; fi
36 done
37 echo -n "…${CNORMAL} " >&2;
38 }
39 msgpass() { echo "${CPASS}PASS${CNORMAL}" >&2; }
40 msgskip() { echo "${CWARNING}SKIP${CNORMAL}" >&2; }
41 msgfail() {
42 if [ $# -gt 0 ]; then echo "${CFAIL}FAIL: $*${CNORMAL}" >&2;
43 else echo "${CFAIL}FAIL${CNORMAL}" >&2; fi
44 EXIT_CODE=$((EXIT_CODE+1));
45 }
46
47 # enable / disable Debugging
48 MSGLEVEL=${MSGLEVEL:-3}
49 if [ $MSGLEVEL -le 0 ]; then
50 msgdie() { true; }
51 fi
52 if [ $MSGLEVEL -le 1 ]; then
53 msgwarn() { true; }
54 msgnwarn() { true; }
55 fi
56 if [ $MSGLEVEL -le 2 ]; then
57 msgmsg() { true; }
58 msgnmsg() { true; }
59 msgtest() { true; }
60 msgpass() { echo -n " ${CPASS}P${CNORMAL}" >&2; }
61 msgskip() { echo -n " ${CWARNING}S${CNORMAL}" >&2; }
62 if [ -n "$CFAIL" ]; then
63 msgfail() { echo -n " ${CFAIL}FAIL${CNORMAL}" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
64 else
65 msgfail() { echo -n " ###FAILED###" >&2; EXIT_CODE=$((EXIT_CODE+1)); }
66 fi
67 fi
68 if [ $MSGLEVEL -le 3 ]; then
69 msginfo() { true; }
70 msgninfo() { true; }
71 fi
72 if [ $MSGLEVEL -le 4 ]; then
73 msgdebug() { true; }
74 msgndebug() { true; }
75 fi
76 msgdone() {
77 if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
78 [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
79 [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
80 [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
81 [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
82 true;
83 else
84 echo "${CDONE}DONE${CNORMAL}" >&2;
85 fi
86 }
87
88 runapt() {
89 msgdebug "Executing: ${CCMD}$*${CDEBUG} "
90 if [ -f ./aptconfig.conf ]; then
91 APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
92 elif [ -f ../aptconfig.conf ]; then
93 APT_CONFIG=../aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
94 else
95 LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
96 fi
97 }
98 aptconfig() { runapt apt-config $*; }
99 aptcache() { runapt apt-cache $*; }
100 aptcdrom() { runapt apt-cdrom $*; }
101 aptget() { runapt apt-get $*; }
102 aptftparchive() { runapt apt-ftparchive $*; }
103 aptkey() { runapt apt-key $*; }
104 aptmark() { runapt apt-mark $*; }
105 dpkg() {
106 $(which dpkg) --root=${TMPWORKINGDIRECTORY}/rootdir --force-not-root --force-bad-path --log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log $*
107 }
108 aptitude() {
109 if [ -f ./aptconfig.conf ]; then
110 APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
111 elif [ -f ../aptconfig.conf ]; then
112 APT_CONFIG=../aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
113 else
114 LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which aptitude) $*
115 fi
116 }
117 gdb() {
118 echo "gdb: run »$*«"
119 APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} $(which gdb) ${BUILDDIRECTORY}/$1
120 }
121
122 exitwithstatus() {
123 # error if we about to overflow, but ...
124 # "255 failures ought to be enough for everybody"
125 if [ $EXIT_CODE -gt 255 ]; then
126 msgdie "Total failure count $EXIT_CODE too big"
127 fi
128 exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
129 }
130
131 addtrap() {
132 if [ "$1" = 'prefix' ]; then
133 CURRENTTRAP="$2 $CURRENTTRAP"
134 else
135 CURRENTTRAP="$CURRENTTRAP $1"
136 fi
137 trap "$CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
138 }
139
140 setupenvironment() {
141 TMPWORKINGDIRECTORY=$(mktemp -d)
142 TESTDIRECTORY=$(readlink -f $(dirname $0))
143 msgninfo "Preparing environment for ${CCMD}$(basename $0)${CINFO} in ${TMPWORKINGDIRECTORY}… "
144 BUILDDIRECTORY="${TESTDIRECTORY}/../../build/bin"
145 test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
146 addtrap "cd /; rm -rf $TMPWORKINGDIRECTORY;"
147 cd $TMPWORKINGDIRECTORY
148 mkdir rootdir aptarchive keys
149 cd rootdir
150 mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
151 mkdir -p var/cache var/lib var/log
152 mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
153 touch var/lib/dpkg/available
154 mkdir -p usr/lib/apt
155 ln -s ${BUILDDIRECTORY}/methods usr/lib/apt/methods
156 cd ..
157 local PACKAGESFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Packages-/' -e 's/^skip-/Packages-/')
158 if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
159 cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
160 fi
161 local SOURCESSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/Sources-/' -e 's/^skip-/Sources-/')
162 if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
163 cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
164 fi
165 cp $(find $TESTDIRECTORY -name '*.pub' -o -name '*.sec') keys/
166 ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
167 echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
168 echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
169 echo "Debug::NoLocking \"true\";" >> aptconfig.conf
170 echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
171 echo "Dir::Bin::Methods \"${BUILDDIRECTORY}/methods\";" >> aptconfig.conf
172 echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
173 echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
174 echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
175 echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
176 echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
177 if ! $(which dpkg) --assert-multi-arch 2>&1 > /dev/null; then
178 echo "DPKG::options:: \"--force-architecture\";" >> aptconfig.conf # Added to test multiarch before dpkg is ready for it…
179 fi
180 echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
181 echo 'quiet::NoUpdate "true";' >> aptconfig.conf
182 export LC_ALL=C
183 export PATH="${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
184 msgdone "info"
185 }
186
187 getarchitecture() {
188 if [ "$1" = "native" -o -z "$1" ]; then
189 eval `aptconfig shell ARCH APT::Architecture`
190 if [ -n "$ARCH" ]; then
191 echo $ARCH
192 else
193 dpkg --print-architecture
194 fi
195 else
196 echo $1
197 fi
198 }
199
200 getarchitectures() {
201 echo "$(aptconfig dump | grep APT::Architecture | cut -d'"' -f 2 | sed '/^$/ d' | sort | uniq | tr '\n' ' ')"
202 }
203
204 configarchitecture() {
205 local CONFFILE=rootdir/etc/apt/apt.conf.d/01multiarch.conf
206 rm -f $CONFFILE
207 echo "APT::Architecture \"$(getarchitecture $1)\";" > $CONFFILE
208 shift
209 while [ -n "$1" ]; do
210 echo "APT::Architectures:: \"$(getarchitecture $1)\";" >> $CONFFILE
211 shift
212 done
213 configdpkg
214 }
215
216 configdpkg() {
217 if [ ! -e rootdir/var/lib/dpkg/status ]; then
218 local STATUSFILE=$(echo "$(basename $0)" | sed -e 's/^test-/status-/' -e 's/^skip-/status-/')
219 if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
220 cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
221 else
222 echo -n > rootdir/var/lib/dpkg/status
223 fi
224 fi
225 if $(which dpkg) --assert-multi-arch 2>&1 > /dev/null; then
226 local ARCHS="$(getarchitectures)"
227 if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
228 DPKGARCH="$(dpkg --print-architecture)"
229 for ARCH in ${ARCHS}; do
230 if [ "${ARCH}" != "${DPKGARCH}" ]; then dpkg --add-architecture ${ARCH}; fi
231 done
232 if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
233 # dpkg doesn't really check the version as long as it is fully installed,
234 # but just to be sure we choose one above the required version
235 insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
236 fi
237 fi
238 fi
239 }
240
241 setupsimplenativepackage() {
242 local NAME="$1"
243 local ARCH="$2"
244 local VERSION="$3"
245 local RELEASE="${4:-unstable}"
246 local DEPENDENCIES="$5"
247 local DESCRIPTION="$6"
248 local SECTION="${7:-others}"
249 local DISTSECTION
250 if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
251 DISTSECTION="main"
252 else
253 DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
254 fi
255 local BUILDDIR=incoming/${NAME}-${VERSION}
256 mkdir -p ${BUILDDIR}/debian/source
257 cd ${BUILDDIR}
258 echo "* most suckless software product ever" > FEATURES
259 test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
260 test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
261
262 * Initial release
263
264 -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
265 test -e debian/control || echo "Source: $NAME
266 Section: $SECTION
267 Priority: optional
268 Maintainer: Joe Sixpack <joe@example.org>
269 Build-Depends: debhelper (>= 7)
270 Standards-Version: 3.9.1
271
272 Package: $NAME" > debian/control
273 if [ "$ARCH" = 'all' ]; then
274 echo "Architecture: all" >> debian/control
275 else
276 echo "Architecture: any" >> debian/control
277 fi
278 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
279 if [ -z "$DESCRIPTION" ]; then
280 echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
281 If you find such a package installed on your system,
282 YOU did something horribly wrong! They are autogenerated
283 und used only by testcases for APT and surf no other propose…" >> debian/control
284 else
285 echo "Description: $DESCRIPTION" >> debian/control
286 fi
287 test -e debian/compat || echo "7" > debian/compat
288 test -e debian/source/format || echo "3.0 (native)" > debian/source/format
289 test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
290 cd - > /dev/null
291 }
292
293 buildsimplenativepackage() {
294 local NAME="$1"
295 local ARCH="$2"
296 local VERSION="$3"
297 local RELEASE="${4:-unstable}"
298 local DEPENDENCIES="$5"
299 local DESCRIPTION="$6"
300 local SECTION="${7:-others}"
301 local PRIORITY="${8:-optional}"
302 local DISTSECTION
303 if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
304 DISTSECTION="main"
305 else
306 DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
307 fi
308 local BUILDDIR=${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}
309
310 msgninfo "Build package ${NAME} in ${VERSION} for ${RELEASE} in ${DISTSECTION}… "
311 mkdir -p $BUILDDIR/debian/source
312 echo "* most suckless software product ever" > ${BUILDDIR}/FEATURES
313 echo "#!/bin/sh
314 echo '$NAME says \"Hello!\"'" > ${BUILDDIR}/${NAME}
315
316 echo "Copyleft by Joe Sixpack $(date +%Y)" > ${BUILDDIR}/debian/copyright
317 echo "$NAME ($VERSION) $RELEASE; urgency=low
318
319 * Initial release
320
321 -- Joe Sixpack <joe@example.org> $(date -R)" > ${BUILDDIR}/debian/changelog
322 echo "Source: $NAME
323 Section: $SECTION
324 Priority: $PRIORITY
325 Maintainer: Joe Sixpack <joe@example.org>
326 Standards-Version: 3.9.3" > ${BUILDDIR}/debian/control
327 local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
328 test -z "$BUILDDEPS" || echo "$BUILDDEPS" >> ${BUILDDIR}/debian/control
329 echo "
330 Package: $NAME" >> ${BUILDDIR}/debian/control
331
332 if [ "$ARCH" = 'all' ]; then
333 echo "Architecture: all" >> ${BUILDDIR}/debian/control
334 else
335 echo "Architecture: any" >> ${BUILDDIR}/debian/control
336 fi
337 local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
338 test -z "$DEPS" || echo "$DEPS" >> ${BUILDDIR}/debian/control
339 if [ -z "$DESCRIPTION" ]; then
340 echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
341 If you find such a package installed on your system,
342 YOU did something horribly wrong! They are autogenerated
343 und used only by testcases for APT and surf no other propose…" >> ${BUILDDIR}/debian/control
344 else
345 echo "Description: $DESCRIPTION" >> ${BUILDDIR}/debian/control
346 fi
347
348 echo '3.0 (native)' > ${BUILDDIR}/debian/source/format
349 (cd ${BUILDDIR}/..; dpkg-source -b ${NAME}-${VERSION} 2>&1) | sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' \
350 | while read SRC; do
351 echo "pool/${SRC}" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist
352 # if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
353 # gpg --yes --no-default-keyring --secret-keyring ./keys/joesixpack.sec \
354 # --keyring ./keys/joesixpack.pub --default-key 'Joe Sixpack' \
355 # --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
356 # mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
357 # fi
358 done
359
360 for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
361 rm -rf ${BUILDDIR}/debian/tmp
362 mkdir -p ${BUILDDIR}/debian/tmp/DEBIAN ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME} ${BUILDDIR}/debian/tmp/usr/bin
363 cp ${BUILDDIR}/debian/copyright ${BUILDDIR}/debian/changelog ${BUILDDIR}/FEATURES ${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}
364 cp ${BUILDDIR}/${NAME} ${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}
365 (cd ${BUILDDIR}; dpkg-gencontrol -DArchitecture=$arch)
366 (cd ${BUILDDIR}/debian/tmp; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
367
368 dpkg-deb --build ${BUILDDIR}/debian/tmp ${BUILDDIR}/.. 2> /dev/null > /dev/null
369 echo "pool/${NAME}_${VERSION}_${arch}.deb" >> ${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist
370 done
371
372 mkdir -p ${BUILDDIR}/../${NAME}_${VERSION}
373 cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}/
374 cp ${BUILDDIR}/debian/changelog ${BUILDDIR}/../${NAME}_${VERSION}.changelog
375 rm -rf "${BUILDDIR}"
376 msgdone "info"
377 }
378
379 buildpackage() {
380 local BUILDDIR=$1
381 local RELEASE=$2
382 local SECTION=$3
383 local ARCH=$(getarchitecture $4)
384 msgninfo "Build package $(echo "$BUILDDIR" | grep -o '[^/]*$') for ${RELEASE} in ${SECTION}"
385 cd $BUILDDIR
386 if [ "$ARCH" = "all" ]; then
387 ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
388 fi
389 local BUILT="$(dpkg-buildpackage -uc -us -a$ARCH 2> /dev/null)"
390 local PKGS="$( echo "$BUILT" | grep '^dpkg-deb: building package' | cut -d'/' -f 2 | sed -e "s#'\.##")"
391 local SRCS="$( echo "$BUILT" | grep '^dpkg-source: info: building' | grep -o '[a-z0-9._+~-]*$')"
392 cd - > /dev/null
393 for PKG in $PKGS; do
394 echo "pool/${PKG}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist
395 done
396 for SRC in $SRCS; do
397 echo "pool/${SRC}" >> ${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist
398 done
399 msgdone "info"
400 }
401
402 buildaptarchive() {
403 if [ -d incoming ]; then
404 buildaptarchivefromincoming $*
405 else
406 buildaptarchivefromfiles $*
407 fi
408 }
409
410 createaptftparchiveconfig() {
411 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' ' ')"
412 if [ -z "$ARCHS" ]; then
413 # the pool is empty, so we will operate on faked packages - let us use the configured archs
414 ARCHS="$(getarchitectures)"
415 fi
416 echo -n 'Dir {
417 ArchiveDir "' >> ftparchive.conf
418 echo -n $(readlink -f .) >> ftparchive.conf
419 echo -n '";
420 CacheDir "' >> ftparchive.conf
421 echo -n $(readlink -f ..) >> ftparchive.conf
422 echo -n '";
423 FileListDir "' >> ftparchive.conf
424 echo -n $(readlink -f pool/) >> ftparchive.conf
425 echo -n '";
426 };
427 Default {
428 Packages::Compress ". gzip bzip2 lzma xz";
429 Sources::Compress ". gzip bzip2 lzma xz";
430 Contents::Compress ". gzip bzip2 lzma xz";
431 Translation::Compress ". gzip bzip2 lzma xz";
432 LongDescription "false";
433 };
434 TreeDefault {
435 Directory "pool/";
436 SrcDirectory "pool/";
437 };
438 APT {
439 FTPArchive {
440 Release {
441 Origin "joesixpack";
442 Label "apttestcases";
443 Suite "unstable";
444 Description "repository with dummy packages";
445 Architectures "' >> ftparchive.conf
446 echo -n "$ARCHS" >> ftparchive.conf
447 echo 'source";
448 };
449 };
450 };' >> ftparchive.conf
451 for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
452 echo -n 'tree "dists/' >> ftparchive.conf
453 echo -n "$DIST" >> ftparchive.conf
454 echo -n '" {
455 Architectures "' >> ftparchive.conf
456 echo -n "$ARCHS" >> ftparchive.conf
457 echo -n 'source";
458 FileList "' >> ftparchive.conf
459 echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
460 echo -n '";
461 SourceFileList "' >> ftparchive.conf
462 echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
463 echo -n '";
464 Sections "' >> ftparchive.conf
465 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
466 echo '";
467 };' >> ftparchive.conf
468 done
469 }
470
471 buildaptftparchivedirectorystructure() {
472 local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
473 for DIST in $DISTS; do
474 local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
475 for SECTION in $SECTIONS; do
476 local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
477 for ARCH in $ARCHS; do
478 mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
479 done
480 mkdir -p dists/${DIST}/${SECTION}/source
481 mkdir -p dists/${DIST}/${SECTION}/i18n
482 done
483 done
484 }
485
486 insertpackage() {
487 local RELEASE="$1"
488 local NAME="$2"
489 local ARCH="$3"
490 local VERSION="$4"
491 local DEPENDENCIES="$5"
492 local PRIORITY="${6:-optional}"
493 local DESCRIPTION="${7}"
494 local ARCHS=""
495 for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
496 if [ "$arch" = 'all' -o "$arch" = 'none' ]; then
497 ARCHS="$(getarchitectures)"
498 else
499 ARCHS="$arch"
500 fi
501 for BUILDARCH in $ARCHS; do
502 local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
503 mkdir -p $PPATH aptarchive/dists/${RELEASE}/main/source
504 touch aptarchive/dists/${RELEASE}/main/source/Sources
505 local FILE="${PPATH}/Packages"
506 echo "Package: $NAME
507 Priority: $PRIORITY
508 Section: other
509 Installed-Size: 42
510 Maintainer: Joe Sixpack <joe@example.org>" >> $FILE
511 test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
512 echo "Version: $VERSION
513 Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb" >> $FILE
514 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
515 echo -n 'Description: ' >> $FILE
516 if [ -z "$DESCRIPTION" ]; then
517 echo "an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
518 If you find such a package installed on your system,
519 YOU did something horribly wrong! They are autogenerated
520 und used only by testcases for APT and surf no other propose…" >> $FILE
521 else
522 echo "$DESCRIPTION" >> $FILE
523 fi
524 echo >> $FILE
525 done
526 done
527 }
528
529 insertsource() {
530 local RELEASE="$1"
531 local NAME="$2"
532 local ARCH="$3"
533 local VERSION="$4"
534 local DEPENDENCIES="$5"
535 local ARCHS=""
536 local SPATH="aptarchive/dists/${RELEASE}/main/source"
537 mkdir -p $SPATH
538 local FILE="${SPATH}/Sources"
539 echo "Package: $NAME
540 Binary: $NAME
541 Version: $VERSION
542 Maintainer: Joe Sixpack <joe@example.org>
543 Architecture: $ARCH" >> $FILE
544 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
545 echo "Files:
546 d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.dsc
547 d41d8cd98f00b204e9800998ecf8427e 0 ${NAME}_${VERSION}.tar.gz
548 " >> $FILE
549 }
550
551 insertinstalledpackage() {
552 local NAME="$1"
553 local ARCH="$2"
554 local VERSION="$3"
555 local DEPENDENCIES="$4"
556 local PRIORITY="${5:-optional}"
557 local STATUS="${6:-install ok installed}"
558 local FILE='rootdir/var/lib/dpkg/status'
559 local INFO='rootdir/var/lib/dpkg/info'
560 for arch in $(echo "$ARCH" | sed -e 's#,#\n#g' | sed -e "s#^native\$#$(getarchitecture 'native')#"); do
561 echo "Package: $NAME
562 Status: $STATUS
563 Priority: $PRIORITY
564 Section: other
565 Installed-Size: 42
566 Maintainer: Joe Sixpack <joe@example.org>
567 Version: $VERSION" >> $FILE
568 test "$arch" = 'none' || echo "Architecture: $arch" >> $FILE
569 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> $FILE
570 echo "Description: an autogenerated dummy ${NAME}=${VERSION}/installed
571 If you find such a package installed on your system,
572 YOU did something horribly wrong! They are autogenerated
573 und used only by testcases for APT and surf no other propose…
574 " >> $FILE
575 if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
576 echo -n > ${INFO}/${NAME}:${arch}.list
577 else
578 echo -n > ${INFO}/${NAME}.list
579 fi
580 done
581 }
582
583
584 buildaptarchivefromincoming() {
585 msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on incoming packages…"
586 cd aptarchive
587 [ -e pool ] || ln -s ../incoming pool
588 [ -e ftparchive.conf ] || createaptftparchiveconfig
589 [ -e dists ] || buildaptftparchivedirectorystructure
590 msgninfo "\tGenerate Packages, Sources and Contents files… "
591 aptftparchive -qq generate ftparchive.conf
592 cd - > /dev/null
593 msgdone "info"
594 generatereleasefiles
595 }
596
597 buildaptarchivefromfiles() {
598 msginfo "Build APT archive for ${CCMD}$(basename $0)${CINFO} based on prebuild files…"
599 find aptarchive -name 'Packages' -o -name 'Sources' | while read line; do
600 msgninfo "\t${line} file… "
601 cat ${line} | gzip > ${line}.gz
602 cat ${line} | bzip2 > ${line}.bz2
603 cat ${line} | xz --format=lzma > ${line}.lzma
604 cat ${line} | xz > ${line}.xz
605 msgdone "info"
606 done
607 generatereleasefiles
608 }
609
610 # can be overridden by testcases for their pleasure
611 getcodenamefromsuite() { echo -n "$1"; }
612 getreleaseversionfromsuite() { true; }
613 getlabelfromsuite() { true; }
614
615 generatereleasefiles() {
616 # $1 is the Date header and $2 is the ValidUntil header to be set
617 # both should be given in notation date/touch can understand
618 msgninfo "\tGenerate Release files… "
619 if [ -e aptarchive/dists ]; then
620 for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
621 local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
622 local CODENAME="$(getcodenamefromsuite $SUITE)"
623 local VERSION="$(getreleaseversionfromsuite $SUITE)"
624 local LABEL="$(getlabelfromsuite $SUITE)"
625 if [ -n "$VERSION" ]; then
626 VERSION="-o APT::FTPArchive::Release::Version=${VERSION}"
627 fi
628 if [ -n "$LABEL" ]; then
629 LABEL="-o APT::FTPArchive::Release::Label=${LABEL}"
630 fi
631 aptftparchive -qq release $dir \
632 -o APT::FTPArchive::Release::Suite="${SUITE}" \
633 -o APT::FTPArchive::Release::Codename="${CODENAME}" \
634 ${LABEL} \
635 ${VERSION} \
636 | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
637 if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
638 sed -i '/^Date: / a\
639 NotAutomatic: yes' $dir/Release
640 fi
641 if [ -n "$1" -a "$1" != "now" ]; then
642 sed -i "s/^Date: .*$/Date: $(date -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')/" $dir/Release
643 fi
644 if [ -n "$2" ]; then
645 sed -i "/^Date: / a\
646 Valid-Until: $(date -d "$2" '+%a, %d %b %Y %H:%M:%S %Z')" $dir/Release
647 fi
648 done
649 else
650 aptftparchive -qq release ./aptarchive | sed -e '/0 Release$/ d' > aptarchive/Release # remove the self reference
651 fi
652 if [ -n "$1" -a "$1" != "now" ]; then
653 for release in $(find ./aptarchive -name 'Release'); do
654 touch -d "$1" $release
655 done
656 fi
657 msgdone "info"
658 }
659
660 setupdistsaptarchive() {
661 local APTARCHIVE=$(readlink -f ./aptarchive)
662 rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
663 rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
664 for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
665 SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
666 msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
667 echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
668 echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
669 msgdone "info"
670 done
671 }
672
673 setupflataptarchive() {
674 local APTARCHIVE=$(readlink -f ./aptarchive)
675 if [ -f ${APTARCHIVE}/Packages ]; then
676 msgninfo "\tadd deb sources.list line… "
677 echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
678 msgdone "info"
679 else
680 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
681 fi
682 if [ -f ${APTARCHIVE}/Sources ]; then
683 msgninfo "\tadd deb-src sources.list line… "
684 echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
685 msgdone "info"
686 else
687 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
688 fi
689 }
690
691 setupaptarchive() {
692 buildaptarchive
693 if [ -e aptarchive/dists ]; then
694 setupdistsaptarchive
695 else
696 setupflataptarchive
697 fi
698 signreleasefiles
699 if [ "$1" != '--no-update' ]; then
700 msgninfo "\tSync APT's cache with the archive… "
701 aptget update -qq
702 msgdone "info"
703 fi
704 }
705
706 signreleasefiles() {
707 local SIGNER="${1:-Joe Sixpack}"
708 msgninfo "\tSign archive with $SIGNER key… "
709 local SECKEYS=""
710 for KEY in $(find keys/ -name '*.sec'); do
711 SECKEYS="$SECKEYS --secret-keyring $KEY"
712 done
713 local PUBKEYS=""
714 for KEY in $(find keys/ -name '*.pub'); do
715 PUBKEYS="$PUBKEYS --keyring $KEY"
716 done
717 for RELEASE in $(find aptarchive/ -name Release); do
718 gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" -abs -o ${RELEASE}.gpg ${RELEASE}
719 gpg --yes --no-default-keyring $SECKEYS $PUBKEYS --default-key "$SIGNER" --clearsign -o "$(echo "${RELEASE}" | sed 's#/Release$#/InRelease#')" $RELEASE
720 done
721 msgdone "info"
722 }
723
724 changetowebserver() {
725 local LOG='/dev/null'
726 if test -x ${BUILDDIRECTORY}/aptwebserver; then
727 cd aptarchive
728 LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/aptwebserver "$@" >$LOG 2>&1 &
729 addtrap "kill $!;"
730 cd - > /dev/null
731 elif [ $# -gt 0 ]; then
732 msgdie 'Need the aptwebserver when passing arguments for the webserver'
733 elif which weborf > /dev/null; then
734 weborf -xb aptarchive/ >$LOG 2>&1 &
735 addtrap "kill $!;"
736 elif which gatling > /dev/null; then
737 cd aptarchive
738 gatling -p 8080 -F -S >$LOG 2>&1 &
739 addtrap "kill $!;"
740 cd - > /dev/null
741 elif which lighttpd > /dev/null; then
742 echo "server.document-root = \"$(readlink -f ./aptarchive)\"
743 server.port = 8080
744 server.stat-cache-engine = \"disable\"" > lighttpd.conf
745 lighttpd -t -f lighttpd.conf >/dev/null || msgdie 'Can not change to webserver: our lighttpd config is invalid'
746 lighttpd -D -f lighttpd.conf >$LOG 2>&1 &
747 addtrap "kill $!;"
748 else
749 msgdie 'You have to build aptwerbserver or install a webserver'
750 fi
751 local APTARCHIVE="file://$(readlink -f ./aptarchive)"
752 for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
753 sed -i $LIST -e "s#$APTARCHIVE#http://localhost:8080/#"
754 done
755 return 0
756 }
757
758 changetocdrom() {
759 mkdir -p rootdir/media/cdrom/.disk
760 local CD="$(readlink -f rootdir/media/cdrom)"
761 echo "acquire::cdrom::mount \"${CD}\";" > rootdir/etc/apt/apt.conf.d/00cdrom
762 echo 'acquire::cdrom::autodetect 0;' >> rootdir/etc/apt/apt.conf.d/00cdrom
763 echo -n "$1" > ${CD}/.disk/info
764 if [ ! -d aptarchive/dists ]; then
765 msgdie 'Flat file archive cdroms can not be created currently'
766 return 1
767 fi
768 mv aptarchive/dists $CD
769 ln -s "$(readlink -f ./incoming)" $CD/pool
770 find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
771 }
772
773 checkdiff() {
774 local DIFFTEXT="$($(which diff) -u $* | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
775 if [ -n "$DIFFTEXT" ]; then
776 echo
777 echo "$DIFFTEXT"
778 return 1
779 else
780 return 0
781 fi
782 }
783
784 testfileequal() {
785 local FILE="$1"
786 shift
787 msgtest "Test for correctness of file" "$FILE"
788 if [ -z "$*" ]; then
789 echo -n "" | checkdiff $FILE - && msgpass || msgfail
790 else
791 echo "$*" | checkdiff $FILE - && msgpass || msgfail
792 fi
793 }
794
795 testempty() {
796 msgtest "Test for no output of" "$*"
797 test -z "$($* 2>&1)" && msgpass || msgfail
798 }
799
800 testequal() {
801 local COMPAREFILE=$(mktemp)
802 addtrap "rm $COMPAREFILE;"
803 echo "$1" > $COMPAREFILE
804 shift
805 msgtest "Test for equality of" "$*"
806 $* 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
807 }
808
809 testequalor2() {
810 local COMPAREFILE1=$(mktemp)
811 local COMPAREFILE2=$(mktemp)
812 local COMPAREAGAINST=$(mktemp)
813 addtrap "rm $COMPAREFILE1 $COMPAREFILE2 $COMPAREAGAINST;"
814 echo "$1" > $COMPAREFILE1
815 echo "$2" > $COMPAREFILE2
816 shift 2
817 msgtest "Test for equality OR of" "$*"
818 $* 2>&1 1> $COMPAREAGAINST
819 (checkdiff $COMPAREFILE1 $COMPAREAGAINST 1> /dev/null ||
820 checkdiff $COMPAREFILE2 $COMPAREAGAINST 1> /dev/null) && msgpass ||
821 ( echo "\n${CINFO}Diff against OR 1${CNORMAL}" "$(checkdiff $COMPAREFILE1 $COMPAREAGAINST)" \
822 "\n${CINFO}Diff against OR 2${CNORMAL}" "$(checkdiff $COMPAREFILE2 $COMPAREAGAINST)" &&
823 msgfail )
824 }
825
826 testshowvirtual() {
827 local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
828 local PACKAGE="$1"
829 shift
830 while [ -n "$1" ]; do
831 VIRTUAL="${VIRTUAL}
832 N: Can't select versions from package '$1' as it is purely virtual"
833 PACKAGE="${PACKAGE} $1"
834 shift
835 done
836 msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
837 VIRTUAL="${VIRTUAL}
838 N: No packages found"
839 local COMPAREFILE=$(mktemp)
840 addtrap "rm $COMPAREFILE;"
841 local ARCH="$(getarchitecture 'native')"
842 echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
843 aptcache show -q=0 $PACKAGE 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
844 }
845
846 testnopackage() {
847 msgtest "Test for non-existent packages" "apt-cache show $*"
848 local SHOWPKG="$(aptcache show $* 2>&1 | grep '^Package: ')"
849 if [ -n "$SHOWPKG" ]; then
850 echo
851 echo "$SHOWPKG"
852 msgfail
853 return 1
854 fi
855 msgpass
856 }
857
858 testdpkginstalled() {
859 msgtest "Test for correctly installed package(s) with" "dpkg -l $*"
860 local PKGS="$(dpkg -l $* 2>/dev/null | grep '^i' | wc -l)"
861 if [ "$PKGS" != $# ]; then
862 echo $PKGS
863 dpkg -l $* | grep '^[a-z]'
864 msgfail
865 return 1
866 fi
867 msgpass
868 }
869
870 testdpkgnotinstalled() {
871 msgtest "Test for correctly not-installed package(s) with" "dpkg -l $*"
872 local PKGS="$(dpkg -l $* 2> /dev/null | grep '^i' | wc -l)"
873 if [ "$PKGS" != 0 ]; then
874 echo
875 dpkg -l $* | grep '^[a-z]'
876 msgfail
877 return 1
878 fi
879 msgpass
880 }
881
882 testmarkedauto() {
883 local COMPAREFILE=$(mktemp)
884 addtrap "rm $COMPAREFILE;"
885 if [ -n "$1" ]; then
886 msgtest 'Test for correctly marked as auto-installed' "$*"
887 while [ -n "$1" ]; do echo "$1"; shift; done | sort > $COMPAREFILE
888 else
889 msgtest 'Test for correctly marked as auto-installed' 'no package'
890 echo -n > $COMPAREFILE
891 fi
892 aptmark showauto 2>&1 | checkdiff $COMPAREFILE - && msgpass || msgfail
893 }
894
895 pause() {
896 echo "STOPPED execution. Press enter to continue"
897 local IGNORE
898 read IGNORE
899 }