eBPF-based security platform
testing: Run end-to-end target tests over SSH
Martynas Pumputisc5a61a0ed708
diff --git a/.agents/skills/testing/SKILL.md b/.agents/skills/testing/SKILL.mdindex 58fa2564..f59ac6ad 100644--- a/.agents/skills/testing/SKILL.md+++ b/.agents/skills/testing/SKILL.md@@ -66,7 +66,10 @@ parser of input from outside the host gets a target in `testing/fuzz/`.- A family's target tests print only `[test-vm] result: PASS` or`FAIL`. Their TAP output is in`build/test-vm/vms/<name>/result/guest.log`, the exit status in- `result/status`.+ `result/status`. The end-to-end VM's tests run over SSH from the+ host, which appends their output to the same `guest.log`; there+ `result/status` is only the guest's preparation, and `the SSH session+ that ran the target tests failed` is the channel failing, not a test.- A fuzz failure leaves its input in `testing/fuzz/crash-<target>-*`.Run the target binary on that file to reproduce it. An `abort()` inthe harness is a property that failed, which can be the harness's own.@@ -154,7 +157,9 @@ make -C testing vm-clean VM_NAME=<name>Adding `VM_SSH_COMMAND='<command>'` to `vm-ssh` runs one command in theguest and returns. In the guest, `shirudo-runtime` is the runtime the-initrd verified; `shirudo-runtime status` shows an initialized target.+initrd verified; `shirudo-runtime status` shows an initialized target,+and `. /etc/shirudo-test-vm.env` sets the environment a family run+gives the target tests.The checkout is mounted read-only at `/workspace/shirudo`. Without`VM_NAME`, `vm-clean` removes all of `build/test-vm/`, the bundles andthe cached runtime image included, so always name the VM.diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 329e1595..42b6abf0 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -96,8 +96,8 @@ signed config image.``--dry-run`` plan* - ``t1007-test-vm-cli``- Test VM scripts: kernel selection, host dependencies, arm64- boot settings, the bundle stamp, node mode only for a core or- edge node of the end-to-end tier+ boot settings, the bundle stamp, serve mode, node mode only for a+ core or edge node of the end-to-end tier* - ``t1008-kernel-ami-config``- The slim kernel configuration for NitroTPM CRB, EC2, EFI and SSM* - ``t1009-upatch-isa``diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex 378edbc8..d89ab522 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -329,6 +329,11 @@ Ubuntu cloud image and a software TPM, and runs ``init`` from a dracutinitrd with the dm-verity runtime image and a signed config image. Thefamily runs, the end-to-end run and the interactive VM all boot it.+A family's VM runs its target tests itself and powers off. The+end-to-end VM prepares itself the same way and waits while the host runs+its target tests over SSH, then powers off. The interactive VM stays up+until you stop it.+Build the test kernel~~~~~~~~~~~~~~~~~~~~~@@ -477,9 +482,11 @@ Boot an interactive VM* - ``TEST_VM_QEMU=<path>``- Another emulator, such as RHEL's ``/usr/libexec/qemu-kvm``.* - ``TEST_VM_TIMEOUT=<seconds>``- - How long a test-mode VM may run, 3600 by default. CI sets 900.+ - How long a VM that runs tests may run, 3600 by default. CI sets+ 900.* - ``TEST_VM_VERBOSE=1``- - Streams the console of a test-mode VM to the terminal.+ - Streams the console of a VM that runs tests to the terminal, and+ what the end-to-end tests print over SSH.Inside the guest:@@ -502,6 +509,8 @@ Inside the guest:* - ``/mnt/shirudo-out``- The result share, ``build/test-vm/vms/<name>/result/`` on thehost.+ * - ``/etc/shirudo-test-vm.env``+ - The environment the target tests run with... _testing-by-hand:@@ -518,9 +527,7 @@ Run a target test by hand.. code-block:: console- target# export SHIRUDO_BIN=/usr/local/bin/shirudo-runtime- target# export SHIRUDO_TEST_FIXTURES_DIR=/mnt/shirudo-bundle/fixtures- target# export SHIRUDO_TEST_KEY_FIXTURES=/mnt/shirudo-bundle/verity+ target# . /etc/shirudo-test-vm.env3. Run one test, or all target tests of a family. The ``image`` testsneed the result directories that ``run-target-tests.sh`` sets, so@@ -622,7 +629,9 @@ CA. It builds ``kernel-obs`` into a package with ``bpm build``, pushesit with ``bpm push`` and appends it to a signed upatch tree with``bpm tree append``. The targetactivates a signed config that points at the tree and the collector,-and its units pull, verify, load and export. The host then verifies the+and its units pull, verify, load and export. The VM's tests, ``t91??``,+run over SSH from the host, which writes what they print to+``result/guest.log`` as a family's VM does. The host then verifies thetarget's TPM evidence with ``attest verify`` and checks that thecollector holds the package's records. The earlier ``kernel-obs`` testsexport each map snapshot as local JSON and OTLP from one read; the hostdiff --git a/testing/Makefile b/testing/Makefileindex 3a725856..c3419021 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -245,7 +245,8 @@ vm-ssh:TEST_VM_WORK_DIR="$(TEST_VM_WORK_DIR)" $(ROOT)/testing/vm/run.sh --sshdefine run-target-vm-VM_MODE=$(or $(4),test) TEST_VM_SUITE="$(1)" VM_VERBOSE="$(TEST_VM_VERBOSE)" VM_NAME="$(2)" VM_TENANT="$(VM_TENANT)" \+VM_MODE=$(or $(4),$(if $(filter $(1),$(VM_SERVE_SUITES)),serve,test)) \+ TEST_VM_SUITE="$(1)" VM_VERBOSE="$(TEST_VM_VERBOSE)" VM_NAME="$(2)" VM_TENANT="$(VM_TENANT)" \TEST_VM_INITRD="$(3)" \TEST_VM_KERNEL="$(abspath $(VM_KERNEL_IMAGE))" \TEST_VM_KERNEL_ARTIFACT_DIR="$(abspath $(VM_KERNEL_ARTIFACT_DIR))" \@@ -278,6 +279,9 @@ family-list = $(shell cd $(ROOT)/testing/functional && \awk -F/ '{ print substr($$2, 2, 1), $$1 }' | sort -u | cut -d' ' -f2)FAMILIES := $(call family-list,[0-9])VM_SUITES := $(call family-list,1) end2end+# The host runs the end-to-end VM's target tests over SSH, where it can act+# between them; the families' VMs run theirs themselves.+VM_SERVE_SUITES := end2endvm-tests = $(if $(filter 0,$(VM)),:,$(MAKE) $(addprefix vm-test-,$(1))).PHONY: $(addprefix unit/,$(UNIT_SUITES)) $(addprefix functional/,$(FAMILIES)) \$(addprefix vm-test-,register $(VM_SUITES)) vm-node-end2enddiff --git a/testing/functional/build/t1007-test-vm-cli.sh b/testing/functional/build/t1007-test-vm-cli.shindex 9ccb5bc6..7984561f 100755--- a/testing/functional/build/t1007-test-vm-cli.sh+++ b/testing/functional/build/t1007-test-vm-cli.sh@@ -511,13 +511,31 @@ test_expect_success 'test harness records a failed test title' 'test_expect_success 'make -C testing vm and test-vm share named mode support' 'grep -q "^[[:space:]]*vm:" ../testing/Makefile &&grep -q "VM_MODE=interactive" ../testing/Makefile &&- grep -Fq "VM_MODE=\$(or \$(4),test)" ../testing/Makefile &&+ grep -Fq "VM_MODE=\$(or \$(4),\$(if \$(filter \$(1),\$(VM_SERVE_SUITES)),serve,test))" \+ ../testing/Makefile &&grep -Fq "VMS_DIR=\"\${VM_ROOT}/vms\"" vm/run.sh &&grep -Fq "mktemp -d \"\${VMS_DIR}/\${VM_MODE}\${VM_SUITE:+-\$VM_SUITE}-XXXXXX\"" \vm/run.sh &&grep -q "VM_TENANT" vm/run.sh &&grep -q "while :; do sleep 3600; done" vm/run.sh'++test_expect_success 'serve mode runs the end-to-end target tests over SSH' '+ grep -q "^VM_SERVE_SUITES := end2end\$" ../testing/Makefile &&+ ! VM_MODE=other "$RUNNER" >"$WORK/mode.out" 2>&1 &&+ grep -q "VM_MODE must be .interactive., .serve., .test. or .node." \+ "$WORK/mode.out" &&+ ! VM_MODE=serve TEST_VM_SUITE= "$RUNNER" >"$WORK/serve-suite.out" 2>&1 &&+ grep -q "serve mode requires TEST_VM_SUITE" "$WORK/serve-suite.out" &&+ ! TEST_VM_SUITE=register VM_MODE=serve TEST_VM_INITRD=0 \+ "$RUNNER" >"$WORK/serve-register.out" 2>&1 &&+ grep -q "register suite requires test mode" "$WORK/serve-register.out" &&+ grep -q "} >/etc/shirudo-test-vm.env" vm/guest.sh &&+ grep -Fq ". /etc/shirudo-test-vm.env && exec ./run-target-tests.sh \$VM_SUITE" \+ vm/run.sh &&+ grep -Fq "while [ ! -e /mnt/shirudo-out/host-done ]" vm/run.sh &&+ grep -Fq ": >\"\$RUN_DIR/result/host-done\"" vm/run.sh+'test_expect_success 'make -C testing vm boots a release kernel at or above the floor' 'grep -q "VM_KERNEL_MIN := 7.4" ../testing/Makefile &&grep -q "make -C testing vm KERNEL=7.4" ../testing/Makefile &&diff --git a/testing/vm/guest.sh b/testing/vm/guest.shindex 801a1038..725cb4a6 100755--- a/testing/vm/guest.sh+++ b/testing/vm/guest.sh@@ -259,6 +259,23 @@ configure_image_identity()mv -f /etc/os-release.tmp /etc/os-release}+# Target tests run from this script in test mode and from an SSH session+# otherwise, which inherits nothing from it.+write_target_test_env()+{+ {+ printf 'export SHIRUDO_BIN=%q\n' "$SHIRUDO_BIN"+ printf 'export SHIRUDO_TEST_KEY_FIXTURES=%q\n' "$BUNDLE/verity"+ printf 'export SHIRUDO_TEST_RESULTS=%q\n' /mnt/shirudo-out/verity+ printf 'export SHIRUDO_TEST_FIXTURES_DIR=%q\n' "$BUNDLE/fixtures"+ printf 'export SHIRUDO_TEST_FAILURES_FILE=%q\n' \+ /mnt/shirudo-out/failed-tests+ printf 'export SHIRUDO_TEST_SUITE_FAILURES_FILE=%q\n' \+ /mnt/shirudo-out/failed-suites+ printf 'export TEST_FILTER=%q\n' "${TEST_FILTER:-}"+ } >/etc/shirudo-test-vm.env+}+configure_oci_caconfigure_bpm_oci_repositoryconfigure_map_export@@ -464,6 +481,7 @@ if [[ -n "${TEST_VM_BPM_TREE_INPUT:-}" ]]; thenfireport_boot_memory+write_target_test_envif [[ "$VM_MODE" == node ]]; thenlog "node prepared; node mode runs no tests and starts no timers"@@ -488,17 +506,17 @@ if [[ "$VM_MODE" == interactive ]]; thenexit 0fi-log "running target tests: $TEST_VM_SUITE": >/mnt/shirudo-out/failed-tests: >/mnt/shirudo-out/failed-suites+if [[ "$VM_MODE" == serve ]]; then+ log "guest prepared; the host runs the target tests over SSH"+ exit 0+fi++log "running target tests: $TEST_VM_SUITE"(cd testing- SHIRUDO_BIN="$SHIRUDO_BIN" \- SHIRUDO_TEST_KEY_FIXTURES="$BUNDLE/verity" \- SHIRUDO_TEST_RESULTS=/mnt/shirudo-out/verity \- SHIRUDO_TEST_FIXTURES_DIR="$BUNDLE/fixtures" \- SHIRUDO_TEST_FAILURES_FILE=/mnt/shirudo-out/failed-tests \- SHIRUDO_TEST_SUITE_FAILURES_FILE=/mnt/shirudo-out/failed-suites \- ./run-target-tests.sh "$TEST_VM_SUITE"+ . /etc/shirudo-test-vm.env+ ./run-target-tests.sh "$TEST_VM_SUITE")log "integration suite passed"diff --git a/testing/vm/run.sh b/testing/vm/run.shindex c0a6ac48..2ae8cadf 100755--- a/testing/vm/run.sh+++ b/testing/vm/run.sh@@ -4,17 +4,19 @@## Boot an Ubuntu guest with a selected Linux kernel and an swtpm vTPM.# In test mode it runs the target tests of the family TEST_VM_SUITE names,-# or the TPM register tests for "register"; in interactive mode it prepares-# the guest and leaves QEMU and swtpm running. Node mode boots another-# node of the end2end suite, with the role TEST_VM_NODE_ROLE names, core-# by default or edge: prepared like the suite's VM, with no tests and no-# timers, and left running until TEST_VM_TIMEOUT or a --stop.+# or the TPM register tests for "register"; in serve mode the guest only+# prepares itself and the host runs the family's target tests in it over+# SSH; in interactive mode it prepares the guest and leaves QEMU and swtpm+# running. Node mode boots another node of the end2end suite, with the role+# TEST_VM_NODE_ROLE names, core by default or edge: prepared like the+# suite's VM, with no tests and no timers, and left running until+# TEST_VM_TIMEOUT or a --stop.#-# VM_MODE=test|interactive|node selects the mode. VM_NAME optionally selects a-# stable state directory; if omitted, a unique name is allocated atomically.-# VM_TENANT is used as the map-export tenant when OTEL export is enabled.-# VM_VERBOSE=1 streams test-mode QEMU output to the terminal as well as the-# console log.+# VM_MODE=test|serve|interactive|node selects the mode. VM_NAME optionally+# selects a stable state directory; if omitted, a unique name is allocated+# atomically. VM_TENANT is used as the map-export tenant when OTEL export is+# enabled. VM_VERBOSE=1 streams the QEMU output of a VM that runs tests to+# the terminal as well as the console log.set -euo pipefail@@ -120,6 +122,7 @@ QEMU_UNIT_FILE=""QEMU_STATUS=1PROGRESS_LINES=0GUEST_STATUS=""+SERVE_STATUS=0GUEST_MEMORY_REPORT=""HOST_MEMORY_REPORT=""FAILURE_LOG_DIR=""@@ -172,19 +175,19 @@ validate_mode()die "dm-verity requires TEST_VM_INITRD=1 and TEST_VM_SKIP_SEAL=0"ficase "$VM_MODE" in- interactive|test) ;;+ interactive|serve|test) ;;node)[[ "$VM_SUITE" == end2end ]] ||die "node mode boots a node of the end2end suite only"[[ "$NODE_ROLE" == core || "$NODE_ROLE" == edge ]] ||die "TEST_VM_NODE_ROLE must be 'core' or 'edge'";;- *) die "VM_MODE must be 'interactive', 'test' or 'node'" ;;+ *) die "VM_MODE must be 'interactive', 'serve', 'test' or 'node'" ;;esaccase "$VM_SUITE" in'')[[ "$VM_MODE" == interactive ]] ||- die "test mode requires TEST_VM_SUITE: register or a test family"+ die "$VM_MODE mode requires TEST_VM_SUITE: register or a test family";;register)[[ "$VM_MODE" == test && "$VM_INITRD" == 0 && "$SKIP_SEAL" == 0 ]] ||@@ -669,14 +672,75 @@ ssh_options()-o GlobalKnownHostsFile=/dev/null-o UserKnownHostsFile="${SSH_DIR}/known_hosts"-o IdentityFile="${SSH_DIR}/id_ed25519"+ -o ServerAliveInterval=15+ -o ServerAliveCountMax=8-p "$SSH_PORT")}-run_ssh()+wait_for_ssh()+{+ local _++ for _ in {1..60}; do+ ssh "${SSH_ARGS[@]}" -o BatchMode=yes [email protected] true \+ >/dev/null 2>&1 && return 0+ sleep 1+ done+ return 1+}++# The family's target tests, run in the prepared guest from an SSH session+# with the environment the guest wrote for them. What they print goes to+# the guest log, where test mode puts it.+run_target_tests_over_ssh()+{+ local guest_log="$RUN_DIR/result/guest.log" pid status++ ssh_options+ wait_for_ssh || {+ log "error: SSH did not become ready for VM '$VM_NAME'"+ return 255+ }+ log "running target tests over SSH: $VM_SUITE"+ : >>"$guest_log"+ GUEST_LOG_BYTES="$(wc -c <"$guest_log")"+ ssh "${SSH_ARGS[@]}" -o BatchMode=yes [email protected] \+ "cd /workspace/shirudo/testing && . /etc/shirudo-test-vm.env && exec ./run-target-tests.sh $VM_SUITE" \+ </dev/null >>"$guest_log" 2>&1 &+ pid=$!+ while kill -0 "$pid" 2>/dev/null; do+ show_progress+ sleep 0.2+ done+ wait "$pid"+ status=$?+ show_progress+ return "$status"+}++# A serve-mode guest that prepared itself waits for the host-done marker in+# its result share, then powers itself off as a test-mode guest does.+serve_target_tests(){- local ready=0+ local guest_status="" _++ if [[ -s "$RUN_DIR/result/status" ]]; then+ guest_status="$(<"$RUN_DIR/result/status")"+ fi+ [[ "$guest_status" == 0 ]] || return 0+ run_target_tests_over_ssh+ SERVE_STATUS=$?+ : >"$RUN_DIR/result/host-done"+ for _ in {1..1200}; do+ kill -0 "$QEMU_PID" 2>/dev/null || return 0+ sleep 0.1+ done+ die "VM '$VM_NAME' did not power off after its target tests (status $SERVE_STATUS)"+}+run_ssh()+{if [[ -z "$VM_NAME_REQUEST" ]]; thenselect_latest_run_dirVM_NAME_REQUEST="$VM_NAME"@@ -690,15 +754,7 @@ run_ssh()[[ "$SSH_PORT" =~ ^[0-9]+$ ]] ||die "VM '$VM_NAME' has an invalid SSH port"ssh_options- for _ in {1..60}; do- if ssh "${SSH_ARGS[@]}" -o BatchMode=yes \- [email protected] true >/dev/null 2>&1; then- ready=1- break- fi- sleep 1- done- [[ "$ready" -eq 1 ]] ||+ wait_for_ssh ||die "SSH did not become ready for VM '$VM_NAME'"if [[ -n "$VM_SSH_COMMAND" ]]; thenexec ssh "${SSH_ARGS[@]}" [email protected] "$VM_SSH_COMMAND"@@ -831,7 +887,7 @@ print_test_summary()local result_share_warning=0declare -A recorded_suites=()- [[ "$VM_MODE" == test ]] || return 0+ [[ "$VM_MODE" == test || "$VM_MODE" == serve ]] || return 0[[ -n "$RUN_DIR" && -d "$RUN_DIR" ]] || return 0guest_log="$RUN_DIR/result/guest.log"@@ -923,7 +979,8 @@ print_quiet_progress()local guest_log="$RUN_DIR/result/guest.log"local line_count- [[ "$VM_MODE" == test && "$VM_VERBOSE" != 1 ]] || return 0+ [[ ( "$VM_MODE" == test || "$VM_MODE" == serve ) &&+ "$VM_VERBOSE" != 1 ]] || return 0[[ -f "$guest_log" ]] || return 0line_count="$(wc -l <"$guest_log")"[[ "$line_count" =~ ^[0-9]+$ ]] || return 0@@ -938,13 +995,15 @@ print_quiet_progress()}CONSOLE_BYTES=0+GUEST_LOG_BYTES=0stream_verbose_console(){local console_log="$RUN_DIR/console.log"local byte_count- [[ "$VM_MODE" == test && "$VM_VERBOSE" == 1 ]] || return 0+ [[ ( "$VM_MODE" == test || "$VM_MODE" == serve ) &&+ "$VM_VERBOSE" == 1 ]] || return 0[[ -f "$console_log" ]] || return 0byte_count="$(wc -c <"$console_log")"[[ "$byte_count" =~ ^[0-9]+$ ]] || return 0@@ -953,6 +1012,31 @@ stream_verbose_console()CONSOLE_BYTES="$byte_count"}+# A serve-mode guest's tests print over SSH, not to its console.+stream_verbose_tests()+{+ local guest_log="$RUN_DIR/result/guest.log"+ local byte_count++ [[ "$VM_MODE" == serve && "$VM_VERBOSE" == 1 ]] || return 0+ [[ -f "$guest_log" ]] || return 0+ byte_count="$(wc -c <"$guest_log")"+ [[ "$byte_count" =~ ^[0-9]+$ ]] || return 0+ (( byte_count > GUEST_LOG_BYTES )) || return 0+ tail -c "+$((GUEST_LOG_BYTES + 1))" "$guest_log"+ GUEST_LOG_BYTES="$byte_count"+}++show_progress()+{+ if [[ "$VM_VERBOSE" == 1 ]]; then+ stream_verbose_console+ stream_verbose_tests+ else+ print_quiet_progress+ fi+}+cleanup(){if [[ -n "$SSH_PORT_LOCK_FD" ]]; then@@ -967,7 +1051,8 @@ cleanup()if [[ "$FINAL_STATUS" -ne 0 ]]; thensave_failure_logsfi- if [[ "$VM_MODE" == test && -n "$RUN_DIR" && -d "$RUN_DIR" ]]; then+ if [[ ( "$VM_MODE" == test || "$VM_MODE" == serve ) &&+ -n "$RUN_DIR" && -d "$RUN_DIR" ]]; thenprint_test_summaryif [[ "$FINAL_STATUS" -eq 0 ]]; then# A passed run keeps its logs, not the disks it booted.@@ -1543,9 +1628,13 @@ cat >>"$USER_DATA" <<'EOF'fiprintf '%s\n' "$rc" >/mnt/shirudo-out/statussync- if [ "${VM_MODE:-test}" != test ] && [ "$rc" -eq 0 ]; then+ if { [ "${VM_MODE:-test}" = interactive ] ||+ [ "${VM_MODE:-test}" = node ]; } && [ "$rc" -eq 0 ]; thenwhile :; do sleep 3600; donefi+ if [ "${VM_MODE:-test}" = serve ] && [ "$rc" -eq 0 ]; then+ while [ ! -e /mnt/shirudo-out/host-done ]; do sleep 1; done+ fipoweroffsleep 10poweroff -f@@ -1793,24 +1882,17 @@ elseQEMU_PID=$!wait_for_ssh_listenerwhile :; do- if [[ "$VM_VERBOSE" == 1 ]]; then- stream_verbose_console- else- print_quiet_progress- fi+ show_progress[[ -s "$RUN_DIR/result/status" ]] && breakkill -0 "$QEMU_PID" 2>/dev/null || breaksleep 0.2donecapture_host_memory_report+ [[ "$VM_MODE" != serve ]] || serve_target_testswait "$QEMU_PID"QEMU_STATUS=$?for _ in {1..5}; do- if [[ "$VM_VERBOSE" == 1 ]]; then- stream_verbose_console- else- print_quiet_progress- fi+ show_progresssleep 0.1doneset -e@@ -1822,6 +1904,11 @@ elsedie "QEMU exited with status $QEMU_STATUS"QEMU_PID=""read_guest_status+ case "$SERVE_STATUS" in+ 0) ;;+ 255) die "the SSH session that ran the target tests failed" ;;+ *) die "target tests failed over SSH with status $SERVE_STATUS" ;;+ esacFINAL_STATUS=0fi