From 29cda567564d548cce5867c9d054ebb6cefcdca0 Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Thu, 3 Mar 2022 20:30:43 +0100 Subject: [PATCH] test: check systemd RPM macros Make sure our RPM macros work as intended. Based on the original PR (#16464) by Mikhail Novosyolov. Co-authored-by: Mikhail Novosyolov (cherry picked from commit 55c09511e13c6a57ffe64bef4a9d0a00f34d37d9) Related: #2017035 --- .github/workflows/unit_tests.sh | 1 + .semaphore/semaphore-runner.sh | 2 +- test/meson.build | 16 ++++ test/test-rpm-macros.sh | 162 ++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100755 test/test-rpm-macros.sh diff --git a/.github/workflows/unit_tests.sh b/.github/workflows/unit_tests.sh index 9c7beb6d19..f41b070e57 100755 --- a/.github/workflows/unit_tests.sh +++ b/.github/workflows/unit_tests.sh @@ -20,6 +20,7 @@ ADDITIONAL_DEPS=( perl python3-libevdev python3-pyparsing + rpm zstd ) diff --git a/.semaphore/semaphore-runner.sh b/.semaphore/semaphore-runner.sh index d02b449e0e..6ccf271a82 100755 --- a/.semaphore/semaphore-runner.sh +++ b/.semaphore/semaphore-runner.sh @@ -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 diff --git a/test/meson.build b/test/meson.build index 8de1043e17..04ae9ebc78 100644 --- a/test/meson.build +++ b/test/meson.build @@ -163,6 +163,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') diff --git a/test/test-rpm-macros.sh b/test/test-rpm-macros.sh new file mode 100755 index 0000000000..5843b72346 --- /dev/null +++ b/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:?}" <>"$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" <"$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