]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/test-rpm-macros.sh
Merge pull request #32249 from CodethinkLabs/vmspawn/predicatable_tap_names
[thirdparty/systemd.git] / test / test-rpm-macros.sh
CommitLineData
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).
7set -eu
8
d7ff5240 9BUILD_DIR="${1:?}"
55c09511
FS
10RPM_MACROS_FILE="${BUILD_DIR:?}/src/rpm/macros.systemd"
11
12if ! 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
15fi
16
17if [[ ! -f "${RPM_MACROS_FILE:?}" ]]; then
18 echo "RPM macros file not found in $RPM_MACROS_FILE!"
19 exit 1
20fi
21
22at_exit() {
23 if [[ -v WORK_DIR && -d "$WORK_DIR" ]]; then
24 rm -frv "$WORK_DIR"
25 fi
26}
27
28trap at_exit EXIT
29
30WORK_DIR="$(mktemp -d)"
31RPM_SPEC="$(mktemp "$WORK_DIR/systemd-test-rpm-macros-XXX.spec")"
32TEMP_LOG="$(mktemp "$WORK_DIR/out-XXX.log")"
33
34die() {
35 echo >&2 "${1:?}"
36 exit 1
37}
38
39mk_mini_spec() {
40 cat >"${RPM_SPEC:?}" <<EOF
41%{load:$RPM_MACROS_FILE}
42Summary: Test systemd RPM macros
43Name: systemd-test-rpm-macros
44License: LGPLv2+ and MIT and GPLv2+
45Version: 1
46Release: 1
47%description
48%{summary}
49END_OF_INITIAL_SPEC
50EOF
51}
52
53echo "=== Test basic loadability ==="
54mk_mini_spec
55# ensure its loadability (macros will be just loaded and not used for now)
56# also check that rpm supports %load
57rpmspec --parse "$RPM_SPEC"
58
59echo "=== Test %systemd_requires ==="
60mk_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.
66echo "%systemd_requires" >>"$RPM_SPEC"
67: >"$TEMP_LOG"
68rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG"
69for i in post preun postun; do
70 echo "== Requires($i) =="
71 grep "^Requires($i): systemd$" "$TEMP_LOG"
72done
73
74echo "=== Test %systemd_ordering ==="
75mk_mini_spec
76echo "%systemd_ordering" >>"$RPM_SPEC"
77: >"$TEMP_LOG"
78rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG"
79for i in post preun postun; do
80 echo "== OrderWithRequires($i) =="
81 grep "^OrderWithRequires($i): systemd$" "$TEMP_LOG"
82done
83
84echo "=== Test macros requiring an argument without specifying such argument ==="
85for 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
97do
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
104done
105
106echo "=== Test macros requiring two arguments ==="
107for i in \
108 tmpfiles_create_package \
109 sysusers_create_package
110do
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
127done
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
134echo "=== Test %*_create_package macros ==="
135for 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
152systemd-$i --replace=$CONF_DIR/TEST_NAME.conf ${EXTRA_ARGS[*]:+${EXTRA_ARGS[@]} }- <<SYSTEMD_INLINE_EOF || :
153TEST_DATA
154SYSTEMD_INLINE_EOF
155EOF
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"
162done