Hi Martin,

On Sun, 13 Mar 2016, Martin Pitt wrote:
> Many thanks for working on this!
> 
> However, this breaks the test suite left and right:
> 
>   http://paste.ubuntu.com/15378313/
> 
> on a lot of "No such file or directory" or "unable to read
> footest.service" errors?

Duh, I missed the existence of this test suite.

Please find attached a patch fixing the test suite. To be applied on top
of my former patch (although it should work without it, too).

Cheers,
-- 
Raphaël Hertzog ◈ Debian Developer

Support Debian LTS: http://www.freexian.com/services/debian-lts.html
Learn to master Debian: http://debian-handbook.info/get/
>From 5cc97b272d7c9d76536c2930ed8c59d06d87cdfb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Rapha=C3=ABl=20Hertzog?= <hert...@debian.org>
Date: Tue, 15 Mar 2016 18:15:40 +0100
Subject: [PATCH] Fix test suite after changes to use systemctl preset

There are multiple problems:

1/ first the behaviour of "dsh enable" is no longer the same when
   the service was already installed or not... and as such the test suite
   must be fixed in multiple places to ensure that the metadata
   in /var/lib/systemd/ is properly purged before calling "enable" again
   and expecting it to do its job.

2/ "systemctl preset" does collaborate with the running systemd to do its
   job but the test suite uses its own filesystem namespace which is not
   shared with the running systemd... and hence we get errors about
   service files not existing in the systemd namespace even though they
   exist in the testsuite namespace.

   To work around this I modified the DEP-8 test to not use a separate
   namespace... and marked the test as breaking the system so that it
   does not get run on a real system. This has some consequences on some
   other tests that assume that the /{lib,etc}/systemd/ directories are
   empty. Those tests have been fixed along the way.
---
 debian/tests/control               |  2 +-
 debian/tests/t                     |  2 ++
 t/001-deb-systemd-helper.t         | 24 ++++++++++++++++++------
 t/002-deb-systemd-helper-update.t  |  8 +++++---
 t/003-deb-systemd-helper-complex.t | 18 ++++++++++++------
 5 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/debian/tests/control b/debian/tests/control
index ae8d2bc..d85b9f4 100644
--- a/debian/tests/control
+++ b/debian/tests/control
@@ -2,4 +2,4 @@ Tests: t
 Depends: @,
   cpanminus,
   build-essential
-Restrictions: needs-root, allow-stderr, isolation-container
+Restrictions: needs-root, breaks-testbed, allow-stderr, isolation-container
diff --git a/debian/tests/t b/debian/tests/t
index 9b518bb..29e4b66 100644
--- a/debian/tests/t
+++ b/debian/tests/t
@@ -4,6 +4,8 @@ set -eu
 mount --make-rprivate /
 cpanm Linux::Clone
 
