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