diff options
author | Michael Stapelberg <stapelberg@debian.org> | 2013-07-23 21:31:18 +0200 |
---|---|---|
committer | Michael Stapelberg <michael@stapelberg.de> | 2013-07-23 21:31:18 +0200 |
commit | 195af04422f9494d9dab83129427dc591a015efa (patch) | |
tree | e875cd6b43aa40295dbe2ac7e47240a55afd1dad | |
parent | 3e80b7d1415885208800e1788d26f6a2e4fa47db (diff) | |
download | init-system-helpers-195af04422f9494d9dab83129427dc591a015efa.tar.gz |
add test case for deb-systemd-helper
-rw-r--r-- | t/001-deb-systemd-helper.t | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/t/001-deb-systemd-helper.t b/t/001-deb-systemd-helper.t new file mode 100644 index 0000000..ed661c8 --- /dev/null +++ b/t/001-deb-systemd-helper.t @@ -0,0 +1,137 @@ +#!perl +# vim:ts=4:sw=4:et + +use strict; +use warnings; +use Test::More; +use File::Temp qw(tempfile tempdir); # in core since perl 5.6.1 +use File::Path qw(make_path); # in core since Perl 5.001 +use File::Basename; # in core since Perl 5 +use FindBin; # in core since Perl 5.00307 +use Linux::Clone; # neither in core nor in Debian :-/ + +# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +# ┃ SETUP: in a new mount namespace, bindmount tmpdirs on /etc/systemd and ┃ +# ┃ /var/lib/systemd to start with clean directories yet use the actual ┃ +# ┃ locations and code paths. ┃ +# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ + +my $dsh = "$FindBin::Bin/../script/deb-systemd-helper"; + +sub _unit_enabled { + my ($unit_file, $cb, $verb) = @_; + + my $retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh is-enabled $unit_file"); + isnt($retval, -1, 'deb-systemd-helper could be executed'); + ok(!($retval & 127), 'deb-systemd-helper did not exit due to a signal'); + $cb->($retval >> 8, 0, "random unit file $verb enabled"); +} + +sub is_enabled { _unit_enabled($_[0], \&is, 'is') } +sub isnt_enabled { _unit_enabled($_[0], \&isnt, 'isnt') } + +my $retval = Linux::Clone::unshare Linux::Clone::NEWNS; +BAIL_OUT("Cannot unshare(NEWNS): $!") if $retval != 0; + +sub bind_mount_tmp { + my ($dir) = @_; + my $tmp = tempdir(CLEANUP => 1); + system("mount -n --bind $tmp $dir") == 0 + or BAIL_OUT("bind-mounting $tmp to $dir failed: $!"); + 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'); + +# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +# ┃ Verify “is-enabled” is not true for a random, non-existing unit file. ┃ +# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ + +my ($fh, $random_unit) = tempfile('unitXXXXX', + SUFFIX => '.service', + TMPDIR => 1, + UNLINK => 1); +close($fh); +$random_unit = basename($random_unit); + +isnt_enabled($random_unit); + +# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +# ┃ Verify “is-enabled” is not true for a random, existing unit file. ┃ +# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ + +my $servicefile_path = "/lib/systemd/system/$random_unit"; +make_path('/lib/systemd/system'); +open($fh, '>', $servicefile_path); +print $fh <<'EOT'; +[Unit] +Description=test unit + +[Service] +ExecStart=/bin/sleep 1 + +[Install] +WantedBy=multi-user.target +EOT +close($fh); + +isnt_enabled($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'); + +$retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh enable $random_unit"); +my $symlink_path = "/etc/systemd/system/multi-user.target.wants/$random_unit"; +ok(-l $symlink_path, "$random_unit was enabled"); +is(readlink($symlink_path), $servicefile_path, + "symlink points to $servicefile_path"); + +# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +# ┃ Verify “is-enabled” now returns true. ┃ +# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ + +is_enabled($random_unit); + +# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +# ┃ Verify deleting the symlinks and running “enable” again does not ┃ +# ┃ re-create the symlinks. ┃ +# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ + +unlink($symlink_path); +ok(! -l $symlink_path, 'symlink deleted'); +isnt_enabled($random_unit); + +$retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh enable $random_unit"); + +isnt_enabled($random_unit); + +# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +# ┃ Verify “disable” when purging deletes the statefile. ┃ +# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ + +my $statefile = "/var/lib/systemd/deb-systemd-helper-enabled/$random_unit.dsh-also"; + +ok(-f $statefile, 'state file exists'); + +$retval = system("DPKG_MAINTSCRIPT_PACKAGE=test _DEB_SYSTEMD_HELPER_PURGE=1 $dsh disable $random_unit"); + +ok(! -f $statefile, 'state file does not exist anymore after purging'); + +# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +# ┃ Verify “enable” after purging does re-create the symlinks. ┃ +# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ + +ok(! -l $symlink_path, 'symlink does not exist yet'); +isnt_enabled($random_unit); + +$retval = system("DPKG_MAINTSCRIPT_PACKAGE=test $dsh enable $random_unit"); + +is_enabled($random_unit); + +done_testing; |