summaryrefslogtreecommitdiff
path: root/test/units/testsuite-68.sh
diff options
context:
space:
mode:
Diffstat (limited to 'test/units/testsuite-68.sh')
-rwxr-xr-xtest/units/testsuite-68.sh253
1 files changed, 129 insertions, 124 deletions
diff --git a/test/units/testsuite-68.sh b/test/units/testsuite-68.sh
index 15e2e0353f..3072a94575 100755
--- a/test/units/testsuite-68.sh
+++ b/test/units/testsuite-68.sh
@@ -24,7 +24,6 @@ wait_on_state_or_fail () {
}
systemd-analyze log-level debug
-systemd-analyze log-target console
# Trigger testservice-failure-exit-handler-68.service
cat >/run/systemd/system/testservice-failure-68.service <<EOF
@@ -33,17 +32,16 @@ Description=TEST-68-PROPAGATE-EXIT-STATUS with OnFailure= trigger
OnFailure=testservice-failure-exit-handler-68.service
[Service]
-ExecStart=/bin/bash -c "exit 1"
+ExecStart=sh -c "exit 1"
EOF
-# Another service which triggers testservice-failure-exit-handler-68.service
-cat >/run/systemd/system/testservice-failure-68-additional.service <<EOF
+cat >/run/systemd/system/testservice-failure-68-template.service <<EOF
[Unit]
-Description=TEST-68-PROPAGATE-EXIT-STATUS Additional service with OnFailure= trigger
-OnFailure=testservice-failure-exit-handler-68.service
+Description=TEST-68-PROPAGATE-EXIT-STATUS with OnFailure= trigger (template)
+OnFailure=testservice-failure-exit-handler-68-template@%n.service
[Service]
-ExecStart=/bin/bash -c "exit 1"
+ExecStart=sh -c "exit 1"
EOF
# Trigger testservice-success-exit-handler-68.service
@@ -53,72 +51,54 @@ Description=TEST-68-PROPAGATE-EXIT-STATUS with OnSuccess= trigger
OnSuccess=testservice-success-exit-handler-68.service
[Service]
-ExecStart=/bin/bash -c "exit 0"
+ExecStart=sh -c "exit 0"
EOF
-# Trigger testservice-success-exit-handler-68.service
-cat >/run/systemd/system/testservice-success-68-additional.service <<EOF
+cat >/run/systemd/system/testservice-success-68-template.service <<EOF
[Unit]
-Description=TEST-68-PROPAGATE-EXIT-STATUS Addition service with OnSuccess= trigger
-OnSuccess=testservice-success-exit-handler-68.service
+Description=TEST-68-PROPAGATE-EXIT-STATUS with OnSuccess= trigger (template)
+OnSuccess=testservice-success-exit-handler-68-template@%n.service
[Service]
-ExecStart=/bin/bash -c "exit 0"
+ExecStart=sh -c "exit 0"
EOF
# Script to check that when an OnSuccess= dependency fires, the correct
-# MONITOR* env variables are passed. This script handles the case where
-# multiple services triggered the unit that calls this script. In this
-# case we need to check the MONITOR_METADATA variable for >= 1 service
-# details since jobs may merge.
-cat >/tmp/check_on_success.sh <<EOF
-#!/usr/bin/env bash
+# MONITOR* env variables are passed.
+cat >/tmp/check_on_success.sh <<"EOF"
+#!/bin/sh
set -ex
+env | sort
+if [ "$MONITOR_SERVICE_RESULT" != "success" ]; then
+ echo "MONITOR_SERVICE_RESULT was '$MONITOR_SERVICE_RESULT', expected 'success'"
+ exit 1
+fi
+
+if [ "$MONITOR_EXIT_CODE" != "exited" ]; then
+ echo "MONITOR_EXIT_CODE was '$MONITOR_EXIT_CODE', expected 'exited'"
+ exit 1
+fi
+
+if [ "$MONITOR_EXIT_STATUS" != "0" ]; then
+ echo "MONITOR_EXIT_STATUS was '$MONITOR_EXIT_STATUS', expected '0'"
+ exit 1
+fi
+
+if [ -z "$MONITOR_INVOCATION_ID" ]; then
+ echo "MONITOR_INVOCATION_ID unset"
+ exit 1
+fi
+
+if [ "$MONITOR_UNIT" != "testservice-success-68.service" ] &&
+ [ "$MONITOR_UNIT" != "testservice-success-68-template.service" ] &&
+ [ "$MONITOR_UNIT" != "testservice-transient-success-68.service" ]; then
+
+ echo "MONITOR_UNIT was '$MONITOR_UNIT', expected 'testservice[-transient]-success-68[-template].service'"
+ exit 1
+fi
-echo "MONITOR_METADATA=\$MONITOR_METADATA"
-
-IFS=';' read -ra ALL_SERVICE_MD <<< "\$MONITOR_METADATA"
-for SERVICE_MD in "\${ALL_SERVICE_MD[@]}"; do
- IFS=',' read -ra METADATA <<< "\$SERVICE_MD"
- IFS='=' read -ra SERVICE_RESULT <<< "\${METADATA[0]}"
- SERVICE_RESULT=\${SERVICE_RESULT[1]}
- IFS='=' read -ra EXIT_CODE <<< "\${METADATA[1]}"
- EXIT_CODE=\${EXIT_CODE[1]}
- IFS='=' read -ra EXIT_STATUS <<< "\${METADATA[2]}"
- EXIT_STATUS=\${EXIT_STATUS[1]}
- IFS='=' read -ra INVOCATION_ID <<< "\${METADATA[3]}"
- INVOCATION_ID=\${INVOCATION_ID[1]}
- IFS='=' read -ra UNIT <<< "\${METADATA[4]}"
- UNIT=\${UNIT[1]}
-
- if [ "\$SERVICE_RESULT" != "success" ]; then
- echo 'SERVICE_RESULT was "\$SERVICE_RESULT", expected "success"';
- exit 1;
- fi
-
- if [ "\$EXIT_CODE" != "exited" ]; then
- echo 'EXIT_CODE was "\$EXIT_CODE", expected "exited"';
- exit 1;
- fi
-
- if [ "\$EXIT_STATUS" != "0" ]; then
- echo 'EXIT_STATUS was "\$EXIT_STATUS", expected "0"';
- exit 1;
- fi
-
- if [ -z "\$INVOCATION_ID" ]; then
- echo 'INVOCATION_ID unset';
- exit 1;
- fi
-
- if [[ "\$UNIT" != "testservice-success-68.service" && "\$UNIT" != "testservice-success-68-additional.service" && "\$UNIT" != "testservice-transient-success-68.service" ]]; then
- echo 'UNIT was "\$UNIT", expected "testservice-success-68{-additional,-transient}.service"';
- exit 1;
- fi
-done
-
-exit 0;
+exit 0
EOF
chmod +x /tmp/check_on_success.sh
@@ -132,59 +112,53 @@ ExecStartPre=/tmp/check_on_success.sh
ExecStart=/tmp/check_on_success.sh
EOF
+# Template version.
+cat >/run/systemd/system/testservice-success-exit-handler-68-template@.service <<EOF
+[Unit]
+Description=TEST-68-PROPAGATE-EXIT-STATUS handle service exiting in success (template)
+
+[Service]
+ExecStartPre=echo "triggered by %i"
+ExecStartPre=/tmp/check_on_success.sh
+ExecStart=/tmp/check_on_success.sh
+EOF
+
# Script to check that when an OnFailure= dependency fires, the correct
-# MONITOR* env variables are passed. This script handles the case where
-# multiple services triggered the unit that calls this script. In this
-# case we need to check the MONITOR_METADATA variable for >=1 service
-# details since jobs may merge.
-cat >/tmp/check_on_failure.sh <<EOF
-#!/usr/bin/env bash
+# MONITOR* env variables are passed.
+cat >/tmp/check_on_failure.sh <<"EOF"
+#!/bin/sh
set -ex
+env | sort
+if [ "$MONITOR_SERVICE_RESULT" != "exit-code" ]; then
+ echo "MONITOR_SERVICE_RESULT was '$MONITOR_SERVICE_RESULT', expected 'exit-code'"
+ exit 1
+fi
+
+if [ "$MONITOR_EXIT_CODE" != "exited" ]; then
+ echo "MONITOR_EXIT_CODE was '$MONITOR_EXIT_CODE', expected 'exited'"
+ exit 1
+fi
+
+if [ "$MONITOR_EXIT_STATUS" != "1" ]; then
+ echo "MONITOR_EXIT_STATUS was '$MONITOR_EXIT_STATUS', expected '1'"
+ exit 1
+fi
+
+if [ -z "$MONITOR_INVOCATION_ID" ]; then
+ echo "MONITOR_INVOCATION_ID unset"
+ exit 1
+fi
+
+if [ "$MONITOR_UNIT" != "testservice-failure-68.service" ] &&
+ [ "$MONITOR_UNIT" != "testservice-failure-68-template.service" ] &&
+ [ "$MONITOR_UNIT" != "testservice-transient-failure-68.service" ]; then
+
+ echo "MONITOR_UNIT was '$MONITOR_UNIT', expected 'testservice[-transient]-failure-68[-template].service'"
+ exit 1
+fi
-echo "MONITOR_METADATA=\$MONITOR_METADATA"
-
-IFS=';' read -ra ALL_SERVICE_MD <<< "\$MONITOR_METADATA"
-for SERVICE_MD in "\${ALL_SERVICE_MD[@]}"; do
- IFS=',' read -ra METADATA <<< "\$SERVICE_MD"
- IFS='=' read -ra SERVICE_RESULT <<< "\${METADATA[0]}"
- SERVICE_RESULT=\${SERVICE_RESULT[1]}
- IFS='=' read -ra EXIT_CODE <<< "\${METADATA[1]}"
- EXIT_CODE=\${EXIT_CODE[1]}
- IFS='=' read -ra EXIT_STATUS <<< "\${METADATA[2]}"
- EXIT_STATUS=\${EXIT_STATUS[1]}
- IFS='=' read -ra INVOCATION_ID <<< "\${METADATA[3]}"
- INVOCATION_ID=\${INVOCATION_ID[1]}
- IFS='=' read -ra UNIT <<< "\${METADATA[4]}"
- UNIT=\${UNIT[1]}
-
- if [ "\$SERVICE_RESULT" != "exit-code" ]; then
- echo 'SERVICE_RESULT was "\$SERVICE_RESULT", expected "exit-code"';
- exit 1;
- fi
-
- if [ "\$EXIT_CODE" != "exited" ]; then
- echo 'EXIT_CODE was "\$EXIT_CODE", expected "exited"';
- exit 1;
- fi
-
- if [ "\$EXIT_STATUS" != "1" ]; then
- echo 'EXIT_STATUS was "\$EXIT_STATUS", expected "1"';
- exit 1;
- fi
-
- if [ -z "\$INVOCATION_ID" ]; then
- echo 'INVOCATION_ID unset';
- exit 1;
- fi
-
- if [[ "\$UNIT" != "testservice-failure-68.service" && "\$UNIT" != "testservice-failure-68-additional.service" && "\$UNIT" != "testservice-transient-failure-68.service" ]]; then
- echo 'UNIT was "\$UNIT", expected "testservice-failure-68{-additional,-transient}.service"';
- exit 1;
- fi
-done
-
-exit 0;
+exit 0
EOF
chmod +x /tmp/check_on_failure.sh
@@ -195,34 +169,65 @@ cat >/run/systemd/system/testservice-failure-exit-handler-68.service <<EOF
Description=TEST-68-PROPAGATE-EXIT-STATUS handle service exiting in failure
[Service]
+# repeat the check to make sure that values are set correctly on repeated invocations
+Type=oneshot
+ExecStartPre=/tmp/check_on_failure.sh
+ExecStartPre=/tmp/check_on_failure.sh
+ExecStart=/tmp/check_on_failure.sh
+ExecStart=/tmp/check_on_failure.sh
+ExecStartPost=test -z '$MONITOR_SERVICE_RESULT'
+EOF
+
+# Template version.
+cat >/run/systemd/system/testservice-failure-exit-handler-68-template@.service <<EOF
+[Unit]
+Description=TEST-68-PROPAGATE-EXIT-STATUS handle service exiting in failure (template)
+
+[Service]
+Type=oneshot
+ExecStartPre=echo "triggered by %i"
ExecStartPre=/tmp/check_on_failure.sh
+ExecStartPre=/tmp/check_on_failure.sh
+ExecStart=/tmp/check_on_failure.sh
ExecStart=/tmp/check_on_failure.sh
+ExecStartPost=test -z '$MONITOR_SERVICE_RESULT'
EOF
systemctl daemon-reload
-# The running of the OnFailure= and OnSuccess= jobs for all of these services
-# may result in jobs being merged.
+: "-------I----------------------------------------------------"
systemctl start testservice-failure-68.service
wait_on_state_or_fail "testservice-failure-exit-handler-68.service" "inactive" "10"
-systemctl start testservice-failure-68-additional.service
-wait_on_state_or_fail "testservice-failure-exit-handler-68.service" "inactive" "10"
+
+: "-------II---------------------------------------------------"
systemctl start testservice-success-68.service
wait_on_state_or_fail "testservice-success-exit-handler-68.service" "inactive" "10"
-systemctl start testservice-success-68-additional.service
-wait_on_state_or_fail "testservice-success-exit-handler-68.service" "inactive" "10"
+
+# Let's get rid of the failed units so the tests below don't fail, and daemon-reload
+# to force garbace collection of the units.
+systemctl reset-failed
+systemctl daemon-reload
# Test some transient units since these exit very quickly.
-systemd-run --unit=testservice-transient-success-68 --property=OnSuccess=testservice-success-exit-handler-68.service /bin/bash -c "exit 0;"
+: "-------III--------------------------------------------------"
+systemd-run --unit=testservice-transient-success-68 --property=OnSuccess=testservice-success-exit-handler-68.service sh -c "exit 0"
wait_on_state_or_fail "testservice-success-exit-handler-68.service" "inactive" "10"
-systemd-run --unit=testservice-transient-failure-68 --property=OnFailure=testservice-failure-exit-handler-68.service /bin/bash -c "exit 1;"
-wait_on_state_or_fail "testservice-failure-exit-handler-68.service" "inactive" "10"
-# These yield a higher chance of resulting in jobs merging.
-systemctl start testservice-failure-68.service testservice-failure-68-additional.service --no-block
+: "-------IIII-------------------------------------------------"
+systemd-run --unit=testservice-transient-failure-68 --property=OnFailure=testservice-failure-exit-handler-68.service sh -c "exit 1"
wait_on_state_or_fail "testservice-failure-exit-handler-68.service" "inactive" "10"
-systemctl start testservice-success-68.service testservice-success-68-additional.service --no-block
-wait_on_state_or_fail "testservice-success-exit-handler-68.service" "inactive" "10"
+
+systemctl reset-failed
+systemctl daemon-reload
+
+# Test template handlers too
+: "-------V---------------------------------------------------"
+systemctl start testservice-success-68-template.service
+wait_on_state_or_fail "testservice-success-exit-handler-68-template@testservice-success-68-template.service.service" "inactive" "10"
+
+: "-------VI----------------------------------------------------"
+systemctl start testservice-failure-68-template.service
+wait_on_state_or_fail "testservice-failure-exit-handler-68-template@testservice-failure-68-template.service.service" "inactive" "10"
systemd-analyze log-level info
echo OK >/testok