1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-27 01:55:22 +03:00

test: validate that fdstore pinning works

This commit is contained in:
Lennart Poettering 2023-04-04 11:41:55 +02:00
parent e8783d7620
commit 3540ce8587
6 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,8 @@
[Service]
Type=notify
NotifyAccess=all
FileDescriptorStoreMax=10
FileDescriptorStorePreserve=restart
ExecStart=/usr/lib/systemd/tests/testdata/testsuite-80.units/fdstore-pin.sh 0
StandardOutput=journal+console
StandardError=journal+console

View File

@ -0,0 +1,8 @@
[Service]
Type=notify
NotifyAccess=all
FileDescriptorStoreMax=10
FileDescriptorStorePreserve=yes
ExecStart=/usr/lib/systemd/tests/testdata/testsuite-80.units/fdstore-pin.sh 1
StandardOutput=journal+console
StandardError=journal+console

View File

@ -0,0 +1,47 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -eux
set -o pipefail
PINNED="$1"
COUNTER="/tmp/fdstore-invoked.$PINNED"
FILE="/tmp/fdstore-data.$PINNED"
# This script is called six times: thrice from a service unit where the fdstore
# is pinned, and thrice where it isn't. The second iteration of each series is
# a restart, the third a stop followed by a start
if [ -e "$COUNTER" ] ; then
read -r N < "$COUNTER"
else
N=0
fi
echo "Invocation #$N with PINNED=$PINNED."
if [ "$N" -eq 0 ] ; then
# First iteration
test "${LISTEN_FDS:-0}" -eq 0
test ! -e "$FILE"
echo waldi > "$FILE"
systemd-notify --fd=3 --fdname="fd-$N-$PINNED" 3< "$FILE"
elif [ "$N" -eq 1 ] || { [ "$N" -eq 2 ] && [ "$PINNED" -eq 1 ]; } ; then
# Second iteration, or iteration with pinning on
test "${LISTEN_FDS:-0}" -eq 1
# We reopen fd #3 here, so that the read offset is at zero each time (hence no <&3 here…)
read -r word < /proc/self/fd/3
test "$word" = "waldi"
else
test "${LISTEN_FDS:-0}" -eq 0
test -e "$FILE"
fi
if [ "$N" -ge 2 ] ; then
rm "$COUNTER" "$FILE"
else
echo $((N + 1)) > "$COUNTER"
fi
systemd-notify --ready --status="Ready"
exec sleep infinity

View File

@ -0,0 +1,3 @@
[Unit]
After=fdstore-pin.service fdstore-nopin.service
Wants=fdstore-pin.service fdstore-nopin.service

View File

@ -6,3 +6,5 @@ Description=TEST-80-NOTIFYACCESS
ExecStartPre=rm -f /failed /testok
ExecStart=/usr/lib/systemd/tests/testdata/units/%N.sh
Type=oneshot
StandardOutput=journal+console
StandardError=journal+console

View File

@ -48,6 +48,8 @@ assert_eq "$(systemctl show notify.service -p NotifyAccess --value)" "all"
rm /tmp/syncfifo1 /tmp/syncfifo2
# Now test basic fdstore behaviour
MYSCRIPT="/tmp/myscript$RANDOM.sh"
cat >> "$MYSCRIPT" <<'EOF'
#!/usr/bin/env bash
@ -74,6 +76,55 @@ systemd-analyze fdstore "$MYUNIT" --json=short | grep -P -q '\[{"fdname":"quux",
systemctl stop "$MYUNIT"
rm "$MYSCRIPT"
systemd-analyze log-level debug
# Test fdstore pinning (this will pull in fdstore-pin.service fdstore-nopin.service)
systemctl start fdstore-pin.target
assert_eq "$(systemctl show fdstore-pin.service -P FileDescriptorStorePreserve)" yes
assert_eq "$(systemctl show fdstore-nopin.service -P FileDescriptorStorePreserve)" restart
assert_eq "$(systemctl show fdstore-pin.service -P SubState)" running
assert_eq "$(systemctl show fdstore-nopin.service -P SubState)" running
assert_eq "$(systemctl show fdstore-pin.service -P NFileDescriptorStore)" 1
assert_eq "$(systemctl show fdstore-nopin.service -P NFileDescriptorStore)" 1
# The file descriptor store should survive service restarts
systemctl restart fdstore-pin.service fdstore-nopin.service
assert_eq "$(systemctl show fdstore-pin.service -P NFileDescriptorStore)" 1
assert_eq "$(systemctl show fdstore-nopin.service -P NFileDescriptorStore)" 1
assert_eq "$(systemctl show fdstore-pin.service -P SubState)" running
assert_eq "$(systemctl show fdstore-nopin.service -P SubState)" running
# It should not survive the service stop plus a later start (unless pinned)
systemctl stop fdstore-pin.service fdstore-nopin.service
assert_eq "$(systemctl show fdstore-pin.service -P NFileDescriptorStore)" 1
assert_eq "$(systemctl show fdstore-nopin.service -P NFileDescriptorStore)" 0
assert_eq "$(systemctl show fdstore-pin.service -P SubState)" dead-resources-pinned
assert_eq "$(systemctl show fdstore-nopin.service -P SubState)" dead
systemctl start fdstore-pin.service fdstore-nopin.service
assert_eq "$(systemctl show fdstore-pin.service -P NFileDescriptorStore)" 1
assert_eq "$(systemctl show fdstore-nopin.service -P NFileDescriptorStore)" 0
assert_eq "$(systemctl show fdstore-pin.service -P SubState)" running
assert_eq "$(systemctl show fdstore-nopin.service -P SubState)" running
systemctl stop fdstore-pin.service fdstore-nopin.service
assert_eq "$(systemctl show fdstore-pin.service -P NFileDescriptorStore)" 1
assert_eq "$(systemctl show fdstore-nopin.service -P NFileDescriptorStore)" 0
assert_eq "$(systemctl show fdstore-pin.service -P SubState)" dead-resources-pinned
assert_eq "$(systemctl show fdstore-nopin.service -P SubState)" dead
systemctl clean fdstore-pin.service --what=fdstore
assert_eq "$(systemctl show fdstore-pin.service -P NFileDescriptorStore)" 0
assert_eq "$(systemctl show fdstore-nopin.service -P NFileDescriptorStore)" 0
assert_eq "$(systemctl show fdstore-pin.service -P SubState)" dead
assert_eq "$(systemctl show fdstore-nopin.service -P SubState)" dead
touch /testok
rm /failed