]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-15.sh
Merge pull request #18481 from keszybz/rpm-restart-post-trans
[thirdparty/systemd.git] / test / units / testsuite-15.sh
1 #! /bin/bash
2 set -e
3 set -x
4
5 _clear_service () {
6 systemctl stop $1.service 2>/dev/null || :
7 rm -f /{etc,run,usr/lib}/systemd/system/$1.service
8 rm -fr /{etc,run,usr/lib}/systemd/system/$1.service.d
9 rm -fr /{etc,run,usr/lib}/systemd/system/$1.service.{wants,requires}
10 if [[ $1 == *@ ]]; then
11 systemctl stop $1*.service 2>/dev/null || :
12 rm -f /{etc,run,usr/lib}/systemd/system/$1*.service
13 rm -fr /{etc,run,usr/lib}/systemd/system/$1*.service.d
14 rm -fr /{etc,run,usr/lib}/systemd/system/$1*.service.{wants,requires}
15 fi
16 }
17
18 clear_services () {
19 for u in $*; do
20 _clear_service $u
21 done
22 systemctl daemon-reload
23 }
24
25 create_service () {
26 clear_services $1
27
28 cat >/etc/systemd/system/$1.service<<EOF
29 [Unit]
30 Description=$1 unit
31
32 [Service]
33 ExecStart=/bin/sleep 100000
34 EOF
35 mkdir -p /{etc,run,usr/lib}/systemd/system/$1.service.d
36 mkdir -p /etc/systemd/system/$1.service.{wants,requires}
37 mkdir -p /run/systemd/system/$1.service.{wants,requires}
38 mkdir -p /usr/lib/systemd/system/$1.service.{wants,requires}
39 }
40
41 create_services () {
42 for u in $*; do
43 create_service $u
44 done
45 }
46
47 check_ok () {
48 [ $# -eq 3 ] || return
49
50 x="$(systemctl show --value -p $2 $1)"
51 case "$x" in
52 *$3*) return 0 ;;
53 *) return 1 ;;
54 esac
55 }
56
57 check_ko () {
58 ! check_ok "$@"
59 }
60
61 test_basic_dropins () {
62 echo "Testing basic dropins..."
63
64 echo "*** test a wants b wants c"
65 create_services test15-a test15-b test15-c
66 ln -s ../test15-b.service /etc/systemd/system/test15-a.service.wants/
67 ln -s ../test15-c.service /etc/systemd/system/test15-b.service.wants/
68 check_ok test15-a Wants test15-b.service
69 check_ok test15-b Wants test15-c.service
70
71 echo "*** test a wants,requires b"
72 create_services test15-a test15-b test15-c
73 ln -s ../test15-b.service /etc/systemd/system/test15-a.service.wants/
74 ln -s ../test15-b.service /etc/systemd/system/test15-a.service.requires/
75 check_ok test15-a Wants test15-b.service
76 check_ok test15-a Requires test15-b.service
77
78 echo "*** test a wants nonexistent"
79 create_service test15-a
80 ln -s ../nonexistent.service /etc/systemd/system/test15-a.service.wants/
81 check_ok test15-a Wants nonexistent.service
82 systemctl start test15-a
83 systemctl stop test15-a
84
85 echo "*** test a requires nonexistent"
86 ln -sf ../nonexistent.service /etc/systemd/system/test15-a.service.requires/
87 systemctl daemon-reload
88 check_ok test15-a Requires nonexistent.service
89
90 # 'b' is already loaded when 'c' pulls it in via a dropin.
91 echo "*** test a,c require b"
92 create_services test15-a test15-b test15-c
93 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.requires/
94 ln -sf ../test15-b.service /etc/systemd/system/test15-c.service.requires/
95 systemctl start test15-a
96 check_ok test15-c Requires test15-b.service
97 systemctl stop test15-a test15-b
98
99 # 'b' is already loaded when 'c' pulls it in via an alias dropin.
100 echo "*** test a wants alias"
101 create_services test15-a test15-b test15-c
102 ln -sf test15-c.service /etc/systemd/system/test15-c1.service
103 ln -sf ../test15-c.service /etc/systemd/system/test15-a.service.wants/
104 ln -sf ../test15-c1.service /etc/systemd/system/test15-b.service.wants/
105 systemctl start test15-a
106 check_ok test15-a Wants test15-c.service
107 check_ok test15-b Wants test15-c.service
108 systemctl stop test15-a test15-c
109
110 echo "*** test service.d/ top level drop-in"
111 create_services test15-a test15-b
112 check_ko test15-a ExecCondition "/bin/echo a"
113 check_ko test15-b ExecCondition "/bin/echo b"
114 mkdir -p /usr/lib/systemd/system/service.d
115 cat >/usr/lib/systemd/system/service.d/override.conf <<EOF
116 [Service]
117 ExecCondition=/bin/echo %n
118 EOF
119 systemctl daemon-reload
120 check_ok test15-a ExecCondition "/bin/echo test15-a"
121 check_ok test15-b ExecCondition "/bin/echo test15-b"
122 rm -rf /usr/lib/systemd/system/service.d
123
124 clear_services test15-a test15-b test15-c
125 }
126
127 test_linked_units () {
128 echo "Testing linked units..."
129 echo "*** test linked unit (same basename)"
130
131 create_service test15-a
132 mv /etc/systemd/system/test15-a.service /
133 ln -s /test15-a.service /etc/systemd/system/
134 ln -s test15-a.service /etc/systemd/system/test15-b.service
135
136 check_ok test15-a Names test15-a.service
137 check_ok test15-a Names test15-b.service
138
139 echo "*** test linked unit (cross basename)"
140
141 mv /test15-a.service /test15-a@.scope
142 ln -fs /test15-a@.scope /etc/systemd/system/test15-a.service
143 systemctl daemon-reload
144
145 check_ok test15-a Names test15-a.service
146 check_ok test15-a Names test15-b.service
147 check_ko test15-a Names test15-b@
148
149 rm /test15-a@.scope
150 clear_services test15-a test15-b
151 }
152
153 test_hierarchical_dropins () {
154 echo "Testing hierarchical dropins..."
155 echo "*** test service.d/ top level drop-in"
156 create_services a-b-c
157 check_ko a-b-c ExecCondition "/bin/echo service.d"
158 check_ko a-b-c ExecCondition "/bin/echo a-.service.d"
159 check_ko a-b-c ExecCondition "/bin/echo a-b-.service.d"
160 check_ko a-b-c ExecCondition "/bin/echo a-b-c.service.d"
161
162 for dropin in service.d a-.service.d a-b-.service.d a-b-c.service.d; do
163 mkdir -p /usr/lib/systemd/system/$dropin
164 echo "
165 [Service]
166 ExecCondition=/bin/echo $dropin
167 " > /usr/lib/systemd/system/$dropin/override.conf
168 systemctl daemon-reload
169 check_ok a-b-c ExecCondition "/bin/echo $dropin"
170 done
171 for dropin in service.d a-.service.d a-b-.service.d a-b-c.service.d; do
172 rm -rf /usr/lib/systemd/system/$dropin
173 done
174
175 clear_services a-b-c
176 }
177
178 test_template_dropins () {
179 echo "Testing template dropins..."
180
181 create_services foo bar@ yup@
182
183 # Declare some deps to check if the body was loaded
184 cat >>/etc/systemd/system/bar@.service <<EOF
185 [Unit]
186 After=bar-template-after.device
187 EOF
188
189 cat >>/etc/systemd/system/yup@.service <<EOF
190 [Unit]
191 After=yup-template-after.device
192 EOF
193
194 ln -s /etc/systemd/system/bar@.service /etc/systemd/system/foo.service.wants/bar@1.service
195 check_ok foo Wants bar@1.service
196
197 echo "*** test bar-alias@.service→bar@.service, but instance symlinks point to yup@.service ***"
198 ln -s bar@.service /etc/systemd/system/bar-alias@.service
199 ln -s bar@1.service /etc/systemd/system/bar-alias@1.service
200 ln -s yup@.service /etc/systemd/system/bar-alias@2.service
201 ln -s yup@3.service /etc/systemd/system/bar-alias@3.service
202
203 # create some dropin deps
204 mkdir -p /etc/systemd/system/bar@{,0,1,2,3}.service.requires/
205 mkdir -p /etc/systemd/system/yup@{,0,1,2,3}.service.requires/
206 mkdir -p /etc/systemd/system/bar-alias@{,0,1,2,3}.service.requires/
207
208 ln -s ../bar-template-requires.device /etc/systemd/system/bar@.service.requires/
209 ln -s ../bar-0-requires.device /etc/systemd/system/bar@0.service.requires/
210 ln -s ../bar-1-requires.device /etc/systemd/system/bar@1.service.requires/
211 ln -s ../bar-2-requires.device /etc/systemd/system/bar@2.service.requires/
212 ln -s ../bar-3-requires.device /etc/systemd/system/bar@3.service.requires/
213
214 ln -s ../yup-template-requires.device /etc/systemd/system/yup@.service.requires/
215 ln -s ../yup-0-requires.device /etc/systemd/system/yup@0.service.requires/
216 ln -s ../yup-1-requires.device /etc/systemd/system/yup@1.service.requires/
217 ln -s ../yup-2-requires.device /etc/systemd/system/yup@2.service.requires/
218 ln -s ../yup-3-requires.device /etc/systemd/system/yup@3.service.requires/
219
220 ln -s ../bar-alias-template-requires.device /etc/systemd/system/bar-alias@.service.requires/
221 ln -s ../bar-alias-0-requires.device /etc/systemd/system/bar-alias@0.service.requires/
222 ln -s ../bar-alias-1-requires.device /etc/systemd/system/bar-alias@1.service.requires/
223 ln -s ../bar-alias-2-requires.device /etc/systemd/system/bar-alias@2.service.requires/
224 ln -s ../bar-alias-3-requires.device /etc/systemd/system/bar-alias@3.service.requires/
225
226 systemctl daemon-reload
227
228 echo '*** bar@0 is aliased by bar-alias@0 ***'
229 systemctl show -p Names,Requires bar@0
230 systemctl show -p Names,Requires bar-alias@0
231 check_ok bar@0 Names bar@0
232 check_ok bar@0 Names bar-alias@0
233
234 check_ok bar@0 After bar-template-after.device
235
236 check_ok bar@0 Requires bar-0-requires.device
237 check_ok bar@0 Requires bar-alias-0-requires.device
238 check_ok bar@0 Requires bar-template-requires.device
239 check_ok bar@0 Requires bar-alias-template-requires.device
240 check_ko bar@0 Requires yup-template-requires.device
241
242 check_ok bar-alias@0 After bar-template-after.device
243
244 check_ok bar-alias@0 Requires bar-0-requires.device
245 check_ok bar-alias@0 Requires bar-alias-0-requires.device
246 check_ok bar-alias@0 Requires bar-template-requires.device
247 check_ok bar-alias@0 Requires bar-alias-template-requires.device
248 check_ko bar-alias@0 Requires yup-template-requires.device
249 check_ko bar-alias@0 Requires yup-0-requires.device
250
251 echo '*** bar@1 is aliased by bar-alias@1 ***'
252 systemctl show -p Names,Requires bar@1
253 systemctl show -p Names,Requires bar-alias@1
254 check_ok bar@1 Names bar@1
255 check_ok bar@1 Names bar-alias@1
256
257 check_ok bar@1 After bar-template-after.device
258
259 check_ok bar@1 Requires bar-1-requires.device
260 check_ok bar@1 Requires bar-alias-1-requires.device
261 check_ok bar@1 Requires bar-template-requires.device
262 # See https://github.com/systemd/systemd/pull/13119#discussion_r308145418
263 check_ok bar@1 Requires bar-alias-template-requires.device
264 check_ko bar@1 Requires yup-template-requires.device
265 check_ko bar@1 Requires yup-1-requires.device
266
267 check_ok bar-alias@1 After bar-template-after.device
268
269 check_ok bar-alias@1 Requires bar-1-requires.device
270 check_ok bar-alias@1 Requires bar-alias-1-requires.device
271 check_ok bar-alias@1 Requires bar-template-requires.device
272 check_ok bar-alias@1 Requires bar-alias-template-requires.device
273 check_ko bar-alias@1 Requires yup-template-requires.device
274 check_ko bar-alias@1 Requires yup-1-requires.device
275
276 echo '*** bar-alias@2 aliases yup@2, bar@2 is independent ***'
277 systemctl show -p Names,Requires bar@2
278 systemctl show -p Names,Requires bar-alias@2
279 check_ok bar@2 Names bar@2
280 check_ko bar@2 Names bar-alias@2
281
282 check_ok bar@2 After bar-template-after.device
283
284 check_ok bar@2 Requires bar-2-requires.device
285 check_ko bar@2 Requires bar-alias-2-requires.device
286 check_ok bar@2 Requires bar-template-requires.device
287 check_ko bar@2 Requires bar-alias-template-requires.device
288 check_ko bar@2 Requires yup-template-requires.device
289 check_ko bar@2 Requires yup-2-requires.device
290
291 check_ko bar-alias@2 After bar-template-after.device
292
293 check_ko bar-alias@2 Requires bar-2-requires.device
294 check_ok bar-alias@2 Requires bar-alias-2-requires.device
295 check_ko bar-alias@2 Requires bar-template-requires.device
296 check_ok bar-alias@2 Requires bar-alias-template-requires.device
297 check_ok bar-alias@2 Requires yup-template-requires.device
298 check_ok bar-alias@2 Requires yup-2-requires.device
299
300 echo '*** bar-alias@3 aliases yup@3, bar@3 is independent ***'
301 systemctl show -p Names,Requires bar@3
302 systemctl show -p Names,Requires bar-alias@3
303 check_ok bar@3 Names bar@3
304 check_ko bar@3 Names bar-alias@3
305
306 check_ok bar@3 After bar-template-after.device
307
308 check_ok bar@3 Requires bar-3-requires.device
309 check_ko bar@3 Requires bar-alias-3-requires.device
310 check_ok bar@3 Requires bar-template-requires.device
311 check_ko bar@3 Requires bar-alias-template-requires.device
312 check_ko bar@3 Requires yup-template-requires.device
313 check_ko bar@3 Requires yup-3-requires.device
314
315 check_ko bar-alias@3 After bar-template-after.device
316
317 check_ko bar-alias@3 Requires bar-3-requires.device
318 check_ok bar-alias@3 Requires bar-alias-3-requires.device
319 check_ko bar-alias@3 Requires bar-template-requires.device
320 check_ok bar-alias@3 Requires bar-alias-template-requires.device
321 check_ok bar-alias@3 Requires yup-template-requires.device
322 check_ok bar-alias@3 Requires yup-3-requires.device
323
324 clear_services foo {bar,yup,bar-alias}@{,1,2,3}
325 }
326
327 test_alias_dropins () {
328 echo "Testing alias dropins..."
329
330 echo "*** test a wants b1 alias of b"
331 create_services test15-a test15-b
332 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
333 ln -sf ../test15-b1.service /etc/systemd/system/test15-a.service.wants/
334 check_ok test15-a Wants test15-b.service
335 systemctl start test15-a
336 systemctl --quiet is-active test15-b
337 systemctl stop test15-a test15-b
338 rm /etc/systemd/system/test15-b1.service
339 clear_services test15-a test15-b
340
341 # Check that dependencies don't vary.
342 echo "*** test 2"
343 create_services test15-a test15-x test15-y
344 mkdir -p /etc/systemd/system/test15-a1.service.wants/
345 ln -sf test15-a.service /etc/systemd/system/test15-a1.service
346 ln -sf ../test15-x.service /etc/systemd/system/test15-a.service.wants/
347 ln -sf ../test15-y.service /etc/systemd/system/test15-a1.service.wants/
348 check_ok test15-a1 Wants test15-x.service # see [1]
349 check_ok test15-a1 Wants test15-y.service
350 systemctl start test15-a
351 check_ok test15-a1 Wants test15-x.service # see [2]
352 check_ok test15-a1 Wants test15-y.service
353 systemctl stop test15-a test15-x test15-y
354 rm /etc/systemd/system/test15-a1.service
355
356 clear_services test15-a test15-x test15-y
357 }
358
359 test_masked_dropins () {
360 echo "Testing masked dropins..."
361
362 create_services test15-a test15-b
363
364 # 'b' is masked for both deps
365 echo "*** test a wants,requires b is masked"
366 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
367 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b.service
368 check_ko test15-a Wants test15-b.service
369 check_ko test15-a Requires test15-b.service
370
371 # 'a' wants 'b' and 'b' is masked at a lower level
372 echo "*** test a wants b, mask override"
373 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.wants/test15-b.service
374 ln -sf /dev/null /usr/lib/systemd/system/test15-a.service.wants/test15-b.service
375 check_ok test15-a Wants test15-b.service
376
377 # 'a' wants 'b' and 'b' is masked at a higher level
378 echo "*** test a wants b, mask"
379 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
380 ln -sf ../test15-b.service /usr/lib/systemd/system/test15-a.service.wants/test15-b.service
381 check_ko test15-a Wants test15-b.service
382
383 # 'a' is masked but has an override config file
384 echo "*** test a is masked but has an override"
385 create_services test15-a test15-b
386 ln -sf /dev/null /etc/systemd/system/test15-a.service
387 cat >/usr/lib/systemd/system/test15-a.service.d/override.conf <<EOF
388 [Unit]
389 After=test15-b.service
390 EOF
391 check_ok test15-a UnitFileState masked
392
393 # 'b1' is an alias for 'b': masking 'b' dep should not influence 'b1' dep
394 echo "*** test a wants b, b1, and one is masked"
395 create_services test15-a test15-b
396 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
397 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
398 ln -sf ../test15-b1.service /usr/lib/systemd/system/test15-a.service.wants/test15-b1.service
399 systemctl cat test15-a
400 systemctl show -p Wants,Requires test15-a
401 systemctl cat test15-b1
402 systemctl show -p Wants,Requires test15-b1
403 check_ok test15-a Wants test15-b.service
404 check_ko test15-a Wants test15-b1.service # the alias does not show up in the list of units
405 rm /etc/systemd/system/test15-b1.service
406
407 # 'b1' is an alias for 'b': masking 'b1' should not influence 'b' dep
408 echo "*** test a wants b, alias dep is masked"
409 create_services test15-a test15-b
410 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
411 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b1.service
412 ln -sf ../test15-b.service /usr/lib/systemd/system/test15-a.service.wants/test15-b.service
413 check_ok test15-a Wants test15-b.service
414 check_ko test15-a Wants test15-b1.service # the alias does not show up in the list of units
415 rm /etc/systemd/system/test15-b1.service
416
417 # 'a' has Wants=b.service but also has a masking
418 # dropin 'b': 'b' should still be pulled in.
419 echo "*** test a wants b both ways"
420 create_services test15-a test15-b
421 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
422 cat >/usr/lib/systemd/system/test15-a.service.d/wants-b.conf<<EOF
423 [Unit]
424 Wants=test15-b.service
425 EOF
426 check_ok test15-a Wants test15-b.service
427
428 # mask a dropin that points to an nonexistent unit.
429 echo "*** test a wants nonexistent is masked"
430 create_services test15-a
431 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/nonexistent.service
432 ln -sf ../nonexistent.service /usr/lib/systemd/system/test15-a.service.requires/
433 check_ko test15-a Requires nonexistent.service
434
435 # 'b' is already loaded when 'c' pulls it in via a dropin but 'b' is
436 # masked at a higher level.
437 echo "*** test a wants b is masked"
438 create_services test15-a test15-b test15-c
439 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.requires/
440 ln -sf ../test15-b.service /run/systemd/system/test15-c.service.requires/
441 ln -sf /dev/null /etc/systemd/system/test15-c.service.requires/test15-b.service
442 systemctl start test15-a
443 check_ko test15-c Requires test15-b.service
444 systemctl stop test15-a test15-b
445
446 # 'b' is already loaded when 'c' pulls it in via a dropin but 'b' is
447 # masked at a lower level.
448 echo "*** test a requires b is masked"
449 create_services test15-a test15-b test15-c
450 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.requires/
451 ln -sf ../test15-b.service /etc/systemd/system/test15-c.service.requires/
452 ln -sf /dev/null /run/systemd/system/test15-c.service.requires/test15-b.service
453 systemctl start test15-a
454 check_ok test15-c Requires test15-b.service
455 systemctl stop test15-a test15-b
456
457 # 'a' requires 2 aliases of 'b' and one of them is a mask.
458 echo "*** test a requires alias of b, other alias masked"
459 create_services test15-a test15-b
460 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
461 ln -sf test15-b.service /etc/systemd/system/test15-b2.service
462 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b1.service
463 ln -sf ../test15-b1.service /run/systemd/system/test15-a.service.requires/
464 ln -sf ../test15-b2.service /usr/lib/systemd/system/test15-a.service.requires/
465 check_ok test15-a Requires test15-b
466
467 # Same as above but now 'b' is masked.
468 echo "*** test a requires alias of b, b dep masked"
469 create_services test15-a test15-b
470 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
471 ln -sf test15-b.service /etc/systemd/system/test15-b2.service
472 ln -sf ../test15-b1.service /run/systemd/system/test15-a.service.requires/
473 ln -sf ../test15-b2.service /usr/lib/systemd/system/test15-a.service.requires/
474 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b.service
475 check_ok test15-a Requires test15-b
476
477 clear_services test15-a test15-b
478 }
479
480 test_invalid_dropins () {
481 echo "Testing invalid dropins..."
482 # Assertion failed on earlier versions, command exits unsuccessfully on later versions
483 systemctl cat nonexistent@.service || true
484 create_services a
485 systemctl daemon-reload
486 # Assertion failed on earlier versions, command exits unsuccessfully on later versions
487 systemctl cat a@.service || true
488 systemctl stop a
489 clear_services a
490 return 0
491 }
492
493 test_basic_dropins
494 test_linked_units
495 test_hierarchical_dropins
496 test_template_dropins
497 test_alias_dropins
498 test_masked_dropins
499 test_invalid_dropins
500
501 touch /testok