summaryrefslogtreecommitdiffstats
path: root/test/units/TEST-17-UDEV.07.sh
blob: 629393a56dea36c6a6a869d48ca2cbcb67683828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -ex
set -o pipefail

# shellcheck source=test/units/util.sh
. "$(dirname "$0")"/util.sh

wait_service_active() {(
    set +ex
    for i in {1..20}; do
        (( i > 1 )) && sleep 0.5
        if systemctl --quiet is-active "${1?}"; then
            return 0
        fi
    done
    return 1
)}

wait_service_inactive() {(
    set +ex
    for i in {1..20}; do
        (( i > 1 )) && sleep 0.5
        systemctl --quiet is-active "${1?}"
        if [[ "$?" == "3" ]]; then
            return 0
        fi
    done
    return 1
)}

mkdir -p /run/systemd/system
cat >/run/systemd/system/both.service <<EOF
[Service]
ExecStart=sleep 1000
EOF

cat >/run/systemd/system/on-add.service <<EOF
[Service]
ExecStart=sleep 1000
EOF

cat >/run/systemd/system/on-change.service <<EOF
[Service]
ExecStart=sleep 1000
EOF

systemctl daemon-reload

mkdir -p /run/udev/rules.d/
cat >/run/udev/rules.d/50-testsuite.rules <<EOF
SUBSYSTEM=="net", KERNEL=="dummy9?", OPTIONS="log_level=debug"
SUBSYSTEM=="net", KERNEL=="dummy9?", ACTION=="add",    TAG+="systemd", ENV{SYSTEMD_WANTS}+="both.service", ENV{SYSTEMD_WANTS}+="on-add.service"
SUBSYSTEM=="net", KERNEL=="dummy9?", ACTION=="change", TAG+="systemd", ENV{SYSTEMD_WANTS}+="both.service", ENV{SYSTEMD_WANTS}+="on-change.service"
EOF

udevadm control --reload

# StopWhenUnneeded=no
ip link add dummy99 type dummy
udevadm wait --settle --timeout=30 /sys/class/net/dummy99
wait_service_active both.service
wait_service_active on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service
systemctl stop both.service on-add.service

udevadm trigger --action=change --settle /sys/class/net/dummy99
udevadm info /sys/class/net/dummy99
wait_service_active both.service
assert_rc 3 systemctl --quiet is-active on-add.service
wait_service_active on-change.service
systemctl stop both.service on-change.service

ip link del dummy99
udevadm wait --settle --timeout=30 --removed /sys/class/net/dummy99
assert_rc 3 systemctl --quiet is-active both.service
assert_rc 3 systemctl --quiet is-active on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

# StopWhenUnneeded=yes
cat >/run/systemd/system/both.service <<EOF
[Unit]
StopWhenUnneeded=yes

[Service]
ExecStart=sleep 1000
Type=simple
EOF

cat >/run/systemd/system/on-add.service <<EOF
[Unit]
StopWhenUnneeded=yes

[Service]
ExecStart=sleep 1000
Type=simple
EOF

cat >/run/systemd/system/on-change.service <<EOF
[Unit]
StopWhenUnneeded=yes

[Service]
ExecStart=echo changed
RemainAfterExit=true
Type=oneshot
EOF

systemctl daemon-reload

# StopWhenUnneeded=yes (single device, only add event)
ip link add dummy99 type dummy
udevadm wait --settle --timeout=30 /sys/class/net/dummy99
wait_service_active both.service
wait_service_active on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

ip link del dummy99
udevadm wait --settle --timeout=30 --removed /sys/class/net/dummy99
wait_service_inactive both.service
wait_service_inactive on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

# StopWhenUnneeded=yes (single device, add and change event)
ip link add dummy99 type dummy
udevadm wait --settle --timeout=30 /sys/class/net/dummy99
wait_service_active both.service
wait_service_active on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

udevadm trigger --action=change --settle /sys/class/net/dummy99
assert_rc 0 systemctl --quiet is-active both.service
wait_service_inactive on-add.service
wait_service_active on-change.service

ip link del dummy99
udevadm wait --settle --timeout=30 --removed /sys/class/net/dummy99
wait_service_inactive both.service
assert_rc 3 systemctl --quiet is-active on-add.service
wait_service_inactive on-change.service

# StopWhenUnneeded=yes (multiple devices, only add events)
ip link add dummy99 type dummy
udevadm wait --settle --timeout=30 /sys/class/net/dummy99
wait_service_active both.service
wait_service_active on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

ip link add dummy98 type dummy
udevadm wait --settle --timeout=30 /sys/class/net/dummy98
assert_rc 0 systemctl --quiet is-active both.service
assert_rc 0 systemctl --quiet is-active on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

ip link del dummy99
udevadm wait --settle --timeout=30 --removed /sys/class/net/dummy99
assert_rc 0 systemctl --quiet is-active both.service
assert_rc 0 systemctl --quiet is-active on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

ip link del dummy98
udevadm wait --settle --timeout=30 --removed /sys/class/net/dummy98
wait_service_inactive both.service
wait_service_inactive on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

# StopWhenUnneeded=yes (multiple devices, add and change events)
ip link add dummy99 type dummy
udevadm wait --settle --timeout=30 /sys/class/net/dummy99
wait_service_active both.service
wait_service_active on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

ip link add dummy98 type dummy
udevadm wait --settle --timeout=30 /sys/class/net/dummy98
assert_rc 0 systemctl --quiet is-active both.service
assert_rc 0 systemctl --quiet is-active on-add.service
assert_rc 3 systemctl --quiet is-active on-change.service

udevadm trigger --action=change --settle /sys/class/net/dummy99
assert_rc 0 systemctl --quiet is-active both.service
assert_rc 0 systemctl --quiet is-active on-add.service
wait_service_active on-change.service

ip link del dummy98
udevadm wait --settle --timeout=30 --removed /sys/class/net/dummy98
assert_rc 0 systemctl --quiet is-active both.service
wait_service_inactive on-add.service
assert_rc 0 systemctl --quiet is-active on-change.service

ip link del dummy99
udevadm wait --settle --timeout=30 --removed /sys/class/net/dummy99
wait_service_inactive both.service
assert_rc 3 systemctl --quiet is-active on-add.service
wait_service_inactive on-change.service

# cleanup
rm -f /run/udev/rules.d/50-testsuite.rules
udevadm control --reload

rm -f /run/systemd/system/on-add.service
rm -f /run/systemd/system/on-change.service
systemctl daemon-reload

exit 0