TEST-15: allow helper functions to accept other unit types

clear_services() is renamed to clear_units() and now takes a full
unit name including the suffix as an argument.

_clear_service() is renamed to clear_unit() and changed likewise.
create_service() didn't have the same underscore prefix, and I don't think
it's useful or needed for a local function, so it is removed.

No functional change.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2022-10-16 12:42:35 +02:00
parent 1f83244641
commit 5731e1378a

View file

@ -3,30 +3,32 @@
set -eux
set -o pipefail
_clear_service () {
local SERVICE_NAME="${1:?}"
systemctl stop "$SERVICE_NAME.service" 2>/dev/null || :
rm -f /{etc,run,usr/lib}/systemd/system/"$SERVICE_NAME".service
rm -fr /{etc,run,usr/lib}/systemd/system/"$SERVICE_NAME".service.d
rm -fr /{etc,run,usr/lib}/systemd/system/"$SERVICE_NAME".service.{wants,requires}
if [[ $SERVICE_NAME == *@ ]]; then
systemctl stop "$SERVICE_NAME"*.service 2>/dev/null || :
rm -f /{etc,run,usr/lib}/systemd/system/"$SERVICE_NAME"*.service
rm -fr /{etc,run,usr/lib}/systemd/system/"$SERVICE_NAME"*.service.d
rm -fr /{etc,run,usr/lib}/systemd/system/"$SERVICE_NAME"*.service.{wants,requires}
clear_unit () {
local UNIT_NAME="${1:?}"
systemctl stop "$UNIT_NAME" 2>/dev/null || :
rm -f /{etc,run,usr/lib}/systemd/system/"$UNIT_NAME"
rm -fr /{etc,run,usr/lib}/systemd/system/"$UNIT_NAME".d
rm -fr /{etc,run,usr/lib}/systemd/system/"$UNIT_NAME".{wants,requires}
if [[ $UNIT_NAME == *@ ]]; then
local base="${UNIT_NAME%@*}"
local suffix="${UNIT_NAME##*.}"
systemctl stop "$base@"*."$suffix" 2>/dev/null || :
rm -f /{etc,run,usr/lib}/systemd/system/"$base@"*."$suffix"
rm -fr /{etc,run,usr/lib}/systemd/system/"$base@"*."$suffix".d
rm -fr /{etc,run,usr/lib}/systemd/system/"$base@"*."$suffix".{wants,requires}
fi
}
clear_services () {
clear_units () {
for u in "$@"; do
_clear_service "$u"
clear_unit "$u"
done
systemctl daemon-reload
}
create_service () {
local SERVICE_NAME="${1:?}"
clear_services "$SERVICE_NAME"
clear_units "${SERVICE_NAME}".service
cat >/etc/systemd/system/"$SERVICE_NAME".service <<EOF
[Unit]
@ -119,7 +121,7 @@ EOF
check_ok test15-b ExecCondition "/bin/echo test15-b"
rm -rf /usr/lib/systemd/system/service.d
clear_services test15-a test15-b test15-c test15-c1
clear_units test15-{a,b,c,c1}.service
}
test_linked_units () {
@ -146,7 +148,7 @@ test_linked_units () {
# Make sure it is completely ignored.
rm /test15-a@.scope
clear_services test15-a test15-b
clear_units test15-{a,b}.service
}
test_template_alias() {
@ -172,7 +174,7 @@ test_template_alias() {
check_ko test15-b@other Names test15-a@other.service
check_ok test15-b@other Names test15-b@other.service
clear_services test15-a@ test15-b@
clear_units test15-{a,b}@.service
}
test_hierarchical_dropins () {
@ -197,7 +199,7 @@ ExecCondition=/bin/echo $dropin
rm -rf /usr/lib/systemd/system/$dropin
done
clear_services a-b-c
clear_units a-b-c.service
}
test_template_dropins () {
@ -346,7 +348,7 @@ EOF
check_ok bar-alias@3 Requires yup-template-requires.device
check_ok bar-alias@3 Requires yup-3-requires.device
clear_services foo {bar,yup,bar-alias}@{,1,2,3}
clear_units foo.service {bar,yup,bar-alias}@{,1,2,3}.service
}
test_alias_dropins () {
@ -361,7 +363,7 @@ test_alias_dropins () {
systemctl --quiet is-active test15-b
systemctl stop test15-a test15-b
rm /etc/systemd/system/test15-b1.service
clear_services test15-a test15-b
clear_units test15-{a,b}.service
# Check that dependencies don't vary.
echo "*** test 2"
@ -378,7 +380,7 @@ test_alias_dropins () {
systemctl stop test15-a test15-x test15-y
rm /etc/systemd/system/test15-a1.service
clear_services test15-a test15-x test15-y
clear_units test15-{a,x,y}.service
}
test_masked_dropins () {
@ -499,7 +501,7 @@ EOF
ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b.service
check_ok test15-a Requires test15-b
clear_services test15-a test15-b
clear_units test15-{a,b}.service
}
test_invalid_dropins () {
@ -511,7 +513,7 @@ test_invalid_dropins () {
# Assertion failed on earlier versions, command exits unsuccessfully on later versions
systemctl cat a@.service || true
systemctl stop a
clear_services a
clear_units a.service
return 0
}
@ -531,7 +533,7 @@ EOF
ln -s /tmp/testsuite-15-test15-a-dropin-directory-regular /usr/lib/systemd/system/test15-a.service.d
check_ok test15-a Description hogehoge
clear_services test15-a
clear_units test15-a.service
}
test_basic_dropins