naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
8d419f
From 29cda567564d548cce5867c9d054ebb6cefcdca0 Mon Sep 17 00:00:00 2001
8d419f
From: Frantisek Sumsal <frantisek@sumsal.cz>
8d419f
Date: Thu, 3 Mar 2022 20:30:43 +0100
8d419f
Subject: [PATCH] test: check systemd RPM macros
8d419f
8d419f
Make sure our RPM macros work as intended. Based on the original PR
8d419f
(#16464) by Mikhail Novosyolov.
8d419f
8d419f
Co-authored-by: Mikhail Novosyolov <m.novosyolov@rosalinux.ru>
8d419f
(cherry picked from commit 55c09511e13c6a57ffe64bef4a9d0a00f34d37d9)
8d419f
8d419f
Related: #2017035
8d419f
---
8d419f
 .github/workflows/unit_tests.sh |   1 +
8d419f
 .semaphore/semaphore-runner.sh  |   2 +-
8d419f
 test/meson.build                |  16 ++++
8d419f
 test/test-rpm-macros.sh         | 162 ++++++++++++++++++++++++++++++++
8d419f
 4 files changed, 180 insertions(+), 1 deletion(-)
8d419f
 create mode 100755 test/test-rpm-macros.sh
8d419f
8d419f
diff --git a/.github/workflows/unit_tests.sh b/.github/workflows/unit_tests.sh
8d419f
index 9c7beb6d19..f41b070e57 100755
8d419f
--- a/.github/workflows/unit_tests.sh
8d419f
+++ b/.github/workflows/unit_tests.sh
8d419f
@@ -20,6 +20,7 @@ ADDITIONAL_DEPS=(
8d419f
     perl
8d419f
     python3-libevdev
8d419f
     python3-pyparsing
8d419f
+    rpm
8d419f
     zstd
8d419f
 )
8d419f
 
8d419f
diff --git a/.semaphore/semaphore-runner.sh b/.semaphore/semaphore-runner.sh
8d419f
index d02b449e0e..6ccf271a82 100755
8d419f
--- a/.semaphore/semaphore-runner.sh
8d419f
+++ b/.semaphore/semaphore-runner.sh
8d419f
@@ -42,7 +42,7 @@ apt-get -q --allow-releaseinfo-change update
8d419f
 apt-get -y dist-upgrade
8d419f
 apt-get install -y eatmydata
8d419f
 # The following four are needed as long as these deps are not covered by Debian's own packaging
8d419f
-apt-get install -y fdisk tree libfdisk-dev libp11-kit-dev libssl-dev libpwquality-dev
8d419f
+apt-get install -y fdisk tree libfdisk-dev libp11-kit-dev libssl-dev libpwquality-dev rpm
8d419f
 apt-get purge --auto-remove -y unattended-upgrades
8d419f
 systemctl unmask systemd-networkd
8d419f
 systemctl enable systemd-networkd
8d419f
diff --git a/test/meson.build b/test/meson.build
8d419f
index 8de1043e17..04ae9ebc78 100644
8d419f
--- a/test/meson.build
8d419f
+++ b/test/meson.build
8d419f
@@ -163,6 +163,22 @@ endif
8d419f
 
8d419f
 ############################################################
8d419f
 
8d419f
+rpm = find_program('rpm', required : false)
8d419f
+rpmspec = find_program('rpmspec', required : false)
8d419f
+test_rpm_macros = find_program('test-rpm-macros.sh')
8d419f
+
8d419f
+if rpm.found() and rpmspec.found()
8d419f
+        if want_tests != 'false'
8d419f
+                test('test-rpm-macros',
8d419f
+                     test_rpm_macros,
8d419f
+                     args : [project_build_root])
8d419f
+        endif
8d419f
+else
8d419f
+      message('Skipping test-rpm-macros since rpm and/or rpmspec are not available')
8d419f
+endif
8d419f
+
8d419f
+############################################################
8d419f
+
8d419f
 if want_tests != 'false' and dmi_arches.contains(host_machine.cpu_family())
8d419f
         udev_dmi_memory_id_test = find_program('udev-dmi-memory-id-test.sh')
8d419f
 
8d419f
diff --git a/test/test-rpm-macros.sh b/test/test-rpm-macros.sh
8d419f
new file mode 100755
8d419f
index 0000000000..5843b72346
8d419f
--- /dev/null
8d419f
+++ b/test/test-rpm-macros.sh
8d419f
@@ -0,0 +1,162 @@
8d419f
+#!/usr/bin/env bash
8d419f
+# SPDX-License-Identifier: LGPL-2.1-or-later
8d419f
+# This test makes some basic checks that RPM macros work correctly.
8d419f
+# RPM is a simple C program available on different Linux distros, not only RPM-based ones,
8d419f
+# and even BSD systems, so it must not be a problem to require it.
8d419f
+# rpmspec utility is required (so this test will work with RPM 4 but won't work with RPM 5).
8d419f
+set -eu
8d419f
+
8d419f
+BUILD_DIR="${1:?Missing argument: build directory}"
8d419f
+RPM_MACROS_FILE="${BUILD_DIR:?}/src/rpm/macros.systemd"
8d419f
+
8d419f
+if ! command -v rpm >/dev/null || ! command -v rpmspec >/dev/null; then
8d419f
+    echo >&2 "Missing necessary utilities (rpm, rpmspec), can't continue"
8d419f
+    exit 1
8d419f
+fi
8d419f
+
8d419f
+if [[ ! -f "${RPM_MACROS_FILE:?}" ]]; then
8d419f
+    echo "RPM macros file not found in $RPM_MACROS_FILE!"
8d419f
+    exit 1
8d419f
+fi
8d419f
+
8d419f
+at_exit() {
8d419f
+    if [[ -v WORK_DIR && -d "$WORK_DIR" ]]; then
8d419f
+        rm -frv "$WORK_DIR"
8d419f
+    fi
8d419f
+}
8d419f
+
8d419f
+trap at_exit EXIT
8d419f
+
8d419f
+WORK_DIR="$(mktemp -d)"
8d419f
+RPM_SPEC="$(mktemp "$WORK_DIR/systemd-test-rpm-macros-XXX.spec")"
8d419f
+TEMP_LOG="$(mktemp "$WORK_DIR/out-XXX.log")"
8d419f
+
8d419f
+die() {
8d419f
+    echo >&2 "${1:?}"
8d419f
+    exit 1
8d419f
+}
8d419f
+
8d419f
+mk_mini_spec() {
8d419f
+    cat >"${RPM_SPEC:?}" <
8d419f
+%{load:$RPM_MACROS_FILE}
8d419f
+Summary: Test systemd RPM macros
8d419f
+Name: systemd-test-rpm-macros
8d419f
+License: LGPLv2+ and MIT and GPLv2+
8d419f
+Version: 1
8d419f
+Release: 1
8d419f
+%description
8d419f
+%{summary}
8d419f
+END_OF_INITIAL_SPEC
8d419f
+EOF
8d419f
+}
8d419f
+
8d419f
+echo "=== Test basic loadability ==="
8d419f
+mk_mini_spec
8d419f
+# ensure its loadability (macros will be just loaded and not used for now)
8d419f
+# also check that rpm supports %load
8d419f
+rpmspec --parse "$RPM_SPEC"
8d419f
+
8d419f
+echo "=== Test %systemd_requires ==="
8d419f
+mk_mini_spec
8d419f
+# The idea of tests is the following:
8d419f
+# - make a minimal spec file
8d419f
+# - add macros into its %description section
8d419f
+# - use rpmspec(8) to print spec file with expanded macros
8d419f
+# - check that macros have been expanded as required.
8d419f
+echo "%systemd_requires" >>"$RPM_SPEC"
8d419f
+: >"$TEMP_LOG"
8d419f
+rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG"
8d419f
+for i in post preun postun; do
8d419f
+    echo "== Requires($i) =="
8d419f
+    grep "^Requires($i): systemd$" "$TEMP_LOG"
8d419f
+done
8d419f
+
8d419f
+echo "=== Test %systemd_ordering ==="
8d419f
+mk_mini_spec
8d419f
+echo "%systemd_ordering" >>"$RPM_SPEC"
8d419f
+: >"$TEMP_LOG"
8d419f
+rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG"
8d419f
+for i in post preun postun; do
8d419f
+    echo "== OrderWithRequires($i) =="
8d419f
+    grep "^OrderWithRequires($i): systemd$" "$TEMP_LOG"
8d419f
+done
8d419f
+
8d419f
+echo "=== Test macros requiring an argument without specifying such argument ==="
8d419f
+for i in \
8d419f
+    systemd_post \
8d419f
+    systemd_preun \
8d419f
+    systemd_postun \
8d419f
+    systemd_postun_with_restart \
8d419f
+    systemd_user_preun \
8d419f
+    systemd_user_postun \
8d419f
+    systemd_user_postun_with_restart \
8d419f
+    tmpfiles_create \
8d419f
+    tmpfiles_create_package \
8d419f
+    sysusers_create \
8d419f
+    sysusers_create_package
8d419f
+do
8d419f
+    echo "== Macro: $i =="
8d419f
+    mk_mini_spec
8d419f
+    echo "%${i}" >>"$RPM_SPEC"
8d419f
+    if rpmspec --parse "$RPM_SPEC"; then
8d419f
+        die "Unexpected pass with macro $i (no arguments)"
8d419f
+    fi
8d419f
+done
8d419f
+
8d419f
+echo "=== Test macros requiring two arguments ==="
8d419f
+for i in \
8d419f
+    tmpfiles_create_package \
8d419f
+    sysusers_create_package
8d419f
+do
8d419f
+    echo "== Macro: $i =="
8d419f
+    # Test with an incorrect number of arguments (0, 1, 3)
8d419f
+    for args in "" "arg1" "arg1 arg2 arg3"; do
8d419f
+        mk_mini_spec
8d419f
+        echo "%${i} $args" >>"$RPM_SPEC"
8d419f
+        if rpmspec --parse "$RPM_SPEC"; then
8d419f
+            die "Unexpected pass with macro $i (arguments: $args)"
8d419f
+        fi
8d419f
+    done
8d419f
+
8d419f
+    # Test with the correct number of arguments (2)
8d419f
+    mk_mini_spec
8d419f
+    echo "%${i} arg1 arg2" >>"$RPM_SPEC"
8d419f
+    if ! rpmspec --parse "$RPM_SPEC"; then
8d419f
+        die "Unexpected fail with macro $i (arguments: $args)"
8d419f
+    fi
8d419f
+done
8d419f
+
8d419f
+
8d419f
+# Test that:
8d419f
+# - *_create_package macros do work correctly
8d419f
+# - shell syntax is correct (https://github.com/systemd/systemd/commit/93406fd37)
8d419f
+# - RPM macros, loaded from macros.in, are actually expanded
8d419f
+echo "=== Test %*_create_package macros ==="
8d419f
+for i in sysusers tmpfiles; do
8d419f
+    echo "== Macro: ${i}_create_package =="
8d419f
+
8d419f
+    PKG_DATA_FILE="$(mktemp "$WORK_DIR/pkg-data-XXX")"
8d419f
+    EXP_OUT="$(mktemp "$WORK_DIR/exp-out-XXX.log")"
8d419f
+    CONF_DIR="$(pkg-config --variable="${i}dir" systemd)"
8d419f
+    EXTRA_ARGS=()
8d419f
+
8d419f
+    if [[ "$i" == tmpfiles ]]; then
8d419f
+        EXTRA_ARGS+=("--create")
8d419f
+    fi
8d419f
+
8d419f
+    echo "TEST_DATA" >"$PKG_DATA_FILE"
8d419f
+    mk_mini_spec
8d419f
+    echo "%${i}_create_package TEST_NAME ${PKG_DATA_FILE}" >>"$RPM_SPEC"
8d419f
+
8d419f
+    cat >"$EXP_OUT" <
8d419f
+systemd-$i --replace=$CONF_DIR/TEST_NAME.conf ${EXTRA_ARGS[*]:+${EXTRA_ARGS[@]} }- <
8d419f
+TEST_DATA
8d419f
+SYSTEMD_INLINE_EOF
8d419f
+EOF
8d419f
+
8d419f
+    : >"$TEMP_LOG"
8d419f
+    rpmspec --parse "$RPM_SPEC" | tee "$TEMP_LOG"
8d419f
+    diff "$EXP_OUT" <(grep -A1 -B1 '^TEST_DATA$' "$TEMP_LOG")
8d419f
+
8d419f
+    rm -f "$PKG_DATA_FILE"
8d419f
+done