Commit d32f00d6 for xz

commit d32f00d601f6ccc48bd5b7ba33b4e96f22f49cf6
Author: Daniel Robbins <drobbins@kernel-seeds.org>
Date:   Sun Aug 9 20:17:52 2026 +0300

    Tests: Add a test for Linux cgroup v2 memory.max and cpu.max

    Co-authored-by: Lasse Collin <lasse.collin@tukaani.org>
    Link: https://github.com/tukaani-project/xz/pull/235

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9699cec9..3490569f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -15,6 +15,7 @@ EXTRA_DIST = \
 	test_compress_generated_text \
 	test_scripts.sh \
 	test_suffix.sh \
+	test_linux_cgroup.sh \
 	xzgrep_expected_output

 AM_CPPFLAGS = \
@@ -71,6 +72,7 @@ TESTS = \
 	test_vli \
 	test_files.sh \
 	test_suffix.sh \
+	test_linux_cgroup.sh \
 	test_compress_generated_abc \
 	test_compress_generated_random \
 	test_compress_generated_text
diff --git a/tests/test_linux_cgroup.sh b/tests/test_linux_cgroup.sh
new file mode 100755
index 00000000..7bab88fa
--- /dev/null
+++ b/tests/test_linux_cgroup.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+# SPDX-License-Identifier: 0BSD
+
+###############################################################################
+#
+# Test that xz uses a Linux cgroup v2 memory.max and cpu.max
+#
+###############################################################################
+
+XZ=${1:-../src/xz/xz}
+test -x "$XZ" || exit 77
+
+# Makefile.am doesn't exclude this test on non-Linux. Use backquotes in this
+# check to keep it compatible with /bin/sh on Solaris 10.
+test "`uname -s`" = "Linux" || exit 77
+
+test -f /proc/self/cgroup || exit 77
+test -f /sys/fs/cgroup/cgroup.controllers || exit 77
+
+# systemd version 236 or later is needed for --collect.
+command -v systemd-run > /dev/null 2>&1 || exit 77
+systemd_version=$(systemd-run --version \
+		| sed -n '1s/^systemd \([0-9]*\) .*$/\1/p')
+test "$systemd_version" -ge 236 || exit 77
+
+# A transient systemd unit gives the test its own cgroup without changing
+# the host's cgroup hierarchy. --pipe is needed to capture xz's output.
+#
+# Use low enough MemoryMax so that the test works on a system with
+# a low amount of RAM.
+MEM=16
+SYSTEMD_RUN="systemd-run --user --quiet --wait --pipe --collect -p MemoryMax=${MEM}M"
+
+# Check that systemd-run is usable. This way failures from xz won't be
+# confused with failures from systemd-run.
+$SYSTEMD_RUN true || exit 77
+
+# The fifth column is the default memory usage limit for -T0. Keep a margin
+# below the cgroup limit because xz itself and allocation overhead also need
+# memory.
+output=$($SYSTEMD_RUN "$XZ" -M0 --robot --info-memory) || exit 1
+default_memlimit=$(printf '%s\n' "$output" | cut -f5)
+test -n "$default_memlimit" || exit 1
+test "$default_memlimit" -le $(($MEM * 1024 * 1024 / 4)) || {
+	echo "xz ignored the cgroup memory limit: $output"
+	exit 1
+}
+
+# Remember the number of threads when CPUQuota isn't set.
+default_threads=$(printf '%s\n' "$output" | cut -f6)
+
+# Percentage limits use the same memory basis as automatic limits.
+# Specify CPUQuota to test cpu.max at the same time.
+output=$($SYSTEMD_RUN -p CPUQuota=277% \
+	"$XZ" -M0 --memlimit-compress=50% --robot --info-memory) || exit 1
+compression_memlimit=$(printf '%s\n' "$output" | cut -f2)
+test "$compression_memlimit" -eq $(($MEM * 1024 * 1024 / 2)) || {
+	echo "xz ignored the cgroup memory limit for percentages: $output"
+	exit 1
+}
+
+limited_threads=$(printf '%s\n' "$output" | cut -f6)
+test "$limited_threads" -eq 2 || test "$default_threads" -eq 1 || {
+	echo "xz ignored the cgroup CPU limit: $output"
+	exit 1
+}
+
+exit 0
diff --git a/tests/tests.cmake b/tests/tests.cmake
index c746bedc..061b1ebe 100644
--- a/tests/tests.cmake
+++ b/tests/tests.cmake
@@ -180,6 +180,18 @@ if(BUILD_TESTING)
         )
     endif()

+    if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+        add_test(NAME test_linux_cgroup.sh
+            COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_linux_cgroup.sh"
+                    "${CMAKE_CURRENT_BINARY_DIR}/xz"
+            WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
+        )
+
+        set_tests_properties(test_linux_cgroup.sh PROPERTIES
+            SKIP_RETURN_CODE 77
+        )
+    endif()
+
     # The test_compress.sh based tests compress and decompress using different
     # filters so run it only if all encoders and decoders have been enabled.
     if(UNIX AND HAVE_ALL_ENCODERS AND HAVE_ALL_DECODERS)