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

1

This was originally posted to stackoverflow (https://stackoverflow.com/questions/47099045/how-do-you-create-a-dpkg-admin-directory) but probably belongs here instead

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?


Update 24/11/2017

I've tried copying using the dpkg dir from an environment created by cowdancer (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 admind 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.

Bruce Adams

Posted 2017-11-23T12:56:49.813

Reputation: 111

Note the original question currently has an open bounty. – Bruce Adams – 2017-11-23T12:57:03.840

Hmm. May be just use debootstrap or cdebootstrap to create a chroot with a minimal "real" installation and then install the package there? You could snapshot the contents of the target directory after bootstrapping and then compare it with the results of the trial package installation. There exist a special solution, cowdancer, which can make this process much faster by "preimaging" the debootstrapped directory.

– kostix – 2017-11-24T07:38:49.903

Otherwise you may look at fakeroot — the stock tools used to build Debian package use it to "virtualize" a call to make install they make — making it think it was called by root and that in installs stuff into a real filesystem directories with restricted access (such as /usr/bin) while actually redirected into a dedicated directory writable by the normal user running the fakeroot encantation. – kostix – 2017-11-24T07:40:48.893

Answers

2

Thanks to Guillem Jover (see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=883700) and a little digging throug the dpkg source code. I now have a solution:

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.

Bruce Adams

Posted 2017-11-23T12:56:49.813

Reputation: 111