richardphibel / rpms / systemd

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