]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: create nspawn config files when collecting coverage
authorFrantisek Sumsal <frantisek@sumsal.cz>
Wed, 17 May 2023 19:49:20 +0000 (21:49 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Thu, 18 May 2023 14:50:24 +0000 (16:50 +0200)
Which bind-mounts the $BUILD_DIR into the container. This whole coverage
thing is getting slightly ridiculous.

Follow-up to 3b2823a749, but for non-machinectl containers.

test/units/testsuite-13.machinectl.sh
test/units/testsuite-13.nspawn-oci.sh
test/units/testsuite-13.nspawn.sh
test/units/util.sh

index 7f8408bd8472d89fd8d3c8685b5a83155d5f5f4c..966b11bc7fb1d6b700b1670f9f9963c77cdc170c 100755 (executable)
@@ -15,6 +15,7 @@ at_exit() {
     machinectl status long-running >/dev/null && machinectl kill --signal=KILL long-running
     mountpoint -q /var/lib/machines && timeout 10 sh -c "while ! umount /var/lib/machines; do sleep .5; done"
     [[ -n "${NSPAWN_FRAGMENT:-}" ]] && rm -f "/etc/systemd/nspawn/$NSPAWN_FRAGMENT" "/var/lib/machines/$NSPAWN_FRAGMENT"
+    rm -f /run/systemd/nspawn/*.nspawn
 }
 
 trap at_exit EXIT
index 1c241addc6f503fd8c065d096d0ea05787a3de87..6329d25f3b78202d4647c521543aa9674f24d8cf 100755 (executable)
@@ -18,6 +18,7 @@ at_exit() {
     [[ -n "${DEV:-}" ]] && rm -f "$DEV"
     [[ -n "${NETNS:-}" ]] && umount "$NETNS" && rm -f "$NETNS"
     [[ -n "${TMPDIR:-}" ]] && rm -fr "$TMPDIR"
+    rm -f /run/systemd/nspawn/*.nspawn
 }
 
 trap at_exit EXIT
index 93d9937dabe45fdedf81c5b8a8fa0eaabeb98132..c449bda865cb8f595ecdc458020debd4839d5808 100755 (executable)
@@ -1,6 +1,25 @@
 #!/usr/bin/env bash
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # shellcheck disable=SC2016
+#
+# Notes on coverage: when collecting coverage we need the $BUILD_DIR present
+# and writable in the container as well. To do this in the least intrusive way,
+# two things are going on in the background (only when built with -Db_coverage=true):
+#   1) the systemd-nspawn@.service is copied to /etc/systemd/system/ with
+#      --bind=$BUILD_DIR appended to the ExecStart= line
+#   2) each create_dummy_container() call also creates an .nspawn file in /run/systemd/nspawn/
+#      with the last fragment from the path used as a name
+#
+# The first change is quite self-contained and applies only to containers run
+# with machinectl. The second one might cause some unexpected side-effects, namely:
+#   - nspawn config (setting) files don't support dropins, so tests that test
+#     the config files might need some tweaking (as seen below with
+#     the $COVERAGE_BUILD_DIR shenanigans) since they overwrite the .nspawn file
+#   - also a note - if /etc/systemd/nspawn/cont-name.nspawn exists, it takes
+#     precedence and /run/systemd/nspawn/cont-name.nspawn won't be read even
+#     if it exists
+#   - in some cases we don't create a test container using create_dummy_container(),
+#     so in that case an explicit call to coverage_create_nspawn_dropin() is needed
 set -eux
 set -o pipefail
 
@@ -14,6 +33,7 @@ at_exit() {
     set +e
 
     mountpoint -q /var/lib/machines && umount /var/lib/machines
+    rm -f /run/systemd/nspawn/*.nspawn
 }
 
 trap at_exit EXIT
@@ -67,6 +87,7 @@ testcase_sanity() {
 
     # --template=
     root="$(mktemp -u -d /var/lib/machines/testsuite-13.sanity.XXX)"
+    coverage_create_nspawn_dropin "$root"
     (! systemd-nspawn --directory="$root" bash -xec 'echo hello')
     # Initialize $root from $template (the $root directory must not exist, hence
     # the `mktemp -u` above)
@@ -523,8 +544,10 @@ testcase_ephemeral_config() {
     container_name="$(basename "$root")"
 
     mkdir -p /run/systemd/nspawn/
+    rm -f "/etc/systemd/nspawn/$container_name.nspawn"
     cat >"/run/systemd/nspawn/$container_name.nspawn" <<EOF
 [Files]
+${COVERAGE_BUILD_DIR:+"Bind=$COVERAGE_BUILD_DIR"}
 BindReadOnly=/tmp/ephemeral-config
 EOF
     touch /tmp/ephemeral-config
index cd4ddcdb2c8579880526e4dd21b8335951ead66a..fdba709734eb25b87bf9dac6626a29948bb2c00a 100755 (executable)
@@ -81,6 +81,23 @@ runas() {
     XDG_RUNTIME_DIR=/run/user/"$(id -u "$userid")" setpriv --reuid="$userid" --init-groups "$@"
 }
 
+coverage_create_nspawn_dropin() {
+    # If we're collecting coverage, bind mount the $BUILD_DIR into the nspawn
+    # container so gcov can update the counters. This is mostly for standalone
+    # containers, as machinectl stuff is handled by overriding the systemd-nspawn@.service
+    # (see test/test-functions:install_systemd())
+    local root="${1:?}"
+    local container
+
+    if [[ -z "${COVERAGE_BUILD_DIR:-}" ]]; then
+        return 0
+    fi
+
+    container="$(basename "$root")"
+    mkdir -p "/run/systemd/nspawn"
+    echo -ne "[Files]\nBind=$COVERAGE_BUILD_DIR\n" >"/run/systemd/nspawn/${container:?}.nspawn"
+}
+
 create_dummy_container() {
     local root="${1:?}"
 
@@ -91,4 +108,5 @@ create_dummy_container() {
 
     mkdir -p "$root"
     cp -a /testsuite-13-container-template/* "$root"
+    coverage_create_nspawn_dropin "$root"
 }