Skip to content

Commit

Permalink
test: check systemd RPM macros
Browse files Browse the repository at this point in the history
Make sure our RPM macros work as intended. Based on the original PR
(#16464) by Mikhail Novosyolov.

Co-authored-by: Mikhail Novosyolov <m.novosyolov@rosalinux.ru>
  • Loading branch information
2 people authored and yuwata committed Mar 6, 2022
1 parent af5e1c5 commit 55c0951
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/unit_tests.sh
Expand Up @@ -22,6 +22,7 @@ ADDITIONAL_DEPS=(
perl
python3-libevdev
python3-pyparsing
rpm
zstd
)

Expand Down
2 changes: 1 addition & 1 deletion .semaphore/semaphore-runner.sh
Expand Up @@ -42,7 +42,7 @@ apt-get -q --allow-releaseinfo-change update
apt-get -y dist-upgrade
apt-get install -y eatmydata
# The following four are needed as long as these deps are not covered by Debian's own packaging
apt-get install -y fdisk tree libfdisk-dev libp11-kit-dev libssl-dev libpwquality-dev
apt-get install -y fdisk tree libfdisk-dev libp11-kit-dev libssl-dev libpwquality-dev rpm
apt-get purge --auto-remove -y unattended-upgrades
systemctl unmask systemd-networkd
systemctl enable systemd-networkd
Expand Down
16 changes: 16 additions & 0 deletions test/meson.build
Expand Up @@ -153,6 +153,22 @@ endif

############################################################

rpm = find_program('rpm', required : false)
rpmspec = find_program('rpmspec', required : false)
test_rpm_macros = find_program('test-rpm-macros.sh')

if rpm.found() and rpmspec.found()
if want_tests != 'false'
test('test-rpm-macros',
test_rpm_macros,
args : [project_build_root])
endif
else
message('Skipping test-rpm-macros since rpm and/or rpmspec are not available')
endif

############################################################

if want_tests != 'false' and dmi_arches.contains(host_machine.cpu_family())
udev_dmi_memory_id_test = find_program('udev-dmi-memory-id-test.sh')

Expand Down
162 changes: 162 additions & 0 deletions test/test-rpm-macros.sh
@@ -0,0 +1,162 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
# This test makes some basic checks that RPM macros work correctly.
# RPM is a simple C program available on different Linux distros, not only RPM-based ones,
# and even BSD systems, so it must not be a problem to require it.
# rpmspec utility is required (so this test will work with RPM 4 but won't work with RPM 5).
set -eu

BUILD_DIR="${1:?Missing argument: build directory}"
RPM_MACROS_FILE="${BUILD_DIR:?}/src/rpm/macros.systemd"

if ! command -v rpm >/dev/null || ! command -v rpmspec >/dev/null; then
echo >&2 "Missing necessary utilities (rpm, rpmspec), can't continue"
exit 1
fi

if [[ ! -f "${RPM_MACROS_FILE:?}" ]]; then
echo "RPM macros file not found in $RPM_MACROS_FILE!"
exit 1
fi

at_exit() {
if [[ -v WORK_DIR && -d "$WORK_DIR" ]]; then
rm -frv "$WORK_DIR"
fi
}

trap at_exit EXIT

WORK_DIR="$(mktemp -d)"
RPM_SPEC="$(mktemp "$WORK_DIR/systemd-test-rpm-macros-XXX.spec")"
TEMP_LOG="$(mktemp "$WORK_DIR/out-XXX.log")"

die() {
echo >&2 "${1:?}"
exit 1
}

mk_mini_spec() {
cat >"${RPM_SPEC:?}" <<EOF
%{load:$RPM_MACROS_FILE}
Summary: Test systemd RPM macros
Name: systemd-test-rpm-macros
License: LGPLv2+ and MIT and GPLv2+
Version: 1
Release: 1
%description
%{summary}
END_OF_INITIAL_SPEC
EOF
}

echo "=== Test basic loadability ==="
mk_mini_spec
# ensure its loadability (macros will be just loaded and not used for now)
# also check that rpm supports %load
rpmspec --parse "$RPM_SPEC"

echo "=== Test %systemd_requires ==="
mk_mini_spec
# The idea of tests is the following:
# - make a minimal spec file
# - add macros into its %description section
# - use rpmspec(8) to print spec file with expanded macros
# - check that macros have been expanded as required.
echo "%systemd_requires" >>"$RPM_SPEC"
: >"$TEMP_LOG"
rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG"
for i in post preun postun; do
echo "== Requires($i) =="
grep "^Requires($i): systemd$" "$TEMP_LOG"
done

echo "=== Test %systemd_ordering ==="
mk_mini_spec
echo "%systemd_ordering" >>"$RPM_SPEC"
: >"$TEMP_LOG"
rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG"
for i in post preun postun; do
echo "== OrderWithRequires($i) =="
grep "^OrderWithRequires($i): systemd$" "$TEMP_LOG"
done

echo "=== Test macros requiring an argument without specifying such argument ==="
for i in \
systemd_post \
systemd_preun \
systemd_postun \
systemd_postun_with_restart \
systemd_user_preun \
systemd_user_postun \
systemd_user_postun_with_restart \
tmpfiles_create \
tmpfiles_create_package \
sysusers_create \
sysusers_create_package
do
echo "== Macro: $i =="
mk_mini_spec
echo "%${i}" >>"$RPM_SPEC"
if rpmspec --parse "$RPM_SPEC"; then
die "Unexpected pass with macro $i (no arguments)"
fi
done

echo "=== Test macros requiring two arguments ==="
for i in \
tmpfiles_create_package \
sysusers_create_package
do
echo "== Macro: $i =="
# Test with an incorrect number of arguments (0, 1, 3)
for args in "" "arg1" "arg1 arg2 arg3"; do
mk_mini_spec
echo "%${i} $args" >>"$RPM_SPEC"
if rpmspec --parse "$RPM_SPEC"; then
die "Unexpected pass with macro $i (arguments: $args)"
fi
done

# Test with the correct number of arguments (2)
mk_mini_spec
echo "%${i} arg1 arg2" >>"$RPM_SPEC"
if ! rpmspec --parse "$RPM_SPEC"; then
die "Unexpected fail with macro $i (arguments: $args)"
fi
done


# Test that:
# - *_create_package macros do work correctly
# - shell syntax is correct (https://github.com/systemd/systemd/commit/93406fd37)
# - RPM macros, loaded from macros.in, are actually expanded
echo "=== Test %*_create_package macros ==="
for i in sysusers tmpfiles; do
echo "== Macro: ${i}_create_package =="

PKG_DATA_FILE="$(mktemp "$WORK_DIR/pkg-data-XXX")"
EXP_OUT="$(mktemp "$WORK_DIR/exp-out-XXX.log")"
CONF_DIR="$(pkg-config --variable="${i}dir" systemd)"
EXTRA_ARGS=()

if [[ "$i" == tmpfiles ]]; then
EXTRA_ARGS+=("--create")
fi

echo "TEST_DATA" >"$PKG_DATA_FILE"
mk_mini_spec
echo "%${i}_create_package TEST_NAME ${PKG_DATA_FILE}" >>"$RPM_SPEC"

cat >"$EXP_OUT" <<EOF
systemd-$i --replace=$CONF_DIR/TEST_NAME.conf ${EXTRA_ARGS[*]:+${EXTRA_ARGS[@]} }- <<SYSTEMD_INLINE_EOF || :
TEST_DATA
SYSTEMD_INLINE_EOF
EOF

: >"$TEMP_LOG"
rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG"
diff "$EXP_OUT" <(grep -A1 -B1 '^TEST_DATA$' "$TEMP_LOG")

rm -f "$PKG_DATA_FILE"
done

0 comments on commit 55c0951

Please sign in to comment.