]>
Commit | Line | Data |
---|---|---|
759ed0a2 YW |
1 | #!/usr/bin/env bash |
2 | # SPDX-License-Identifier: LGPL-2.1-or-later | |
3 | ||
cb153b4f | 4 | # Utility functions for shell tests |
759ed0a2 | 5 | |
d170b475 | 6 | assert_true() {( |
d170b475 YW |
7 | set +ex |
8 | ||
386427cf YW |
9 | local rc |
10 | ||
759ed0a2 YW |
11 | "$@" |
12 | rc=$? | |
d170b475 | 13 | if [[ $rc -ne 0 ]]; then |
759ed0a2 YW |
14 | echo "FAIL: command '$*' failed with exit code $rc" >&2 |
15 | exit 1 | |
16 | fi | |
d170b475 YW |
17 | )} |
18 | ||
759ed0a2 | 19 | |
d170b475 YW |
20 | assert_eq() {( |
21 | set +ex | |
759ed0a2 | 22 | |
d170b475 | 23 | if [[ "${1?}" != "${2?}" ]]; then |
759ed0a2 YW |
24 | echo "FAIL: expected: '$2' actual: '$1'" >&2 |
25 | exit 1 | |
26 | fi | |
d170b475 YW |
27 | )} |
28 | ||
29 | assert_in() {( | |
30 | set +ex | |
759ed0a2 | 31 | |
d170b475 | 32 | if ! [[ "${2?}" =~ ${1?} ]]; then |
759ed0a2 YW |
33 | echo "FAIL: '$1' not found in:" >&2 |
34 | echo "$2" >&2 | |
35 | exit 1 | |
36 | fi | |
d170b475 YW |
37 | )} |
38 | ||
39 | assert_not_in() {( | |
40 | set +ex | |
41 | ||
42 | if [[ "${2?}" =~ ${1?} ]]; then | |
43 | echo "FAIL: '$1' found in:" >&2 | |
44 | echo "$2" >&2 | |
45 | exit 1 | |
46 | fi | |
47 | )} | |
48 | ||
49 | assert_rc() {( | |
d170b475 | 50 | set +ex |
759ed0a2 | 51 | |
386427cf YW |
52 | local rc exp="${1?}" |
53 | ||
759ed0a2 | 54 | shift |
759ed0a2 YW |
55 | "$@" |
56 | rc=$? | |
759ed0a2 | 57 | assert_eq "$rc" "$exp" |
d170b475 | 58 | )} |
5ff1c6fc | 59 | |
1a36d267 FS |
60 | run_and_grep() {( |
61 | set +ex | |
62 | ||
63 | local expression | |
64 | local log ec | |
65 | local exp_ec=0 | |
66 | ||
67 | # Invert the grep condition - i.e. check if the expression is _not_ in command's output | |
68 | if [[ "${1:?}" == "-n" ]]; then | |
69 | exp_ec=1 | |
70 | shift | |
71 | fi | |
72 | ||
73 | expression="${1:?}" | |
74 | shift | |
75 | ||
76 | if [[ $# -eq 0 ]]; then | |
77 | echo >&2 "FAIL: Not enough arguments for ${FUNCNAME[0]}()" | |
78 | return 1 | |
79 | fi | |
80 | ||
81 | log="$(mktemp)" | |
82 | if ! "$@" |& tee "${log:?}"; then | |
83 | echo >&2 "FAIL: Command '$*' failed" | |
84 | return 1 | |
85 | fi | |
86 | ||
87 | grep -qE "$expression" "$log" && ec=0 || ec=$? | |
88 | if [[ "$exp_ec" -eq 0 && "$ec" -ne 0 ]]; then | |
89 | echo >&2 "FAIL: Expression '$expression' not found in the output of '$*'" | |
90 | return 1 | |
91 | elif [[ "$exp_ec" -ne 0 && "$ec" -eq 0 ]]; then | |
92 | echo >&2 "FAIL: Expression '$expression' found in the output of '$*'" | |
93 | return 1 | |
94 | fi | |
95 | ||
96 | rm -f "$log" | |
97 | )} | |
98 | ||
5ff1c6fc FS |
99 | get_cgroup_hierarchy() { |
100 | case "$(stat -c '%T' -f /sys/fs/cgroup)" in | |
101 | cgroup2fs) | |
102 | echo "unified" | |
103 | ;; | |
104 | tmpfs) | |
105 | if [[ -d /sys/fs/cgroup/unified && "$(stat -c '%T' -f /sys/fs/cgroup/unified)" == cgroup2fs ]]; then | |
106 | echo "hybrid" | |
107 | else | |
108 | echo "legacy" | |
109 | fi | |
110 | ;; | |
111 | *) | |
112 | echo >&2 "Failed to determine host's cgroup hierarchy" | |
113 | exit 1 | |
114 | esac | |
115 | } | |
7c6fa5bf FS |
116 | |
117 | runas() { | |
118 | local userid="${1:?}" | |
119 | shift | |
120 | XDG_RUNTIME_DIR=/run/user/"$(id -u "$userid")" setpriv --reuid="$userid" --init-groups "$@" | |
121 | } | |
5656759d | 122 | |
28ed2326 FS |
123 | coverage_create_nspawn_dropin() { |
124 | # If we're collecting coverage, bind mount the $BUILD_DIR into the nspawn | |
125 | # container so gcov can update the counters. This is mostly for standalone | |
126 | # containers, as machinectl stuff is handled by overriding the systemd-nspawn@.service | |
127 | # (see test/test-functions:install_systemd()) | |
128 | local root="${1:?}" | |
129 | local container | |
130 | ||
131 | if [[ -z "${COVERAGE_BUILD_DIR:-}" ]]; then | |
132 | return 0 | |
133 | fi | |
134 | ||
135 | container="$(basename "$root")" | |
136 | mkdir -p "/run/systemd/nspawn" | |
137 | echo -ne "[Files]\nBind=$COVERAGE_BUILD_DIR\n" >"/run/systemd/nspawn/${container:?}.nspawn" | |
138 | } | |
139 | ||
5656759d FS |
140 | create_dummy_container() { |
141 | local root="${1:?}" | |
142 | ||
143 | if [[ ! -d /testsuite-13-container-template ]]; then | |
144 | echo >&2 "Missing container template, probably not running in TEST-13-NSPAWN?" | |
145 | exit 1 | |
146 | fi | |
147 | ||
148 | mkdir -p "$root" | |
149 | cp -a /testsuite-13-container-template/* "$root" | |
28ed2326 | 150 | coverage_create_nspawn_dropin "$root" |
5656759d | 151 | } |