From e7bd636e75a5435b80a1df478e9e637dd2f7b851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 7 Mar 2022 12:15:42 +0100 Subject: [PATCH] test: add test for systemctl link & enable This test has overlap with test-install-root, but it tests things at a different level, so I think it's useful to add. It immediately shows various bugs which will be fixed in later patches. (cherry picked from commit 50c5f5a3d907f819fa139e1853f660ad4fd82c55) Related: #2082131 --- meson.build | 9 +- test/meson.build | 1 + test/test-systemctl-enable.sh | 523 ++++++++++++++++++++++++++++++++++ 3 files changed, 532 insertions(+), 1 deletion(-) create mode 100644 test/test-systemctl-enable.sh diff --git a/meson.build b/meson.build index fdf02b8110..005af872cf 100644 --- a/meson.build +++ b/meson.build @@ -2371,7 +2371,7 @@ public_programs += executable( install_rpath : rootlibexecdir, install : true) -public_programs += executable( +exe = executable( 'systemctl', systemctl_sources, include_directories : includes, @@ -2385,6 +2385,13 @@ public_programs += executable( install_rpath : rootlibexecdir, install : true, install_dir : rootbindir) +public_programs += exe +if want_tests != 'false' + test('test-systemctl-enable', + test_systemctl_enable_sh, + # https://github.com/mesonbuild/meson/issues/2681 + args : exe.full_path()) +endif if conf.get('ENABLE_PORTABLED') == 1 dbus_programs += executable( diff --git a/test/meson.build b/test/meson.build index c5d8d6917b..c69d8a0204 100644 --- a/test/meson.build +++ b/test/meson.build @@ -88,6 +88,7 @@ endif test_fstab_generator_sh = find_program('test-fstab-generator.sh') test_network_generator_conversion_sh = find_program('test-network-generator-conversion.sh') +test_systemctl_enable_sh = find_program('test-systemctl-enable.sh') test_systemd_tmpfiles_py = find_program('test-systemd-tmpfiles.py') hwdb_test_sh = find_program('hwdb-test.sh') diff --git a/test/test-systemctl-enable.sh b/test/test-systemctl-enable.sh new file mode 100644 index 0000000000..30ba6532e7 --- /dev/null +++ b/test/test-systemctl-enable.sh @@ -0,0 +1,523 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: LGPL-2.1-or-later +set -ex + +# Silence warning from running_in_chroot_or_offline() +export SYSTEMD_IGNORE_CHROOT=1 + +systemctl=${1:-systemctl} + +unset root +cleanup() { + [ -n "$root" ] && rm -rf "$root" +} +trap cleanup exit +root=$(mktemp -d --tmpdir systemctl-test.XXXXXX) + +islink() { + test -h "$1" || return 1 + test "$(readlink "$1")" = "$2" || return 2 +} + +: ------enablement nonexistent-------------------------------- +"$systemctl" --root="$root" enable test1.service && { echo "Expected failure" >&2; exit 1; } + +: ------basic enablement-------------------------------------- +mkdir -p "$root/etc/systemd/system" +cat >"$root/etc/systemd/system/test1.service" <>"$root/etc/systemd/system/test1.service" <"$root/etc/systemd/system/test2.socket" <"$root/etc/systemd/system/test2.service" <&2; exit 1; } +test ! -e "$root/etc/systemd/system/link1.path" + +cat >"$root/link1.path" <&2; exit 1; } +islink "$root/etc/systemd/system/link1.path" "/link1.path" + +: -------link bad suffix-------------------------------------- +cp "$root/link1.path" "$root/subdir/link1.suffix" +"$systemctl" --root="$root" link '/subdir/link1.suffix' && { echo "Expected failure" >&2; exit 1; } +test ! -e "$root/etc/systemd/system/link1.suffix" + +: -------unlink by unit name---------------------------------- +"$systemctl" --root="$root" disable 'link1.path' +test ! -e "$root/etc/systemd/system/link1.path" + +: -------unlink by path--------------------------------------- +"$systemctl" --root="$root" link '/link1.path' +test -h "$root/etc/systemd/system/link1.path" +"$systemctl" --root="$root" disable '/link1.path' +test ! -e "$root/etc/systemd/system/link1.path" + +: -------unlink by wrong path--------------------------------- +"$systemctl" --root="$root" link '/link1.path' +test -h "$root/etc/systemd/system/link1.path" +"$systemctl" --root="$root" disable '/subdir/link1.path' # we only care about the name +test ! -e "$root/etc/systemd/system/link1.path" + + +: -------link and enable-------------------------------------- +"$systemctl" --root="$root" enable '/link1.path' +islink "$root/etc/systemd/system/link1.path" "/link1.path" +islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path" + +: -------enable already linked same path---------------------- +"$systemctl" --root="$root" enable '/link1.path' +islink "$root/etc/systemd/system/link1.path" "/link1.path" +islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path" + +: -------enable already linked different path----------------- +# FIXME +# "$systemctl" --root="$root" enable '/subdir/link1.path' && { echo "Expected failure" >&2; exit 1; } +# test -h "$root/etc/systemd/system/link1.path" +# readlink "$root/etc/systemd/system/link1.path" +# test -h "$root/etc/systemd/system/paths.target.wants/link1.path" +# readlink "$root/etc/systemd/system/paths.target.wants/link1.path" + +: -------enable bad suffix------------------------------------ +cp "$root/link1.path" "$root/subdir/link1.suffix" +"$systemctl" --root="$root" enable '/subdir/link1.suffix' && { echo "Expected failure" >&2; exit 1; } +test ! -e "$root/etc/systemd/system/link1.suffix" +test ! -e "$root/etc/systemd/system/paths.target.wants/link1.suffix" + +: -------disable by unit name--------------------------------- +"$systemctl" --root="$root" disable 'link1.path' +test ! -e "$root/etc/systemd/system/link1.path" +test ! -e "$root/etc/systemd/system/paths.target.wants/link1.path" + +: -------disable by path-------------------------------------- +"$systemctl" --root="$root" enable '/link1.path' +test -h "$root/etc/systemd/system/link1.path" +test -h "$root/etc/systemd/system/paths.target.wants/link1.path" +"$systemctl" --root="$root" disable '/link1.path' +test ! -e "$root/etc/systemd/system/link1.path" +test ! -e "$root/etc/systemd/system/paths.target.wants/link1.path" + + +: -------link then enable------------------------------------- +"$systemctl" --root="$root" link '/link1.path' +islink "$root/etc/systemd/system/link1.path" "/link1.path" +test ! -h "$root/etc/systemd/system/paths.target.wants/link1.path" + +"$systemctl" --root="$root" enable 'link1.path' +islink "$root/etc/systemd/system/link1.path" "/link1.path" +islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path" + +# FIXME +# "$systemctl" --root="$root" reenable 'link1.path' +# islink "$root/etc/systemd/system/link1.path" "/link1.path" +# islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path" + + +: -------manual link------------------------------------------ +cat >"$root/link3.suffix" <&2; exit 1; } +"$systemctl" --root="$root" enable '/etc/systemd/system/masked.service' && { echo "Expected failure" >&2; exit 1; } + +: -------enable on masked alias------------------------------- +test -h "$root/etc/systemd/system/masked.service" +ln -s "masked.service" "$root/etc/systemd/system/masked-alias.service" +"$systemctl" --root="$root" enable 'masked-alias.service' && { echo "Expected failure" >&2; exit 1; } +"$systemctl" --root="$root" enable '/etc/systemd/system/masked-alias.service' && { echo "Expected failure" >&2; exit 1; } + +: -------issue 22000: link in subdirectory-------------------- +mkdir -p "$root/etc/systemd/system/myown.d" +cat >"$root/etc/systemd/system/link5-also.service" <"$root/etc/systemd/system/myown.d/link5.service" <&2; exit 1; } +test ! -h "$root/etc/systemd/system/services.target.wants/link5.service" +test ! -h "$root/etc/systemd/system/services.target.wants/link5-also.service" + +"$systemctl" --root="$root" enable 'link5-also.service' +test ! -h "$root/etc/systemd/system/services.target.wants/link5.service" +islink "$root/etc/systemd/system/services.target.wants/link5-also.service" "/etc/systemd/system/link5-also.service" + +: -------template enablement---------------------------------- +cat >"$root/etc/systemd/system/templ1@.service" <&2; exit 1; } +test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service" + +"$systemctl" --root="$root" enable 'templ1@one.service' +test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service" +islink "$root/etc/systemd/system/services.target.wants/templ1@one.service" "/etc/systemd/system/templ1@.service" + +"$systemctl" --root="$root" enable 'templ1@two.service' +test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service" +islink "$root/etc/systemd/system/services.target.wants/templ1@one.service" "/etc/systemd/system/templ1@.service" +islink "$root/etc/systemd/system/services.target.wants/templ1@two.service" "/etc/systemd/system/templ1@.service" + +"$systemctl" --root="$root" disable 'templ1@one.service' +test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service" +test ! -h "$root/etc/systemd/system/services.target.wants/templ1@one.service" +islink "$root/etc/systemd/system/services.target.wants/templ1@two.service" "/etc/systemd/system/templ1@.service" + +"$systemctl" --root="$root" disable 'templ1@two.service' +test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service" +test ! -h "$root/etc/systemd/system/services.target.wants/templ1@one.service" +test ! -h "$root/etc/systemd/system/services.target.wants/templ1@two.service" + +: -------template enablement w/ default instance-------------- +cat >>"$root/etc/systemd/system/templ1@.service" <"$root/etc/systemd/system/link4.service" <"$root/etc/systemd/system/link5.service" <"$root/etc/systemd/system/link5@.path" <"$root/etc/systemd/system/multilink.mount" <"$root/etc/systemd/system/some-some-link6@.socket" <&2; exit 1; } + +check_alias z 'z' && { echo "Expected failure because %z is not known" >&2; exit 1; } + +# FIXME: if there's an invalid Alias=, we shouldn't preach about empty [Install] + +exit 0 # yes, this is needed because the last test above fails + +# TODO: repeat the tests above for presets