summaryrefslogtreecommitdiff
path: root/test/units/generator-utils.sh
blob: 1973efefba92c98595d6252935d491e3cbd53d76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later

link_endswith() {
    [[ -h "${1:?}" && "$(readlink "${1:?}")" =~ ${2:?}$ ]]
}

link_eq() {
    [[ -h "${1:?}" && "$(readlink "${1:?}")" == "${2:?}" ]]
}

# Get the value from a 'key=value' assignment
opt_get_arg() {
    local arg

    IFS="=" read -r _ arg <<< "${1:?}"
    test -n "$arg"
    echo "$arg"
}

in_initrd() {
    [[ "${SYSTEMD_IN_INITRD:-0}" -ne 0 ]]
}

# Check if we're parsing host's fstab in initrd
in_initrd_host() {
    in_initrd && [[ "${SYSTEMD_SYSROOT_FSTAB:-/dev/null}" != /dev/null ]]
}

in_container() {
    systemd-detect-virt -qc
}

# Filter out "unwanted" options, i.e. options that the fstab-generator doesn't
# propagate to the final mount unit
opt_filter_consumed() {(
    set +x
    local opt split_options filtered_options

    IFS="," read -ra split_options <<< "${1:?}"
    for opt in "${split_options[@]}"; do
        if [[ "$opt" =~ ^x-systemd.device-timeout= ]]; then
            continue
        fi

        filtered_options+=("$opt")
    done

    IFS=","; printf "%s" "${filtered_options[*]}"
)}

# Run the given generator $1 with target directory $2 - clean the target
# directory beforehand
run_and_list() {
    local generator="${1:?}"
    local out_dir="${2:?}"

    rm -fr "${out_dir:?}"/*
    "$generator" "$out_dir"
    ls -lR "$out_dir"
}