summaryrefslogtreecommitdiffstats
path: root/t/dh_installsystemd
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 21:06:40 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 21:06:40 +0000
commitd827c6cf1631209f5042a9d1d8a7ecc24223c8a0 (patch)
tree91a431d301efd0e524bdfb0c46e97d591a9d7b03 /t/dh_installsystemd
parentInitial commit. (diff)
downloaddebhelper-d827c6cf1631209f5042a9d1d8a7ecc24223c8a0.tar.xz
debhelper-d827c6cf1631209f5042a9d1d8a7ecc24223c8a0.zip
Adding upstream version 13.11.4.upstream/13.11.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--t/dh_installsystemd/debian/changelog5
-rw-r--r--t/dh_installsystemd/debian/control20
-rw-r--r--t/dh_installsystemd/debian/foo.init4
-rw-r--r--t/dh_installsystemd/debian/foo.service8
-rw-r--r--t/dh_installsystemd/debian/foo2.service8
-rwxr-xr-xt/dh_installsystemd/dh_installsystemd.t255
-rwxr-xr-xt/dh_installsystemd/dh_installsystemd_tmpfiles.t87
-rwxr-xr-xt/dh_installsystemd/dh_systemd.t123
-rw-r--r--t/dh_installsystemd/simple/debian/changelog5
-rw-r--r--t/dh_installsystemd/simple/debian/control10
-rw-r--r--t/dh_installsystemd/simple/debian/foo.service8
-rw-r--r--t/dh_installsystemduser/debian/baz.user.service8
-rw-r--r--t/dh_installsystemduser/debian/changelog5
-rw-r--r--t/dh_installsystemduser/debian/control10
-rw-r--r--t/dh_installsystemduser/debian/foo.user.service8
-rwxr-xr-xt/dh_installsystemduser/dh_installsystemduser.t112
16 files changed, 676 insertions, 0 deletions
diff --git a/t/dh_installsystemd/debian/changelog b/t/dh_installsystemd/debian/changelog
new file mode 100644
index 0000000..5850f0e
--- /dev/null
+++ b/t/dh_installsystemd/debian/changelog
@@ -0,0 +1,5 @@
+foo (1.0-1) unstable; urgency=low
+
+ * Initial release. (Closes: #XXXXXX)
+
+ -- Test <testing@nowhere> Mon, 11 Jul 2016 18:10:59 +0200
diff --git a/t/dh_installsystemd/debian/control b/t/dh_installsystemd/debian/control
new file mode 100644
index 0000000..48d4de2
--- /dev/null
+++ b/t/dh_installsystemd/debian/control
@@ -0,0 +1,20 @@
+Source: foo
+Section: misc
+Priority: optional
+Maintainer: Test <testing@nowhere>
+Standards-Version: 3.9.8
+
+Package: foo
+Architecture: all
+Description: package foo
+ Package foo
+
+Package: bar
+Architecture: all
+Description: package bar
+ Package bar
+
+Package: baz
+Architecture: all
+Description: package baz
+ Package baz
diff --git a/t/dh_installsystemd/debian/foo.init b/t/dh_installsystemd/debian/foo.init
new file mode 100644
index 0000000..2b77921
--- /dev/null
+++ b/t/dh_installsystemd/debian/foo.init
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+some script
+
diff --git a/t/dh_installsystemd/debian/foo.service b/t/dh_installsystemd/debian/foo.service
new file mode 100644
index 0000000..2b48a78
--- /dev/null
+++ b/t/dh_installsystemd/debian/foo.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=A unit
+
+[Service]
+ExecStart=/bin/true
+
+[Install]
+WantedBy=multi-user.target
diff --git a/t/dh_installsystemd/debian/foo2.service b/t/dh_installsystemd/debian/foo2.service
new file mode 100644
index 0000000..42b9445
--- /dev/null
+++ b/t/dh_installsystemd/debian/foo2.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Another unit
+
+[Service]
+ExecStart=/bin/true
+
+[Install]
+WantedBy=multi-user.target
diff --git a/t/dh_installsystemd/dh_installsystemd.t b/t/dh_installsystemd/dh_installsystemd.t
new file mode 100755
index 0000000..bbb4eb0
--- /dev/null
+++ b/t/dh_installsystemd/dh_installsystemd.t
@@ -0,0 +1,255 @@
+#!/usr/bin/perl
+use strict;
+use Test::More;
+
+use File::Basename qw(dirname);
+use lib dirname(dirname(__FILE__));
+use Test::DH;
+use File::Path qw(remove_tree make_path);
+use Debian::Debhelper::Dh_Lib qw(!dirname);
+
+plan(tests => 4);
+
+sub write_file {
+ my ($path, $content) = @_;
+
+ my $dir = dirname($path);
+ mkdirs($dir);
+
+ open(my $fd, '>>', $path) or error("open($path) failed: $!");
+ print {$fd} $content . '\n';
+ close($fd) or error("close($path) failed: $!");
+}
+
+sub unit_is_enabled {
+ my ($package, $unit, $num_enables) = @_;
+ my @output;
+ my $matches;
+ my @postinst_snippets = find_script($package, 'postinst');
+ @output=`cat @postinst_snippets` if @postinst_snippets;
+ # Match exactly one tab; the "dont-enable" script has an "enable"
+ # line for re-enabling the service if the admin had it enabled.
+ # But we do not want to include that in our count.
+ $matches = grep { m{^\tif deb-systemd-helper .* was-enabled .*'\Q$unit\E\.service'} } @output;
+ ok($matches == $num_enables) or diag("$unit appears to have been enabled $matches times (expected $num_enables)");
+}
+
+sub unit_is_started {
+ my ($package, $unit, $num_starts, $num_stops) = @_;
+ my @output;
+ my $matches;
+ $num_stops = $num_stops // $num_starts;
+ my @postinst_snippets = find_script($package, 'postinst');
+ @output=`cat @postinst_snippets` if @postinst_snippets;
+ $matches = grep { m{deb-systemd-invoke \$_dh_action .*'\Q$unit\E.service'} } @output;
+ ok($matches == $num_starts) or diag("$unit appears to have been started $matches times (expected $num_starts)");
+ my @prerm_snippets = find_script($package, 'prerm');
+ @output=`cat @prerm_snippets` if @prerm_snippets;
+ $matches = grep { m{deb-systemd-invoke stop .*'\Q$unit\E.service'} } @output;
+ ok($matches == $num_stops) or diag("$unit appears to have been stopped $matches times (expected $num_stops)");
+}
+
+
+#
+# Test a simple source package defining a single binary package
+#
+our $TEST_DH_FIXTURE_DIR = 'simple';
+our @TEST_DH_EXTRA_TEMPLATE_FILES = (qw(
+ debian/changelog
+ debian/control
+ debian/foo.service
+));
+
+each_compat_subtest {
+ ok(run_dh_tool('dh_installsystemd'));
+ ok(-e 'debian/foo/lib/systemd/system/foo.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ ok(run_dh_tool('dh_installsystemd', '--no-start'));
+ ok(-e 'debian/foo/lib/systemd/system/foo.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 0, 0);
+ ok(run_dh_tool('dh_clean'));
+
+ ok(run_dh_tool('dh_installsystemd', '--no-start', 'foo.service'));
+ ok(-e 'debian/foo/lib/systemd/system/foo.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 0, 0);
+ ok(run_dh_tool('dh_clean'));
+};
+
+
+#
+# Test a more complex source package defining three binary packages
+#
+$TEST_DH_FIXTURE_DIR = '.';
+@TEST_DH_EXTRA_TEMPLATE_FILES = (qw(
+ debian/changelog
+ debian/control
+ debian/foo.service
+ debian/foo2.service
+));
+
+each_compat_subtest {
+ ok(run_dh_tool( 'dh_installsystemd'));
+ ok(-e 'debian/foo/lib/systemd/system/foo.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 1);
+ unit_is_enabled('foo', 'foo2', 0);
+ unit_is_started('foo', 'foo2', 0);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_installsystemd'));
+ ok(-e 'debian/foo/lib/systemd/system/foo.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 1);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 1);
+ ok(run_dh_tool('dh_clean'));
+
+
+ # lib -> usr/lib (if we ever support that)
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_installsystemd'));
+ ok(-e 'debian/foo/lib/systemd/system/foo2.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 1);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ # lib -> usr/lib with both no-empty (if we ever suport that)
+ make_path('debian/foo/lib/systemd/system/');
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/bar.service');
+ ok(run_dh_tool('dh_installsystemd'));
+ ok(-e 'debian/foo/lib/systemd/system/bar.service');
+ ok(-e 'debian/foo/lib/systemd/system/foo2.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 1);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_installsystemd', '--no-start'));
+ ok(-e 'debian/foo/lib/systemd/system/foo.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 0, 0);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 0, 0);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_installsystemd', '-p', 'foo', '--no-start', 'foo.service'));
+ ok(run_dh_tool('dh_installsystemd', '-p', 'foo', 'foo2.service'));
+ ok(-e 'debian/foo/lib/systemd/system/foo.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 0, 0);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_installsystemd', '-p', 'foo', '--no-enable', 'foo.service'));
+ ok(run_dh_tool('dh_installsystemd', '-p', 'foo', 'foo2.service'));
+ ok(-e 'debian/foo/lib/systemd/system/foo.service');
+ ok(find_script('foo', 'postinst'));
+ unit_is_enabled('foo', 'foo', 0);
+ unit_is_started('foo', 'foo', 1, 1);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ ok(run_dh_tool('dh_installsystemd', '--no-restart-after-upgrade'));
+ my @foo_postinst = find_script('foo', 'postinst');
+ ok(@foo_postinst);
+ my $matches = @foo_postinst ? grep { m{deb-systemd-invoke start .*foo.service} } `cat @foo_postinst` : -1;
+ ok($matches == 1);
+ ok(run_dh_tool('dh_clean'));
+
+ # Quoting #764730
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo\x2dfuse.service');
+ ok(run_dh_tool('dh_installsystemd'));
+ unit_is_enabled('foo', 'foo\x2dfuse', 1);
+ unit_is_started('foo', 'foo\x2dfuse', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ # --name flag #870768
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_installsystemd', '--name=foo'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 1);
+ unit_is_enabled('foo', 'foo2', 0);
+ unit_is_started('foo', 'foo2', 0);
+ ok(run_dh_tool('dh_installsystemd', '--name=foo2'));
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 1);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service');
+ make_symlink_raw_target('target.service', 'debian/foo/lib/systemd/system/source.service');
+ ok(run_dh_tool('dh_installsystemd'));
+ unit_is_enabled('foo', 'foo', 1);
+ # Alias= realized by symlinks are not enabled in maintaner scripts
+ unit_is_enabled('foo', 'source', 0);
+ unit_is_enabled('foo', 'target', 1);
+ ok(run_dh_tool('dh_clean'));
+};
+
+each_compat_up_to_and_incl_subtest(11, sub {
+ make_path('debian/foo/lib/systemd/system/');
+ make_path('debian/foo/etc/init.d/');
+ copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service');
+ make_symlink_raw_target('target.service', 'debian/foo/lib/systemd/system/source.service');
+ write_file('debian/foo/etc/init.d/source', '# something');
+ ok(run_dh_tool('dh_installsystemd'));
+ unit_is_enabled('foo', 'foo', 1);
+ # Alias= realized by symlinks are not enabled in maintaner scripts
+ unit_is_enabled('foo', 'source', 0);
+ unit_is_enabled('foo', 'target', 1);
+ # The presence of a sysvinit script for the alias unit inhibits start of both
+ unit_is_started('foo', 'source', 0);
+ unit_is_started('foo', 'target', 0);
+ ok(run_dh_tool('dh_clean'));
+});
+
+each_compat_from_and_above_subtest(12, sub {
+ make_path('debian/foo/lib/systemd/system/');
+ make_path('debian/foo/etc/init.d/');
+ copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/target.service');
+ make_symlink_raw_target('target.service', 'debian/foo/lib/systemd/system/source.service');
+ write_file('debian/foo/etc/init.d/source', '# something');
+ ok(run_dh_tool('dh_installsystemd'));
+ unit_is_enabled('foo', 'foo', 1);
+ # Alias= realized by symlinks are not enabled in maintaner scripts
+ unit_is_enabled('foo', 'source', 0);
+ unit_is_enabled('foo', 'target', 1);
+ unit_is_started('foo', 'source', 0);
+ unit_is_started('foo', 'target', 1);
+ ok(run_dh_tool('dh_clean'));
+});
diff --git a/t/dh_installsystemd/dh_installsystemd_tmpfiles.t b/t/dh_installsystemd/dh_installsystemd_tmpfiles.t
new file mode 100755
index 0000000..0fffe57
--- /dev/null
+++ b/t/dh_installsystemd/dh_installsystemd_tmpfiles.t
@@ -0,0 +1,87 @@
+#!/usr/bin/perl
+use strict;
+use Test::More;
+
+use File::Basename qw(dirname);
+use lib dirname(dirname(__FILE__));
+use Test::DH;
+use File::Path qw(remove_tree make_path);
+use Debian::Debhelper::Dh_Lib qw(!dirname);
+
+our @TEST_DH_EXTRA_TEMPLATE_FILES = (qw(
+ debian/changelog
+ debian/control
+ debian/foo.service
+ debian/foo.init
+));
+
+plan(tests => 3);
+
+
+# Units are installed and enabled
+each_compat_from_x_to_and_incl_y_subtest(11, 12, sub {
+ make_path('debian/foo/usr/lib/tmpfiles.d');
+ create_empty_file('debian/foo/usr/lib/tmpfiles.d/foo.conf');
+ ok(run_dh_tool('dh_installinit'));
+ ok(run_dh_tool('dh_installsystemd'));
+ ok(-e "debian/foo/etc/init.d/foo");
+ ok(-e "debian/foo/lib/systemd/system/foo.service");
+ my @postinst = find_script('foo', 'postinst');
+ # We should have two snippets (one for the tmpfiles and one for the services).
+ is(scalar(@postinst), 2);
+ if (scalar(@postinst) == 2) {
+ open(my $fd, '<', $postinst[0]) or error("open($postinst[0]) failed: $!");
+ my $early_snippet = readlines($fd);
+ close($fd);
+ open($fd, '<', $postinst[1]) or error("open($postinst[1]) failed: $!");
+ my $late_snippet = readlines($fd);
+ close($fd);
+ ok(! grep { m/(?:invoke|update)-rc.d|deb-systemd-invoke/ } @{$early_snippet});
+ ok(grep { m/(?:invoke|update)-rc.d|deb-systemd-invoke/ } @{$late_snippet});
+ ok(grep { m/systemd-tmpfiles/ } @{$early_snippet});
+ ok(! grep { m/systemd-tmpfiles/ } @{$late_snippet});
+ }
+ ok(run_dh_tool('dh_clean'));
+
+});
+
+each_compat_from_and_above_subtest(13, sub {
+ make_path('debian/foo/usr/lib/tmpfiles.d');
+ create_empty_file('debian/foo/usr/lib/tmpfiles.d/foo.conf');
+ ok(run_dh_tool('dh_installinit'));
+ ok(run_dh_tool('dh_installsystemd'));
+ ok(-e "debian/foo/etc/init.d/foo");
+ ok(-e "debian/foo/lib/systemd/system/foo.service");
+ my @postinst = find_script('foo', 'postinst');
+ # We should have one snippet (one for the services).
+ is(scalar(@postinst), 1);
+ if (scalar(@postinst) == 1) {
+ open(my $fd, '<', $postinst[0]) or error("open($postinst[0]) failed: $!");
+ my $snippet = readlines($fd);
+ close($fd);
+ ok(grep { m/(?:invoke|update)-rc.d|deb-systemd-invoke/ } @{$snippet});
+ ok(! grep { m/systemd-tmpfiles/ } @{$snippet});
+ }
+ ok(run_dh_tool('dh_clean'));
+});
+
+
+each_compat_from_and_above_subtest(13, sub {
+ make_path('debian/foo/usr/lib/tmpfiles.d');
+ create_empty_file('debian/foo/usr/lib/tmpfiles.d/foo.conf');
+ ok(run_dh_tool('dh_installtmpfiles'));
+ # dh_installtmpfiles do not install services
+ ok(!-e "debian/foo/etc/init.d/foo");
+ ok(!-e "debian/foo/lib/systemd/system/foo.service");
+ my @postinst = find_script('foo', 'postinst');
+ # We should have too snippets (one for the tmpfiles and one for the services).
+ is(scalar(@postinst), 1);
+ if (scalar(@postinst) == 1) {
+ open(my $fd, '<', $postinst[0]) or error("open($postinst[0]) failed: $!");
+ my $snippet = readlines($fd);
+ close($fd);
+ ok(! grep { m/(?:invoke|update)-rc.d|deb-systemd-invoke/ } @{$snippet});
+ ok(grep { m/systemd-tmpfiles/ } @{$snippet});
+ }
+ ok(run_dh_tool('dh_clean'));
+});
diff --git a/t/dh_installsystemd/dh_systemd.t b/t/dh_installsystemd/dh_systemd.t
new file mode 100755
index 0000000..c3c9401
--- /dev/null
+++ b/t/dh_installsystemd/dh_systemd.t
@@ -0,0 +1,123 @@
+#!/usr/bin/perl
+use strict;
+use Test::More;
+
+use File::Basename qw(dirname);
+use lib dirname(dirname(__FILE__));
+use Test::DH;
+use File::Path qw(remove_tree make_path);
+use Debian::Debhelper::Dh_Lib qw(!dirname);
+
+our @TEST_DH_EXTRA_TEMPLATE_FILES = (qw(
+ debian/changelog
+ debian/control
+ debian/foo.service
+ debian/foo2.service
+));
+
+plan(tests => 1);
+
+sub unit_is_enabled {
+ my ($package, $unit, $num_enables) = @_;
+ my @output;
+ my $matches;
+ @output=`cat debian/$package.postinst.debhelper`;
+ # Match exactly one tab; the "dont-enable" script has an "enable"
+ # line for re-enabling the service if the admin had it enabled.
+ # But we do not want to include that in our count.
+ $matches = grep { m{^\tif deb-systemd-helper .* was-enabled .*'\Q$unit\E\.service'} } @output;
+ ok($matches == $num_enables) or diag("$unit appears to have been enabled $matches times (expected $num_enables)");
+}
+sub unit_is_started {
+ my ($package, $unit, $num_starts, $num_stops) = @_;
+ my @output;
+ my $matches;
+ $num_stops = $num_stops // $num_starts;
+ @output=`cat debian/$package.postinst.debhelper`;
+ $matches = grep { m{deb-systemd-invoke \$_dh_action .*'\Q$unit\E.service'} } @output;
+ ok($matches == $num_starts) or diag("$unit appears to have been started $matches times (expected $num_starts)");
+ @output=`cat debian/$package.prerm.debhelper`;
+ $matches = grep { m{deb-systemd-invoke stop .*'\Q$unit\E.service'} } @output;
+ ok($matches == $num_stops) or diag("$unit appears to have been stopped $matches times (expected $num_stops)");
+}
+
+# Units are installed and enabled
+each_compat_from_x_to_and_incl_y_subtest(10, 10, sub {
+ ok(run_dh_tool('dh_systemd_enable'));
+ ok(run_dh_tool('dh_systemd_start'));
+ ok(-e "debian/foo/lib/systemd/system/foo.service");
+ ok(-e "debian/foo.postinst.debhelper");
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 1);
+ unit_is_enabled('foo', 'foo2', 0);
+ unit_is_started('foo', 'foo2', 0);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_systemd_enable'));
+ ok(run_dh_tool('dh_systemd_start'));
+ ok(-e "debian/foo/lib/systemd/system/foo.service");
+ ok(-e "debian/foo.postinst.debhelper");
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 1);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_systemd_enable'));
+ ok(run_dh_tool('dh_systemd_start', '--no-start'));
+ ok(-e "debian/foo/lib/systemd/system/foo.service");
+ ok(-e "debian/foo.postinst.debhelper");
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 0, 1); # present units are stopped on remove even if no start
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 0, 1);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_systemd_enable'));
+ ok(run_dh_tool('dh_systemd_start', '--no-start', 'debian/foo.service'));
+ ok(run_dh_tool('dh_systemd_start', '-p', 'foo', 'foo2.service'));
+ ok(-e "debian/foo/lib/systemd/system/foo.service");
+ ok(-e "debian/foo.postinst.debhelper");
+ unit_is_enabled('foo', 'foo', 1);
+ unit_is_started('foo', 'foo', 0, 1);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo2.service', 'debian/foo/lib/systemd/system/foo2.service');
+ ok(run_dh_tool('dh_systemd_enable', '--no-enable', 'debian/foo.service'));
+ ok(run_dh_tool('dh_systemd_enable', '-p', 'foo', 'foo2.service'));
+ ok(run_dh_tool('dh_systemd_start'));
+ ok(-e "debian/foo/lib/systemd/system/foo.service");
+ ok(-e "debian/foo.postinst.debhelper");
+ unit_is_enabled('foo', 'foo', 0);
+ unit_is_started('foo', 'foo', 1, 1);
+ unit_is_enabled('foo', 'foo2', 1);
+ unit_is_started('foo', 'foo2', 1);
+ ok(run_dh_tool('dh_clean'));
+
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo.service');
+ ok(run_dh_tool('dh_systemd_start', '--no-restart-after-upgrade'));
+ my $matches = grep { m{deb-systemd-invoke start .*foo.service} } `cat debian/foo.postinst.debhelper`;
+ ok($matches == 1);
+ ok(run_dh_tool('dh_clean'));
+
+ # Quoting #764730
+ make_path('debian/foo/lib/systemd/system/');
+ copy_file('debian/foo.service', 'debian/foo/lib/systemd/system/foo\x2dfuse.service');
+ ok(run_dh_tool('dh_systemd_enable'));
+ ok(run_dh_tool('dh_systemd_start'));
+ unit_is_enabled('foo', 'foo\x2dfuse', 1);
+ unit_is_started('foo', 'foo\x2dfuse', 1);
+ ok(run_dh_tool('dh_clean'));
+});
+
+
diff --git a/t/dh_installsystemd/simple/debian/changelog b/t/dh_installsystemd/simple/debian/changelog
new file mode 100644
index 0000000..5b1a8fe
--- /dev/null
+++ b/t/dh_installsystemd/simple/debian/changelog
@@ -0,0 +1,5 @@
+foo (1.0-1) unstable; urgency=low
+
+ * Initial release. (Closes: #XXXXXX)
+
+ -- Test <test@example.org> Mon, 11 Jul 2016 18:10:59 +0200
diff --git a/t/dh_installsystemd/simple/debian/control b/t/dh_installsystemd/simple/debian/control
new file mode 100644
index 0000000..4f4238e
--- /dev/null
+++ b/t/dh_installsystemd/simple/debian/control
@@ -0,0 +1,10 @@
+Source: foo
+Section: misc
+Priority: optional
+Maintainer: Test <testing@nowhere>
+Standards-Version: 3.9.8
+
+Package: foo
+Architecture: all
+Description: package foo
+ Package foo
diff --git a/t/dh_installsystemd/simple/debian/foo.service b/t/dh_installsystemd/simple/debian/foo.service
new file mode 100644
index 0000000..2b48a78
--- /dev/null
+++ b/t/dh_installsystemd/simple/debian/foo.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=A unit
+
+[Service]
+ExecStart=/bin/true
+
+[Install]
+WantedBy=multi-user.target
diff --git a/t/dh_installsystemduser/debian/baz.user.service b/t/dh_installsystemduser/debian/baz.user.service
new file mode 100644
index 0000000..3af041d
--- /dev/null
+++ b/t/dh_installsystemduser/debian/baz.user.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Baz User Unit
+
+[Service]
+ExecStart=/bin/true
+
+[Install]
+WantedBy=default.target
diff --git a/t/dh_installsystemduser/debian/changelog b/t/dh_installsystemduser/debian/changelog
new file mode 100644
index 0000000..5850f0e
--- /dev/null
+++ b/t/dh_installsystemduser/debian/changelog
@@ -0,0 +1,5 @@
+foo (1.0-1) unstable; urgency=low
+
+ * Initial release. (Closes: #XXXXXX)
+
+ -- Test <testing@nowhere> Mon, 11 Jul 2016 18:10:59 +0200
diff --git a/t/dh_installsystemduser/debian/control b/t/dh_installsystemduser/debian/control
new file mode 100644
index 0000000..adccbc6
--- /dev/null
+++ b/t/dh_installsystemduser/debian/control
@@ -0,0 +1,10 @@
+Source: foo
+Section: misc
+Priority: optional
+Maintainer: Test <testing@exampe.org>
+Standards-Version: 3.9.8
+
+Package: foo
+Architecture: all
+Description: package foo
+ Package foo
diff --git a/t/dh_installsystemduser/debian/foo.user.service b/t/dh_installsystemduser/debian/foo.user.service
new file mode 100644
index 0000000..7ef597d
--- /dev/null
+++ b/t/dh_installsystemduser/debian/foo.user.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Foo User Unit
+
+[Service]
+ExecStart=/bin/true
+
+[Install]
+WantedBy=default.target
diff --git a/t/dh_installsystemduser/dh_installsystemduser.t b/t/dh_installsystemduser/dh_installsystemduser.t
new file mode 100755
index 0000000..5171728
--- /dev/null
+++ b/t/dh_installsystemduser/dh_installsystemduser.t
@@ -0,0 +1,112 @@
+#!/usr/bin/perl
+use strict;
+use Test::More;
+use File::Path qw(make_path);
+use File::Basename qw(dirname);
+use lib dirname(dirname(__FILE__));
+use Test::DH;
+use Debian::Debhelper::Dh_Lib qw(!dirname);
+
+plan(tests => 1);
+
+our @TEST_DH_EXTRA_TEMPLATE_FILES = (qw(
+ debian/changelog
+ debian/control
+ debian/foo.user.service
+ debian/baz.user.service
+));
+
+
+sub read_script {
+ my ($package, $name) = @_;
+ my @lines;
+
+ foreach my $script (find_script($package, $name)) {
+ open(my $fh, '<', $script) or die("open($script): $!");
+ push @lines, $_ for <$fh>;
+ close($fh);
+ }
+
+ return @lines;
+}
+
+sub _unit_check_user_enabled {
+ my ($package, $unit, $enabled) = @_;
+ my $verb = $enabled ? "is" : "isnt";
+ my $matches;
+
+ my @postinst = read_script($package, 'postinst');
+ # Match exactly two tab character. The "dont-enable" script has
+ # an "enable" line for re-enabling the service if the admin had it
+ # enabled, but we do not want to include that in our count.
+ $matches = grep { m{^\t\tif deb-systemd-helper( --\w+)* --user was-enabled.*'\Q$unit'} } @postinst;
+ is($matches, $enabled, "$unit $verb enabled");
+}
+
+sub _unit_check_user_started {
+ my ($package, $unit, $started) = @_;
+ my $verb = $started ? "is" : "isnt";
+ my $matches;
+
+ my @postinst = read_script($package, 'postinst');
+ # Match exactly two tab character. The "dont-enable" script has
+ # an "enable" line for re-enabling the service if the admin had it
+ # enabled, but we do not want to include that in our count.
+ $matches = grep { m{deb-systemd-invoke --user restart.*'\Q$unit'} } @postinst;
+ is($matches, $started, "$unit $verb started");
+
+ my @prerm = read_script($package, 'prerm');
+ $matches = grep { m{deb-systemd-invoke --user stop.*'\Q$unit'} } @prerm;
+ is($matches, $started, "$unit $verb stopped");
+}
+
+sub is_enabled { _unit_check_user_enabled(@_, 1); }
+sub isnt_enabled { _unit_check_user_enabled(@_, 0); }
+sub is_started { _unit_check_user_started(@_, 1); }
+sub isnt_started { _unit_check_user_started(@_, 0); }
+
+each_compat_subtest {
+ my ($compat) = @_;
+ make_path('debian/foo/usr/lib/systemd/user/');
+ copy_file('debian/foo.user.service', 'debian/foo/usr/lib/systemd/user/bar.service');
+ ok(run_dh_tool('dh_installsystemduser'));
+ ok(-e 'debian/foo/usr/lib/systemd/user/foo.service');
+ is_enabled('foo', 'foo.service');
+ is_enabled('foo', 'bar.service');
+ if ($compat > 13) {
+ is_started('foo', 'foo.service');
+ is_started('foo', 'bar.service');
+ } else {
+ isnt_started('foo', 'foo.service');
+ isnt_started('foo', 'bar.service');
+ }
+ ok(run_dh_tool('dh_clean'));
+
+ ok(run_dh_tool('dh_installsystemduser'));
+ ok(-e 'debian/foo/usr/lib/systemd/user/foo.service');
+ ok(! -e 'debian/foo/usr/lib/systemd/user/baz.service');
+ is_enabled('foo', 'foo.service');
+ isnt_enabled('foo', 'baz.service');
+ if ($compat > 13) {
+ is_started('foo', 'foo.service');
+ isnt_started('foo', 'baz.service');
+ } else {
+ isnt_started('foo', 'bar.service');
+ isnt_started('foo', 'baz.service');
+ }
+ ok(run_dh_tool('dh_clean'));
+
+ ok(run_dh_tool('dh_installsystemduser', '--name', 'baz'));
+ ok(! -e 'debian/foo/usr/lib/systemd/user/foo.service');
+ ok(-e 'debian/foo/usr/lib/systemd/user/baz.service');
+ isnt_enabled('foo', 'foo.service');
+ is_enabled('foo', 'baz.service');
+ if ($compat > 13) {
+ isnt_started('foo', 'foo.service');
+ is_started('foo', 'baz.service');
+ } else {
+ isnt_started('foo', 'foo.service');
+ isnt_started('foo', 'baz.service');
+ }
+ ok(run_dh_tool('dh_clean'));
+};