Search code examples
debdpkg

How do you create a fake install of a debian package for use in testing?


I have a package that previously only targeted RPM based distros for which I am now building .deb packages for Debian based distros.

The aim is to simulate a test installation from user-space that is isolated from the system you are building on. It may be multi-user and you do not want to require root access just to build the software. Many of our tests simulate the installation directory structure already. This is for the next step up to simulate an actual installation using packages built.

For the RPM packages I was able to create test installations using:

WSDIR=/where/I/want/my/tests/to/run
rpmdb --initdb --dbpath "$WSDIR"/rpmdb
rpm --relocate /opt="$WSDIR"/opt --dbpath $WSDIR/rpmdb -i <package>.rpm 

The equivalent in the Debian world is something like:

dpkg --force-not-root --admindir=$WSDIR/dpkg --root=$WSDIR/install --install "$DEB" 

However, I am stuck over the equivalent to the rpmdb --initdb step.

Note that I can just unpack the archive using:

dpkg-deb -x "$DEB" $WSDIR/install

But I would prefer to be closer to how a real package is installed. Also I don't think this will run preinstall and postinstall scripts.

Similar questions have suggested using deboostrap to create a chroot environment but this creates a complete new installation. As well as being overkill it is too slow for an automated test. I intend to use this for quick tests of the installation package prior to further testing in actual test environments.

My experiments so far:

(cd $WSDIR/dpkg && mkdir alternatives info parts triggers updates)
cp /var/lib/dpkg/status $WSDIR/dpkg/status

have at best resulted in:

dpkg: error: unable to access dpkg status area: No such file or directory

which does not indicate clear what is wrong.

So how do you create a dpkg admin directory?

Cross posted as https://superuser.com/questions/1271145/how-do-you-create-a-dpkg-admin-directory


Update 24/11/2017

I've tried copying using the dpkg dir from an environment created by [cowdancer][1] (which uses deboostrap under the hood) or copying the real one from /var/lib/dpkg but I still get the same error message so perhaps the error (and/or the --admindir option) doesn't mean quite what I think it means.

Note that:

sudo dpkg --force-not-root --root=$WSDIR/install  --admindir=/var/lib/dpkg --install "$DEB"

does work. So it is something to do with the admin dir. I've also retitled the question as "How do you create a dpkg admin directory" is interesting question but the answer is not necessarily the solution to my problem.


Solution

  • I eventually found an answer for this. Thanks to Guillem Jover for some of this. Pasting a copy of it here:

    mkdir fake
    mkdir fake/install
    mkdir -p fake/dpkg/info
    mkdir -p fake/dpkg/updates
    touch fake/dpkg/status
    PATH=/sbin:/usr/sbin:$PATH fakeroot dpkg --force-script-chrootless --log=`pwd`/fake/dpkg.log --root=`pwd`/fake --instdir `pwd`/fake --admindir=`pwd`/fake/dpkg --install *.deb
    

    Some points to note:

    • --force-not-root is not enough. fakeroot is required.

    • ldconfig and start-stop-daemon must be on the path. (hence PATH=/sbin:/usr/sbin:$PATH)

    • The log file needs to be relocated from the default /var/log/dpkg.log

    • The order of arguments is significant. If used --root must be before --instdir and --admindir.

    • The admindir is supposed to have a the installation dir as a prefix.

    • If the package contains any pre or post installation scripts (preinst,postinst) then --force-script-chrootless is required as these scripts are normally run via chroot() which gives operation not permitted when attempted under fakeroot.


    Its worth mentioning that a modern solution would be to do a normal install in a docker sandbox environment.