1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -e
4
5if [[ -n "$1" ]]; then
6    generator=$1
7elif [[ -x /usr/lib/systemd/system-generators/systemd-fstab-generator ]]; then
8    generator=/usr/lib/systemd/system-generators/systemd-fstab-generator
9elif [[ -x /lib/systemd/system-generators/systemd-fstab-generator ]]; then
10    generator=/lib/systemd/system-generators/systemd-fstab-generator
11else
12    exit 1
13fi
14
15src="$(dirname "$0")/testdata/test-fstab-generator"
16
17for f in "$src"/test-*.input; do
18    echo "*** Running $f"
19
20    (
21        out=$(mktemp --tmpdir --directory "test-fstab-generator.XXXXXXXXXX")
22        # shellcheck disable=SC2064
23        trap "rm -rf '$out'" EXIT INT QUIT PIPE
24
25        # shellcheck disable=SC2046
26        SYSTEMD_LOG_LEVEL=debug SYSTEMD_IN_INITRD=yes SYSTEMD_PROC_CMDLINE="fstab=no $(cat "$f")" $generator "$out" "$out" "$out"
27
28        if [[ -f "$out"/systemd-fsck-root.service ]]; then
29            # For split-usr system
30            sed -i -e 's:ExecStart=/lib/systemd/systemd-fsck:ExecStart=/usr/lib/systemd/systemd-fsck:' "$out"/systemd-fsck-root.service
31        fi
32
33        # We store empty files rather than symlinks, so that they don't get pruned when packaged up, so compare
34        # the list of filenames rather than their content
35        if ! diff -u <(find "$out" -printf '%P\n' | sort) <(find "${f%.input}.expected" -printf '%P\n' | sort); then
36            echo "**** Unexpected output for $f"
37            exit 1
38        fi
39    ) || exit 1
40done
41