1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -ex
4
5# Silence warning from running_in_chroot_or_offline()
6export SYSTEMD_IGNORE_CHROOT=1
7
8systemctl=${1:-systemctl}
9systemd_id128=${2:-systemd-id128}
10
11unset root
12cleanup() {
13    [ -n "$root" ] && rm -rf "$root"
14}
15trap cleanup exit
16root=$(mktemp -d --tmpdir systemctl-test.XXXXXX)
17
18islink() {
19    test -h "$1" || return 1
20    test "$(readlink "$1")" = "$2" || return 2
21}
22
23: '------enable nonexistent------------------------------------'
24( ! "$systemctl" --root="$root" enable test1.service )
25
26: '------basic enablement--------------------------------------'
27mkdir -p "$root/etc/systemd/system"
28cat >"$root/etc/systemd/system/test1.service" <<EOF
29[Install]
30WantedBy=default.target
31RequiredBy=special.target
32EOF
33
34"$systemctl" --root="$root" enable test1.service
35test -h "$root/etc/systemd/system/default.target.wants/test1.service"
36test -h "$root/etc/systemd/system/special.target.requires/test1.service"
37
38"$systemctl" --root="$root" reenable test1.service
39test -h "$root/etc/systemd/system/default.target.wants/test1.service"
40test -h "$root/etc/systemd/system/special.target.requires/test1.service"
41
42"$systemctl" --root="$root" disable test1.service
43test ! -h "$root/etc/systemd/system/default.target.wants/test1.service"
44test ! -h "$root/etc/systemd/system/special.target.requires/test1.service"
45
46: '------enable when link already exists-----------------------'
47# We don't read the symlink target, so it's OK for the symlink to point
48# to something else. We should just silently accept this.
49
50mkdir -p "$root/etc/systemd/system/default.target.wants"
51mkdir -p "$root/etc/systemd/system/special.target.requires"
52ln -s /usr/lib/systemd/system/test1.service "$root/etc/systemd/system/default.target.wants/test1.service"
53ln -s /usr/lib/systemd/system/test1.service "$root/etc/systemd/system/special.target.requires/test1.service"
54
55"$systemctl" --root="$root" enable test1.service
56test -h "$root/etc/systemd/system/default.target.wants/test1.service"
57test -h "$root/etc/systemd/system/special.target.requires/test1.service"
58
59"$systemctl" --root="$root" reenable test1.service
60test -h "$root/etc/systemd/system/default.target.wants/test1.service"
61test -h "$root/etc/systemd/system/special.target.requires/test1.service"
62
63"$systemctl" --root="$root" disable test1.service
64test ! -h "$root/etc/systemd/system/default.target.wants/test1.service"
65test ! -h "$root/etc/systemd/system/special.target.requires/test1.service"
66
67: '------suffix guessing---------------------------------------'
68"$systemctl" --root="$root" enable test1
69test -h "$root/etc/systemd/system/default.target.wants/test1.service"
70test -h "$root/etc/systemd/system/special.target.requires/test1.service"
71
72"$systemctl" --root="$root" reenable test1
73test -h "$root/etc/systemd/system/default.target.wants/test1.service"
74test -h "$root/etc/systemd/system/special.target.requires/test1.service"
75
76"$systemctl" --root="$root" disable test1
77test ! -e "$root/etc/systemd/system/default.target.wants/test1.service"
78test ! -e "$root/etc/systemd/system/special.target.requires/test1.service"
79
80: '-------aliases----------------------------------------------'
81cat >>"$root/etc/systemd/system/test1.service" <<EOF
82Alias=test1-goodalias.service
83Alias=test1@badalias.service
84Alias=test1-badalias.target
85Alias=test1-badalias.socket
86# we have a series of good, bad, and then good again
87Alias=test1-goodalias2.service
88EOF
89
90( ! "$systemctl" --root="$root" enable test1 )
91test -h "$root/etc/systemd/system/default.target.wants/test1.service"
92test -h "$root/etc/systemd/system/special.target.requires/test1.service"
93test ! -e "$root/etc/systemd/system/test1-goodalias.service"
94test -h "$root/etc/systemd/system/test1-goodalias.service"
95test ! -e "$root/etc/systemd/system/test1@badalias.service"
96test ! -e "$root/etc/systemd/system/test1-badalias.target"
97test ! -e "$root/etc/systemd/system/test1-badalias.socket"
98test -h "$root/etc/systemd/system/test1-goodalias2.service"
99
100: '-------aliases in reeanble----------------------------------'
101( ! "$systemctl" --root="$root" reenable test1 )
102test -h "$root/etc/systemd/system/default.target.wants/test1.service"
103test ! -e "$root/etc/systemd/system/test1-goodalias.service"
104test -h "$root/etc/systemd/system/test1-goodalias.service"
105
106test ! -e "$root/etc/systemd/system/test1@badalias.service"
107test ! -e "$root/etc/systemd/system/test1-badalias.target"
108test ! -e "$root/etc/systemd/system/test1-badalias.socket"
109
110"$systemctl" --root="$root" disable test1
111test ! -e "$root/etc/systemd/system/default.target.wants/test1.service"
112test ! -e "$root/etc/systemd/system/special.target.requires/test1.service"
113test ! -e "$root/etc/systemd/system/test1-goodalias.service"
114
115: '-------aliases when link already exists---------------------'
116cat >"$root/etc/systemd/system/test1a.service" <<EOF
117[Install]
118Alias=test1a-alias.service
119EOF
120
121ln -s /usr/lib/systemd/system/test1a.service "$root/etc/systemd/system/test1a-alias.service"
122
123"$systemctl" --root="$root" enable test1a.service
124test -h "$root/etc/systemd/system/test1a-alias.service"
125
126"$systemctl" --root="$root" disable test1a.service
127test ! -h "$root/etc/systemd/system/test1a-alias.service"
128
129: '-------also units-------------------------------------------'
130cat >"$root/etc/systemd/system/test2.socket" <<EOF
131[Install]
132WantedBy=sockets.target
133Also=test2.service
134EOF
135
136cat >"$root/etc/systemd/system/test2.service" <<EOF
137[Install]
138WantedBy=default.target
139Also=test2.socket
140EOF
141
142"$systemctl" --root="$root" reenable test2.service
143test -h "$root/etc/systemd/system/default.target.wants/test2.service"
144test -h "$root/etc/systemd/system/sockets.target.wants/test2.socket"
145
146"$systemctl" --root="$root" reenable test2.socket
147test -h "$root/etc/systemd/system/default.target.wants/test2.service"
148test -h "$root/etc/systemd/system/sockets.target.wants/test2.socket"
149
150"$systemctl" --root="$root" disable test2.socket
151test ! -e "$root/etc/systemd/system/default.target.wants/test2.service"
152test ! -e "$root/etc/systemd/system/sockets.target.wants/test2.socket"
153
154
155: '-------link-------------------------------------------------'
156# File doesn't exist yet
157test ! -e "$root/link1.path"
158( ! "$systemctl" --root="$root" link '/link1.path' )
159test ! -e "$root/etc/systemd/system/link1.path"
160
161cat >"$root/link1.path" <<EOF
162[Install]
163WantedBy=paths.target
164EOF
165
166"$systemctl" --root="$root" link '/link1.path'
167islink "$root/etc/systemd/system/link1.path" "/link1.path"
168
169: '-------link already linked same path------------------------'
170SYSTEMD_LOG_LEVEL=debug "$systemctl" --root="$root" link '/link1.path'  # this passes
171islink "$root/etc/systemd/system/link1.path" "/link1.path"
172
173: '-------link already linked different path-------------------'
174mkdir "$root/subdir"
175cp "$root/link1.path" "$root/subdir/"
176( ! "$systemctl" --root="$root" link '/subdir/link1.path' )
177islink "$root/etc/systemd/system/link1.path" "/link1.path"
178
179: '-------link bad suffix--------------------------------------'
180cp "$root/link1.path" "$root/subdir/link1.suffix"
181( ! "$systemctl" --root="$root" link '/subdir/link1.suffix' )
182test ! -e "$root/etc/systemd/system/link1.suffix"
183
184: '-------unlink by unit name----------------------------------'
185"$systemctl" --root="$root" disable 'link1.path'
186test ! -e "$root/etc/systemd/system/link1.path"
187
188: '-------unlink by path---------------------------------------'
189"$systemctl" --root="$root" link '/link1.path'
190test -h "$root/etc/systemd/system/link1.path"
191"$systemctl" --root="$root" disable '/link1.path'
192test ! -e "$root/etc/systemd/system/link1.path"
193
194: '-------unlink by wrong path---------------------------------'
195"$systemctl" --root="$root" link '/link1.path'
196test -h "$root/etc/systemd/system/link1.path"
197"$systemctl" --root="$root" disable '/subdir/link1.path'  # we only care about the name
198test ! -e "$root/etc/systemd/system/link1.path"
199
200
201: '-------link and enable--------------------------------------'
202"$systemctl" --root="$root" enable '/link1.path'
203islink "$root/etc/systemd/system/link1.path" "/link1.path"
204islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
205
206: '-------enable already linked same path----------------------'
207"$systemctl" --root="$root" enable '/link1.path'
208islink "$root/etc/systemd/system/link1.path" "/link1.path"
209islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
210
211: '-------enable already linked different path-----------------'
212( ! "$systemctl" --root="$root" enable '/subdir/link1.path' )
213islink "$root/etc/systemd/system/link1.path" "/link1.path"
214islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
215
216: '-------enable bad suffix------------------------------------'
217cp "$root/link1.path" "$root/subdir/link1.suffix"
218( ! "$systemctl" --root="$root" enable '/subdir/link1.suffix' )
219test ! -e "$root/etc/systemd/system/link1.suffix"
220test ! -e "$root/etc/systemd/system/paths.target.wants/link1.suffix"
221
222: '-------disable by unit name---------------------------------'
223"$systemctl" --root="$root" disable 'link1.path'
224test ! -e "$root/etc/systemd/system/link1.path"
225test ! -e "$root/etc/systemd/system/paths.target.wants/link1.path"
226
227: '-------disable by path--------------------------------------'
228"$systemctl" --root="$root" enable '/link1.path'
229test -h "$root/etc/systemd/system/link1.path"
230test -h "$root/etc/systemd/system/paths.target.wants/link1.path"
231"$systemctl" --root="$root" disable '/link1.path'
232test ! -e "$root/etc/systemd/system/link1.path"
233test ! -e "$root/etc/systemd/system/paths.target.wants/link1.path"
234
235
236: '-------link and enable-------------------------------------'
237"$systemctl" --root="$root" link '/link1.path'
238islink "$root/etc/systemd/system/link1.path" "/link1.path"
239test ! -h "$root/etc/systemd/system/paths.target.wants/link1.path"
240
241"$systemctl" --root="$root" enable 'link1.path'
242islink "$root/etc/systemd/system/link1.path" "/link1.path"
243islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
244
245"$systemctl" --root="$root" reenable 'link1.path'
246islink "$root/etc/systemd/system/link1.path" "/link1.path"
247islink "$root/etc/systemd/system/paths.target.wants/link1.path" "/link1.path"
248
249: '-------manual link------------------------------------------'
250cat >"$root/link3.suffix" <<EOF
251[Install]
252WantedBy=services.target
253EOF
254
255# We wouldn't create such a link ourselves, but it should accept it when present.
256ln -s "/link3.suffix" "$root/etc/systemd/system/link3.service"
257
258SYSTEMD_LOG_LEVEL=debug SYSTEMD_LOG_LOCATION=1 "$systemctl" --root="$root" enable 'link3.service'
259islink "$root/etc/systemd/system/link3.service" "/link3.suffix"
260islink "$root/etc/systemd/system/services.target.wants/link3.service" "/link3.suffix"
261
262SYSTEMD_LOG_LEVEL=debug SYSTEMD_LOG_LOCATION=1 "$systemctl" --root="$root" disable 'link3.service'
263test ! -h "$root/etc/systemd/system/link3.service"
264test ! -h "$root/etc/systemd/system/services.target.wants/link3.service"
265
266: '-------enable on masked-------------------------------------'
267ln -s "/dev/null" "$root/etc/systemd/system/masked.service"
268( ! "$systemctl" --root="$root" enable 'masked.service' )
269( ! "$systemctl" --root="$root" enable '/etc/systemd/system/masked.service' )
270
271: '-------enable on masked alias-------------------------------'
272test -h "$root/etc/systemd/system/masked.service"
273ln -s "masked.service" "$root/etc/systemd/system/masked-alias.service"
274( ! "$systemctl" --root="$root" enable 'masked-alias.service' )
275( ! "$systemctl" --root="$root" enable '/etc/systemd/system/masked-alias.service' )
276
277: '-------issue 22000: link in subdirectory--------------------'
278mkdir -p "$root/etc/systemd/system/myown.d"
279cat >"$root/etc/systemd/system/link5-also.service" <<EOF
280[Install]
281WantedBy=services.target
282Also=link5.service
283EOF
284cat >"$root/etc/systemd/system/myown.d/link5.service" <<EOF
285[Install]
286WantedBy=services.target
287Also=link5-also.service
288EOF
289
290( ! "$systemctl" --root="$root" enable 'link5.service' )
291test ! -h "$root/etc/systemd/system/services.target.wants/link5.service"
292test ! -h "$root/etc/systemd/system/services.target.wants/link5-also.service"
293
294"$systemctl" --root="$root" enable 'link5-also.service'
295test ! -h "$root/etc/systemd/system/services.target.wants/link5.service"
296islink "$root/etc/systemd/system/services.target.wants/link5-also.service" "/etc/systemd/system/link5-also.service"
297
298: '-------template enablement----------------------------------'
299cat >"$root/etc/systemd/system/templ1@.service" <<EOF
300[Install]
301WantedBy=services.target
302EOF
303
304# No instance here — this can't succeed.
305( ! "$systemctl" --root="$root" enable 'templ1@.service' )
306test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
307
308"$systemctl" --root="$root" enable 'templ1@one.service'
309test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
310islink "$root/etc/systemd/system/services.target.wants/templ1@one.service" "/etc/systemd/system/templ1@.service"
311
312"$systemctl" --root="$root" enable 'templ1@two.service'
313test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
314islink "$root/etc/systemd/system/services.target.wants/templ1@one.service" "/etc/systemd/system/templ1@.service"
315islink "$root/etc/systemd/system/services.target.wants/templ1@two.service" "/etc/systemd/system/templ1@.service"
316
317"$systemctl" --root="$root" disable 'templ1@one.service'
318test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
319test ! -h "$root/etc/systemd/system/services.target.wants/templ1@one.service"
320islink "$root/etc/systemd/system/services.target.wants/templ1@two.service" "/etc/systemd/system/templ1@.service"
321
322"$systemctl" --root="$root" disable 'templ1@two.service'
323test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
324test ! -h "$root/etc/systemd/system/services.target.wants/templ1@one.service"
325test ! -h "$root/etc/systemd/system/services.target.wants/templ1@two.service"
326
327: '-------template enablement w/ default instance--------------'
328cat >"$root/etc/systemd/system/templ1@.service" <<EOF
329[Install]
330# check enablement with
331WantedBy=services.target services.target
332RequiredBy=other@templ1.target other@%p.target
333DefaultInstance=333
334EOF
335
336"$systemctl" --root="$root" enable 'templ1@.service'
337test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
338islink "$root/etc/systemd/system/services.target.wants/templ1@333.service" "/etc/systemd/system/templ1@.service"
339islink "$root/etc/systemd/system/other@templ1.target.requires/templ1@333.service" "/etc/systemd/system/templ1@.service"
340
341"$systemctl" --root="$root" enable 'templ1@one.service'
342test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
343islink "$root/etc/systemd/system/services.target.wants/templ1@333.service" "/etc/systemd/system/templ1@.service"
344islink "$root/etc/systemd/system/other@templ1.target.requires/templ1@333.service" "/etc/systemd/system/templ1@.service"
345islink "$root/etc/systemd/system/services.target.wants/templ1@one.service" "/etc/systemd/system/templ1@.service"
346islink "$root/etc/systemd/system/other@templ1.target.requires/templ1@one.service" "/etc/systemd/system/templ1@.service"
347
348"$systemctl" --root="$root" enable 'templ1@two.service'
349test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
350islink "$root/etc/systemd/system/services.target.wants/templ1@333.service" "/etc/systemd/system/templ1@.service"
351islink "$root/etc/systemd/system/other@templ1.target.requires/templ1@333.service" "/etc/systemd/system/templ1@.service"
352islink "$root/etc/systemd/system/services.target.wants/templ1@one.service" "/etc/systemd/system/templ1@.service"
353islink "$root/etc/systemd/system/other@templ1.target.requires/templ1@one.service" "/etc/systemd/system/templ1@.service"
354islink "$root/etc/systemd/system/services.target.wants/templ1@two.service" "/etc/systemd/system/templ1@.service"
355islink "$root/etc/systemd/system/other@templ1.target.requires/templ1@two.service" "/etc/systemd/system/templ1@.service"
356
357"$systemctl" --root="$root" disable 'templ1@one.service'
358test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
359islink "$root/etc/systemd/system/services.target.wants/templ1@333.service" "/etc/systemd/system/templ1@.service"
360islink "$root/etc/systemd/system/other@templ1.target.requires/templ1@333.service" "/etc/systemd/system/templ1@.service"
361test ! -h "$root/etc/systemd/system/services.target.wants/templ1@one.service"
362test ! -h "$root/etc/systemd/system/other@templ1.target.requires/templ1@one.service"
363islink "$root/etc/systemd/system/services.target.wants/templ1@two.service" "/etc/systemd/system/templ1@.service"
364islink "$root/etc/systemd/system/other@templ1.target.requires/templ1@two.service" "/etc/systemd/system/templ1@.service"
365
366# disable remaining links here
367"$systemctl" --root="$root" disable 'templ1@.service'
368test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
369test ! -h "$root/etc/systemd/system/services.target.wants/templ1@333.service"
370test ! -h "$root/etc/systemd/system/other@templ1.target.requires/templ1@333.service"
371test ! -h "$root/etc/systemd/system/services.target.wants/templ1@one.service"
372test ! -h "$root/etc/systemd/system/other@templ1.target.requires/templ1@one.service"
373test ! -h "$root/etc/systemd/system/services.target.wants/templ1@two.service"
374test ! -h "$root/etc/systemd/system/other@templ1.target.requires/templ1@two.service"
375
376: '-------removal of relative enablement symlinks--------------'
377test ! -h "$root/etc/systemd/system/services.target.wants/templ1@.service"
378ln -s '../templ1@one.service' "$root/etc/systemd/system/services.target.wants/templ1@one.service"
379ln -s 'templ1@two.service' "$root/etc/systemd/system/services.target.wants/templ1@two.service"
380ln -s '../templ1@.service' "$root/etc/systemd/system/services.target.wants/templ1@three.service"
381ln -s 'templ1@.service' "$root/etc/systemd/system/services.target.wants/templ1@four.service"
382ln -s '/usr/lib/systemd/system/templ1@.service' "$root/etc/systemd/system/services.target.wants/templ1@five.service"
383ln -s '/etc/systemd/system/templ1@.service' "$root/etc/systemd/system/services.target.wants/templ1@six.service"
384ln -s '/run/system/templ1@.service' "$root/etc/systemd/system/services.target.wants/templ1@seven.service"
385
386# this should remove all links
387"$systemctl" --root="$root" disable 'templ1@.service'
388test ! -h "$root/etc/systemd/system/services.target.wants/templ1@one.service"
389test ! -h "$root/etc/systemd/system/services.target.wants/templ1@two.service"
390test ! -h "$root/etc/systemd/system/services.target.wants/templ1@three.service"
391test ! -h "$root/etc/systemd/system/services.target.wants/templ1@four.service"
392test ! -h "$root/etc/systemd/system/services.target.wants/templ1@five.service"
393test ! -h "$root/etc/systemd/system/services.target.wants/templ1@six.service"
394test ! -h "$root/etc/systemd/system/services.target.wants/templ1@seven.service"
395
396: '-------template enablement for another template-------------'
397cat >"$root/etc/systemd/system/templ2@.service" <<EOF
398[Install]
399RequiredBy=another-template@.target
400EOF
401
402"$systemctl" --root="$root" enable 'templ2@.service'
403islink "$root/etc/systemd/system/another-template@.target.requires/templ2@.service" "/etc/systemd/system/templ2@.service"
404
405"$systemctl" --root="$root" enable 'templ2@two.service'
406islink "$root/etc/systemd/system/another-template@.target.requires/templ2@.service" "/etc/systemd/system/templ2@.service"
407islink "$root/etc/systemd/system/another-template@.target.requires/templ2@two.service" "/etc/systemd/system/templ2@.service"
408
409"$systemctl" --root="$root" disable 'templ2@other.service'
410islink "$root/etc/systemd/system/another-template@.target.requires/templ2@.service" "/etc/systemd/system/templ2@.service"
411islink "$root/etc/systemd/system/another-template@.target.requires/templ2@two.service" "/etc/systemd/system/templ2@.service"
412
413"$systemctl" --root="$root" disable 'templ2@two.service'
414islink "$root/etc/systemd/system/another-template@.target.requires/templ2@.service" "/etc/systemd/system/templ2@.service"
415test ! -h "$root/etc/systemd/system/another-template@.target.requires/templ2@two.service"
416
417"$systemctl" --root="$root" disable 'templ2@.service'
418test ! -h "$root/etc/systemd/system/another-template@.target.requires/templ2@.service"
419test ! -h "$root/etc/systemd/system/another-template@.target.requires/templ2@two.service"
420
421: '-------aliases w/ and w/o instance--------------------------'
422test ! -e "$root/etc/systemd/system/link4.service"
423cat >"$root/etc/systemd/system/link4.service" <<EOF
424[Install]
425Alias=link4.service
426Alias=link4@.service
427Alias=link4@inst.service
428Alias=link4alias.service
429Alias=link4alias2.service
430EOF
431
432( ! "$systemctl" --root="$root" enable 'link4.service' )
433test ! -h "$root/etc/systemd/system/link4.service"  # this is our file
434test ! -h "$root/etc/systemd/system/link4@.service"
435test ! -h "$root/etc/systemd/system/link4@inst.service"
436islink "$root/etc/systemd/system/link4alias.service" "/etc/systemd/system/link4.service"
437islink "$root/etc/systemd/system/link4alias2.service" "/etc/systemd/system/link4.service"
438
439"$systemctl" --root="$root" disable 'link4.service'
440test ! -h "$root/etc/systemd/system/link4.service"
441test ! -h "$root/etc/systemd/system/link4@.service"
442test ! -h "$root/etc/systemd/system/link4@inst.service"
443test ! -h "$root/etc/systemd/system/link4alias.service"
444test ! -h "$root/etc/systemd/system/link4alias2.service"
445
446: '-------systemctl enable on path to unit file----------------'
447cat >"$root/etc/systemd/system/link4.service" <<EOF
448[Install]
449Alias=link4alias.service
450Alias=link4alias2.service
451EOF
452
453# Apparently this works. I'm not sure what to think.
454"$systemctl" --root="$root" enable '/etc/systemd/system/link4.service'
455test ! -h "$root/etc/systemd/system/link4.service"  # this is our file
456islink "$root/etc/systemd/system/link4alias.service" "/etc/systemd/system/link4.service"
457islink "$root/etc/systemd/system/link4alias2.service" "/etc/systemd/system/link4.service"
458
459"$systemctl" --root="$root" disable '/etc/systemd/system/link4.service'
460test ! -h "$root/etc/systemd/system/link4.service"
461test ! -h "$root/etc/systemd/system/link4alias.service"
462test ! -h "$root/etc/systemd/system/link4alias2.service"
463
464: '-------issue 661: enable on unit file--------------'
465test ! -e "$root/etc/systemd/system/link5.service"
466cat >"$root/etc/systemd/system/link5.service" <<EOF
467[Install]
468Alias=link5.service
469Alias=link5alias.service
470Alias=link5alias2.service
471EOF
472
473"$systemctl" --root="$root" enable 'link5.service'
474test ! -h "$root/etc/systemd/system/link5.service"  # this is our file
475islink "$root/etc/systemd/system/link5alias.service" "/etc/systemd/system/link5.service"
476islink "$root/etc/systemd/system/link5alias2.service" "/etc/systemd/system/link5.service"
477
478"$systemctl" --root="$root" disable 'link5.service'
479test ! -h "$root/etc/systemd/system/link5alias.service"
480test ! -h "$root/etc/systemd/system/link5alias2.service"
481
482: '-------issue 661: link and enable on unit file--------------'
483test ! -e "$root/etc/systemd/system/link5copy.service"
484cat >"$root/link5copy.service" <<EOF
485[Install]
486Alias=link5copy.service
487Alias=link5alias.service
488Alias=link5alias2.service
489EOF
490
491test ! -e "$root/etc/systemd/system/link5copy.service"
492
493"$systemctl" --root="$root" link '/link5copy.service'
494islink "$root/etc/systemd/system/link5copy.service" '/link5copy.service'
495test ! -h "$root/etc/systemd/system/link5alias.service"
496test ! -h "$root/etc/systemd/system/link5alias2.service"
497
498# FIXME: we must create link5alias2 and link5alias as relative links to link5.service
499# When they are independent links to /link5.service, systemd doesn't know that
500# they are aliases, because we do not follow symlinks outside of the search paths.
501
502"$systemctl" --root="$root" disable 'link5copy.service'
503test ! -h "$root/etc/systemd/system/link5copy.service"
504test ! -h "$root/etc/systemd/system/link5alias.service"
505test ! -h "$root/etc/systemd/system/link5alias2.service"
506
507"$systemctl" --root="$root" enable '/link5copy.service'
508islink "$root/etc/systemd/system/link5copy.service" '/link5copy.service'
509islink "$root/etc/systemd/system/link5alias.service" '/link5copy.service'
510islink "$root/etc/systemd/system/link5alias2.service" '/link5copy.service'
511
512"$systemctl" --root="$root" disable 'link5copy.service'
513test ! -h "$root/etc/systemd/system/link5copy.service"
514test ! -h "$root/etc/systemd/system/link5alias.service"
515test ! -h "$root/etc/systemd/system/link5alias2.service"
516
517: '----issue 19437: plain templates in .wants/ or .requires/---'
518test ! -e "$root/etc/systemd/system/link5@.path"
519cat >"$root/etc/systemd/system/link5@.path" <<EOF
520[Install]
521WantedBy=target5@.target
522RequiredBy=target5@.target
523WantedBy=target5@inst.target
524RequiredBy=target5@inst.target
525EOF
526
527"$systemctl" --root="$root" enable 'link5@.path'
528test ! -h "$root/etc/systemd/system/link5@.path"  # this is our file
529islink "$root/etc/systemd/system/target5@.target.wants/link5@.path" "/etc/systemd/system/link5@.path"
530islink "$root/etc/systemd/system/target5@.target.requires/link5@.path" "/etc/systemd/system/link5@.path"
531islink "$root/etc/systemd/system/target5@inst.target.wants/link5@.path" "/etc/systemd/system/link5@.path"
532islink "$root/etc/systemd/system/target5@inst.target.requires/link5@.path" "/etc/systemd/system/link5@.path"
533
534"$systemctl" --root="$root" disable 'link5@.path'
535test ! -h "$root/etc/systemd/system/link5@.path"  # this is our file
536test ! -h "$root/etc/systemd/system/target5@.target.wants/link5@.path"
537test ! -h "$root/etc/systemd/system/target5@.target.requires/link5@.path"
538test ! -h "$root/etc/systemd/system/target5@inst.target.wants/link5@.path"
539test ! -h "$root/etc/systemd/system/target5@inst.target.requires/link5@.path"
540
541: '-------removal of symlinks not listed in [Install]----------'
542# c.f. 66a19d85a533b15ed32f4066ec880b5a8c06babd
543test ! -e "$root/etc/systemd/system/multilink.mount"
544cat >"$root/etc/systemd/system/multilink.mount" <<EOF
545[Install]
546WantedBy=multilink.target
547EOF
548
549mkdir -p "$root/etc/systemd/system/default.target.wants"
550ln -s ../multilink.mount "$root/etc/systemd/system/default.target.wants/"
551ln -s ../multilink.mount "$root/etc/systemd/system/multilink-alias.mount"
552ln -s ../multilink.mount "$root/etc/systemd/system/multilink-badalias.service"
553
554"$systemctl" --root="$root" disable 'multilink.mount'
555test -e "$root/etc/systemd/system/multilink.mount"  # this is our file
556test ! -h "$root/etc/systemd/system/default.target.wants/"
557test ! -h "$root/etc/systemd/system/multilink-alias.mount"
558test ! -h "$root/etc/systemd/system/multilink-badalias.service"
559
560: '-------merge 20017: specifiers in the unit file-------------'
561test ! -e "$root/etc/systemd/system/some-some-link6@.socket"
562# c.f. de61a04b188f81a85cdb5c64ddb4987dcd9d30d3
563
564check_alias() {
565    : "------------------ %$1 -------------------------------------"
566    cat >"$root/etc/systemd/system/some-some-link6@.socket" <<EOF
567[Install]
568Alias=target@$1:%$1.socket
569EOF
570    SYSTEMD_LOG_LEVEL=debug "$systemctl" --root="$root" enable 'some-some-link6@.socket' || return 1
571    islink "$root/etc/systemd/system/target@$1:$2.socket" "/etc/systemd/system/some-some-link6@.socket" || return 2
572}
573
574# TODO: our architecture names are different than what uname -m returns.
575# Add something like 'systemd-detect-virt --print-architecture' and use it here.
576check_alias a "$(uname -m | tr '_' '-')" || :
577
578test ! -e "$root/etc/os-release"
579test ! -e "$root/usr/lib/os-release"
580
581( ! check_alias A '' )
582( ! check_alias B '' )
583( ! check_alias M '' )
584( ! check_alias o '' )
585( ! check_alias w '' )
586( ! check_alias W '' )
587
588cat >"$root/etc/os-release" <<EOF
589# empty
590EOF
591
592check_alias A ''
593check_alias B ''
594check_alias M ''
595check_alias o ''
596check_alias w ''
597check_alias W ''
598
599cat >"$root/etc/os-release" <<EOF
600ID='the-id'
601VERSION_ID=39a
602BUILD_ID=build-id
603VARIANT_ID=wrong
604VARIANT_ID=right
605IMAGE_ID="foobar"
606IMAGE_VERSION='1-2-3'
607EOF
608
609check_alias A '1-2-3'
610check_alias B 'build-id'
611check_alias M 'foobar'
612check_alias o 'the-id'
613check_alias w '39a'
614check_alias W 'right'
615
616check_alias b "$("$systemd_id128" boot-id)"
617
618# Specifiers not available for [Install]
619( ! check_alias C '' )
620( ! check_alias E '' )
621( ! check_alias f '' )
622( ! check_alias h '' )
623( ! check_alias I '' )
624( ! check_alias J '' )
625( ! check_alias L '' )
626( ! check_alias P '' )
627( ! check_alias s '' )
628( ! check_alias S '' )
629( ! check_alias t '' )
630( ! check_alias T '' )
631( ! check_alias V '' )
632
633check_alias g root
634check_alias G 0
635check_alias u root
636check_alias U 0
637
638check_alias i ""
639
640check_alias j 'link6'
641
642check_alias l "$(uname -n | sed 's/\..*//')"
643
644test ! -e "$root/etc/machine-id"
645( ! check_alias m '' )
646
647"$systemd_id128" new >"$root/etc/machine-id"
648check_alias m "$(cat "$root/etc/machine-id")"
649
650check_alias n 'some-some-link6@.socket'
651check_alias N 'some-some-link6@'
652
653check_alias p 'some-some-link6'
654
655uname -r | grep -q '[^a-zA-Z0-9_.\\-]' || \
656    check_alias v "$(uname -r)"
657
658# % is not legal in unit name
659( ! check_alias % '%' )
660
661# %z is not defined
662( ! check_alias z 'z' )
663
664: '-------specifiers in WantedBy-------------------------------'
665# We don't need to repeat all the tests. Let's do a basic check that specifier
666# expansion is performed.
667
668cat >"$root/etc/systemd/system/some-some-link7.socket" <<EOF
669[Install]
670WantedBy=target@%p.target
671WantedBy=another-target@.target
672RequiredBy=target2@%p.target
673RequiredBy=another-target2@.target
674EOF
675
676"$systemctl" --root="$root" enable 'some-some-link7.socket'
677islink "$root/etc/systemd/system/target@some-some-link7.target.wants/some-some-link7.socket" "/etc/systemd/system/some-some-link7.socket"
678islink "$root/etc/systemd/system/another-target@.target.wants/some-some-link7.socket" "/etc/systemd/system/some-some-link7.socket"
679islink "$root/etc/systemd/system/target2@some-some-link7.target.requires/some-some-link7.socket" "/etc/systemd/system/some-some-link7.socket"
680islink "$root/etc/systemd/system/another-target2@.target.requires/some-some-link7.socket" "/etc/systemd/system/some-some-link7.socket"
681
682"$systemctl" --root="$root" disable 'some-some-link7.socket'
683test ! -h "$root/etc/systemd/system/target@some-some-link7.target.wants/some-some-link7.socket"
684test ! -h "$root/etc/systemd/system/another-target@.target.wants/some-some-link7.socket"
685test ! -h "$root/etc/systemd/system/target2@some-some-link7.target.requires/some-some-link7.socket"
686test ! -h "$root/etc/systemd/system/another-target2@.target.requires/some-some-link7.socket"
687
688# TODO: repeat the tests above for presets
689
690: '-------SYSTEMD_OS_RELEASE relative to root-------------------'
691# check that os-release overwriting works as expected with root
692test -e "$root/etc/os-release"
693
694cat >"$root/etc/os-release2" <<EOF
695ID='the-id2'
696EOF
697
698SYSTEMD_OS_RELEASE="$root/etc/os-release2" check_alias o 'the-id2'
699