]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/units/testsuite-15.sh
add ipv6 range element creation test cases
[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_hierarchical_dropins () {
128 echo "Testing hierarchical dropins..."
129 echo "*** test service.d/ top level drop-in"
130 create_services a-b-c
131 check_ko a-b-c ExecCondition "/bin/echo service.d"
132 check_ko a-b-c ExecCondition "/bin/echo a-.service.d"
133 check_ko a-b-c ExecCondition "/bin/echo a-b-.service.d"
134 check_ko a-b-c ExecCondition "/bin/echo a-b-c.service.d"
135
136 for dropin in service.d a-.service.d a-b-.service.d a-b-c.service.d; do
137 mkdir -p /usr/lib/systemd/system/$dropin
138 echo "
139 [Service]
140 ExecCondition=/bin/echo $dropin
141 " > /usr/lib/systemd/system/$dropin/override.conf
142 systemctl daemon-reload
143 check_ok a-b-c ExecCondition "/bin/echo $dropin"
144 done
145 for dropin in service.d a-.service.d a-b-.service.d a-b-c.service.d; do
146 rm -rf /usr/lib/systemd/system/$dropin
147 done
148
149 clear_services a-b-c
150 }
151
152 test_template_dropins () {
153 echo "Testing template dropins..."
154
155 create_services foo bar@ yup@
156
157 # Declare some deps to check if the body was loaded
158 cat >>/etc/systemd/system/bar@.service <<EOF
159 [Unit]
160 After=bar-template-after.device
161 EOF
162
163 cat >>/etc/systemd/system/yup@.service <<EOF
164 [Unit]
165 After=yup-template-after.device
166 EOF
167
168 ln -s /etc/systemd/system/bar@.service /etc/systemd/system/foo.service.wants/bar@1.service
169 check_ok foo Wants bar@1.service
170
171 echo "*** test bar-alias@.service→bar@.service, but instance symlinks point to yup@.service ***"
172 ln -s bar@.service /etc/systemd/system/bar-alias@.service
173 ln -s bar@1.service /etc/systemd/system/bar-alias@1.service
174 ln -s yup@.service /etc/systemd/system/bar-alias@2.service
175 ln -s yup@3.service /etc/systemd/system/bar-alias@3.service
176
177 # create some dropin deps
178 mkdir -p /etc/systemd/system/bar@{,0,1,2,3}.service.requires/
179 mkdir -p /etc/systemd/system/yup@{,0,1,2,3}.service.requires/
180 mkdir -p /etc/systemd/system/bar-alias@{,0,1,2,3}.service.requires/
181
182 ln -s ../bar-template-requires.device /etc/systemd/system/bar@.service.requires/
183 ln -s ../bar-0-requires.device /etc/systemd/system/bar@0.service.requires/
184 ln -s ../bar-1-requires.device /etc/systemd/system/bar@1.service.requires/
185 ln -s ../bar-2-requires.device /etc/systemd/system/bar@2.service.requires/
186 ln -s ../bar-3-requires.device /etc/systemd/system/bar@3.service.requires/
187
188 ln -s ../yup-template-requires.device /etc/systemd/system/yup@.service.requires/
189 ln -s ../yup-0-requires.device /etc/systemd/system/yup@0.service.requires/
190 ln -s ../yup-1-requires.device /etc/systemd/system/yup@1.service.requires/
191 ln -s ../yup-2-requires.device /etc/systemd/system/yup@2.service.requires/
192 ln -s ../yup-3-requires.device /etc/systemd/system/yup@3.service.requires/
193
194 ln -s ../bar-alias-template-requires.device /etc/systemd/system/bar-alias@.service.requires/
195 ln -s ../bar-alias-0-requires.device /etc/systemd/system/bar-alias@0.service.requires/
196 ln -s ../bar-alias-1-requires.device /etc/systemd/system/bar-alias@1.service.requires/
197 ln -s ../bar-alias-2-requires.device /etc/systemd/system/bar-alias@2.service.requires/
198 ln -s ../bar-alias-3-requires.device /etc/systemd/system/bar-alias@3.service.requires/
199
200 systemctl daemon-reload
201
202 echo '*** bar@0 is aliased by bar-alias@0 ***'
203 systemctl show -p Names,Requires bar@0
204 systemctl show -p Names,Requires bar-alias@0
205 check_ok bar@0 Names bar@0
206 check_ok bar@0 Names bar-alias@0
207
208 check_ok bar@0 After bar-template-after.device
209
210 check_ok bar@0 Requires bar-0-requires.device
211 check_ok bar@0 Requires bar-alias-0-requires.device
212 check_ok bar@0 Requires bar-template-requires.device
213 check_ok bar@0 Requires bar-alias-template-requires.device
214 check_ko bar@0 Requires yup-template-requires.device
215
216 check_ok bar-alias@0 After bar-template-after.device
217
218 check_ok bar-alias@0 Requires bar-0-requires.device
219 check_ok bar-alias@0 Requires bar-alias-0-requires.device
220 check_ok bar-alias@0 Requires bar-template-requires.device
221 check_ok bar-alias@0 Requires bar-alias-template-requires.device
222 check_ko bar-alias@0 Requires yup-template-requires.device
223 check_ko bar-alias@0 Requires yup-0-requires.device
224
225 echo '*** bar@1 is aliased by bar-alias@1 ***'
226 systemctl show -p Names,Requires bar@1
227 systemctl show -p Names,Requires bar-alias@1
228 check_ok bar@1 Names bar@1
229 check_ok bar@1 Names bar-alias@1
230
231 check_ok bar@1 After bar-template-after.device
232
233 check_ok bar@1 Requires bar-1-requires.device
234 check_ok bar@1 Requires bar-alias-1-requires.device
235 check_ok bar@1 Requires bar-template-requires.device
236 # See https://github.com/systemd/systemd/pull/13119#discussion_r308145418
237 check_ok bar@1 Requires bar-alias-template-requires.device
238 check_ko bar@1 Requires yup-template-requires.device
239 check_ko bar@1 Requires yup-1-requires.device
240
241 check_ok bar-alias@1 After bar-template-after.device
242
243 check_ok bar-alias@1 Requires bar-1-requires.device
244 check_ok bar-alias@1 Requires bar-alias-1-requires.device
245 check_ok bar-alias@1 Requires bar-template-requires.device
246 check_ok bar-alias@1 Requires bar-alias-template-requires.device
247 check_ko bar-alias@1 Requires yup-template-requires.device
248 check_ko bar-alias@1 Requires yup-1-requires.device
249
250 echo '*** bar-alias@2 aliases yup@2, bar@2 is independent ***'
251 systemctl show -p Names,Requires bar@2
252 systemctl show -p Names,Requires bar-alias@2
253 check_ok bar@2 Names bar@2
254 check_ko bar@2 Names bar-alias@2
255
256 check_ok bar@2 After bar-template-after.device
257
258 check_ok bar@2 Requires bar-2-requires.device
259 check_ko bar@2 Requires bar-alias-2-requires.device
260 check_ok bar@2 Requires bar-template-requires.device
261 check_ko bar@2 Requires bar-alias-template-requires.device
262 check_ko bar@2 Requires yup-template-requires.device
263 check_ko bar@2 Requires yup-2-requires.device
264
265 check_ko bar-alias@2 After bar-template-after.device
266
267 check_ko bar-alias@2 Requires bar-2-requires.device
268 check_ok bar-alias@2 Requires bar-alias-2-requires.device
269 check_ko bar-alias@2 Requires bar-template-requires.device
270 check_ok bar-alias@2 Requires bar-alias-template-requires.device
271 check_ok bar-alias@2 Requires yup-template-requires.device
272 check_ok bar-alias@2 Requires yup-2-requires.device
273
274 echo '*** bar-alias@3 aliases yup@3, bar@3 is independent ***'
275 systemctl show -p Names,Requires bar@3
276 systemctl show -p Names,Requires bar-alias@3
277 check_ok bar@3 Names bar@3
278 check_ko bar@3 Names bar-alias@3
279
280 check_ok bar@3 After bar-template-after.device
281
282 check_ok bar@3 Requires bar-3-requires.device
283 check_ko bar@3 Requires bar-alias-3-requires.device
284 check_ok bar@3 Requires bar-template-requires.device
285 check_ko bar@3 Requires bar-alias-template-requires.device
286 check_ko bar@3 Requires yup-template-requires.device
287 check_ko bar@3 Requires yup-3-requires.device
288
289 check_ko bar-alias@3 After bar-template-after.device
290
291 check_ko bar-alias@3 Requires bar-3-requires.device
292 check_ok bar-alias@3 Requires bar-alias-3-requires.device
293 check_ko bar-alias@3 Requires bar-template-requires.device
294 check_ok bar-alias@3 Requires bar-alias-template-requires.device
295 check_ok bar-alias@3 Requires yup-template-requires.device
296 check_ok bar-alias@3 Requires yup-3-requires.device
297
298 clear_services foo {bar,yup,bar-alias}@{,1,2,3}
299 }
300
301 test_alias_dropins () {
302 echo "Testing alias dropins..."
303
304 echo "*** test a wants b1 alias of b"
305 create_services test15-a test15-b
306 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
307 ln -sf ../test15-b1.service /etc/systemd/system/test15-a.service.wants/
308 check_ok test15-a Wants test15-b.service
309 systemctl start test15-a
310 systemctl --quiet is-active test15-b
311 systemctl stop test15-a test15-b
312 rm /etc/systemd/system/test15-b1.service
313 clear_services test15-a test15-b
314
315 # Check that dependencies don't vary.
316 echo "*** test 2"
317 create_services test15-a test15-x test15-y
318 mkdir -p /etc/systemd/system/test15-a1.service.wants/
319 ln -sf test15-a.service /etc/systemd/system/test15-a1.service
320 ln -sf ../test15-x.service /etc/systemd/system/test15-a.service.wants/
321 ln -sf ../test15-y.service /etc/systemd/system/test15-a1.service.wants/
322 check_ok test15-a1 Wants test15-x.service # see [1]
323 check_ok test15-a1 Wants test15-y.service
324 systemctl start test15-a
325 check_ok test15-a1 Wants test15-x.service # see [2]
326 check_ok test15-a1 Wants test15-y.service
327 systemctl stop test15-a test15-x test15-y
328 rm /etc/systemd/system/test15-a1.service
329
330 clear_services test15-a test15-x test15-y
331 }
332
333 test_masked_dropins () {
334 echo "Testing masked dropins..."
335
336 create_services test15-a test15-b
337
338 # 'b' is masked for both deps
339 echo "*** test a wants,requires b is masked"
340 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
341 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b.service
342 check_ko test15-a Wants test15-b.service
343 check_ko test15-a Requires test15-b.service
344
345 # 'a' wants 'b' and 'b' is masked at a lower level
346 echo "*** test a wants b, mask override"
347 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.wants/test15-b.service
348 ln -sf /dev/null /usr/lib/systemd/system/test15-a.service.wants/test15-b.service
349 check_ok test15-a Wants test15-b.service
350
351 # 'a' wants 'b' and 'b' is masked at a higher level
352 echo "*** test a wants b, mask"
353 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
354 ln -sf ../test15-b.service /usr/lib/systemd/system/test15-a.service.wants/test15-b.service
355 check_ko test15-a Wants test15-b.service
356
357 # 'a' is masked but has an override config file
358 echo "*** test a is masked but has an override"
359 create_services test15-a test15-b
360 ln -sf /dev/null /etc/systemd/system/test15-a.service
361 cat >/usr/lib/systemd/system/test15-a.service.d/override.conf <<EOF
362 [Unit]
363 After=test15-b.service
364 EOF
365 check_ok test15-a UnitFileState masked
366
367 # 'b1' is an alias for 'b': masking 'b' dep should not influence 'b1' dep
368 echo "*** test a wants b, b1, and one is masked"
369 create_services test15-a test15-b
370 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
371 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
372 ln -sf ../test15-b1.service /usr/lib/systemd/system/test15-a.service.wants/test15-b1.service
373 systemctl cat test15-a
374 systemctl show -p Wants,Requires test15-a
375 systemctl cat test15-b1
376 systemctl show -p Wants,Requires test15-b1
377 check_ok test15-a Wants test15-b.service
378 check_ko test15-a Wants test15-b1.service # the alias does not show up in the list of units
379 rm /etc/systemd/system/test15-b1.service
380
381 # 'b1' is an alias for 'b': masking 'b1' should not influence 'b' dep
382 echo "*** test a wants b, alias dep is masked"
383 create_services test15-a test15-b
384 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
385 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b1.service
386 ln -sf ../test15-b.service /usr/lib/systemd/system/test15-a.service.wants/test15-b.service
387 check_ok test15-a Wants test15-b.service
388 check_ko test15-a Wants test15-b1.service # the alias does not show up in the list of units
389 rm /etc/systemd/system/test15-b1.service
390
391 # 'a' has Wants=b.service but also has a masking
392 # dropin 'b': 'b' should still be pulled in.
393 echo "*** test a wants b both ways"
394 create_services test15-a test15-b
395 ln -sf /dev/null /etc/systemd/system/test15-a.service.wants/test15-b.service
396 cat >/usr/lib/systemd/system/test15-a.service.d/wants-b.conf<<EOF
397 [Unit]
398 Wants=test15-b.service
399 EOF
400 check_ok test15-a Wants test15-b.service
401
402 # mask a dropin that points to an nonexistent unit.
403 echo "*** test a wants nonexistent is masked"
404 create_services test15-a
405 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/nonexistent.service
406 ln -sf ../nonexistent.service /usr/lib/systemd/system/test15-a.service.requires/
407 check_ko test15-a Requires nonexistent.service
408
409 # 'b' is already loaded when 'c' pulls it in via a dropin but 'b' is
410 # masked at a higher level.
411 echo "*** test a wants b is masked"
412 create_services test15-a test15-b test15-c
413 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.requires/
414 ln -sf ../test15-b.service /run/systemd/system/test15-c.service.requires/
415 ln -sf /dev/null /etc/systemd/system/test15-c.service.requires/test15-b.service
416 systemctl start test15-a
417 check_ko test15-c Requires test15-b.service
418 systemctl stop test15-a test15-b
419
420 # 'b' is already loaded when 'c' pulls it in via a dropin but 'b' is
421 # masked at a lower level.
422 echo "*** test a requires b is masked"
423 create_services test15-a test15-b test15-c
424 ln -sf ../test15-b.service /etc/systemd/system/test15-a.service.requires/
425 ln -sf ../test15-b.service /etc/systemd/system/test15-c.service.requires/
426 ln -sf /dev/null /run/systemd/system/test15-c.service.requires/test15-b.service
427 systemctl start test15-a
428 check_ok test15-c Requires test15-b.service
429 systemctl stop test15-a test15-b
430
431 # 'a' requires 2 aliases of 'b' and one of them is a mask.
432 echo "*** test a requires alias of b, other alias masked"
433 create_services test15-a test15-b
434 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
435 ln -sf test15-b.service /etc/systemd/system/test15-b2.service
436 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b1.service
437 ln -sf ../test15-b1.service /run/systemd/system/test15-a.service.requires/
438 ln -sf ../test15-b2.service /usr/lib/systemd/system/test15-a.service.requires/
439 check_ok test15-a Requires test15-b
440
441 # Same as above but now 'b' is masked.
442 echo "*** test a requires alias of b, b dep masked"
443 create_services test15-a test15-b
444 ln -sf test15-b.service /etc/systemd/system/test15-b1.service
445 ln -sf test15-b.service /etc/systemd/system/test15-b2.service
446 ln -sf ../test15-b1.service /run/systemd/system/test15-a.service.requires/
447 ln -sf ../test15-b2.service /usr/lib/systemd/system/test15-a.service.requires/
448 ln -sf /dev/null /etc/systemd/system/test15-a.service.requires/test15-b.service
449 check_ok test15-a Requires test15-b
450
451 clear_services test15-a test15-b
452 }
453
454 test_invalid_dropins () {
455 echo "Testing invalid dropins..."
456 # Assertion failed on earlier versions, command exits unsuccessfully on later versions
457 systemctl cat nonexistent@.service || true
458 create_services a
459 systemctl daemon-reload
460 # Assertion failed on earlier versions, command exits unsuccessfully on later versions
461 systemctl cat a@.service || true
462 systemctl stop a
463 clear_services a
464 return 0
465 }
466
467 test_basic_dropins
468 test_hierarchical_dropins
469 test_template_dropins
470 test_alias_dropins
471 test_masked_dropins
472 test_invalid_dropins
473
474 touch /testok