#!/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 B '' && { echo "Expected failure" >&2; exit 1; } check_alias M '' && { echo "Expected failure" >&2; exit 1; } check_alias o '' && { echo "Expected failure" >&2; exit 1; } check_alias w '' && { echo "Expected failure" >&2; exit 1; } check_alias W '' && { echo "Expected failure" >&2; exit 1; } cat >"$root/etc/os-release" <"$root/etc/os-release" <&2; exit 1; } check_alias E '' && { echo "Expected failure" >&2; exit 1; } check_alias f '' && { echo "Expected failure" >&2; exit 1; } check_alias h '' && { echo "Expected failure" >&2; exit 1; } check_alias I '' && { echo "Expected failure" >&2; exit 1; } check_alias J '' && { echo "Expected failure" >&2; exit 1; } check_alias L '' && { echo "Expected failure" >&2; exit 1; } check_alias P '' && { echo "Expected failure" >&2; exit 1; } check_alias s '' && { echo "Expected failure" >&2; exit 1; } check_alias S '' && { echo "Expected failure" >&2; exit 1; } check_alias t '' && { echo "Expected failure" >&2; exit 1; } check_alias T '' && { echo "Expected failure" >&2; exit 1; } check_alias V '' && { echo "Expected failure" >&2; exit 1; } # FIXME: we use the calling user instead of root :( check_alias g root || : check_alias G 0 || : check_alias i "" check_alias j 'link6' check_alias l "$(uname -n | sed 's/\..*//')" test ! -e "$root/etc/machine-id" check_alias m '' && { echo "Expected failure" >&2; exit 1; } systemd-id128 new >"$root/etc/machine-id" check_alias m "$(cat "$root/etc/machine-id")" check_alias n 'some-some-link6@.socket' check_alias N 'some-some-link6@' check_alias p 'some-some-link6' # FIXME: we use the calling user instead of root :( check_alias u root || : check_alias U 0 || : check_alias v "$(uname -r)" check_alias % '%' && { echo "Expected failure because % is not legal in unit name" >&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] # TODO: repeat the tests above for presets : -------SYSTEMD_OS_RELEASE relative to root------------------ # check that os-release overwriting works as expected with root test -e "$root/etc/os-release" cat >"$root/etc/os-release2" <