2 # SPDX-License-Identifier: LGPL-2.1-or-later
4 # Utility functions for shell tests
6 # shellcheck disable=SC2034
7 [[ -e /var
/tmp
/.systemd_reboot_count
]] && REBOOT_COUNT
="$(</var/tmp/.systemd_reboot_count)" || REBOOT_COUNT
=0
16 if [[ $rc -ne 0 ]]; then
17 echo "FAIL: command '$*' failed with exit code $rc" >&2
25 if [[ "${1?}" != "${2?}" ]]; then
26 echo "FAIL: expected: '$2' actual: '$1'" >&2
34 if ! [[ "${2?}" =~
${1?} ]]; then
35 echo "FAIL: '$1' not found in:" >&2
44 if [[ "${2?}" =~
${1?} ]]; then
45 echo "FAIL: '$1' found in:" >&2
59 assert_eq
"$rc" "$exp"
62 assert_not_reached
() {
63 echo >&2 "Code should not be reached at ${BASH_SOURCE[1]}:${BASH_LINENO[1]}, function ${FUNCNAME[1]}()"
74 # Invert the grep condition - i.e. check if the expression is _not_ in command's output
75 if [[ "${1:?}" == "-n" ]]; then
83 if [[ $# -eq 0 ]]; then
84 echo >&2 "FAIL: Not enough arguments for ${FUNCNAME[0]}()"
89 if ! "$@" |
& tee "${log:?}"; then
90 echo >&2 "FAIL: Command '$*' failed"
94 grep -qE "$expression" "$log" && ec
=0 || ec
=$?
95 if [[ "$exp_ec" -eq 0 && "$ec" -ne 0 ]]; then
96 echo >&2 "FAIL: Expression '$expression' not found in the output of '$*'"
98 elif [[ "$exp_ec" -ne 0 && "$ec" -eq 0 ]]; then
99 echo >&2 "FAIL: Expression '$expression' found in the output of '$*'"
106 get_cgroup_hierarchy
() {
107 case "$(stat -c '%T' -f /sys/fs/cgroup)" in
112 if [[ -d /sys
/fs
/cgroup
/unified
&& "$(stat -c '%T' -f /sys/fs/cgroup/unified)" == cgroup2fs
]]; then
119 echo >&2 "Failed to determine host's cgroup hierarchy"
125 local userid
="${1:?}"
127 XDG_RUNTIME_DIR
=/run
/user
/"$(id -u "$userid")" setpriv
--reuid="$userid" --init-groups "$@"
130 coverage_create_nspawn_dropin
() {
131 # If we're collecting coverage, bind mount the $BUILD_DIR into the nspawn
132 # container so gcov can update the counters. This is mostly for standalone
133 # containers, as machinectl stuff is handled by overriding the systemd-nspawn@.service
134 # (see test/test-functions:install_systemd())
138 if [[ -z "${COVERAGE_BUILD_DIR:-}" ]]; then
142 container
="$(basename "$root")"
143 mkdir
-p "/run/systemd/nspawn"
144 echo -ne "[Files]\nBind=$COVERAGE_BUILD_DIR\n" >"/run/systemd/nspawn/${container:?}.nspawn"
147 create_dummy_container
() {
150 if [[ ! -d /testsuite-13-container-template
]]; then
151 echo >&2 "Missing container template, probably not running in TEST-13-NSPAWN?"
156 cp -a /testsuite-13-container-template
/* "$root"
157 coverage_create_nspawn_dropin
"$root"
160 # Bump the reboot counter and call systemctl with the given arguments
164 if [[ $# -eq 0 ]]; then
165 echo >&2 "Missing arguments"
169 [[ -e /var
/tmp
/.systemd_reboot_count
]] && counter
="$(</var/tmp/.systemd_reboot_count)" || counter
=0
170 echo "$((counter + 1))" >/var
/tmp
/.systemd_reboot_count
175 cgroupfs_supports_user_xattrs
() {
178 xattr
="user.supported_$RANDOM"
179 # shellcheck disable=SC2064
180 trap "setfattr --remove=$xattr /sys/fs/cgroup || :" RETURN
182 setfattr
--name="$xattr" --value=254 /sys
/fs
/cgroup
183 [[ "$(getfattr --name="$xattr" --absolute-names --only-values /sys/fs/cgroup)" -eq 254 ]]
187 local algorithm
="${1:?}"
190 [[ -f "/sys/class/tpm/tpm0/pcr-$algorithm/$pcr" ]]
193 openssl_supports_kdf
() {
196 # The arguments will need to be adjusted to make this work for other KDFs than SSKDF,
197 # but let's do that when/if the need arises
198 openssl kdf
-keylen 16 -kdfopt digest
:SHA2-256
-kdfopt key
:foo
-out /dev
/null
"$kdf"
201 kernel_supports_lsm
() {
205 if [[ ! -e /sys
/kernel
/security
/lsm
]]; then
206 echo "/sys/kernel/security/lsm doesn't exist, assuming $lsm is not supported"
210 mapfile
-t -d, items
</sys
/kernel
/security
/lsm
211 for item
in "${items[@]}"; do
212 if [[ "$item" == "$lsm" ]]; then
220 MOUNTED_USR_OVERLAY
=false
222 maybe_mount_usr_overlay
() {
223 if [[ ! -w /usr
]]; then
224 mkdir
-p /tmp
/usr-overlay
/{upperdir
,workdir
}
225 mount
-t overlay
-o lowerdir
=/usr
,upperdir
=/tmp
/usr-overlay
/upperdir
,workdir
=/tmp
/usr-overlay
/workdir overlay
/usr
226 MOUNTED_USR_OVERLAY
=true
230 maybe_umount_usr_overlay
() {
231 if "$MOUNTED_USR_OVERLAY"; then