sign the created Release files with our own dummy key "Joe Sixpack"
[ntk/apt.git] / test / integration / framework
1 #!/bin/sh -- # no runable script, just for vi
2
3 # we all like colorful messages
4 CERROR="\e[1;31m" # red
5 CWARNING="\e[1;33m" # yellow
6 CMSG="\e[1;32m" # green
7 CINFO="\e[1;96m" # light blue
8 CDEBUG="\e[1;94m" # blue
9 CNORMAL="\e[0;39m" # default system console color
10 CDONE="\e[1;32m" # green
11 CPASS="\e[1;32m" # green
12 CFAIL="\e[1;31m" # red
13 CCMD="\e[1;35m" # pink
14
15 msgdie() { echo "${CERROR}E: $1${CNORMAL}" >&2; exit 1; }
16 msgwarn() { echo "${CWARNING}W: $1${CNORMAL}" >&2; }
17 msgmsg() { echo "${CMSG}$1${CNORMAL}" >&2; }
18 msginfo() { echo "${CINFO}I: $1${CNORMAL}" >&2; }
19 msgdebug() { echo "${CDEBUG}D: $1${CNORMAL}" >&2; }
20 msgdone() { echo "${CDONE}DONE${CNORMAL}" >&2; }
21 msgnwarn() { echo -n "${CWARNING}W: $1${CNORMAL}" >&2; }
22 msgnmsg() { echo -n "${CMSG}$1${CNORMAL}" >&2; }
23 msgninfo() { echo -n "${CINFO}I: $1${CNORMAL}" >&2; }
24 msgndebug() { echo -n "${CDEBUG}D: $1${CNORMAL}" >&2; }
25 msgtest() { echo -n "${CINFO}$1 ${CCMD}$(echo "$2" | sed -e 's/^aptc/apt-c/' -e 's/^aptg/apt-g/' -e 's/^aptf/apt-f/')${CINFO} …${CNORMAL} " >&2; }
26 msgpass() { echo "${CPASS}PASS${CNORMAL}" >&2; }
27 msgskip() { echo "${CWARNING}SKIP${CNORMAL}" >&2; }
28 msgfail() { echo "${CFAIL}FAIL${CNORMAL}" >&2; }
29
30 # enable / disable Debugging
31 MSGLEVEL=${MSGLEVEL:-3}
32 if [ $MSGLEVEL -le 0 ]; then
33 msgdie() { true; }
34 fi
35 if [ $MSGLEVEL -le 1 ]; then
36 msgwarn() { true; }
37 msgnwarn() { true; }
38 fi
39 if [ $MSGLEVEL -le 2 ]; then
40 msgmsg() { true; }
41 msgnmsg() { true; }
42 fi
43 if [ $MSGLEVEL -le 3 ]; then
44 msginfo() { true; }
45 msgninfo() { true; }
46 fi
47 if [ $MSGLEVEL -le 4 ]; then
48 msgdebug() { true; }
49 msgndebug() { true; }
50 fi
51 msgdone() {
52 if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
53 [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
54 [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
55 [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
56 [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
57 true;
58 else
59 echo "${CDONE}DONE${CNORMAL}" >&2;
60 fi
61 }
62
63 runapt() {
64 msgdebug "Executing: ${CCMD}$*${CDEBUG} "
65 if [ -f ./aptconfig.conf ]; then
66 APT_CONFIG=aptconfig.conf LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
67 else
68 LD_LIBRARY_PATH=${BUILDDIRECTORY} ${BUILDDIRECTORY}/$*
69 fi
70 }
71 aptconfig() { runapt apt-config $*; }
72 aptcache() { runapt apt-cache $*; }
73 aptget() { runapt apt-get $*; }
74 aptftparchive() { runapt apt-ftparchive $*; }
75
76 setupenvironment() {
77 local TMPWORKINGDIRECTORY=$(mktemp -d)
78 local TESTDIR=$(readlink -f $(dirname $0))
79 msgninfo "Preparing environment for ${CCMD}$0${CINFO} in ${TMPWORKINGDIRECTORY}… "
80 BUILDDIRECTORY="${TESTDIR}/../../build/bin"
81 test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
82 local OLDWORKINGDIRECTORY=$(pwd)
83 trap "cd /; rm -rf $TMPWORKINGDIRECTORY; cd $OLDWORKINGDIRECTORY" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
84 cd $TMPWORKINGDIRECTORY
85 mkdir rootdir aptarchive keys
86 cd rootdir
87 mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d var/cache
88 mkdir -p var/log/apt var/lib/apt
89 mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
90 local STATUSFILE=$(echo "$(basename $0)" | sed 's/^test-/status-/')
91 if [ -f "${TESTDIR}/${STATUSFILE}" ]; then
92 cp "${TESTDIR}/${STATUSFILE}" var/lib/dpkg/status
93 else
94 touch var/lib/dpkg/status
95 fi
96 touch var/lib/dpkg/available
97 mkdir -p usr/lib/apt
98 ln -s ${BUILDDIRECTORY}/methods usr/lib/apt/methods
99 cd ..
100 local PACKAGESFILE=$(echo "$(basename $0)" | sed 's/^test-/Packages-/')
101 if [ -f "${TESTDIR}/${PACKAGESFILE}" ]; then
102 cp "${TESTDIR}/${PACKAGESFILE}" aptarchive/Packages
103 else
104 touch aptarchive/Packages
105 fi
106 cp $(find $TESTDIR -name '*.pub' -o -name '*.sec') keys/
107 ln -s ${TMPWORKINGDIRECTORY}/keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
108 echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
109 echo "Debug::NoLocking \"true\";" >> aptconfig.conf
110 echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
111 echo "Dir::Bin::dpkg \"fakeroot\";" >> aptconfig.conf
112 echo "DPKG::options:: \"dpkg\";" >> aptconfig.conf
113 echo "DPKG::options:: \"--root=${TMPWORKINGDIRECTORY}/rootdir\";" >> aptconfig.conf
114 echo "DPKG::options:: \"--force-not-root\";" >> aptconfig.conf
115 echo "DPKG::options:: \"--force-bad-path\";" >> aptconfig.conf
116 echo "DPKG::options:: \"--log=${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log\";" >> aptconfig.conf
117 export LC_ALL=C
118 msgdone "info"
119 }
120
121 configarchitecture() {
122 local CONFFILE=rootdir/etc/apt/apt.conf.d/01multiarch.conf
123 echo "APT::Architecture \"$1\";" > $CONFFILE
124 shift
125 while [ -n "$1" ]; do
126 echo "APT::Architectures:: \"$1\";" >> $CONFFILE
127 shift
128 done
129 }
130
131 buildsimplenativepackage() {
132 local NAME="$1"
133 local ARCH="$2"
134 local VERSION="$3"
135 local RELEASE="${4:-unstable}"
136 local DEPENDENCIES="$5"
137 local DESCRIPTION="$6"
138 local SECTION="${7:-others}"
139 local DISTSECTION
140 if [ "$SECTION" = "$(echo "$SECTION" | cut -d'/' -f 2)" ]; then
141 DISTSECTION="main"
142 else
143 DISTSECTION="$(echo "$SECTION" | cut -d'/' -f 1)"
144 fi
145 msgninfo "Build package ${CCMD}${NAME}=${VERSION}/${RELEASE}${CINFO}… "
146 local BUILDDIR=incoming/${NAME}-${VERSION}
147 mkdir -p ${BUILDDIR}/debian/source
148 cd ${BUILDDIR}
149 echo "* most suckless software product ever" > FEATURES
150 test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date +%Y)" > debian/copyright
151 test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
152
153 * Initial release
154
155 -- Joe Sixpack <joe@example.org> $(date -R)" > debian/changelog
156 test -e debian/control || echo "Source: $NAME
157 Section: $SECTION
158 Priority: optional
159 Maintainer: Joe Sixpack <joe@example.org>
160 Build-Depends: debhelper (>= 7)
161 Standards-Version: 3.9.1
162
163 Package: $NAME
164 Architecture: $ARCH" > debian/control
165 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
166 if [ -z "$DESCRIPTION" ]; then
167 echo "Description: an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
168 If you find such a package installed on your system,
169 YOU did something horribly wrong! They are autogenerated
170 und used only by testcases for APT and surf no other propose…" >> debian/control
171 else
172 echo "Description: $DESCRIPTION" >> debian/control
173 fi
174 test -e debian/compat || echo "7" > debian/compat
175 test -e debian/source/format || echo "3.0 (native)" > debian/source/format
176 test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
177 local BUILT="$(dpkg-buildpackage -uc -us -a$ARCH 2> /dev/null)"
178 local PKGS="$( echo "$BUILT" | grep '^dpkg-deb: building package' | cut -d'/' -f 2 | sed -e "s#'\.##")"
179 local SRCS="$( echo "$BUILT" | grep '^dpkg-source: info: building' | grep -o '[a-z0-9._-]*$')"
180 cd - > /dev/null
181 for PKG in $PKGS; do
182 echo "pool/${PKG}" >> ./incoming/${RELEASE}.${DISTSECTION}.pkglist
183 done
184 for SRC in $SRCS; do
185 echo "pool/${SRC}" >> ./incoming/${RELEASE}.${DISTSECTION}.srclist
186 done
187 rm -rf $BUILDDIR
188 msgdone "info"
189 }
190
191 buildaptarchive() {
192 if [ -d incoming ]; then
193 buildaptarchivefromincoming $*
194 else
195 buildaptarchivefromfiles $*
196 fi
197 }
198
199 createaptftparchiveconfig() {
200 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' ' ')"
201 echo -n 'Dir {
202 ArchiveDir "' >> ftparchive.conf
203 echo -n $(readlink -f .) >> ftparchive.conf
204 echo -n '";
205 CacheDir "' >> ftparchive.conf
206 echo -n $(readlink -f ..) >> ftparchive.conf
207 echo -n '";
208 FileListDir "' >> ftparchive.conf
209 echo -n $(readlink -f pool/) >> ftparchive.conf
210 echo -n '";
211 };
212 Default {
213 Packages::Compress ". gzip bzip2 lzma";
214 Sources::Compress ". gzip bzip2 lzma";
215 Contents::Compress ". gzip bzip2 lzma";
216 };
217 TreeDefault {
218 Directory "pool/";
219 SrcDirectory "pool/";
220 };
221 APT {
222 FTPArchive {
223 Release {
224 Origin "joesixpack";
225 Label "apttestcases";
226 Suite "unstable";
227 Description "repository with dummy packages";
228 Architectures "' >> ftparchive.conf
229 echo -n "$ARCHS" >> ftparchive.conf
230 echo 'source";
231 };
232 };
233 };' >> ftparchive.conf
234 for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
235 echo -n 'tree "dists/' >> ftparchive.conf
236 echo -n "$DIST" >> ftparchive.conf
237 echo -n '" {
238 Architectures "' >> ftparchive.conf
239 echo -n "$ARCHS" >> ftparchive.conf
240 echo -n 'source";
241 FileList "' >> ftparchive.conf
242 echo -n "${DIST}.\$(SECTION).pkglist" >> ftparchive.conf
243 echo -n '";
244 SourceFileList "' >> ftparchive.conf
245 echo -n "${DIST}.\$(SECTION).srclist" >> ftparchive.conf
246 echo -n '";
247 Sections "' >> ftparchive.conf
248 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
249 echo '";
250 };' >> ftparchive.conf
251 done
252 }
253
254 buildaptftparchivedirectorystructure() {
255 local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
256 for DIST in $DISTS; do
257 local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
258 for SECTION in $SECTIONS; do
259 local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
260 for ARCH in $ARCHS; do
261 mkdir -p dists/${DIST}/${SECTION}/binary-${ARCH}
262 done
263 mkdir -p dists/${DIST}/${SECTION}/source
264 mkdir -p dists/${DIST}/${SECTION}/i18n
265 done
266 done
267 }
268
269 buildaptarchivefromincoming() {
270 msginfo "Build APT archive for ${CCMD}$0${CINFO} based on incoming packages…"
271 cd aptarchive
272 [ -e pool ] || ln -s ../incoming pool
273 [ -e ftparchive.conf ] || createaptftparchiveconfig
274 [ -e dists ] || buildaptftparchivedirectorystructure
275 msgninfo "\tGenerate Packages, Sources and Contents files… "
276 aptftparchive -qq generate ftparchive.conf
277 msgdone "info"
278 msgninfo "\tGenerate Release files… "
279 for dir in $(find ./dists -mindepth 1 -maxdepth 1 -type d); do
280 aptftparchive -qq release $dir -o APT::FTPArchive::Release::Codename="$(echo "$dir" | cut -d'/' -f 3)" | sed -e '/0 Release$/ d' > $dir/Release # remove the self reference
281 done
282 cd - > /dev/null
283 msgdone "info"
284 }
285
286 buildaptarchivefromfiles() {
287 msginfo "Build APT archive for ${CCMD}$0${CINFO} based on prebuild files…"
288 cd aptarchive
289 if [ -f Packages ]; then
290 msgninfo "\tPackages file… "
291 cat Packages | gzip > Packages.gz
292 cat Packages | bzip2 > Packages.bz2
293 cat Packages | lzma > Packages.lzma
294 msgdone "info"
295 fi
296 if [ -f Sources ]; then
297 msgninfo "\tSources file… "
298 cat Sources | gzip > Sources.gz
299 cat Sources | bzip2 > Sources.bz2
300 cat Sources | lzma > Sources.lzma
301 msgdone "info"
302 fi
303 msgninfo "\tRelease file… "
304 aptftparchive -qq release . | sed -e '/0 Release$/ d' > Release # remove the self reference
305 msgdone "info"
306 cd ..
307 }
308
309 setupdistsaptarchive() {
310 local APTARCHIVE=$(readlink -f ./aptarchive)
311 rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
312 rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
313 for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
314 SECTIONS=$(find ./aptarchive/dists/${DISTS}/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
315 msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
316 echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list
317 echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list
318 msgdone "info"
319 done
320 }
321
322 setupflataptarchive() {
323 local APTARCHIVE=$(readlink -f ./aptarchive)
324 if [ -f ${APTARCHIVE}/Packages ]; then
325 msgninfo "\tadd deb sources.list line… "
326 echo "deb file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
327 msgdone "info"
328 else
329 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list
330 fi
331 if [ -f ${APTARCHIVE}/Sources ]; then
332 msgninfo "\tadd deb-src sources.list line… "
333 echo "deb-src file://$APTARCHIVE /" > rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
334 msgdone "info"
335 else
336 rm -f rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list
337 fi
338 }
339
340 setupaptarchive() {
341 buildaptarchive
342 if [ -e aptarchive/dists ]; then
343 setupdistsaptarchive
344 else
345 setupflataptarchive
346 fi
347 msgninfo "\tSign archive with Joe Sixpack key… "
348 for RELEASE in $(find aptarchive/ -name Release); do
349 gpg --no-default-keyring --secret-keyring ./keys/joesixpack.sec --keyring ./keys/joesixpack.pub --default-key Joe -abs -o ${RELEASE}.gpg ${RELEASE}
350 done
351 msgdone "info"
352 msgninfo "\tSync APT's cache with the archive… "
353 aptget update -qq
354 msgdone "info"
355 }
356
357 diff() {
358 local DIFFTEXT="$($(which diff) -u $* | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
359 if [ -n "$DIFFTEXT" ]; then
360 echo
361 echo "$DIFFTEXT"
362 return 1
363 else
364 return 0
365 fi
366 }
367
368 testequal() {
369 local COMPAREFILE=$(mktemp)
370 echo "$1" > $COMPAREFILE
371 shift
372 msgtest "Test for equality of" "$*"
373 $* 2>&1 | diff $COMPAREFILE - && msgpass || msgfail
374 rm $COMPAREFILE
375 }
376
377 testequalor2() {
378 local COMPAREFILE1=$(mktemp)
379 local COMPAREFILE2=$(mktemp)
380 local COMPAREAGAINST=$(mktemp)
381 echo "$1" > $COMPAREFILE1
382 echo "$2" > $COMPAREFILE2
383 shift 2
384 msgtest "Test for equality OR of" "$*"
385 $* 2>&1 1> $COMPAREAGAINST
386 (diff $COMPAREFILE1 $COMPAREAGAINST 1> /dev/null ||
387 diff $COMPAREFILE2 $COMPAREAGAINST 1> /dev/null) && msgpass ||
388 ( echo "\n${CINFO}Diff against OR 1${CNORMAL}" "$(diff $COMPAREFILE1 $COMPAREAGAINST)" \
389 "\n${CINFO}Diff against OR 2${CNORMAL}" "$(diff $COMPAREFILE2 $COMPAREAGAINST)" &&
390 msgfail )
391 rm $COMPAREFILE1 $COMPAREFILE2 $COMPAREAGAINST
392 }
393
394 testshowvirtual() {
395 local VIRTUAL="N: Can't select versions from package '$1' as it purely virtual"
396 local PACKAGE="$1"
397 shift
398 while [ -n "$1" ]; do
399 VIRTUAL="${VIRTUAL}
400 N: Can't select versions from package '$1' as it purely virtual"
401 PACKAGE="${PACKAGE} $1"
402 shift
403 done
404 msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
405 VIRTUAL="${VIRTUAL}
406 N: No packages found"
407 local COMPAREFILE=$(mktemp)
408 local ARCH=$(dpkg-architecture -qDEB_HOST_ARCH_CPU)
409 eval `apt-config shell ARCH APT::Architecture`
410 echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' > $COMPAREFILE
411 aptcache show $PACKAGE 2>&1 | diff $COMPAREFILE - && msgpass || msgfail
412 rm $COMPAREFILE
413 }
414
415 testnopackage() {
416 msgtest "Test for non-existent packages" "apt-cache show $*"
417 local SHOWPKG="$(aptcache show $* 2>&1 | grep '^Package: ')"
418 if [ -n "$SHOWPKG" ]; then
419 echo
420 echo "$SHOWPKG"
421 msgfail
422 return 1
423 fi
424 msgpass
425 }