summaryrefslogtreecommitdiffstats
path: root/mkosi.coverage
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2024-06-04 10:54:22 +0200
committerDaan De Meyer <daan.j.demeyer@gmail.com>2024-12-05 00:21:57 +0100
commitc45174f05dbb1973038742addf665f3314ecc294 (patch)
treee0d7175c7038c0acf060a87751b6a156530d9286 /mkosi.coverage
parenttest-execute: Make /coverage writable in DynamicUser= tests (diff)
downloadsystemd-c45174f05dbb1973038742addf665f3314ecc294.tar.xz
systemd-c45174f05dbb1973038742addf665f3314ecc294.zip
ci: Implement coverage on top of mkosi
Diffstat (limited to 'mkosi.coverage')
-rw-r--r--mkosi.coverage/mkosi.conf9
-rwxr-xr-xmkosi.coverage/mkosi.postinst56
2 files changed, 65 insertions, 0 deletions
diff --git a/mkosi.coverage/mkosi.conf b/mkosi.coverage/mkosi.conf
new file mode 100644
index 0000000000..a9224195b4
--- /dev/null
+++ b/mkosi.coverage/mkosi.conf
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+[Match]
+Environment=COVERAGE=1
+
+[Content]
+KernelCommandLine=
+ COVERAGE_BUILD_DIR=/coverage
+ systemd.setenv=COVERAGE_BUILD_DIR=/coverage
diff --git a/mkosi.coverage/mkosi.postinst b/mkosi.coverage/mkosi.postinst
new file mode 100755
index 0000000000..ccb153f76d
--- /dev/null
+++ b/mkosi.coverage/mkosi.postinst
@@ -0,0 +1,56 @@
+#!/bin/bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+set -e
+
+(
+ shopt -s nullglob
+ rm -f "$BUILDROOT"/coverage/*.gcda
+)
+
+# When using -fprofile-dir=, GCC creates all gcda files under the given directory at the same location as the
+# gcno file in the build directory, but with each '/' replaced with '#'. LLVM creates each gcda file under
+# the given directory without replacing each '/' with '#'. Because we want all processes to be able to write
+# gcda files under /coverage regardless of which user they are running as, we pre-create all files under
+# /coverage and make them world readable and writable so that we don't have to mess with umasks for each
+# process that writes to /coverage.
+if ((LLVM)); then
+ rsync --recursive --include='*/' --exclude='*' --relative "$BUILDDIR" "$BUILDROOT/coverage"
+ find "$BUILDDIR" -name '*.gcno' | sed 's/gcno/gcda/' | xargs -I '{}' touch "$BUILDROOT/coverage/{}"
+else
+ find "$BUILDDIR" -name '*.gcno' | sed 's/gcno/gcda/' | sed 's/\//#/g' | xargs -I '{}' touch "$BUILDROOT/coverage/{}"
+fi
+
+chmod --recursive 777 "$BUILDROOT/coverage"
+
+# When built with gcov, disable ProtectSystem= and ProtectHome= in the test images, since it prevents gcov to
+# write the coverage reports (*.gcda files).
+mkdir -p "$BUILDROOT/usr/lib/systemd/system/service.d/"
+cat >"$BUILDROOT/usr/lib/systemd/system/service.d/99-gcov-override.conf" <<EOF
+[Service]
+ProtectSystem=no
+ProtectHome=no
+EOF
+
+# Similarly, set ReadWritePaths= to the coverage directory in the test image to make the coverage work with
+# units using DynamicUser=yes. Do this only for services with test- prefix and a couple of known-to-use
+# DynamicUser=yes services, as setting this system-wide has many undesirable side-effects, as it creates its
+# own namespace.
+for service in capsule@ test- systemd-journal-{gatewayd,upload}; do
+ mkdir -p "$BUILDROOT/usr/lib/systemd/system/$service.service.d/"
+ cat >"$BUILDROOT/usr/lib/systemd/system/$service.service.d/99-gcov-rwpaths-override.conf" <<EOF
+[Service]
+ReadWritePaths=/coverage
+EOF
+done
+
+# Ditto, but for the user daemon.
+mkdir -p "$BUILDROOT/usr/lib/systemd/user/test-.service.d/"
+cat >"$BUILDROOT/usr/lib/systemd/user/test-.service.d/99-gcov-rwpaths-override.conf" <<EOF
+[Service]
+ReadWritePaths=/coverage
+EOF
+
+# Bind the coverage directory into nspawn containers that are executed using machinectl. Unfortunately, the
+# .nspawn files don't support drop-ins so we have to inject the bind mount directly into the
+# systemd-nspawn@.service unit.
+sed -ri "s/^ExecStart=.+$/& --bind=\/coverage/" "$BUILDROOT/usr/lib/systemd/system/systemd-nspawn@.service"