+export TEST_ON_REAL_SYSTEM=1
+
 for test in t/*.t; do
     echo "======== $(basename $test) ======="
     perl $test
diff --git a/t/001-deb-systemd-helper.t b/t/001-deb-systemd-helper.t
index 6def515..64332e3 100644
--- a/t/001-deb-systemd-helper.t
+++ b/t/001-deb-systemd-helper.t
@@ -44,9 +44,11 @@ sub bind_mount_tmp {
     return $tmp;
 }
 
-my $etc_systemd = bind_mount_tmp('/etc/systemd');
-my $lib_systemd = bind_mount_tmp('/lib/systemd');
-my $var_lib_systemd = bind_mount_tmp('/var/lib/systemd');
+unless ($ENV{'TEST_ON_REAL_SYSTEM'}) {
+    my $etc_systemd = bind_mount_tmp('/etc/systemd');
+    my $lib_systemd = bind_mount_tmp('/lib/systemd');
+    my $var_lib_systemd = bind_mount_tmp('/var/lib/systemd');
+}
 
 # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
 # ┃ Verify “is-enabled” is not true for a random, non-existing unit file.     ┃
@@ -88,8 +90,11 @@ isnt_debian_installed($random_unit);
 # ┃ Verify “enable” creates the requested symlinks.                           ┃
 # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 
-ok(! -d '/etc/systemd/system/multi-user.target.wants',
-    'multi-user.target.wants does not exist yet');
+unless ($ENV{'TEST_ON_REAL_SYSTEM'}) {
+    # This might exist if we don't start from a fresh directory
+    ok(! -d '/etc/systemd/system/multi-user.target.wants',
+       'multi-user.target.wants does not exist yet');
+}
 
 $retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh enable $random_unit");
 is($retval, 0, "enable command succeeded");
@@ -176,6 +181,7 @@ $retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh purge $random_unit");
 is($retval, 0, "purge command succeeded");
 
 isnt_enabled($random_unit);
+isnt_debian_installed($random_unit);
 
 # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
 # ┃ Verify “enable” after purging does re-create the symlinks.                ┃
@@ -284,6 +290,9 @@ unlink($mask_path);
 # ┃ Verify Alias= handling.                                                   ┃
 # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 
+$retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh purge $random_unit");
+is($retval, 0, "purge command succeeded");
+
 open($fh, '>', $servicefile_path);
 print $fh <<'EOT';
 [Unit]
@@ -326,6 +335,8 @@ ok(! -l $alias_path, 'alias link does not exist any more');
 # ┃ Verify Alias/mask with removed package (as in postrm)                     ┃
 # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 
+$retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh purge $random_unit");
+is($retval, 0, "purge command succeeded");
 $retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh enable $random_unit");
 is($retval, 0, "enable command succeeded");
 is(readlink($alias_path), $servicefile_path, 'correct alias link');
@@ -370,7 +381,8 @@ isnt_enabled('footest.service');
 $retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh enable $random_unit");
 is($retval, 0, "enable command succeeded");
 is_enabled($random_unit);
-ok(! -l $mask_path, 'mask link does not exist yet');
+# systemctl enable does create the alias link even if it's not needed
+#ok(! -l $mask_path, 'mask link does not exist yet');
 
 unlink($servicefile_path);
 
diff --git a/t/002-deb-systemd-helper-update.t b/t/002-deb-systemd-helper-update.t
index ae401e9..d16acd9 100644
--- a/t/002-deb-systemd-helper-update.t
+++ b/t/002-deb-systemd-helper-update.t
@@ -54,9 +54,11 @@ sub bind_mount_tmp {
     return $tmp;
 }
 
-my $etc_systemd = bind_mount_tmp('/etc/systemd');
-my $lib_systemd = bind_mount_tmp('/lib/systemd');
-my $var_lib_systemd = bind_mount_tmp('/var/lib/systemd');
+unless ($ENV{'TEST_ON_REAL_SYSTEM'}) {
+    my $etc_systemd = bind_mount_tmp('/etc/systemd');
+    my $lib_systemd = bind_mount_tmp('/lib/systemd');
+    my $var_lib_systemd = bind_mount_tmp('/var/lib/systemd');
+}
 
 # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
 # ┃ Verify “is-enabled” is not true for a random, non-existing unit file.     ┃
diff --git a/t/003-deb-systemd-helper-complex.t b/t/003-deb-systemd-helper-complex.t
index 924013a..4bcf4be 100644
--- a/t/003-deb-systemd-helper-complex.t
+++ b/t/003-deb-systemd-helper-complex.t
@@ -44,9 +44,11 @@ sub bind_mount_tmp {
     return $tmp;
 }
 
-my $etc_systemd = bind_mount_tmp('/etc/systemd');
-my $lib_systemd = bind_mount_tmp('/lib/systemd');
-my $var_lib_systemd = bind_mount_tmp('/var/lib/systemd');
+unless ($ENV{'TEST_ON_REAL_SYSTEM'}) {
+    my $etc_systemd = bind_mount_tmp('/etc/systemd');
+    my $lib_systemd = bind_mount_tmp('/lib/systemd');
+    my $var_lib_systemd = bind_mount_tmp('/var/lib/systemd');
+}
 
 # ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
 # ┃ Create two unit files with random names; one refers to the other (Also=). ┃
@@ -106,12 +108,16 @@ isnt_debian_installed($random_unit2);
 # ┃ Verify “enable” creates all symlinks.                                     ┃
 # ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
 
-ok(! -d '/etc/systemd/system/multi-user.target.wants',
-    'multi-user.target.wants does not exist yet');
+unless ($ENV{'TEST_ON_REAL_SYSTEM'}) {
+    # This might already exist if we don't start from a fresh directory
+    ok(! -d '/etc/systemd/system/multi-user.target.wants',
+       'multi-user.target.wants does not exist yet');
+}
 
 $retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh enable $random_unit1");
 my %links = map { (basename($_), readlink($_)) }
-    </etc/systemd/system/multi-user.target.wants/*.service>;
+    ("/etc/systemd/system/multi-user.target.wants/$random_unit1",
+     "/etc/systemd/system/multi-user.target.wants/$random_unit2");
 is_deeply(
     \%links,
     {
-- 
2.7.0

Reply via email to