]>
Commit | Line | Data |
---|---|---|
55c09511 FS |
1 | #!/usr/bin/env bash |
2 | # SPDX-License-Identifier: LGPL-2.1-or-later | |
3 | # This test makes some basic checks that RPM macros work correctly. | |
4 | # RPM is a simple C program available on different Linux distros, not only RPM-based ones, | |
5 | # and even BSD systems, so it must not be a problem to require it. | |
6 | # rpmspec utility is required (so this test will work with RPM 4 but won't work with RPM 5). | |
7 | set -eu | |
8 | ||
d7ff5240 | 9 | BUILD_DIR="${1:?}" |
55c09511 FS |
10 | RPM_MACROS_FILE="${BUILD_DIR:?}/src/rpm/macros.systemd" |
11 | ||
12 | if ! command -v rpm >/dev/null || ! command -v rpmspec >/dev/null; then | |
13 | echo >&2 "Missing necessary utilities (rpm, rpmspec), can't continue" | |
14 | exit 1 | |
15 | fi | |
16 | ||
17 | if [[ ! -f "${RPM_MACROS_FILE:?}" ]]; then | |
18 | echo "RPM macros file not found in $RPM_MACROS_FILE!" | |
19 | exit 1 | |
20 | fi | |
21 | ||
22 | at_exit() { | |
23 | if [[ -v WORK_DIR && -d "$WORK_DIR" ]]; then | |
24 | rm -frv "$WORK_DIR" | |
25 | fi | |
26 | } | |
27 | ||
28 | trap at_exit EXIT | |
29 | ||
30 | WORK_DIR="$(mktemp -d)" | |
31 | RPM_SPEC="$(mktemp "$WORK_DIR/systemd-test-rpm-macros-XXX.spec")" | |
32 | TEMP_LOG="$(mktemp "$WORK_DIR/out-XXX.log")" | |
33 | ||
34 | die() { | |
35 | echo >&2 "${1:?}" | |
36 | exit 1 | |
37 | } | |
38 | ||
39 | mk_mini_spec() { | |
40 | cat >"${RPM_SPEC:?}" <<EOF | |
41 | %{load:$RPM_MACROS_FILE} | |
42 | Summary: Test systemd RPM macros | |
43 | Name: systemd-test-rpm-macros | |
44 | License: LGPLv2+ and MIT and GPLv2+ | |
45 | Version: 1 | |
46 | Release: 1 | |
47 | %description | |
48 | %{summary} | |
49 | END_OF_INITIAL_SPEC | |
50 | EOF | |
51 | } | |
52 | ||
53 | echo "=== Test basic loadability ===" | |
54 | mk_mini_spec | |
55 | # ensure its loadability (macros will be just loaded and not used for now) | |
56 | # also check that rpm supports %load | |
57 | rpmspec --parse "$RPM_SPEC" | |
58 | ||
59 | echo "=== Test %systemd_requires ===" | |
60 | mk_mini_spec | |
61 | # The idea of tests is the following: | |
62 | # - make a minimal spec file | |
63 | # - add macros into its %description section | |
64 | # - use rpmspec(8) to print spec file with expanded macros | |
65 | # - check that macros have been expanded as required. | |
66 | echo "%systemd_requires" >>"$RPM_SPEC" | |
67 | : >"$TEMP_LOG" | |
68 | rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG" | |
69 | for i in post preun postun; do | |
70 | echo "== Requires($i) ==" | |
71 | grep "^Requires($i): systemd$" "$TEMP_LOG" | |
72 | done | |
73 | ||
74 | echo "=== Test %systemd_ordering ===" | |
75 | mk_mini_spec | |
76 | echo "%systemd_ordering" >>"$RPM_SPEC" | |
77 | : >"$TEMP_LOG" | |
78 | rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG" | |
79 | for i in post preun postun; do | |
80 | echo "== OrderWithRequires($i) ==" | |
81 | grep "^OrderWithRequires($i): systemd$" "$TEMP_LOG" | |
82 | done | |
83 | ||
84 | echo "=== Test macros requiring an argument without specifying such argument ===" | |
85 | for i in \ | |
86 | systemd_post \ | |
87 | systemd_preun \ | |
88 | systemd_postun \ | |
89 | systemd_postun_with_restart \ | |
90 | systemd_user_preun \ | |
91 | systemd_user_postun \ | |
92 | systemd_user_postun_with_restart \ | |
93 | tmpfiles_create \ | |
94 | tmpfiles_create_package \ | |
95 | sysusers_create \ | |
96 | sysusers_create_package | |
97 | do | |
98 | echo "== Macro: $i ==" | |
99 | mk_mini_spec | |
100 | echo "%${i}" >>"$RPM_SPEC" | |
101 | if rpmspec --parse "$RPM_SPEC"; then | |
102 | die "Unexpected pass with macro $i (no arguments)" | |
103 | fi | |
104 | done | |
105 | ||
106 | echo "=== Test macros requiring two arguments ===" | |
107 | for i in \ | |
108 | tmpfiles_create_package \ | |
109 | sysusers_create_package | |
110 | do | |
111 | echo "== Macro: $i ==" | |
112 | # Test with an incorrect number of arguments (0, 1, 3) | |
113 | for args in "" "arg1" "arg1 arg2 arg3"; do | |
114 | mk_mini_spec | |
115 | echo "%${i} $args" >>"$RPM_SPEC" | |
116 | if rpmspec --parse "$RPM_SPEC"; then | |
117 | die "Unexpected pass with macro $i (arguments: $args)" | |
118 | fi | |
119 | done | |
120 | ||
121 | # Test with the correct number of arguments (2) | |
122 | mk_mini_spec | |
123 | echo "%${i} arg1 arg2" >>"$RPM_SPEC" | |
124 | if ! rpmspec --parse "$RPM_SPEC"; then | |
125 | die "Unexpected fail with macro $i (arguments: $args)" | |
126 | fi | |
127 | done | |
128 | ||
129 | ||
130 | # Test that: | |
131 | # - *_create_package macros do work correctly | |
132 | # - shell syntax is correct (https://github.com/systemd/systemd/commit/93406fd37) | |
133 | # - RPM macros, loaded from macros.in, are actually expanded | |
134 | echo "=== Test %*_create_package macros ===" | |
135 | for i in sysusers tmpfiles; do | |
136 | echo "== Macro: ${i}_create_package ==" | |
137 | ||
138 | PKG_DATA_FILE="$(mktemp "$WORK_DIR/pkg-data-XXX")" | |
139 | EXP_OUT="$(mktemp "$WORK_DIR/exp-out-XXX.log")" | |
140 | CONF_DIR="$(pkg-config --variable="${i}dir" systemd)" | |
141 | EXTRA_ARGS=() | |
142 | ||
143 | if [[ "$i" == tmpfiles ]]; then | |
144 | EXTRA_ARGS+=("--create") | |
145 | fi | |
146 | ||
147 | echo "TEST_DATA" >"$PKG_DATA_FILE" | |
148 | mk_mini_spec | |
149 | echo "%${i}_create_package TEST_NAME ${PKG_DATA_FILE}" >>"$RPM_SPEC" | |
150 | ||
151 | cat >"$EXP_OUT" <<EOF | |
152 | systemd-$i --replace=$CONF_DIR/TEST_NAME.conf ${EXTRA_ARGS[*]:+${EXTRA_ARGS[@]} }- <<SYSTEMD_INLINE_EOF || : | |
153 | TEST_DATA | |
154 | SYSTEMD_INLINE_EOF | |
155 | EOF | |
156 | ||
157 | : >"$TEMP_LOG" | |
158 | rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG" | |
159 | diff "$EXP_OUT" <(grep -A1 -B1 '^TEST_DATA$' "$TEMP_LOG") | |
160 | ||
161 | rm -f "$PKG_DATA_FILE" | |
162 | done |