2020-03-04 12:35:06 +03:00
#!/usr/bin/env bash
2021-10-17 19:13:06 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2021-04-09 20:39:41 +03:00
set -eux
set -o pipefail
2013-01-25 23:10:00 +04:00
2023-03-16 11:23:17 +03:00
# shellcheck source=test/units/util.sh
. " $( dirname " $0 " ) " /util.sh
2022-07-01 23:21:52 +03:00
# Simple test for that daemon-reexec works in container.
# See: https://github.com/systemd/systemd/pull/23883
systemctl daemon-reexec
2015-05-18 07:58:24 +03:00
# Test merging of a --job-mode=ignore-dependencies job into a previously
2013-01-25 23:10:00 +04:00
# installed job.
systemctl start --no-block hello-after-sleep.target
2016-01-17 06:21:52 +03:00
2021-04-08 01:09:55 +03:00
systemctl list-jobs >/root/list-jobs.txt
2023-09-06 15:51:07 +03:00
until grep 'sleep\.service.*running' /root/list-jobs.txt; do
2021-04-08 01:09:55 +03:00
systemctl list-jobs >/root/list-jobs.txt
2016-01-17 06:21:52 +03:00
done
2017-08-07 22:09:21 +03:00
grep 'hello\.service.*waiting' /root/list-jobs.txt
2013-01-25 23:10:00 +04:00
# This is supposed to finish quickly, not wait for sleep to finish.
START_SEC = $( date -u '+%s' )
2015-05-18 07:58:24 +03:00
systemctl start --job-mode= ignore-dependencies hello
2013-01-25 23:10:00 +04:00
END_SEC = $( date -u '+%s' )
2021-04-09 20:50:52 +03:00
ELAPSED = $(( END_SEC-START_SEC))
2013-01-25 23:10:00 +04:00
2021-04-08 02:27:33 +03:00
test " $ELAPSED " -lt 3
2013-01-25 23:10:00 +04:00
# sleep should still be running, hello not.
2021-04-08 01:09:55 +03:00
systemctl list-jobs >/root/list-jobs.txt
2017-08-07 22:09:21 +03:00
grep 'sleep\.service.*running' /root/list-jobs.txt
2013-01-25 23:10:00 +04:00
grep 'hello\.service' /root/list-jobs.txt && exit 1
2017-08-07 22:09:21 +03:00
systemctl stop sleep.service hello-after-sleep.target
2014-11-26 18:33:40 +03:00
2019-03-26 19:39:36 +03:00
# Some basic testing that --show-transaction does something useful
2023-04-05 16:50:42 +03:00
( ! systemctl is-active systemd-importd)
2019-03-26 19:39:36 +03:00
systemctl -T start systemd-importd
systemctl is-active systemd-importd
systemctl --show-transaction stop systemd-importd
2023-04-05 16:50:42 +03:00
( ! systemctl is-active systemd-importd)
2019-03-26 19:39:36 +03:00
2015-11-06 22:01:21 +03:00
# Test for a crash when enqueuing a JOB_NOP when other job already exists
2017-08-07 22:09:21 +03:00
systemctl start --no-block hello-after-sleep.target
2014-11-26 18:33:40 +03:00
# hello.service should still be waiting, so these try-restarts will collapse
# into NOPs.
2017-08-07 22:09:21 +03:00
systemctl try-restart --job-mode= fail hello.service
systemctl try-restart hello.service
systemctl stop hello.service sleep.service hello-after-sleep.target
2013-01-25 23:10:00 +04:00
# TODO: add more job queueing/merging tests here.
2022-08-23 01:03:42 +03:00
# Test that restart propagates to activating units
systemctl -T --no-block start always-activating.service
systemctl list-jobs | grep 'always-activating.service'
ACTIVATING_ID_PRE = $( systemctl show -P InvocationID always-activating.service)
systemctl -T start always-activating.socket # Wait for the socket to come up
systemctl -T restart always-activating.socket
ACTIVATING_ID_POST = $( systemctl show -P InvocationID always-activating.service)
[ " $ACTIVATING_ID_PRE " != " $ACTIVATING_ID_POST " ] || exit 1
2013-02-22 21:59:07 +04:00
# Test for irreversible jobs
2017-08-07 22:09:21 +03:00
systemctl start unstoppable.service
2013-02-22 21:59:07 +04:00
# This is expected to fail with 'job cancelled'
systemctl stop unstoppable.service && exit 1
# But this should succeed
2017-08-07 22:09:21 +03:00
systemctl stop --job-mode= replace-irreversibly unstoppable.service
2013-02-22 21:59:07 +04:00
# We're going to shutdown soon. Let's see if it succeeds when
# there's an active service that tries to be unstoppable.
# Shutdown of the container/VM will hang if not.
2017-08-07 22:09:21 +03:00
systemctl start unstoppable.service
2013-02-22 21:59:07 +04:00
2022-10-15 17:06:20 +03:00
# Test waiting for a started units to terminate again
2021-04-08 01:09:55 +03:00
cat <<EOF >/run/systemd/system/wait2.service
2016-09-05 14:14:36 +03:00
[ Unit]
Description = Wait for 2 seconds
[ Service]
2024-01-04 17:24:52 +03:00
ExecStart = sh -ec 'sleep 2'
2016-09-05 14:14:36 +03:00
EOF
2021-04-08 01:09:55 +03:00
cat <<EOF >/run/systemd/system/wait5fail.service
2016-09-05 14:14:36 +03:00
[ Unit]
Description = Wait for 5 seconds and fail
[ Service]
2024-01-04 17:24:52 +03:00
ExecStart = sh -ec 'sleep 5; false'
2016-09-05 14:14:36 +03:00
EOF
# wait2 succeeds
START_SEC = $( date -u '+%s' )
2017-08-07 22:09:21 +03:00
systemctl start --wait wait2.service
2016-09-05 14:14:36 +03:00
END_SEC = $( date -u '+%s' )
2021-04-09 20:50:52 +03:00
ELAPSED = $(( END_SEC-START_SEC))
2016-11-07 21:51:20 +03:00
[ [ " $ELAPSED " -ge 2 ] ] && [ [ " $ELAPSED " -le 4 ] ] || exit 1
2016-09-05 14:14:36 +03:00
# wait5fail fails, so systemctl should fail
START_SEC = $( date -u '+%s' )
2023-04-05 16:50:42 +03:00
( ! systemctl start --wait wait2.service wait5fail.service)
2016-09-05 14:14:36 +03:00
END_SEC = $( date -u '+%s' )
2021-04-09 20:50:52 +03:00
ELAPSED = $(( END_SEC-START_SEC))
2016-09-05 14:14:36 +03:00
[ [ " $ELAPSED " -ge 5 ] ] && [ [ " $ELAPSED " -le 7 ] ] || exit 1
2019-06-12 10:45:26 +03:00
# Test time-limited scopes
START_SEC = $( date -u '+%s' )
set +e
systemd-run --scope --property= RuntimeMaxSec = 3s sleep 10
RESULT = $?
END_SEC = $( date -u '+%s' )
2021-04-09 20:50:52 +03:00
ELAPSED = $(( END_SEC-START_SEC))
2019-06-12 10:45:26 +03:00
[ [ " $ELAPSED " -ge 3 ] ] && [ [ " $ELAPSED " -le 5 ] ] || exit 1
[ [ " $RESULT " -ne 0 ] ] || exit 1
2023-06-23 15:06:42 +03:00
# Test transactions with cycles
# Provides coverage for issues like https://github.com/systemd/systemd/issues/26872
for i in { 0..19} ; do
cat >" /run/systemd/system/transaction-cycle $i .service " <<EOF
[ Unit]
After = transaction-cycle$(( ( i + 1 ) % 20 )) .service
Requires = transaction-cycle$(( ( i + 1 ) % 20 )) .service
[ Service]
ExecStart = true
EOF
done
systemctl daemon-reload
for i in { 0..19} ; do
systemctl start " transaction-cycle $i .service "
done
2023-03-16 11:23:17 +03:00
# Test PropagatesStopTo= when restart (issue #26839)
systemctl start propagatestopto-and-pullin.target
systemctl --quiet is-active propagatestopto-and-pullin.target
systemctl restart propagatestopto-and-pullin.target
systemctl --quiet is-active propagatestopto-and-pullin.target
systemctl --quiet is-active sleep-infinity-simple.service
systemctl start propagatestopto-only.target
systemctl --quiet is-active propagatestopto-only.target
systemctl --quiet is-active sleep-infinity-simple.service
systemctl restart propagatestopto-only.target
assert_rc 3 systemctl --quiet is-active sleep-infinity-simple.service
2023-07-05 21:40:01 +03:00
systemctl start propagatesstopto-indirect.target propagatestopto-and-pullin.target
systemctl --quiet is-active propagatestopto-indirect.target
systemctl --quiet is-active propagatestopto-and-pullin.target
systemctl restart propagatestopto-indirect.target
assert_rc 3 systemctl --quiet is-active propagatestopto-and-pullin.target
assert_rc 3 systemctl --quiet is-active sleep-infinity-simple.service
2023-07-06 15:33:52 +03:00
# Test restart mode direct
systemctl start succeeds-on-restart-restartdirect.target
assert_rc 0 systemctl --quiet is-active succeeds-on-restart-restartdirect.target
systemctl start fails-on-restart-restartdirect.target || :
assert_rc 3 systemctl --quiet is-active fails-on-restart-restartdirect.target
systemctl start succeeds-on-restart.target || :
assert_rc 3 systemctl --quiet is-active succeeds-on-restart.target
systemctl start fails-on-restart.target || :
assert_rc 3 systemctl --quiet is-active fails-on-restart.target
2013-01-25 23:10:00 +04:00
touch /testok