]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-15.sh
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / test / units / testsuite-15.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 set -eux
4 set -o pipefail
5
6 # shellcheck source=test/units/test-control.sh
7 . "$(dirname "$0")"/test-control.sh
8 # shellcheck source=test/units/util.sh
9 . "$(dirname "$0")"/util.sh
10
11 maybe_mount_usr_overlay
12 trap 'maybe_umount_usr_overlay' EXIT
13
14 clear_unit() {
15 local unit_name="${1:?}"
16 local base suffix
17
18 systemctl stop "$unit_name" 2>/dev/null || :
19 rm -f /{etc,run,usr/lib}/systemd/system/"$unit_name"
20 rm -fr /{etc,run,usr/lib}/systemd/system/"$unit_name".d
21 rm -fr /{etc,run,usr/lib}/systemd/system/"$unit_name".{wants,requires}
22 if [[ $unit_name == *@* ]]; then
23 base="${unit_name%@*}"
24 suffix="${unit_name##*.}"
25 systemctl stop "$base@"*."$suffix" 2>/dev/null || :
26 rm -f /{etc,run,usr/lib}/systemd/system/"$base@"*."$suffix"
27 rm -fr /{etc,run,usr/lib}/systemd/system/"$base@"*."$suffix".d
28 rm -fr /{etc,run,usr/lib}/systemd/system/"$base@"*."$suffix".{wants,requires}
29 fi
30 }
31
32 clear_units() {
33 for u in "$@"; do
34 clear_unit "$u"
35 done
36 systemctl daemon-reload
37 }
38
39 create_service() {
40 local service_name="${1:?}"
41 clear_units "${service_name}".service
42
43 cat >/etc/systemd/system/"$service_name".service <<EOF
44 [Unit]
45 Description=$service_name unit
46
47 [Service]
48 ExecStart=sleep 100000
49 EOF
50 mkdir -p /{etc,run,usr/lib}/systemd/system/"$service_name".service.{d,wants,requires}
51 }
52
53 create_services() {
54 for u in "$@"; do
55 create_service "$u"
56 done
57 }
58
59 check_ok() {
60 x="$(systemctl show --value -p "${2:?}" "${1:?}")"
61 case "$x" in
62 *${3:?}*) return 0 ;;
63 *) return 1 ;;
64 esac
65 }
66
67 check_ko() {
68 ! check_ok "$@"
69 }
70
71 testcase_basic_dropins() {
72 echo "Testing basic dropins..."
73
74 echo "*** test a wants b wants c"
75 create_services test15-a test15-b test15-c
76 ln -s ../test15-b.service /etc/systemd/system/test15-a.service.wants/
77 ln -s ../test15-c.service /etc/systemd/system/test15-b.service.wants/
78 check_ok test15-a Wants test15-b.service
79 check_ok test15-b Wants test15-c.service
80
81 echo "*** test a wants,requires b"
82 create_services test15-a test15-b test15-c
83 ln -s ../test15-b.service /etc/systemd/system/test15-a.service.wants/
84 ln -s ../test15-b.service /etc/systemd/system/test15-a.service.requires/
85 check_ok test15-a Wants test15-b.service
86 check_ok test15-a Requires test15-b.service
87
88 echo "*** test a wants nonexistent"
89 create_service test15-a
90 ln -s ../nonexistent.service /etc/systemd/system/test15-a.service.wants/
91 check_ok test15-a Wants nonexistent.service
92 systemctl start test15-a
93 systemctl stop test15-a
94
95 echo "*** test a requires nonexistent"
96 ln -sf ../nonexistent.service /etc/systemd/system/test15-a.service.requires/
97 systemctl daemon-reload
98 check_ok test15-a Requires nonexistent.service
99
100 # 'b' is already loaded when 'c' pulls it in via a dropin.
101 echo "*** test a,c require b"
102 create_services test15-a test15-b test15-c
103 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.requires/
104 ln -sf ../test15-b.service /etc/systemd/system/test15-c.service.requires/
105 systemctl start test15-a
106 check_ok test15-c Requires test15-b.service
107 systemctl stop test15-a test15-b
108
109 # 'b' is already loaded when 'c' pulls it in via an alias dropin.
110 echo "*** test a wants alias"
111 create_services test15-a test15-b test15-c
112 ln -sf test15-c.service /etc/systemd/system/test15-c1.service
113 ln -sf ../test15-c.service /etc/systemd/system/test15-a.service.wants/
114 ln -sf ../test15-c1.service /etc/systemd/system/test15-b.service.wants/
115 systemctl start test15-a
116 check_ok test15-a Wants test15-c.service
117 check_ok test15-b Wants test15-c.service
118 systemctl stop test15-a test15-c
119
120 echo "*** test service.d/ top level drop-in"
121 create_services test15-a test15-b
122 check_ko test15-a ExecCondition "/bin/echo a"
123 check_ko test15-b ExecCondition "/bin/echo b"
124 mkdir -p /run/systemd/system/service.d
125 cat >/run/systemd/system/service.d/override.conf <<EOF
126 [Service]
127 ExecCondition=/bin/echo %n
128 EOF
129 systemctl daemon-reload
130 check_ok test15-a ExecCondition "/bin/echo test15-a"
131 check_ok test15-b ExecCondition "/bin/echo test15-b"
132 rm -rf /run/systemd/system/service.d
133
134 clear_units test15-{a,b,c,c1}.service
135 }
136
137 testcase_linked_units() {
138 echo "Testing linked units..."
139 echo "*** test linked unit (same basename)"
140
141 create_service test15-a
142 mv /etc/systemd/system/test15-a.service /
143 ln -s /test15-a.service /etc/systemd/system/
144 ln -s test15-a.service /etc/systemd/system/test15-b.service
145
146 check_ok test15-a Names test15-a.service
147 check_ok test15-a Names test15-b.service
148
149 echo "*** test linked unit (cross basename)"
150
151 mv /test15-a.service /test15-a@.scope
152 ln -fs /test15-a@.scope /etc/systemd/system/test15-a.service
153 systemctl daemon-reload
154
155 check_ok test15-a Names test15-a.service
156 check_ok test15-a Names test15-b.service
157 check_ko test15-a Names test15-a@ # test15-a@.scope is the symlink target.
158 # Make sure it is completely ignored.
159
160 rm /test15-a@.scope
161 clear_units test15-{a,b}.service
162 }
163
164 testcase_template_alias() {
165 echo "Testing instance alias..."
166 echo "*** forward"
167
168 create_service test15-a@
169 ln -s test15-a@inst.service /etc/systemd/system/test15-b@inst.service # alias
170
171 check_ok test15-a@inst Names test15-a@inst.service
172 check_ok test15-a@inst Names test15-b@inst.service
173
174 check_ok test15-a@other Names test15-a@other.service
175 check_ko test15-a@other Names test15-b@other.service
176
177 echo "*** reverse"
178
179 systemctl daemon-reload
180
181 check_ok test15-b@inst Names test15-a@inst.service
182 check_ok test15-b@inst Names test15-b@inst.service
183
184 check_ko test15-b@other Names test15-a@other.service
185 check_ok test15-b@other Names test15-b@other.service
186
187 clear_units test15-{a,b}@.service
188 }
189
190 testcase_hierarchical_service_dropins() {
191 echo "Testing hierarchical service dropins..."
192 echo "*** test service.d/ top level drop-in"
193 create_services a-b-c
194 check_ko a-b-c ExecCondition "echo service.d"
195 check_ko a-b-c ExecCondition "echo a-.service.d"
196 check_ko a-b-c ExecCondition "echo a-b-.service.d"
197 check_ko a-b-c ExecCondition "echo a-b-c.service.d"
198
199 for dropin in service.d a-.service.d a-b-.service.d a-b-c.service.d; do
200 mkdir -p "/run/systemd/system/$dropin"
201 cat >"/run/systemd/system/$dropin/override.conf" <<EOF
202 [Service]
203 ExecCondition=echo $dropin
204 EOF
205 systemctl daemon-reload
206 check_ok a-b-c ExecCondition "echo $dropin"
207
208 # Check that we can start a transient service in presence of the drop-ins
209 systemd-run -u a-b-c2.service -p Description='sleepy' sleep infinity
210
211 # The transient setting replaces the default
212 check_ok a-b-c2.service Description "sleepy"
213
214 # The override takes precedence for ExecCondition
215 # (except the last iteration when it only applies to the other service)
216 if [ "$dropin" != "a-b-c.service.d" ]; then
217 check_ok a-b-c2.service ExecCondition "echo $dropin"
218 fi
219
220 # Check that things are the same after a reload
221 systemctl daemon-reload
222 check_ok a-b-c2.service Description "sleepy"
223 if [ "$dropin" != "a-b-c.service.d" ]; then
224 check_ok a-b-c2.service ExecCondition "echo $dropin"
225 fi
226
227 systemctl stop a-b-c2.service
228 done
229 for dropin in service.d a-.service.d a-b-.service.d a-b-c.service.d; do
230 rm -rf "/run/systemd/system/$dropin"
231 done
232
233 clear_units a-b-c.service
234 }
235
236 testcase_hierarchical_slice_dropins() {
237 echo "Testing hierarchical slice dropins..."
238 echo "*** test slice.d/ top level drop-in"
239 # Slice units don't even need a fragment, so we test the defaults here
240 check_ok a-b-c.slice Description "Slice /a/b/c"
241 check_ok a-b-c.slice MemoryMax "infinity"
242
243 # Test drop-ins
244 for dropin in slice.d a-.slice.d a-b-.slice.d a-b-c.slice.d; do
245 mkdir -p "/run/systemd/system/$dropin"
246 cat >"/run/systemd/system/$dropin/override.conf" <<EOF
247 [Slice]
248 MemoryMax=1000000000
249 EOF
250 systemctl daemon-reload
251 check_ok a-b-c.slice MemoryMax "1000000000"
252
253 busctl call \
254 org.freedesktop.systemd1 \
255 /org/freedesktop/systemd1 \
256 org.freedesktop.systemd1.Manager \
257 StartTransientUnit 'ssa(sv)a(sa(sv))' \
258 'a-b-c.slice' 'replace' \
259 2 \
260 'Description' s 'slice too' \
261 'MemoryMax' t 1000000002 \
262 0
263
264 # The override takes precedence for MemoryMax
265 check_ok a-b-c.slice MemoryMax "1000000000"
266 # The transient setting replaces the default
267 check_ok a-b-c.slice Description "slice too"
268
269 # Check that things are the same after a reload
270 systemctl daemon-reload
271 check_ok a-b-c.slice MemoryMax "1000000000"
272 check_ok a-b-c.slice Description "slice too"
273
274 busctl call \
275 org.freedesktop.systemd1 \
276 /org/freedesktop/systemd1 \
277 org.freedesktop.systemd1.Manager \
278 StopUnit 'ss' \
279 'a-b-c.slice' 'replace'
280
281 rm -f "/run/systemd/system/$dropin/override.conf"
282 done
283
284 # Test unit with a fragment
285 cat >/run/systemd/system/a-b-c.slice <<EOF
286 [Slice]
287 MemoryMax=1000000001
288 EOF
289 systemctl daemon-reload
290 check_ok a-b-c.slice MemoryMax "1000000001"
291
292 clear_units a-b-c.slice
293 }
294
295 testcase_transient_service_dropins() {
296 echo "Testing dropins for a transient service..."
297 echo "*** test transient service drop-ins"
298
299 mkdir -p /etc/systemd/system/service.d
300 mkdir -p /etc/systemd/system/a-.service.d
301 mkdir -p /etc/systemd/system/a-b-.service.d
302 mkdir -p /etc/systemd/system/a-b-c.service.d
303
304 echo -e '[Service]\nStandardInputText=aaa' >/etc/systemd/system/service.d/drop1.conf
305 echo -e '[Service]\nStandardInputText=bbb' >/etc/systemd/system/a-.service.d/drop2.conf
306 echo -e '[Service]\nStandardInputText=ccc' >/etc/systemd/system/a-b-.service.d/drop3.conf
307 echo -e '[Service]\nStandardInputText=ddd' >/etc/systemd/system/a-b-c.service.d/drop4.conf
308
309 # There's no fragment yet, so this fails
310 systemctl cat a-b-c.service && exit 1
311
312 # xxx → eHh4Cg==
313 systemd-run -u a-b-c.service -p StandardInputData=eHh4Cg== sleep infinity
314
315 data=$(systemctl show -P StandardInputData a-b-c.service)
316 # xxx\naaa\n\bbb\nccc\nddd\n → eHh4…
317 test "$data" = "eHh4CmFhYQpiYmIKY2NjCmRkZAo="
318
319 # Do a reload and check again
320 systemctl daemon-reload
321 data=$(systemctl show -P StandardInputData a-b-c.service)
322 test "$data" = "eHh4CmFhYQpiYmIKY2NjCmRkZAo="
323
324 clear_units a-b-c.service
325 rm /etc/systemd/system/service.d/drop1.conf \
326 /etc/systemd/system/a-.service.d/drop2.conf \
327 /etc/systemd/system/a-b-.service.d/drop3.conf
328 }
329
330 testcase_transient_slice_dropins() {
331 echo "Testing dropins for a transient slice..."
332 echo "*** test transient slice drop-ins"
333
334 # FIXME: implement reloading of individual units.
335 #
336 # The settings here are loaded twice. For most settings it doesn't matter,
337 # but Documentation is not deduplicated, so we current get repeated entried
338 # which is a bug.
339
340 mkdir -p /etc/systemd/system/slice.d
341 mkdir -p /etc/systemd/system/a-.slice.d
342 mkdir -p /etc/systemd/system/a-b-.slice.d
343 mkdir -p /etc/systemd/system/a-b-c.slice.d
344
345 echo -e '[Unit]\nDocumentation=man:drop1' >/etc/systemd/system/slice.d/drop1.conf
346 echo -e '[Unit]\nDocumentation=man:drop2' >/etc/systemd/system/a-.slice.d/drop2.conf
347 echo -e '[Unit]\nDocumentation=man:drop3' >/etc/systemd/system/a-b-.slice.d/drop3.conf
348 echo -e '[Unit]\nDocumentation=man:drop4' >/etc/systemd/system/a-b-c.slice.d/drop4.conf
349
350 # Invoke daemon-reload to make sure that the call below doesn't fail
351 systemctl daemon-reload
352
353 # No fragment is required, so this works
354 systemctl cat a-b-c.slice
355
356 busctl call \
357 org.freedesktop.systemd1 \
358 /org/freedesktop/systemd1 \
359 org.freedesktop.systemd1.Manager \
360 StartTransientUnit 'ssa(sv)a(sa(sv))' \
361 'a-b-c.slice' 'replace' \
362 1 \
363 'Documentation' as 1 'man:drop5' \
364 0
365
366 data=$(systemctl show -P Documentation a-b-c.slice)
367 test "$data" = "man:drop1 man:drop2 man:drop3 man:drop4 man:drop5 man:drop1 man:drop2 man:drop3 man:drop4"
368
369 # Do a reload and check again
370 systemctl daemon-reload
371 data=$(systemctl show -P Documentation a-b-c.slice)
372 test "$data" = "man:drop5 man:drop1 man:drop2 man:drop3 man:drop4"
373
374 clear_units a-b-c.slice
375 rm /etc/systemd/system/slice.d/drop1.conf \
376 /etc/systemd/system/a-.slice.d/drop2.conf \
377 /etc/systemd/system/a-b-.slice.d/drop3.conf
378 }
379
380 testcase_template_dropins() {
381 echo "Testing template dropins..."
382
383 create_services foo bar@ yup@
384
385 # Declare some deps to check if the body was loaded
386 cat >>/etc/systemd/system/bar@.service <<EOF
387 [Unit]
388 After=bar-template-after.device
389 EOF
390
391 cat >>/etc/systemd/system/yup@.service <<EOF
392 [Unit]
393 After=yup-template-after.device
394 EOF
395
396 ln -s /etc/systemd/system/bar@.service /etc/systemd/system/foo.service.wants/bar@1.service
397 check_ok foo Wants bar@1.service
398
399 echo "*** test bar-alias@.service→bar@.service, but instance symlinks point to yup@.service ***"
400 ln -s bar@.service /etc/systemd/system/bar-alias@.service
401 ln -s bar@1.service /etc/systemd/system/bar-alias@1.service
402 ln -s yup@.service /etc/systemd/system/bar-alias@2.service
403 ln -s yup@3.service /etc/systemd/system/bar-alias@3.service
404
405 # create some dropin deps
406 mkdir -p /etc/systemd/system/bar@{,0,1,2,3}.service.requires/
407 mkdir -p /etc/systemd/system/yup@{,0,1,2,3}.service.requires/
408 mkdir -p /etc/systemd/system/bar-alias@{,0,1,2,3}.service.requires/
409
410 ln -s ../bar-template-requires.device /etc/systemd/system/bar@.service.requires/
411 ln -s ../bar-0-requires.device /etc/systemd/system/bar@0.service.requires/
412 ln -s ../bar-1-requires.device /etc/systemd/system/bar@1.service.requires/
413 ln -s ../bar-2-requires.device /etc/systemd/system/bar@2.service.requires/
414 ln -s ../bar-3-requires.device /etc/systemd/system/bar@3.service.requires/
415
416 ln -s ../yup-template-requires.device /etc/systemd/system/yup@.service.requires/
417 ln -s ../yup-0-requires.device /etc/systemd/system/yup@0.service.requires/
418 ln -s ../yup-1-requires.device /etc/systemd/system/yup@1.service.requires/
419 ln -s ../yup-2-requires.device /etc/systemd/system/yup@2.service.requires/
420 ln -s ../yup-3-requires.device /etc/systemd/system/yup@3.service.requires/
421
422 ln -s ../bar-alias-template-requires.device /etc/systemd/system/bar-alias@.service.requires/
423 ln -s ../bar-alias-0-requires.device /etc/systemd/system/bar-alias@0.service.requires/
424 ln -s ../bar-alias-1-requires.device /etc/systemd/system/bar-alias@1.service.requires/
425 ln -s ../bar-alias-2-requires.device /etc/systemd/system/bar-alias@2.service.requires/
426 ln -s ../bar-alias-3-requires.device /etc/systemd/system/bar-alias@3.service.requires/
427
428 systemctl daemon-reload
429
430 echo '*** bar@0 is aliased by bar-alias@0 ***'
431 systemctl show -p Names,Requires bar@0
432 systemctl show -p Names,Requires bar-alias@0
433 check_ok bar@0 Names bar@0
434 check_ok bar@0 Names bar-alias@0
435
436 check_ok bar@0 After bar-template-after.device
437
438 check_ok bar@0 Requires bar-0-requires.device
439 check_ok bar@0 Requires bar-alias-0-requires.device
440 check_ok bar@0 Requires bar-template-requires.device
441 check_ok bar@0 Requires bar-alias-template-requires.device
442 check_ko bar@0 Requires yup-template-requires.device
443
444 check_ok bar-alias@0 After bar-template-after.device
445
446 check_ok bar-alias@0 Requires bar-0-requires.device
447 check_ok bar-alias@0 Requires bar-alias-0-requires.device
448 check_ok bar-alias@0 Requires bar-template-requires.device
449 check_ok bar-alias@0 Requires bar-alias-template-requires.device
450 check_ko bar-alias@0 Requires yup-template-requires.device
451 check_ko bar-alias@0 Requires yup-0-requires.device
452
453 echo '*** bar@1 is aliased by bar-alias@1 ***'
454 systemctl show -p Names,Requires bar@1
455 systemctl show -p Names,Requires bar-alias@1
456 check_ok bar@1 Names bar@1
457 check_ok bar@1 Names bar-alias@1
458
459 check_ok bar@1 After bar-template-after.device
460
461 check_ok bar@1 Requires bar-1-requires.device
462 check_ok bar@1 Requires bar-alias-1-requires.device
463 check_ok bar@1 Requires bar-template-requires.device
464 # See https://github.com/systemd/systemd/pull/13119#discussion_r308145418
465 check_ok bar@1 Requires bar-alias-template-requires.device
466 check_ko bar@1 Requires yup-template-requires.device
467 check_ko bar@1 Requires yup-1-requires.device
468
469 check_ok bar-alias@1 After bar-template-after.device
470
471 check_ok bar-alias@1 Requires bar-1-requires.device
472 check_ok bar-alias@1 Requires bar-alias-1-requires.device
473 check_ok bar-alias@1 Requires bar-template-requires.device
474 check_ok bar-alias@1 Requires bar-alias-template-requires.device
475 check_ko bar-alias@1 Requires yup-template-requires.device
476 check_ko bar-alias@1 Requires yup-1-requires.device
477
478 echo '*** bar-alias@2 aliases yup@2, bar@2 is independent ***'
479 systemctl show -p Names,Requires bar@2
480 systemctl show -p Names,Requires bar-alias@2
481 check_ok bar@2 Names bar@2
482 check_ko bar@2 Names bar-alias@2
483
484 check_ok bar@2 After bar-template-after.device
485
486 check_ok bar@2 Requires bar-2-requires.device
487 check_ko bar@2 Requires bar-alias-2-requires.device
488 check_ok bar@2 Requires bar-template-requires.device
489 check_ko bar@2 Requires bar-alias-template-requires.device
490 check_ko bar@2 Requires yup-template-requires.device
491 check_ko bar@2 Requires yup-2-requires.device
492
493 check_ko bar-alias@2 After bar-template-after.device
494
495 check_ko bar-alias@2 Requires bar-2-requires.device
496 check_ok bar-alias@2 Requires bar-alias-2-requires.device
497 check_ko bar-alias@2 Requires bar-template-requires.device
498 check_ok bar-alias@2 Requires bar-alias-template-requires.device
499 check_ok bar-alias@2 Requires yup-template-requires.device
500 check_ok bar-alias@2 Requires yup-2-requires.device
501
502 echo '*** bar-alias@3 aliases yup@3, bar@3 is independent ***'
503 systemctl show -p Names,Requires bar@3
504 systemctl show -p Names,Requires bar-alias@3
505 check_ok bar@3 Names bar@3
506 check_ko bar@3 Names bar-alias@3
507
508 check_ok bar@3 After bar-template-after.device
509
510 check_ok bar@3 Requires bar-3-requires.device
511 check_ko bar@3 Requires bar-alias-3-requires.device
512 check_ok bar@3 Requires bar-template-requires.device
513 check_ko bar@3 Requires bar-alias-template-requires.device
514 check_ko bar@3 Requires yup-template-requires.device
515 check_ko bar@3 Requires yup-3-requires.device
516
517 check_ko bar-alias@3 After bar-template-after.device
518
519 check_ko bar-alias@3 Requires bar-3-requires.device
520 check_ok bar-alias@3 Requires bar-alias-3-requires.device
521 check_ko bar-alias@3 Requires bar-template-requires.device
522 check_ok bar-alias@3 Requires bar-alias-template-requires.device
523 check_ok bar-alias@3 Requires yup-template-requires.device
524 check_ok bar-alias@3 Requires yup-3-requires.device
525
526 clear_units foo.service {bar,yup,bar-alias}@{,1,2,3}.service
527 }
528
529 testcase_alias_dropins() {
530 echo "Testing alias dropins..."
531
532 echo "*** test a wants b1 alias of b"
533 create_services test15-a test15-b
534 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
535 ln -sf ../test15-b1.service /etc/systemd/system/test15-a.service.wants/
536 check_ok test15-a Wants test15-b.service
537 systemctl start test15-a
538 systemctl --quiet is-active test15-b
539 systemctl stop test15-a test15-b
540 rm /etc/systemd/system/test15-b1.service
541 clear_units test15-{a,b}.service
542
543 # Check that dependencies don't vary.
544 echo "*** test 2"
545 create_services test15-a test15-x test15-y
546 mkdir -p /etc/systemd/system/test15-a1.service.wants/
547 ln -sf test15-a.service /etc/systemd/system/test15-a1.service
548 ln -sf ../test15-x.service /etc/systemd/system/test15-a.service.wants/
549 ln -sf ../test15-y.service /etc/systemd/system/test15-a1.service.wants/
550 check_ok test15-a1 Wants test15-x.service # see [1]
551 check_ok test15-a1 Wants test15-y.service
552 systemctl start test15-a
553 check_ok test15-a1 Wants test15-x.service # see [2]
554 check_ok test15-a1 Wants test15-y.service
555 systemctl stop test15-a test15-x test15-y
556 rm /etc/systemd/system/test15-a1.service
557
558 clear_units test15-{a,x,y}.service
559 }
560
561 testcase_masked_dropins() {
562 echo "Testing masked dropins..."
563
564 create_services test15-a test15-b
565
566 # 'b' is masked for both deps
567 echo "*** test a wants,requires b is masked"
568 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
569 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b.service
570 check_ko test15-a Wants test15-b.service
571 check_ko test15-a Requires test15-b.service
572
573 # 'a' wants 'b' and 'b' is masked at a lower level
574 echo "*** test a wants b, mask override"
575 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.wants/test15-b.service
576 ln -sf /dev/null /usr/lib/systemd/system/test15-a.service.wants/test15-b.service
577 check_ok test15-a Wants test15-b.service
578
579 # 'a' wants 'b' and 'b' is masked at a higher level
580 echo "*** test a wants b, mask"
581 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
582 ln -sf ../test15-b.service /usr/lib/systemd/system/test15-a.service.wants/test15-b.service
583 check_ko test15-a Wants test15-b.service
584
585 # 'a' is masked but has an override config file
586 echo "*** test a is masked but has an override"
587 create_services test15-a test15-b
588 ln -sf /dev/null /etc/systemd/system/test15-a.service
589 cat >/usr/lib/systemd/system/test15-a.service.d/override.conf <<EOF
590 [Unit]
591 After=test15-b.service
592 EOF
593 check_ok test15-a UnitFileState masked
594
595 # 'b1' is an alias for 'b': masking 'b' dep should not influence 'b1' dep
596 echo "*** test a wants b, b1, and one is masked"
597 create_services test15-a test15-b
598 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
599 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
600 ln -sf ../test15-b1.service /usr/lib/systemd/system/test15-a.service.wants/test15-b1.service
601 systemctl cat test15-a
602 systemctl show -p Wants,Requires test15-a
603 systemctl cat test15-b1
604 systemctl show -p Wants,Requires test15-b1
605 check_ok test15-a Wants test15-b.service
606 check_ko test15-a Wants test15-b1.service # the alias does not show up in the list of units
607 rm /etc/systemd/system/test15-b1.service
608
609 # 'b1' is an alias for 'b': masking 'b1' should not influence 'b' dep
610 echo "*** test a wants b, alias dep is masked"
611 create_services test15-a test15-b
612 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
613 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b1.service
614 ln -sf ../test15-b.service /usr/lib/systemd/system/test15-a.service.wants/test15-b.service
615 check_ok test15-a Wants test15-b.service
616 check_ko test15-a Wants test15-b1.service # the alias does not show up in the list of units
617 rm /etc/systemd/system/test15-b1.service
618
619 # 'a' has Wants=b.service but also has a masking
620 # dropin 'b': 'b' should still be pulled in.
621 echo "*** test a wants b both ways"
622 create_services test15-a test15-b
623 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
624 cat >/usr/lib/systemd/system/test15-a.service.d/wants-b.conf <<EOF
625 [Unit]
626 Wants=test15-b.service
627 EOF
628 check_ok test15-a Wants test15-b.service
629
630 # mask a dropin that points to an nonexistent unit.
631 echo "*** test a wants nonexistent is masked"
632 create_services test15-a
633 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/nonexistent.service
634 ln -sf ../nonexistent.service /usr/lib/systemd/system/test15-a.service.requires/
635 check_ko test15-a Requires nonexistent.service
636
637 # 'b' is already loaded when 'c' pulls it in via a dropin but 'b' is
638 # masked at a higher level.
639 echo "*** test a wants b is masked"
640 create_services test15-a test15-b test15-c
641 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.requires/
642 ln -sf ../test15-b.service /run/systemd/system/test15-c.service.requires/
643 ln -sf /dev/null /etc/systemd/system/test15-c.service.requires/test15-b.service
644 systemctl start test15-a
645 check_ko test15-c Requires test15-b.service
646 systemctl stop test15-a test15-b
647
648 # 'b' is already loaded when 'c' pulls it in via a dropin but 'b' is
649 # masked at a lower level.
650 echo "*** test a requires b is masked"
651 create_services test15-a test15-b test15-c
652 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.requires/
653 ln -sf ../test15-b.service /etc/systemd/system/test15-c.service.requires/
654 ln -sf /dev/null /run/systemd/system/test15-c.service.requires/test15-b.service
655 systemctl start test15-a
656 check_ok test15-c Requires test15-b.service
657 systemctl stop test15-a test15-b
658
659 # 'a' requires 2 aliases of 'b' and one of them is a mask.
660 echo "*** test a requires alias of b, other alias masked"
661 create_services test15-a test15-b
662 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
663 ln -sf test15-b.service /etc/systemd/system/test15-b2.service
664 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b1.service
665 ln -sf ../test15-b1.service /run/systemd/system/test15-a.service.requires/
666 ln -sf ../test15-b2.service /usr/lib/systemd/system/test15-a.service.requires/
667 check_ok test15-a Requires test15-b
668
669 # Same as above but now 'b' is masked.
670 echo "*** test a requires alias of b, b dep masked"
671 create_services test15-a test15-b
672 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
673 ln -sf test15-b.service /etc/systemd/system/test15-b2.service
674 ln -sf ../test15-b1.service /run/systemd/system/test15-a.service.requires/
675 ln -sf ../test15-b2.service /usr/lib/systemd/system/test15-a.service.requires/
676 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b.service
677 check_ok test15-a Requires test15-b
678
679 clear_units test15-{a,b}.service
680 }
681
682 testcase_invalid_dropins() {
683 echo "Testing invalid dropins..."
684 # Assertion failed on earlier versions, command exits unsuccessfully on later versions
685 systemctl cat nonexistent@.service || true
686 create_services a
687 systemctl daemon-reload
688 # Assertion failed on earlier versions, command exits unsuccessfully on later versions
689 systemctl cat a@.service || true
690 systemctl stop a
691 clear_units a.service
692 return 0
693 }
694
695 testcase_symlink_dropin_directory() {
696 # For issue #21920.
697 echo "Testing symlink drop-in directory..."
698 create_services test15-a
699 rmdir /{etc,run,usr/lib}/systemd/system/test15-a.service.d
700 mkdir -p /tmp/testsuite-15-test15-a-dropin-directory
701 ln -s /tmp/testsuite-15-test15-a-dropin-directory /etc/systemd/system/test15-a.service.d
702 cat >/tmp/testsuite-15-test15-a-dropin-directory/override.conf <<EOF
703 [Unit]
704 Description=hogehoge
705 EOF
706 ln -s /tmp/testsuite-15-test15-a-dropin-directory-nonexistent /run/systemd/system/test15-a.service.d
707 touch /tmp/testsuite-15-test15-a-dropin-directory-regular
708 ln -s /tmp/testsuite-15-test15-a-dropin-directory-regular /usr/lib/systemd/system/test15-a.service.d
709 check_ok test15-a Description hogehoge
710
711 clear_units test15-a.service
712 }
713
714 run_testcases
715
716 touch /testok