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