]> git.saurik.com Git - apt.git/blame - test/integration/test-failing-maintainer-scripts
ensure proper teardown in dpkg error cases
[apt.git] / test / integration / test-failing-maintainer-scripts
CommitLineData
d151adbf
DK
1#!/bin/sh
2set -e
3
4TESTDIR=$(readlink -f $(dirname $0))
5. $TESTDIR/framework
6
7setupenvironment
8configarchitecture 'native'
9
10# create a bunch of failures
11createfailure() {
12 setupsimplenativepackage "failure-$1" 'native' '1.0' 'unstable' 'Depends: dependee'
13 BUILDDIR="incoming/failure-$1-1.0"
14 echo '#!/bin/sh
15exit 29' > ${BUILDDIR}/debian/$1
16 buildpackage "$BUILDDIR" 'unstable' 'main' 'native'
17 rm -rf "$BUILDDIR"
18}
19
20buildsimplenativepackage 'dependee' 'native' '1.0' 'unstable'
21createfailure 'preinst'
22createfailure 'postinst'
23createfailure 'prerm'
24createfailure 'postrm'
25
26setupaptarchive
27
28# create a library to noop chroot() and rewrite maintainer script executions
29# via execvp() as used by dpkg as we don't want our rootdir to be a fullblown
30# chroot directory dpkg could chroot into to execute the maintainer scripts
31cat << EOF > noopchroot.c
32#define _GNU_SOURCE
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <dlfcn.h>
37
38static char * chrootdir = NULL;
39
40int chroot(const char *path) {
41 printf("WARNING: CHROOTing to %s was ignored!\n", path);
42 free(chrootdir);
43 chrootdir = strdup(path);
44 return 0;
45}
46int execvp(const char *file, char *const argv[]) {
47 static int (*func_execvp) (const char *, char * const []) = NULL;
48 if (func_execvp == NULL)
49 func_execvp = (int (*) (const char *, char * const [])) dlsym(RTLD_NEXT, "execvp");
50 if (chrootdir == NULL || strncmp(file, "/var/lib/dpkg/", strlen("/var/lib/dpkg/")) != 0)
51 return func_execvp(file, argv);
52 printf("REWRITE execvp call %s into %s\n", file, chrootdir);
53 char newfile[strlen(chrootdir) + strlen(file)];
54 strcpy(newfile, chrootdir);
55 strcat(newfile, file);
56 return func_execvp(newfile, argv);
57}
58EOF
59testsuccess gcc -fPIC -shared -o noopchroot.so noopchroot.c -ldl
60
61mkdir -p "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/"
62DPKG="${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg"
63echo "#!/bin/sh
64if [ -n \"\$LD_PRELOAD\" ]; then
65 export LD_PRELOAD=\"${TMPWORKINGDIRECTORY}/noopchroot.so \${LD_PRELOAD}\"
66else
67 export LD_PRELOAD=\"${TMPWORKINGDIRECTORY}/noopchroot.so\"
68fi
69dpkg \"\$@\"" > $DPKG
70chmod +x $DPKG
71sed -ie "s|^DPKG::options:: \"dpkg\";\$|DPKG::options:: \"$DPKG\";|" aptconfig.conf
72
73# setup some pre- and post- invokes to check the output isn't garbled later
74APTHOOK="${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apthook"
75echo '#!/bin/sh
76echo "$1: START"
77echo "$1: MaiN"
78echo "$1: ENd"' > $APTHOOK
79chmod +x $APTHOOK
80echo "DPKG::Pre-Invoke:: \"${APTHOOK} PRE\";
81DPKG::Post-Invoke:: \"${APTHOOK} POST\";" > rootdir/etc/apt/apt.conf.d/99apthooks
82
83testmyfailure() {
84 local PROGRESS='rootdir/tmp/progress.log'
85 exec 3> $PROGRESS
86 testfailure "$@" -o APT::Status-Fd=3
87 msgtest 'Test for failure message of maintainerscript in' 'console log'
88 local TEST='rootdir/tmp/testfailure.output'
89 if grep -q 'exit status 29$' "$TEST"; then
90 msgpass
91 else
92 cat $TEST
93 msgfail
94 fi
95 msgtest 'Test for proper execution of invoke scripts in' 'console log'
96 if grep -q '^PRE: START$' $TEST &&
97 grep -q '^PRE: MaiN$' $TEST &&
98 grep -q '^PRE: ENd$' $TEST &&
99 grep -q '^POST: START$' $TEST &&
100 grep -q '^POST: MaiN$' $TEST &&
101 grep -q '^POST: ENd$' $TEST; then
102 msgpass
103 else
104 cat $TEST
105 msgfail
106 fi
107 msgtest 'Test for failure message of maintainerscript in' 'progress log'
108 if grep -q '^pmerror:.\+exit status 29$' "$PROGRESS"; then
109 msgpass
110 else
111 cat $PROGRESS
112 msgfail
113 fi
114 testmarkedauto 'dependee'
115}
116
117cp -a rootdir/var/lib/dpkg/status rootdir/var/lib/dpkg/status.backup
118testmyfailure aptget install failure-preinst -y
119cp -a rootdir/var/lib/dpkg/status.backup rootdir/var/lib/dpkg/status
120testmyfailure aptget install failure-postinst -y
121cp -a rootdir/var/lib/dpkg/status.backup rootdir/var/lib/dpkg/status
122testsuccess aptget install failure-prerm -y
123testdpkginstalled failure-prerm
124testmyfailure aptget purge failure-prerm -y
125cp -a rootdir/var/lib/dpkg/status.backup rootdir/var/lib/dpkg/status
126testsuccess aptget install failure-postrm -y
127testdpkginstalled failure-postrm
128testmyfailure aptget purge failure-postrm -y
129
130# FIXME: test with output going to a PTY as it usually does
131#cp -a rootdir/var/lib/dpkg/status.backup rootdir/var/lib/dpkg/status
132#aptget install failure-preinst -y