From ccd75db12e295e8e51b0effa1981d2a4cdaddf49 Mon Sep 17 00:00:00 2001 From: Jakub Czajka Date: Wed, 15 Jul 2026 22:49:55 +0000 Subject: [PATCH] [test] Add VPS test harness infrastructure. Boot a marionette VM that inherits from vps-operating-system with static networking and a throwaway SSH key for dak. Add test-vps and test-vps-fresh Makefile targets with cache. Cover SSH, NTP, the home packages, and home activation: the activation test runs vps-home-environment's activate script as dak. --- .claude/hooks/block-builds | 2 +- Makefile | 19 ++++++++ tests/base.scm | 38 ++++++++++++++++ tests/vps-home.scm | 84 +++++++++++++++++++++++++++++++++++ tests/vps-system.scm | 91 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 tests/base.scm create mode 100644 tests/vps-home.scm create mode 100644 tests/vps-system.scm diff --git a/.claude/hooks/block-builds b/.claude/hooks/block-builds index 924c138..ce3922d 100755 --- a/.claude/hooks/block-builds +++ b/.claude/hooks/block-builds @@ -33,7 +33,7 @@ case "$command" in '}}' exit 0 ;; - *'make build-'*) + *'make build-'*|*'make test-'*) $GUIX_BIN/echo \ '{"hookSpecificOutput": {' \ ' "hookEventName": "PreToolUse",' \ diff --git a/Makefile b/Makefile index a6ab116..46a68a4 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,25 @@ build-vps-home: build-vps: build-vps-system build-vps-home +test-vps: build-vps-system build-vps-home + @out=$$(GUIX_PACKAGE_PATH=$(CURDIR) guix build -L $(CURDIR) \ + -e '(@ (tests base) %vps-system-test)' 2>/dev/null); \ + if [ -n "$$out" ] && [ -e "$$out" ]; then \ + echo "📋 Cached: $$out"; \ + echo " Run 'make test-vps-fresh' to re-execute."; \ + else \ + GUIX_PACKAGE_PATH=$(CURDIR) guix build -L $(CURDIR) \ + -e '(@ (tests base) %vps-system-test)'; \ + fi + +test-vps-fresh: build-vps-system build-vps-home + @out=$$(GUIX_PACKAGE_PATH=$(CURDIR) guix build -L $(CURDIR) \ + -e '(@ (tests base) %vps-system-test)' 2>/dev/null); \ + [ -n "$$out" ] && [ -e "$$out" ] \ + && guix gc --delete "$$out" 2>/dev/null; \ + GUIX_PACKAGE_PATH=$(CURDIR) guix build -L $(CURDIR) \ + -e '(@ (tests base) %vps-system-test)' + tags: find $(guix-store) -wholename "*guix-$(guix-version)*.scm" -or \ -wholename "*guix-module-union*.scm" -or \ diff --git a/tests/base.scm b/tests/base.scm new file mode 100644 index 0000000..412832b --- /dev/null +++ b/tests/base.scm @@ -0,0 +1,38 @@ +;; Copyright (c) 2026 Jakub Czajka +;; License: GPL-3.0 or later. +;; +;; The VPS VM test harness. + +(define-module (tests base) + #:use-module (gnu tests) + #:use-module (gnu system vm) + #:use-module (guix gexp) + #:use-module (tests vps-home) + #:use-module (tests vps-system) + #:export (%vps-system-test)) + +(define (run-vps-test test-cases) + (let* ((vm (virtual-machine (operating-system + %vps-test-os) + (memory-size 1024))) + (builder #~(begin + (use-modules (gnu build marionette) + (srfi srfi-64)) + ;; -cpu host requires KVM. + (define marionette + (make-marionette (list #$vm "-cpu" "host"))) + (marionette-eval '(use-modules (gnu services herd)) + marionette) + (test-runner-current (system-test-runner #$output)) + (test-begin "vps") + #$@(map (lambda (test-case) + (test-case #~marionette)) test-cases) + (test-end "vps")))) + (gexp->derivation "vps-test" builder + #:modules '((gnu build marionette))))) + +(define %vps-system-test + (system-test (name "vps") + (description "VPS test suite.") + (value (run-vps-test (list sshd-test-cases daemons-test-cases + vps-home-test-cases))))) diff --git a/tests/vps-home.scm b/tests/vps-home.scm new file mode 100644 index 0000000..7fcc3de --- /dev/null +++ b/tests/vps-home.scm @@ -0,0 +1,84 @@ +;; Copyright (c) 2026 Jakub Czajka +;; License: GPL-3.0 or later. +;; +;; Run the Guix Home activate script and then the home tests. + +(define-module (tests vps-home) + #:use-module (gnu packages admin) + #:use-module (gnu packages base) + #:use-module (conf home claude) + #:use-module (conf home paseo) + #:use-module (guix gexp) + #:use-module (tests vps-system) + #:use-module (vps-home) + #:export (vps-home-test-cases)) + +(define %activate + (file-append vps-home-environment "/activate")) +(define %timeout + (file-append coreutils "/bin/timeout")) + +(define %home-prelude + `(begin + (use-modules (ice-9 popen) + (ice-9 rdelim)) + + (define (run-command-output command) + (let* ((port (open-input-pipe command)) + (output (read-string port))) + (list (eqv? 0 + (status:exit-val (close-pipe port))) output))) + + (define runtime-dir + (format #f "/run/user/~a" + (passwd:uid (getpwnam ,%test-user)))) + (system* ,(file-append coreutils "/bin/install") "--directory" + "--mode=700" + ,(string-append "--owner=" %test-user) runtime-dir) + (define (run command) + (let ((prefix (format #f + "~a 90 ~a -n -u ~a -- ~a XDG_RUNTIME_DIR=" + ,%timeout + ,(file-append sudo "/bin/sudo") + ,%test-user + ,(file-append coreutils "/bin/env")))) + (run-command-output (string-append prefix runtime-dir " " command + " 2>&1")))))) + +(define %home-activation-body + `(let ((result (run (string-append ,%activate " > /tmp/home-activate.log")))) + (unless (car result) + (format (current-error-port) "~a" + (call-with-input-file "/tmp/home-activate.log" + read-string))) + (car result))) + +(define (assert-home marionette label body) + #~(test-assert #$label + (let ((result (marionette-eval '#$body + #$marionette))) + (or (eq? result #t) + (begin + (format (current-error-port) "~a: ~s~%" + #$label result) #f))))) + +(define (vps-home-test-cases marionette) + #~(begin + (marionette-eval '#$%home-prelude + #$marionette) + #$(assert-home marionette "home: vps-home activates" + %home-activation-body) + (test-assert "home: dak user exists" + (marionette-eval '(file-exists? "/home/dak") + #$marionette)) + (test-assert "home: claude binary exists" + (marionette-eval '(file-exists? (string-append #$claude-code + "/bin/claude")) + #$marionette)) + (test-assert "home: paseo binary exists" + (marionette-eval '(file-exists? (string-append #$paseo + "/bin/paseo")) + #$marionette)) + (test-assert "home: .guile exists" + (marionette-eval '(file-exists? "/home/dak/.guile") + #$marionette)))) diff --git a/tests/vps-system.scm b/tests/vps-system.scm new file mode 100644 index 0000000..3fc5ef2 --- /dev/null +++ b/tests/vps-system.scm @@ -0,0 +1,91 @@ +;; Copyright (c) 2026 Jakub Czajka +;; License: GPL-3.0 or later. +;; +;; System tests and the VM test operating system. + +(define-module (tests vps-system) + #:use-module (gnu tests) + #:use-module (gnu system) + #:use-module ((gnu services) + #:select ((delete . remove-service) service modify-services)) + #:use-module (gnu services base) + #:use-module (gnu services ssh) + #:use-module (gnu services networking) + #:use-module (gnu packages ssh) + #:use-module (conf vps sshd) + #:use-module (guix gexp) + #:use-module (vps-system) + #:export (%vps-test-os %test-user daemons-test-cases sshd-test-cases)) + +(define %test-user + "dak") + +(define %sshd-login-keypair + (computed-file "sshd-login-keypair" + #~(begin + (mkdir #$output) + (chdir #$output) + (let ((keygen #$(file-append openssh "/bin/ssh-keygen")) + (args '("-t" "ed25519" "-f" "login-key" "-N" ""))) + (unless (zero? (apply system* keygen args)) + (exit 1)))))) + +(define %sshd-key-auth-body + `(let ((ssh ,(file-append openssh "/bin/ssh")) + (key "/tmp/login-key")) + (copy-file ,(file-append %sshd-login-keypair "/login-key") key) + (chmod key #o600) + (zero? (system* ssh "-n" "-i" key + ,@(append '("-oStrictHostKeyChecking=no" + "-oUserKnownHostsFile=/dev/null" + "-oBatchMode=yes" "-oConnectTimeout=10" "-p") + (list (number->string %sshd-port) + (string-append %test-user "@localhost") + "true")))))) + +(define (sshd-test-cases marionette) + #~(test-assert "sshd: shell command via key auth" + (begin + (marionette-eval '(start-service 'ssh-daemon) + #$marionette) + (wait-for-tcp-port #$%sshd-port + #$marionette) + (marionette-eval '#$%sshd-key-auth-body + #$marionette)))) + +;; QEMU's default e1000 NIC is ens3, not eth0. +(define %static-network-config-to-avoid-qemu-dhcp-hang + (let ((addr (network-address (device "ens3") + (value "10.0.2.15/24"))) + (route (network-route (destination "default") + (gateway "10.0.2.2")))) + (static-networking (addresses (list addr)) + (routes (list route))))) + +(define %static-networking + (service static-networking-service-type + (list %static-network-config-to-avoid-qemu-dhcp-hang))) + +(define (authorize-login config) + (let ((public-key (file-append %sshd-login-keypair "/login-key.pub"))) + (openssh-configuration (inherit config) + (authorized-keys `((,%test-user ,public-key)))))) + +(define %test-services + (cons %static-networking + (modify-services (operating-system-user-services vps-operating-system) + (remove-service dhcpcd-service-type) + (openssh-service-type config => + (authorize-login config))))) + +(define (daemons-test-cases marionette) + #~(test-assert "services: NTP is up" + (marionette-eval '(start-service 'ntpd) + #$marionette))) + +(define %vps-test-os + (marionette-operating-system (operating-system + (inherit vps-operating-system) + (services + %test-services)) + #:imported-modules '((gnu services herd)))) -- 2.47.3