11#! /usr/bin/env bash
22# SPDX-License-Identifier: LGPL-2.1-or-later
3- set -e
3+ set -eux
44shopt -s nullglob
55shopt -s globstar
66
7- if [[ -n " $1 " ]]; then
7+ if [[ -n " ${1 :- } " ]]; then
88 generator=$1
99elif [[ -x /usr/lib/systemd/system-generators/systemd-fstab-generator ]]; then
1010 generator=/usr/lib/systemd/system-generators/systemd-fstab-generator
@@ -23,97 +23,105 @@ PATH=$PATH:/usr/sbin
2323# of the host system. Override the measurement to avoid the issue.
2424export SYSTEMD_FORCE_MEASURE=0
2525
26- for f in " $src " /test-* .input; do
27- echo " *** Running $f "
28-
29- (
30- out=$( mktemp --tmpdir --directory " test-fstab-generator.XXXXXXXXXX" )
31- # shellcheck disable=SC2064
32- trap " rm -rf '$out '" EXIT INT QUIT PIPE
33-
34- exp=" ${f% .input} .expected"
35- if [[ " ${f##*/ } " =~ swap ]] && systemd-detect-virt --container > /dev/null; then
36- exp=" ${exp} .container"
37- fi
38-
39- if [[ " ${f##*/ } " =~ \. fstab\. input ]]; then
40- SYSTEMD_LOG_LEVEL=debug SYSTEMD_IN_INITRD=yes SYSTEMD_SYSFS_CHECK=no SYSTEMD_PROC_CMDLINE=" fstab=yes root=fstab" SYSTEMD_FSTAB=" $f " SYSTEMD_SYSROOT_FSTAB=" /dev/null" $generator " $out " " $out " " $out "
41- else
42- SYSTEMD_LOG_LEVEL=debug SYSTEMD_IN_INITRD=yes SYSTEMD_SYSFS_CHECK=no SYSTEMD_PROC_CMDLINE=" fstab=no $( cat " $f " ) " $generator " $out " " $out " " $out "
43- fi
44-
45- # The option x-systemd.growfs creates symlink to system's [email protected] in .mount.wants directory. 46- # The system that the test is currently running on may not have or may have outdated unit file.
47- # Let's replace the symlink with an empty file.
48- for i in " $out " /* /systemd-growfs@* .service; do
49- [[ -L " $i " ]] || continue
50- rm " $i "
51- touch " $i "
26+ test_one () (
27+ local input out exp i j k dir fname expf
28+
29+ input=${1?}
30+
31+ : " *** Running $input "
32+
33+ out=$( mktemp --tmpdir --directory " test-fstab-generator.XXXXXXXXXX" )
34+ # shellcheck disable=SC2064
35+ trap " rm -rf '$out '" EXIT INT QUIT PIPE
36+
37+ exp=" ${input% .input} .expected"
38+ if [[ " ${input##*/ } " =~ swap ]] && systemd-detect-virt --container > /dev/null; then
39+ exp=" ${exp} .container"
40+ fi
41+
42+ if [[ " ${input##*/ } " =~ \. fstab\. input ]]; then
43+ SYSTEMD_LOG_LEVEL=debug SYSTEMD_IN_INITRD=yes SYSTEMD_SYSFS_CHECK=no SYSTEMD_PROC_CMDLINE=" fstab=yes root=fstab" SYSTEMD_FSTAB=" $input " SYSTEMD_SYSROOT_FSTAB=" /dev/null" $generator " $out " " $out " " $out "
44+ else
45+ SYSTEMD_LOG_LEVEL=debug SYSTEMD_IN_INITRD=yes SYSTEMD_SYSFS_CHECK=no SYSTEMD_PROC_CMDLINE=" fstab=no $( cat " $input " ) " $generator " $out " " $out " " $out "
46+ fi
47+
48+ # The option x-systemd.growfs creates symlink to system's [email protected] in .mount.wants directory. 49+ # The system that the test is currently running on may not have or may have outdated unit file.
50+ # Let's replace the symlink with an empty file.
51+ for i in " $out " /* /systemd-growfs@* .service; do
52+ [[ -L " $i " ]] || continue
53+ rm " $i "
54+ touch " $i "
55+ done
56+
57+ # For split-usr system
58+ for i in " $out " /systemd-* .service; do
59+ sed -i -e ' s:ExecStart=/lib/systemd/:ExecStart=/usr/lib/systemd/:' " $i "
60+ done
61+
62+ if [[ " ${input##*/ } " =~ \. fstab\. input ]]; then
63+ for i in " $out " /* .{automount,mount,swap}; do
64+ sed -i -e ' s:SourcePath=.*$:SourcePath=/etc/fstab:' " $i "
5265 done
53-
54- # For split-usr system
55- for i in " $out " /systemd-* .service; do
56- sed -i -e ' s:ExecStart=/lib/systemd/:ExecStart=/usr/lib/systemd/:' " $i "
57- done
58-
59- if [[ " ${f##*/ } " =~ \. fstab\. input ]]; then
60- for i in " $out " /* .{automount,mount,swap}; do
61- sed -i -e ' s:SourcePath=.*$:SourcePath=/etc/fstab:' " $i "
62- done
66+ fi
67+
68+ # .deb packager seems to dislike files named with backslash. So, as a workaround, we store files
69+ # without backslash in .expected.
70+ for i in " $out " /** /* \\ * .{mount,swap}; do
71+ k=" ${i// \\ / } "
72+ if [[ " $i " != " $k " ]]; then
73+ if [[ -f " $i " ]]; then
74+ mv " $i " " $k "
75+ elif [[ -L " $i " ]]; then
76+ dest=$( readlink " $i " )
77+ rm " $i "
78+ ln -s " ${dest// \\ / } " " $k "
79+ fi
6380 fi
64-
65- # .deb packager seems to dislike files named with backslash. So, as a workaround, we store files
66- # without backslash in .expected.
67- for i in " $out " /** /* \\ * .{mount,swap}; do
68- k=" ${i// \\ / } "
69- if [[ " $i " != " $k " ]]; then
70- if [[ -f " $i " ]]; then
71- mv " $i " " $k "
72- elif [[ -L " $i " ]]; then
73- dest=$( readlink " $i " )
74- rm " $i "
75- ln -s " ${dest// \\ / } " " $k "
81+ done
82+
83+ # We store empty files rather than dead symlinks, so that they don't get pruned when packaged up, so compare
84+ # the list of filenames rather than their content
85+ if ! diff -u <( find " $out " -printf ' %P\n' | sort) <( find " $exp " -printf ' %P\n' | sort) ; then
86+ : " **** Unexpected output for $input "
87+ return 1
88+ fi
89+
90+ # Check the main units.
91+ if ! diff -u " $out " " $exp " ; then
92+ : " **** Unexpected output for $input "
93+ return 1
94+ fi
95+
96+ # Also check drop-ins.
97+ for i in " $out " /* ; do
98+ [[ -d " $i " ]] || continue
99+
100+ dir=" ${i##*/ } "
101+
102+ for j in " $i " /* ; do
103+ fname=" ${j##*/ } "
104+ expf=" $exp /$dir /$fname "
105+
106+ if [[ -L " $j " && ! -e " $j " ]]; then
107+ # For dead symlink, we store an empty file.
108+ if [[ ! -e " $expf " || -n " $( cat " $expf " ) " ]]; then
109+ : " **** Unexpected symlink $j created by $input "
110+ return 1
76111 fi
112+ continue
77113 fi
78- done
79114
80- # We store empty files rather than dead symlinks, so that they don't get pruned when packaged up, so compare
81- # the list of filenames rather than their content
82- if ! diff -u <( find " $out " -printf ' %P\n' | sort) <( find " $exp " -printf ' %P\n' | sort) ; then
83- echo " **** Unexpected output for $f "
84- exit 1
85- fi
86-
87- # Check the main units.
88- if ! diff -u " $out " " $exp " ; then
89- echo " **** Unexpected output for $f "
90- exit 1
91- fi
92-
93- # Also check drop-ins.
94- for i in " $out " /* ; do
95- [[ -d " $i " ]] || continue
96-
97- dir=" ${i##*/ } "
98-
99- for j in " $i " /* ; do
100- fname=" ${j##*/ } "
101- expf=" $exp /$dir /$fname "
115+ if ! diff -u " $j " " $expf " ; then
116+ : " **** Unexpected output in $j for $input "
117+ return 1
118+ fi
119+ done
120+ done
102121
103- if [[ -L " $j " && ! -e " $j " ]]; then
104- # For dead symlink, we store an empty file.
105- if [[ ! -e " $expf " || -n " $( cat " $expf " ) " ]]; then
106- echo " **** Unexpected symlink $j created by $f "
107- exit 1
108- fi
109- continue
110- fi
122+ return 0
123+ )
111124
112- if ! diff -u " $j " " $expf " ; then
113- echo " **** Unexpected output in $j for $f "
114- exit 1
115- fi
116- done
117- done
118- ) || exit 1
125+ for f in " $src " /test-* .input; do
126+ test_one " $f "
119127done
0 commit comments