| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | set -ex |
| 4 | set -o pipefail |
| 5 | |
| 6 | # Wait for a service to enter a state within a timeout period, if it doesn't |
| 7 | # enter the desired state within the timeout period then this function will |
| 8 | # exit the test case with a non zero exit code. |
| 9 | wait_on_state_or_failscip-shell shell systemd 261~rc4-1 wait_on_state_or_fail().() { |
| 10 | service=$1 |
| 11 | expected_state=$2 |
| 12 | timeout=$3 |
| 13 | |
| 14 | state=$(systemctl show "$service" --property=ActiveState --value) |
| 15 | while [scip-shell system . . `[`. "$state" != "$expected_state" ]; do |
| 16 | if [scip-shell system . . `[`. "$timeout" = "0" ]; then |
| 17 | exit 1 |
| 18 | fi |
| 19 | timeout=$((timeout - 1)) |
| 20 | sleepscip-shell system . . sleep. 1 |
| 21 | state=$(systemctl show "$service" --property=ActiveState --value) |
| 22 | done |
| 23 | } |
| 24 | |
| 25 | catscip-shell system . . cat. >/run/systemd/system/testservice-failure-68.servicescip-shell filesystem . . `/run/systemd/system/testservice-failure-68.service`. <<EOF |
| 26 | [Unit] |
| 27 | OnFailure=testservice-failure-exit-handler-68.service |
| 28 | |
| 29 | [Service] |
| 30 | ExecStart=false |
| 31 | EOF |
| 32 | |
| 33 | catscip-shell system . . cat. >/run/systemd/system/testservice-failure-68-template.servicescip-shell filesystem . . `/run/systemd/system/testservice-failure-68-template.service`. <<EOF |
| 34 | [Unit] |
| 35 | OnFailure=testservice-failure-exit-handler-68-template@%n.service |
| 36 | |
| 37 | [Service] |
| 38 | ExecStart=false |
| 39 | EOF |
| 40 | |
| 41 | catscip-shell system . . cat. >/run/systemd/system/testservice-success-68.servicescip-shell filesystem . . `/run/systemd/system/testservice-success-68.service`. <<EOF |
| 42 | [Unit] |
| 43 | OnSuccess=testservice-success-exit-handler-68.service |
| 44 | |
| 45 | [Service] |
| 46 | ExecStart=true |
| 47 | EOF |
| 48 | |
| 49 | catscip-shell system . . cat. >/run/systemd/system/testservice-success-68-template.servicescip-shell filesystem . . `/run/systemd/system/testservice-success-68-template.service`. <<EOF |
| 50 | [Unit] |
| 51 | OnSuccess=testservice-success-exit-handler-68-template@%n.service |
| 52 | |
| 53 | [Service] |
| 54 | ExecStart=true |
| 55 | EOF |
| 56 | |
| 57 | catscip-shell system . . cat. >/run/systemd/system/testservice-success-and-failure-68.servicescip-shell filesystem . . `/run/systemd/system/testservice-success-and-failure-68.service`. <<EOF |
| 58 | [Unit] |
| 59 | OnSuccess=testservice-failure-exit-handler-68.service |
| 60 | OnFailure=testservice-failure-exit-handler-68.service |
| 61 | |
| 62 | [Service] |
| 63 | ExecStart=true |
| 64 | EOF |
| 65 | |
| 66 | # Script to check that when an OnSuccess= dependency fires, the correct |
| 67 | # MONITOR* env variables are passed. |
| 68 | catscip-shell system . . cat. >/tmp/check_on_success.shscip-shell filesystem . . `/tmp/check_on_success.sh`. <<"EOF" |
| 69 | #!/usr/bin/env bash |
| 70 | |
| 71 | set -ex |
| 72 | env | sort |
| 73 | if [ "$MONITOR_SERVICE_RESULT" != "success" ]; then |
| 74 | echo "MONITOR_SERVICE_RESULT was '$MONITOR_SERVICE_RESULT', expected 'success'" |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | if [ "$MONITOR_EXIT_CODE" != "exited" ]; then |
| 79 | echo "MONITOR_EXIT_CODE was '$MONITOR_EXIT_CODE', expected 'exited'" |
| 80 | exit 1 |
| 81 | fi |
| 82 | |
| 83 | if [ "$MONITOR_EXIT_STATUS" != "0" ]; then |
| 84 | echo "MONITOR_EXIT_STATUS was '$MONITOR_EXIT_STATUS', expected '0'" |
| 85 | exit 1 |
| 86 | fi |
| 87 | |
| 88 | if [ -z "$MONITOR_INVOCATION_ID" ]; then |
| 89 | echo "MONITOR_INVOCATION_ID unset" |
| 90 | exit 1 |
| 91 | fi |
| 92 | |
| 93 | if [ "$MONITOR_UNIT" != "testservice-success-68.service" ] && |
| 94 | [ "$MONITOR_UNIT" != "testservice-success-68-template.service" ] && |
| 95 | [ "$MONITOR_UNIT" != "testservice-transient-success-68.service" ] && |
| 96 | [ "$MONITOR_UNIT" != "testservice-success-and-failure-68.service" ]; then |
| 97 | |
| 98 | echo "MONITOR_UNIT was '$MONITOR_UNIT', expected 'testservice[-transient]-success[-and-failure]-68[-template].service'" |
| 99 | exit 1 |
| 100 | fi |
| 101 | |
| 102 | exit 0 |
| 103 | EOF |
| 104 | chmodscip-shell system . . chmod. +x /tmp/check_on_success.shscip-shell filesystem . . `/tmp/check_on_success.sh`. |
| 105 | |
| 106 | catscip-shell system . . cat. >/run/systemd/system/testservice-success-exit-handler-68.servicescip-shell filesystem . . `/run/systemd/system/testservice-success-exit-handler-68.service`. <<EOF |
| 107 | [Service] |
| 108 | ExecStartPre=/tmp/check_on_success.sh |
| 109 | ExecStart=/tmp/check_on_success.sh |
| 110 | EOF |
| 111 | |
| 112 | cscip-shell system . . cp.pscip-shell system . . cp. scip-shell filesystem . . `/run/systemd/system/testservice-transient-success-exit-handler-68.service`./run/systemd/system/testservice-success-exit-handler-68.servicescip-shell filesystem . . `/run/systemd/system/testservice-success-exit-handler-68.service`. \scip-shell filesystem . . `/run/systemd/system/testservice-transient-success-exit-handler-68.service`. |
| 113 | /run/systemd/system/testservice-transient-success-exit-handler-68.service |
| 114 | |
| 115 | # Template version. |
| 116 | catscip-shell system . . cat. >/run/systemd/system/[email protected]scip-shell filesystem . . `/run/systemd/system/[email protected]`. <<EOF |
| 117 | [Service] |
| 118 | ExecStartPre=echo "triggered by %i" |
| 119 | ExecStartPre=/tmp/check_on_success.sh |
| 120 | ExecStart=/tmp/check_on_success.sh |
| 121 | EOF |
| 122 | |
| 123 | # Script to check that when an OnFailure= dependency fires, the correct |
| 124 | # MONITOR* env variables are passed. |
| 125 | catscip-shell system . . cat. >/tmp/check_on_failure.shscip-shell filesystem . . `/tmp/check_on_failure.sh`. <<"EOF" |
| 126 | #!/usr/bin/env bash |
| 127 | |
| 128 | set -ex |
| 129 | env | sort |
| 130 | if [ "$MONITOR_SERVICE_RESULT" != "exit-code" ]; then |
| 131 | echo "MONITOR_SERVICE_RESULT was '$MONITOR_SERVICE_RESULT', expected 'exit-code'" |
| 132 | exit 1 |
| 133 | fi |
| 134 | |
| 135 | if [ "$MONITOR_EXIT_CODE" != "exited" ]; then |
| 136 | echo "MONITOR_EXIT_CODE was '$MONITOR_EXIT_CODE', expected 'exited'" |
| 137 | exit 1 |
| 138 | fi |
| 139 | |
| 140 | if [ "$MONITOR_EXIT_STATUS" != "1" ]; then |
| 141 | echo "MONITOR_EXIT_STATUS was '$MONITOR_EXIT_STATUS', expected '1'" |
| 142 | exit 1 |
| 143 | fi |
| 144 | |
| 145 | if [ -z "$MONITOR_INVOCATION_ID" ]; then |
| 146 | echo "MONITOR_INVOCATION_ID unset" |
| 147 | exit 1 |
| 148 | fi |
| 149 | |
| 150 | if [ "$MONITOR_UNIT" != "testservice-failure-68.service" ] && |
| 151 | [ "$MONITOR_UNIT" != "testservice-failure-68-template.service" ] && |
| 152 | [ "$MONITOR_UNIT" != "testservice-transient-failure-68.service" ]; then |
| 153 | |
| 154 | echo "MONITOR_UNIT was '$MONITOR_UNIT', expected 'testservice[-transient]-failure-68[-template].service'" |
| 155 | exit 1 |
| 156 | fi |
| 157 | |
| 158 | exit 0 |
| 159 | EOF |
| 160 | chmodscip-shell system . . chmod. +x /tmp/check_on_failure.shscip-shell filesystem . . `/tmp/check_on_failure.sh`. |
| 161 | |
| 162 | |
| 163 | catscip-shell system . . cat. >/run/systemd/system/testservice-failure-exit-handler-68.servicescip-shell filesystem . . `/run/systemd/system/testservice-failure-exit-handler-68.service`. <<EOF |
| 164 | [Service] |
| 165 | # repeat the check to make sure that values are set correctly on repeated invocations |
| 166 | Type=oneshot |
| 167 | ExecStartPre=/tmp/check_on_failure.sh |
| 168 | ExecStartPre=/tmp/check_on_failure.sh |
| 169 | ExecStart=/tmp/check_on_failure.sh |
| 170 | ExecStart=/tmp/check_on_failure.sh |
| 171 | ExecStartPost=test -z '$MONITOR_SERVICE_RESULT' |
| 172 | EOF |
| 173 | |
| 174 | cscip-shell system . . cp.pscip-shell system . . cp. scip-shell filesystem . . `/run/systemd/system/testservice-transient-failure-exit-handler-68.service`./run/systemd/system/testservice-failure-exit-handler-68.servicescip-shell filesystem . . `/run/systemd/system/testservice-failure-exit-handler-68.service`. \scip-shell filesystem . . `/run/systemd/system/testservice-transient-failure-exit-handler-68.service`. |
| 175 | /run/systemd/system/testservice-transient-failure-exit-handler-68.service |
| 176 | |
| 177 | # Template version. |
| 178 | catscip-shell system . . cat. >/run/systemd/system/[email protected]scip-shell filesystem . . `/run/systemd/system/[email protected]`. <<EOF |
| 179 | [Service] |
| 180 | Type=oneshot |
| 181 | ExecStartPre=echo "triggered by %i" |
| 182 | ExecStartPre=/tmp/check_on_failure.sh |
| 183 | ExecStartPre=/tmp/check_on_failure.sh |
| 184 | ExecStart=/tmp/check_on_failure.sh |
| 185 | ExecStart=/tmp/check_on_failure.sh |
| 186 | ExecStartPost=test -z '$MONITOR_SERVICE_RESULT' |
| 187 | EOF |
| 188 | |
| 189 | systemctl daemon-reload |
| 190 | |
| 191 | : "-------I----------------------------------------------------" |
| 192 | systemctl start testservice-failure-68.service |
| 193 | wait_on_state_or_failscip-shell shell systemd 261~rc4-1 wait_on_state_or_fail(). "testservice-failure-exit-handler-68.service" "inactive" "10" |
| 194 | |
| 195 | : "-------II---------------------------------------------------" |
| 196 | systemctl start testservice-success-68.service |
| 197 | wait_on_state_or_failscip-shell shell systemd 261~rc4-1 wait_on_state_or_fail(). "testservice-success-exit-handler-68.service" "inactive" "10" |
| 198 | |
| 199 | # Test some transient units since these exit very quickly. |
| 200 | : "-------III--------------------------------------------------" |
| 201 | systemd-run --unit=testservice-transient-success-68 \ |
| 202 | --property=OnSuccess=testservice-transient-success-exit-handler-68.service \ |
| 203 | true |
| 204 | wait_on_state_or_failscip-shell shell systemd 261~rc4-1 wait_on_state_or_fail(). "testservice-success-exit-handler-68.service" "inactive" "10" |
| 205 | |
| 206 | : "-------IIII-------------------------------------------------" |
| 207 | systemd-run --unit=testservice-transient-failure-68 \ |
| 208 | --property=OnFailure=testservice-transient-failure-exit-handler-68.service \ |
| 209 | false |
| 210 | wait_on_state_or_failscip-shell shell systemd 261~rc4-1 wait_on_state_or_fail(). "testservice-failure-exit-handler-68.service" "inactive" "10" |
| 211 | |
| 212 | # Test template handlers too |
| 213 | : "-------V---------------------------------------------------" |
| 214 | systemctl start testservice-success-68-template.service |
| 215 | wait_on_state_or_failscip-shell shell systemd 261~rc4-1 wait_on_state_or_fail(). "testservice-success-exit-handler-68-template@testservice-success-68-template.service.service" "inactive" "10" |
| 216 | |
| 217 | : "-------VI----------------------------------------------------" |
| 218 | systemctl start testservice-failure-68-template.service |
| 219 | wait_on_state_or_failscip-shell shell systemd 261~rc4-1 wait_on_state_or_fail(). "testservice-failure-exit-handler-68-template@testservice-failure-68-template.service.service" "inactive" "10" |
| 220 | |
| 221 | # Test that monitor variables are set as long as OnSuccess= and OnFailure= dependant is the same |
| 222 | : "-------VII----------------------------------------------------" |
| 223 | systemctl start testservice-success-and-failure-68.service |
| 224 | wait_on_state_or_failscip-shell shell systemd 261~rc4-1 wait_on_state_or_fail(). "testservice-success-exit-handler-68.service" "inactive" "10" |
| 225 | |
| 226 | touchscip-shell system . . touch. /testokscip-shell filesystem . . `/testok`. |