Skip to content

Commit

Permalink
TEST-15: allow helper functions to accept other unit types
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit 5731e1378ad6256e34f3da33ee993343f025c075)

Related: #2156620
  • Loading branch information
keszybz authored and dtardon committed Jul 13, 2023
1 parent 93daeac commit f133ef6
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions test/TEST-15-DROPIN/test-dropin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@
set -e
set -x

_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]
Expand Down Expand Up @@ -123,7 +125,7 @@ EOF
check_ok b ExecCondition "/bin/echo b"
rm -rf /usr/lib/systemd/system/service.d

clear_services a b c
clear_units {a,b,c}.service
}

test_hierarchical_dropins () {
Expand All @@ -148,7 +150,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 () {
Expand All @@ -159,7 +161,7 @@ test_template_dropins () {
ln -s /etc/systemd/system/bar@.service /etc/systemd/system/foo.service.wants/bar@1.service
check_ok foo Wants bar@1.service

clear_services foo bar@ yup@
clear_units {foo,bar@,yup@}.service
}

test_alias_dropins () {
Expand All @@ -174,7 +176,7 @@ test_alias_dropins () {
systemctl --quiet is-active b
systemctl stop a b
rm /etc/systemd/system/b1.service
clear_services a b
clear_units {a,b}.service

# A weird behavior: the dependencies for 'a' may vary. It can be
# changed by loading an alias...
Expand All @@ -198,7 +200,7 @@ test_alias_dropins () {
systemctl stop a x y
rm /etc/systemd/system/a1.service

clear_services a x y
clear_units {a,x,y}.service
}

test_masked_dropins () {
Expand Down Expand Up @@ -319,7 +321,7 @@ EOF
ln -sf /dev/null /etc/systemd/system/a.service.requires/b.service
check_ok a Requires b

clear_services a b
clear_units {a,b}.service
}

test_invalid_dropins () {
Expand All @@ -331,7 +333,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
}

Expand Down

0 comments on commit f133ef6

Please sign in to comment.