From a90bccf9125f47d1c3f52172098c212593362cf6 Mon Sep 17 00:00:00 2001 From: Jakub Czajka Date: Tue, 24 Nov 2026 23:21:43 +0000 Subject: [PATCH] [test] Add a PC VM test and share the VM test harness. Turn home.scm and system.scm into importable modules exporting pc-home-environment and pc-operating-system, then split the tests into tests/base.scm for the marionette harness, tests/home.scm for the shared home activation prelude and checks, and tests/{pc,vps}/ for the suites. The PC suite lets GDM auto-login run the home activation as its X session and then exec Guix's xinitrc, so dwm and the home services start through the real login path; one test case checks that dwm runs as the test user and that paseo-daemon, status-bar, and xcompmgr are running. The harness now wraps every OS in marionette-operating-system, runs the prelude before the tests, and aborts when it fails. The VPS suite keeps the sshd, daemon, and claude-proxy checks and uses assert-vm-true for the guest-side assertions. Move the claude-proxy live test and fake upstream under tests/vps, add a test-pc Makefile target, and let 20-env.sh keep GDM's DISPLAY instead of always forcing :1. Co-Authored-By: Claude --- Makefile | 12 +- bash/.config/profile.d/20-env.sh | 4 +- home.scm | 74 +++--- system.scm | 271 +++++++++++---------- tests/base.scm | 73 +++--- tests/home.scm | 99 ++++++++ tests/pc/base.scm | 49 ++++ tests/pc/home.scm | 36 +++ tests/vps-home.scm | 271 --------------------- tests/vps/base.scm | 22 ++ tests/{ => vps}/claude-proxy-live.sh | 2 +- tests/{ => vps}/claude-proxy-test-cert.pem | 0 tests/{ => vps}/claude-proxy-test-key.pem | 0 tests/{ => vps}/fake-llm.scm | 6 +- tests/vps/home.scm | 184 ++++++++++++++ tests/{vps-system.scm => vps/system.scm} | 97 ++++---- 16 files changed, 671 insertions(+), 529 deletions(-) create mode 100644 tests/home.scm create mode 100644 tests/pc/base.scm create mode 100644 tests/pc/home.scm delete mode 100644 tests/vps-home.scm create mode 100644 tests/vps/base.scm rename tests/{ => vps}/claude-proxy-live.sh (99%) rename tests/{ => vps}/claude-proxy-test-cert.pem (100%) rename tests/{ => vps}/claude-proxy-test-key.pem (100%) rename tests/{ => vps}/fake-llm.scm (97%) create mode 100644 tests/vps/home.scm rename tests/{vps-system.scm => vps/system.scm} (65%) diff --git a/Makefile b/Makefile index c3aff2c..68d57c5 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,15 @@ # Copyright (c) 2022-2026 Jakub Czajka # License: GPL-3.0 or later. -.PHONY: stop-home-services test-claude-proxy-live +.PHONY: stop-home-services test-claude-proxy-live test-pc guix-env := env -u GUILE_LOAD_PATH -u GUILE_LOAD_COMPILED_PATH \ -u GUILE_EXTENSIONS_PATH GUIX_PACKAGE_PATH=$(CURDIR) vps-test := $(guix-env) guix build -L $(CURDIR) \ - -e '(@ (tests base) %vps-system-test)' + -e '(@ (tests vps base) %vps-system-test)' + +pc-test := $(guix-env) PROFILE=nonfree guix build -L $(CURDIR) \ + -e '(@ (tests pc base) %system-test)' stop-home-services: @for svc in claude-proxy paseo-daemon; do \ @@ -64,8 +67,11 @@ test-vps-fresh: build-vps-system [ -z "$$out" ] || guix gc --delete "$$out" || exit 1; \ $(vps-test) +test-pc: build-system-nonfree + @$(pc-test) + test-claude-proxy-live: - @tests/claude-proxy-live.sh + @tests/vps/claude-proxy-live.sh tags: gv=$$($(guix-env) guix show guix | sed --quiet 's/^version: //p'); \ diff --git a/bash/.config/profile.d/20-env.sh b/bash/.config/profile.d/20-env.sh index 07ae4e2..97e4629 100644 --- a/bash/.config/profile.d/20-env.sh +++ b/bash/.config/profile.d/20-env.sh @@ -1,9 +1,9 @@ -# Copyright (c) 2022-2024 Jakub Czajka +# Copyright (c) 2022-2026 Jakub Czajka # License: GPL-3.0 or later. # # 20-env.sh - generic environment variables. export EDITOR="emacs" -export DISPLAY=":1" +export DISPLAY="${DISPLAY:-:1}" export PATH="${HOME}/.local/bin:${PATH}" export TERM="st-256color" diff --git a/home.scm b/home.scm index d4f8396..e15ae29 100644 --- a/home.scm +++ b/home.scm @@ -7,38 +7,44 @@ ;; PROFILE=nonfree -- allows non-free software. ;; PROFILE=libre -- free software only. -(use-modules (gnu home) - (conf home bash) - (conf home claude) - (conf home emoji) - (conf home desktop) - (conf home emacs) - (conf home guix) - (conf home notify) - (conf home paseo) - (conf home pi) - (conf home suckless) - (gnu packages gnuzilla) - (nongnu packages mozilla)) +(define-module (home) + #:use-module (gnu home) + #:use-module (conf home bash) + #:use-module (conf home claude) + #:use-module (conf home emoji) + #:use-module (conf home desktop) + #:use-module (conf home emacs) + #:use-module (conf home guix) + #:use-module (conf home notify) + #:use-module (conf home paseo) + #:use-module (conf home pi) + #:use-module (conf home suckless) + #:use-module (gnu packages gnuzilla) + #:use-module (nongnu packages mozilla) + #:export (pc-home-environment)) -(let ((profile (string->symbol (or (getenv "PROFILE") "nonfree")))) - (home-environment - (packages (case profile - ((nonfree) - (list firefox - pi - claude-code - paseo - select-emoji - st-libxft-bgra)) - ((libre) - (list icecat-minimal select-emoji st-libxft-bgra)))) - (services - (case profile - ((nonfree) - (append (list autorandr-service channels-service notify-service - paseo-daemon-service screenshot-service) bash-services - desktop-services emacs-services)) - ((libre) - (append (list autorandr-service notify-service screenshot-service) - bash-services desktop-services emacs-services)))))) +(define pc-home-environment + (let ((profile (string->symbol (or (getenv "PROFILE") "nonfree")))) + (home-environment + (packages (case profile + ((nonfree) + (list firefox + pi + claude-code + paseo + select-emoji + st-libxft-bgra)) + ((libre) + (list icecat-minimal select-emoji st-libxft-bgra)))) + (services + (case profile + ((nonfree) + (append (list autorandr-service channels-service notify-service + paseo-daemon-service screenshot-service) bash-services + desktop-services emacs-services)) + ((libre) + (append (list autorandr-service notify-service screenshot-service) + bash-services desktop-services emacs-services))))))) + +;; Keep the file usable as a `guix home reconfigure' script. +pc-home-environment diff --git a/system.scm b/system.scm index 9c14adc..40aa912 100644 --- a/system.scm +++ b/system.scm @@ -7,144 +7,151 @@ ;; PROFILE=nonfree — allows non-free firmware. ;; PROFILE=libre — free software only. -(use-modules (conf common substitutes) - (conf system bluetooth) - (conf system desktop) - (conf system volume) - (gnu) - (gnu system) - (gnu system nss)) +(define-module (system) + #:use-module (conf common substitutes) + #:use-module (conf system bluetooth) + #:use-module (conf system desktop) + #:use-module (conf system volume) + #:use-module (gnu) + #:use-module (gnu system) + #:use-module (gnu system linux-initrd) + #:use-module (gnu system nss) + #:use-module (nongnu packages linux) + #:use-module (nongnu system linux-initrd) + #:export (pc-operating-system)) (use-package-modules base certs linux pulseaudio xdisorg) - (use-service-modules desktop networking xorg) - (use-system-modules setuid) -(use-modules (nongnu packages linux) - (nongnu system linux-initrd)) -(use-modules (gnu system linux-initrd)) - -(let ((profile (string->symbol (or (getenv "PROFILE") "nonfree")))) - (operating-system - (initrd (case profile - ((nonfree) - microcode-initrd) - ((libre) - base-initrd))) - - (kernel (case profile - ((nonfree) - linux) - ((libre) - linux-libre))) - - (firmware (case profile +(define %nonfree-root-uuid + "b8be09cf-d416-4a49-ab69-173ee227acb6") + +(define %libre-root-uuid + "565d174f-3d96-45a1-8bef-fdab38e4d35a") + +(define pc-operating-system + (let ((profile (string->symbol (or (getenv "PROFILE") "nonfree")))) + (operating-system + (initrd (case profile ((nonfree) - (list linux-firmware)) + microcode-initrd) ((libre) - '()))) - - (locale "en_US.UTF-8") - (timezone "Europe/Warsaw") - - (keyboard-layout (keyboard-layout "us")) - - (host-name "dak") - (users (append (list (user-account - (name "dak") - (group "users") - (supplementary-groups '("audio" "cdrom" - "lp" - "netdev" - "tape" - "video" - "wheel")))) - %base-user-accounts)) - - (bootloader (case profile + base-initrd))) + + (kernel (case profile + ((nonfree) + linux) + ((libre) + linux-libre))) + + (firmware (case profile ((nonfree) - (bootloader-configuration - (bootloader grub-efi-bootloader) - (targets '("/boot/efi")) - (keyboard-layout keyboard-layout))) + (list linux-firmware)) ((libre) - (bootloader-configuration - (bootloader (bootloader (inherit grub-bootloader) - (installer #~(const #t)))) - (keyboard-layout keyboard-layout))))) - - (mapped-devices (case profile - ((nonfree) - (list (mapped-device - (source (uuid - "b8be09cf-d416-4a49-ab69-173ee227acb6")) - (target "root") - (type luks-device-mapping)))) - ((libre) - (list (mapped-device - (source (uuid - "565d174f-3d96-45a1-8bef-fdab38e4d35a")) - (target "root") - (type luks-device-mapping)) - (mapped-device - (source "matrix") - (targets (list "matrix-rootvol" - "matrix-swapvol")) - (type lvm-device-mapping)))))) - - (file-systems (append (case profile - ((nonfree) - (list (file-system - (type "ext4") - (mount-point "/") - (device (file-system-label "root")) - (dependencies mapped-devices)) - (file-system - (type "vfat") - (mount-point "/boot/efi") - (device (file-system-label "EFI")) - (needed-for-boot? #t)))) - ((libre) - (list (file-system - (type "ext4") - (mount-point "/") - (device (file-system-label "root")) - (flags '(no-atime)) - (needed-for-boot? #t) - (dependencies mapped-devices))))) - %base-file-systems)) - - (swap-devices (list (swap-space - (target (file-system-label "swap")) - (dependencies mapped-devices)))) - - (name-service-switch %mdns-host-lookup-nss) - - (setuid-programs (let* ((xsecurelock-path - "/libexec/xsecurelock/authproto_pam") - (xsecurelock-auth (file-append xsecurelock - xsecurelock-path))) - (cons* (setuid-program (program xsecurelock-auth)) - %setuid-programs))) - - (sudoers-file (let ((content (string-append "root ALL=(ALL) ALL\n" - "%wheel ALL=(ALL) ALL\n" - "dak ALL=(ALL) NOPASSWD: " - "/run/current-system/profile/bin/guix " - "system reconfigure *\n"))) - (plain-file "sudoers" content))) - - (packages (append (list gnu-make bluez pulseaudio volume) - dwm-desktop-packages %base-packages)) - - (services - (modify-services (append (list bluez-service %resolv-conf-service - keyboard-layout-service) - %custom-desktop-services) - (guix-service-type config => - (case profile - ((nonfree) - (augment-guix-configuration config)) - ((libre) - config))))))) + '()))) + + (locale "en_US.UTF-8") + (timezone "Europe/Warsaw") + + (keyboard-layout (keyboard-layout "us")) + + (host-name "dak") + (users (append (list (user-account + (name "dak") + (group "users") + (supplementary-groups '("audio" "cdrom" + "lp" + "netdev" + "tape" + "video" + "wheel")))) + %base-user-accounts)) + + (bootloader (case profile + ((nonfree) + (bootloader-configuration + (bootloader grub-efi-bootloader) + (targets '("/boot/efi")) + (keyboard-layout keyboard-layout))) + ((libre) + (bootloader-configuration + (bootloader (bootloader (inherit grub-bootloader) + (installer #~(const #t)))) + (keyboard-layout keyboard-layout))))) + + (mapped-devices (case profile + ((nonfree) + (list (mapped-device + (source (uuid %nonfree-root-uuid)) + (target "root") + (type luks-device-mapping)))) + ((libre) + (list (mapped-device + (source (uuid %libre-root-uuid)) + (target "root") + (type luks-device-mapping)) + (mapped-device + (source "matrix") + (targets (list "matrix-rootvol" + "matrix-swapvol")) + (type lvm-device-mapping)))))) + + (file-systems (append (case profile + ((nonfree) + (list (file-system + (type "ext4") + (mount-point "/") + (device (file-system-label "root")) + (dependencies mapped-devices)) + (file-system + (type "vfat") + (mount-point "/boot/efi") + (device (file-system-label "EFI")) + (needed-for-boot? #t)))) + ((libre) + (list (file-system + (type "ext4") + (mount-point "/") + (device (file-system-label "root")) + (flags '(no-atime)) + (needed-for-boot? #t) + (dependencies mapped-devices))))) + %base-file-systems)) + + (swap-devices (list (swap-space + (target (file-system-label "swap")) + (dependencies mapped-devices)))) + + (name-service-switch %mdns-host-lookup-nss) + + (setuid-programs (let* ((xsecurelock-path + "/libexec/xsecurelock/authproto_pam") + (xsecurelock-auth (file-append xsecurelock + xsecurelock-path))) + (cons* (setuid-program (program xsecurelock-auth)) + %setuid-programs))) + + (sudoers-file (let ((content (string-append "root ALL=(ALL) ALL\n" + "%wheel ALL=(ALL) ALL\n" + "dak ALL=(ALL) NOPASSWD: " + "/run/current-system/profile/bin/guix " + "system reconfigure *\n"))) + (plain-file "sudoers" content))) + + (packages (append (list gnu-make bluez pulseaudio volume) + dwm-desktop-packages %base-packages)) + + (services + (modify-services (append (list bluez-service %resolv-conf-service + keyboard-layout-service) + %custom-desktop-services) + (guix-service-type config => + (case profile + ((nonfree) + (augment-guix-configuration config)) + ((libre) + config)))))))) + +;; Keep the file usable as a `guix system reconfigure' script. +pc-operating-system diff --git a/tests/base.scm b/tests/base.scm index 412832b..155e8a7 100644 --- a/tests/base.scm +++ b/tests/base.scm @@ -1,38 +1,53 @@ ;; Copyright (c) 2026 Jakub Czajka ;; License: GPL-3.0 or later. ;; -;; The VPS VM test harness. +;; The VM test harness. (define-module (tests base) - #:use-module (gnu tests) #:use-module (gnu system vm) + #:use-module (gnu tests) #:use-module (guix gexp) - #:use-module (tests vps-home) - #:use-module (tests vps-system) - #:export (%vps-system-test)) + #:export (%test-user assert-vm-true vm-system-test)) + +(define %test-user + "dak") + +(define (assert-vm-true vm label body) + #~(test-equal #$label #t + (marionette-eval '#$body + #$vm))) + +(define %vm-imported-modules + '((gnu services herd))) -(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 (vm-test-builder name machine test-cases prelude) + #~(begin + (use-modules (gnu build marionette) + (srfi srfi-64)) + (define kvm-vm + (make-marionette (list #$machine "-cpu" "host"))) + (unless (marionette-eval '#$(cons 'begin + (append prelude + '(#t))) kvm-vm) + (error "guest test prelude failed")) + (test-runner-current (system-test-runner #$output)) + (test-begin #$name) + #$@(map (lambda (test-case) + (test-case #~kvm-vm)) test-cases) + (test-end #$name))) -(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))))) +(define (vm-system-test name os test-cases prelude memory-size) + (let* ((vm-os (marionette-operating-system os + #:imported-modules + %vm-imported-modules)) + (machine (virtual-machine (operating-system + vm-os) + (memory-size memory-size))) + (test-builder (vm-test-builder name machine test-cases prelude)) + (test-modules '((gnu build marionette))) + (test-derivation (gexp->derivation (string-append name "-test") + test-builder + #:modules test-modules))) + (system-test (name name) + (description (string-append name " test suite.")) + (value test-derivation)))) diff --git a/tests/home.scm b/tests/home.scm new file mode 100644 index 0000000..ae1ab65 --- /dev/null +++ b/tests/home.scm @@ -0,0 +1,99 @@ +;; Copyright (c) 2026 Jakub Czajka +;; License: GPL-3.0 or later. +;; +;; Generic home activation test cases. + +(define-module (tests home) + #:use-module (gnu packages admin) + #:use-module (gnu packages base) + #:use-module (guix gexp) + #:use-module (tests base) + #:export (home-activation-test home-system-test wait-for-home-service)) + +(define (home-prelude extra) + `((use-modules (gnu services herd) + (ice-9 popen) + (ice-9 rdelim)) + (define (wait-for ok? seconds) + (let loop + ((remaining seconds)) + (or (ok?) + (and (positive? remaining) + (sleep 1) + (loop (- remaining 1)))))) + + (define runtime-dir + (string-append "/run/user/" + (number->string (passwd:uid (getpwnam ,%test-user))))) + + (define (test-user-command command) + (let* ((timeout ,(file-append coreutils "/bin/timeout")) + (sudo ,(file-append sudo "/bin/sudo")) + (env ,(file-append coreutils "/bin/env")) + (prefix (string-append timeout " 90 " + (string-append sudo " -n -u " + ,%test-user " -- " env) + " HOME=")) + (home-directory (passwd:dir (getpwnam ,%test-user))) + (environment (string-append " XDG_RUNTIME_DIR=" runtime-dir " "))) + (string-append prefix home-directory environment command))) + + (define (run-as-test-user command) + (let* ((wrapped-command (string-append (test-user-command command) + " 2>&1")) + (port (open-input-pipe wrapped-command)) + (output (read-string port))) + (list (eqv? 0 + (status:exit-val (close-pipe port))) output))) + + (define (run-as-test-user-inheriting-output command) + (eqv? 0 + (status:exit-val (system (test-user-command command))))) + + ,@extra)) + +(define (home-system-test name os test-cases extra-prelude memory-size) + (vm-system-test name os test-cases + (home-prelude extra-prelude) memory-size)) + +(define (activate-home home-environment environment) + (let ((activate (file-append home-environment "/activate"))) + `(begin + (system* ,(file-append coreutils "/bin/install") "--directory" + "--mode=700" + ,(string-append "--owner=" %test-user) runtime-dir) + (run-as-test-user-inheriting-output (string-append ,environment + ,activate))))) + +(define (wait-for-home-service home-environment service) + (let ((herd (file-append home-environment "/profile/bin/herd"))) + `(wait-for (lambda () + (let ((result (run-as-test-user (string-append ,herd + " status " + ,service)))) + (and (car result) + (string-contains (cadr result) "It is running") #t))) + 60))) + +(define (home-service-assertions vm home-environment services assertions) + (append (map (lambda (service) + (assert-vm-true vm + (string-append service " is running") + (wait-for-home-service home-environment + service))) services) + (map (lambda (assertion) + (assert-vm-true vm + (car assertion) + (cdr assertion))) assertions))) + +(define* (home-activation-test home-environment + #:key (services '()) + (environment "") + (assertions '())) + (lambda (vm) + (let ((checks (home-service-assertions vm home-environment services + assertions))) + #~(begin + #$(assert-vm-true vm "home activates" + (activate-home home-environment environment)) + #$@checks)))) diff --git a/tests/pc/base.scm b/tests/pc/base.scm new file mode 100644 index 0000000..991ee72 --- /dev/null +++ b/tests/pc/base.scm @@ -0,0 +1,49 @@ +;; Copyright (c) 2026 Jakub Czajka +;; License: GPL-3.0 or later. + +(define-module (tests pc base) + #:use-module (gnu bootloader) + #:use-module (gnu bootloader grub) + #:use-module (gnu services) + #:use-module (gnu services xorg) + #:use-module (gnu system) + #:use-module (guix gexp) + #:use-module (home) + #:use-module (system) + #:use-module (tests base) + #:use-module (tests home) + #:use-module (tests pc home) + #:export (%system-test)) + +;; GDM's session runs activation with DISPLAY, then Guix's xinitrc. +(define %home-session + (let ((activate (file-append pc-home-environment "/activate"))) + (program-file "home-session" + #~(begin + (unless (zero? (system* #$activate)) + (exit 1)) + (execl #$(xinitrc) "xinitrc"))))) + +(define %services + (modify-services (operating-system-user-services pc-operating-system) + (gdm-service-type config => + (gdm-configuration (inherit config) + (auto-login? #t) + (default-user %test-user) + (x-session %home-session))))) + +;; The VM boots via -kernel; its MBR image rejects grub-efi. +(define %test-os + (operating-system + (inherit pc-operating-system) + (mapped-devices '()) + (bootloader (bootloader-configuration + (bootloader grub-bootloader) + (targets '("/dev/vda")))) + (services + %services))) + +(define %system-test + (home-system-test "pc" %test-os + (list (login-session-test pc-home-environment)) + %x-session-prelude 2048)) diff --git a/tests/pc/home.scm b/tests/pc/home.scm new file mode 100644 index 0000000..44a1d7c --- /dev/null +++ b/tests/pc/home.scm @@ -0,0 +1,36 @@ +;; Copyright (c) 2026 Jakub Czajka +;; License: GPL-3.0 or later. + +(define-module (tests pc home) + #:use-module (guix gexp) + #:use-module (tests base) + #:use-module (tests home) + #:export (%x-session-prelude login-session-test)) + +(define %x-session-prelude + `((use-modules (ice-9 ftw) + (srfi srfi-1)) + (define (find-process name) + (find (lambda (pid) + (let* ((file (string-append "/proc/" pid "/comm")) + (comm (false-if-exception (call-with-input-file file + read-string)))) + (string=? (string-append name "\n") + (or comm "")))) + (scandir "/proc"))))) + +(define (session-check home-environment) + `(and (let ((pid (wait-for (lambda () + (find-process "dwm")) 300))) + (and pid + (= (stat:uid (stat (string-append "/proc/" pid))) + (passwd:uid (getpwnam ,%test-user))))) + ,@(map (lambda (service) + (wait-for-home-service home-environment service)) + '("paseo-daemon" "status-bar" "xcompmgr")))) + +(define (login-session-test home-environment) + (lambda (vm) + #~(test-assert "PC boots into a dwm session" + (marionette-eval '#$(session-check home-environment) + #$vm)))) diff --git a/tests/vps-home.scm b/tests/vps-home.scm deleted file mode 100644 index ae30c70..0000000 --- a/tests/vps-home.scm +++ /dev/null @@ -1,271 +0,0 @@ -;; 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 (guix gexp) - #:use-module (guix packages) - #:use-module (conf home pi) - #:use-module (tests vps-system) - #:use-module (vps-home) - #:export (vps-home-test-cases)) - -(define %home-proxy-port - 16890) - -(define %activate - (file-append vps-home-environment "/activate")) -(define %herd - (file-append vps-home-environment "/profile/bin/herd")) -(define %claude - (file-append vps-home-environment "/profile/bin/claude")) -(define %pi - (file-append vps-home-environment "/profile/bin/pi")) -(define %timeout - (file-append coreutils "/bin/timeout")) - -(define %home-prelude - `(begin - (use-modules (ice-9 popen) - (ice-9 rdelim) - (ice-9 rw)) - - (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 one-second - internal-time-units-per-second) - (define (read-timed socket start) - (let loop - ((chunks '()) - (first-tick #f)) - (let* ((buffer (make-string 8192)) - (n (or (false-if-exception (read-string!/partial buffer socket)) - 0))) - (cond - ((positive? n) - (loop (cons (substring buffer 0 n) chunks) - (or first-tick - (- (get-internal-real-time) start)))) - ((pair? (car (select (list socket) - '() - '() 0))) - (list first-tick - (- (get-internal-real-time) start) - (string-concatenate-reverse chunks))) - (else #f))))) - - (define (proxy-request request) - (false-if-exception (let ((socket (socket PF_INET SOCK_STREAM 0)) - (address (make-socket-address AF_INET - INADDR_LOOPBACK - ,%home-proxy-port)) - (start (get-internal-real-time))) - (connect socket address) - (setsockopt socket SOL_SOCKET SO_RCVTIMEO - (cons 4 0)) - (display request socket) - (force-output socket) - (let ((response (read-timed socket start))) - (close-port socket) response)))) - - (define (fetch path body) - (let ((head (string-append "POST " path " HTTP/1.1\r\n" - "Host: 127.0.0.1\r\n")) - (tail (string-append "Content-Length: " - (number->string (string-length body)) - "\r\n\r\n" body))) - (proxy-request (string-append head - "Content-Type: application/json\r\n" - "x-api-key: sk-test\r\n" tail)))) - - (define (wait-for-port port) - (let ((address (make-socket-address AF_INET INADDR_LOOPBACK port))) - (let loop - ((remaining 20)) - (let ((socket (socket PF_INET SOCK_STREAM 0))) - (let ((accepting? (false-if-exception (connect socket address)))) - (close-port socket) - (if accepting? #t - (and (positive? remaining) - (sleep 1) - (loop (- remaining 1))))))))) - - (define-syntax ok? - (syntax-rules () - ((_ result body check ...) - (let ((response result)) - (and response - (let ((body (caddr response))) - (and check ...))))))) - - (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 (run-contains command needle) - (let ((result (run command))) - (and (car result) - (string-contains (cadr result) needle) #t))))) - -(define (req-with-options stream fields) - (string-append "{\"stream\":" stream - ",\"model\":\"deepseek-chat\",\"max_tokens\":16," fields - "\"messages\":[{\"role\":\"user\",\"content\":\"hello\"}]}")) - -(define %req-classifier-fields - "\"reasoning_effort\":\"high\",\"output_config\":{\"budget\":\"auto\"},") - -(define %stream-request - (req-with-options "true" "")) - -(define %req-thinking-fields - "\"thinking\":{\"type\":\"enabled\",\"budget_tokens\":1000},") - -(define %count-tokens-request - (string-append "{\"model\":\"deepseek-chat\"," %req-classifier-fields - "\"messages\":[{\"role\":\"user\",\"content\":\"hello\"}]}")) - -(define %home-activation-body - `(let* ((env (string-append "CLAUDE_PROXY_UPSTREAM=https://127.0.0.1:" - (number->string ,fake-upstream-port) - " GUILE_TLS_CERTIFICATE_DIRECTORY=" - ,claude-proxy-test-ca-dir " ")) - (result (run (string-append env - ,%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 (home-herd-body service) - `(run-contains (string-append ,%herd " status " - ,service) "It is running")) - -(define %health-stream-body - `(let ((stream (fetch "/v1/messages" - ,%stream-request))) - (and (ok? (proxy-request "GET /health HTTP/1.1\nHost: 127.0.0.1\n\n") - body - (string-contains body "200") - (string-contains body "\"status\":\"ok\"")) - (ok? stream body - (string-contains body "data: [DONE]") - (string-contains body "message_start")) - (< (car stream) - (* 5/2 one-second)) - (>= (cadr stream) - (* 5/2 one-second))))) - -(define %classifier-rewrite-body - `(let* ((plain (fetch "/v1/messages" - ,(req-with-options "false" %req-classifier-fields))) - (explicit (fetch "/v1/messages" - ,(req-with-options "false" %req-thinking-fields))) - (tokens (fetch "/v1/messages/count_tokens" - ,%count-tokens-request))) - (and (ok? plain - body - (string-contains body "200") - (string-contains body "\"type\":\"disabled\"") - (not (string-contains body "reasoning_effort")) - (not (string-contains body "output_config"))) - (ok? explicit body - (string-contains body "\"type\":\"enabled\"") - (string-contains body "budget_tokens")) - (ok? tokens - body - (string-contains body "200") - (string-contains body "reasoning_effort") - (string-contains body "output_config") - (not (string-contains body "thinking")))))) - -(define %relay-and-fail-fast-body - `(and (ok? (fetch "/other" - "{\"stream\":false,\"model\":\"x\",\"max_tokens\":1}") - body - (string-contains body "401") - (string-contains body "authentication_error")) - (stop-service 'fake-upstream) - (let ((down (fetch "/v1/messages" - ,%stream-request))) - (and (ok? down body - (string-contains body "502") - (string-contains body "upstream unavailable")) - (< (cadr down) - (* 2 one-second)) - (start-service 'fake-upstream) - (wait-for-port ,fake-upstream-port))))) - -(define %claude-e2e-body - `(let* ((env (string-append - "HOME=/tmp/claude-e2e ANTHROPIC_API_KEY=test-key " - "ANTHROPIC_BASE_URL=http://127.0.0.1:" - (number->string ,%home-proxy-port) " DISABLE_TELEMETRY=1 " - "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 ")) - (claude ,%claude) - (args " --bare --model claude-sonnet-4-6 -p hello 2>&1 < /dev/null") - (command (string-append env - ,%timeout " 90 " claude args)) - (result (run-command-output command))) - (and (car result) - (string-contains (cadr result) "Hello from fake") #t))) - -(define %pi-version-body - `(run-contains (string-append "HOME=/tmp/pi-e2e " - ,%pi " --version") - ,(package-version pi))) - -(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) - #$(assert-home marionette "home: claude-proxy is listening" - `(wait-for-port ,%home-proxy-port)) - #$(assert-home marionette "home: claude-proxy is running" - (home-herd-body "claude-proxy")) - #$(assert-home marionette "home: paseo-daemon is running" - (home-herd-body "paseo-daemon")) - #$(assert-home marionette "home: pi runs" %pi-version-body) - #$(assert-home marionette - "home: claude-proxy: health and incremental SSE" - %health-stream-body) - #$(assert-home marionette - "home: claude-proxy: thinking rewrite only for classifier calls" - %classifier-rewrite-body) - #$(assert-home marionette - "home: claude-proxy: Claude Code -p gets a reply" - %claude-e2e-body) - #$(assert-home marionette - "home: claude-proxy: upstream errors relay and fail fast" - %relay-and-fail-fast-body))) diff --git a/tests/vps/base.scm b/tests/vps/base.scm new file mode 100644 index 0000000..4d09943 --- /dev/null +++ b/tests/vps/base.scm @@ -0,0 +1,22 @@ +;; Copyright (c) 2026 Jakub Czajka +;; License: GPL-3.0 or later. +;; +;; The VPS VM test entry point. + +(define-module (tests vps base) + #:use-module (tests home) + #:use-module (tests vps home) + #:use-module (tests vps system) + #:use-module (vps-home) + #:export (%vps-system-test)) + +(define %vps-system-test + (home-system-test "vps" %vps-test-os + (list daemons-test-case + (home-activation-test vps-home-environment + #:services '("claude-proxy" + "paseo-daemon") + #:environment + %activation-environment + #:assertions %proxy-assertions) + sshd-test-case) %proxy-prelude 1024)) diff --git a/tests/claude-proxy-live.sh b/tests/vps/claude-proxy-live.sh similarity index 99% rename from tests/claude-proxy-live.sh rename to tests/vps/claude-proxy-live.sh index 90a41c4..0d504fc 100755 --- a/tests/claude-proxy-live.sh +++ b/tests/vps/claude-proxy-live.sh @@ -6,7 +6,7 @@ # to both the deployed service and this build, then diff their canonicalized # log-and-response transcripts; the key and live log are host state. -repo_root=$(cd "$(dirname "$0")/.." && pwd) +repo_root=$(cd "$(dirname "$0")/../.." && pwd) src_port=16890 src_log="${XDG_STATE_HOME:-$HOME/.local/state}/claude-proxy.log" diff --git a/tests/claude-proxy-test-cert.pem b/tests/vps/claude-proxy-test-cert.pem similarity index 100% rename from tests/claude-proxy-test-cert.pem rename to tests/vps/claude-proxy-test-cert.pem diff --git a/tests/claude-proxy-test-key.pem b/tests/vps/claude-proxy-test-key.pem similarity index 100% rename from tests/claude-proxy-test-key.pem rename to tests/vps/claude-proxy-test-key.pem diff --git a/tests/fake-llm.scm b/tests/vps/fake-llm.scm similarity index 97% rename from tests/fake-llm.scm rename to tests/vps/fake-llm.scm index 83acb05..fb1bc13 100644 --- a/tests/fake-llm.scm +++ b/tests/vps/fake-llm.scm @@ -4,7 +4,7 @@ ;; The build sandbox has no network route or API key, so this fake stands ;; in for DeepSeek; use make test-claude-proxy-live for the live diff. -(define-module (tests fake-llm) +(define-module (tests vps fake-llm) #:use-module (gnutls) #:use-module (ice-9 threads) #:use-module (json) @@ -64,7 +64,7 @@ (uri-query uri) (utf8->string (or body #vu8()))))) -(define %unauthorized-body +(define %unauthorized-response (scm->json-string '((error (type . "authentication_error"))))) (define (dispatch-request! sock path query body-str) @@ -79,7 +79,7 @@ (equal? path "/v1/messages/count_tokens")) (respond-sized! sock "200 OK" "application/json" body-str)) (else (respond-sized! sock "401 Unauthorized" "application/json" - %unauthorized-body)))) + %unauthorized-response)))) (define (serve-tls-session! conn cert key) (let ((session (make-session connection-end/server)) diff --git a/tests/vps/home.scm b/tests/vps/home.scm new file mode 100644 index 0000000..d973a8a --- /dev/null +++ b/tests/vps/home.scm @@ -0,0 +1,184 @@ +;; Copyright (c) 2026 Jakub Czajka +;; License: GPL-3.0 or later. +;; +;; VPS home tests and claude-proxy assertions. + +(define-module (tests vps home) + #:use-module (conf home pi) + #:use-module (guix gexp) + #:use-module (guix packages) + #:use-module (tests vps system) + #:use-module (vps-home) + #:export (%activation-environment %proxy-assertions %proxy-prelude)) + +(define %home-proxy-port + 16890) + +(define %claude + (file-append vps-home-environment "/profile/bin/claude")) + +(define %pi + (file-append vps-home-environment "/profile/bin/pi")) + +(define %activation-environment + `(string-append "CLAUDE_PROXY_UPSTREAM=https://127.0.0.1:" + ,(number->string fake-upstream-port) + " GUILE_TLS_CERTIFICATE_DIRECTORY=" + ,claude-proxy-test-ca-dir " ")) + +(define %proxy-prelude + `((use-modules (ice-9 rw)) + (define one-second + internal-time-units-per-second) + + (define (read-timed socket start) + (let loop + ((chunks '()) + (first-tick #f)) + (let* ((buffer (make-string 8192)) + (n (or (false-if-exception (read-string!/partial buffer socket)) + 0))) + (cond + ((positive? n) + (loop (cons (substring buffer 0 n) chunks) + (or first-tick + (- (get-internal-real-time) start)))) + ((pair? (car (select (list socket) + '() + '() 0))) + (list first-tick + (- (get-internal-real-time) start) + (string-concatenate-reverse chunks))) + (else #f))))) + + (define (proxy-request request) + (let ((port ,%home-proxy-port)) + (false-if-exception (let ((socket (socket PF_INET SOCK_STREAM 0)) + (address (make-socket-address AF_INET + INADDR_LOOPBACK port)) + (start (get-internal-real-time))) + (connect socket address) + (setsockopt socket SOL_SOCKET SO_RCVTIMEO + (cons 4 0)) + (display request socket) + (force-output socket) + (let ((response (read-timed socket start))) + (close-port socket) response))))) + + (define (fetch path body) + (let ((head (string-append "POST " path " HTTP/1.1\r\n" + "Host: 127.0.0.1\r\n")) + (tail (string-append "Content-Length: " + (number->string (string-length body)) + "\r\n\r\n" body))) + (proxy-request (string-append head + "Content-Type: application/json\r\n" + "x-api-key: sk-test\r\n" tail)))) + + (define (wait-for-port port) + (let ((address (make-socket-address AF_INET INADDR_LOOPBACK port))) + (wait-for (lambda () + (let ((socket (socket PF_INET SOCK_STREAM 0))) + (let ((accepting? (false-if-exception (connect socket + address)))) + (close-port socket) accepting?))) 20))) + + (define (body-contains? response text) + (and response + (string-contains (caddr response) text))))) + +(define (req-with-options stream fields) + (string-append "{\"stream\":" stream + ",\"model\":\"deepseek-chat\",\"max_tokens\":16," fields + "\"messages\":[{\"role\":\"user\",\"content\":\"hello\"}]}")) + +(define %req-classifier-fields + "\"reasoning_effort\":\"high\",\"output_config\":{\"budget\":\"auto\"},") + +(define %stream-request + (req-with-options "true" "")) + +(define %req-thinking-fields + "\"thinking\":{\"type\":\"enabled\",\"budget_tokens\":1000},") + +(define %count-tokens-request + (string-append "{\"model\":\"deepseek-chat\"," %req-classifier-fields + "\"messages\":[{\"role\":\"user\",\"content\":\"hello\"}]}")) + +(define check-proxy-health-and-stream + `(let ((health (proxy-request "GET /health HTTP/1.1\nHost: 127.0.0.1\n\n")) + (stream (fetch "/v1/messages" + ,%stream-request))) + (and (body-contains? health "200") + (body-contains? health "\"status\":\"ok\"") + (body-contains? stream "data: [DONE]") + (body-contains? stream "message_start") + (< (car stream) + (* 5/2 one-second)) + (>= (cadr stream) + (* 5/2 one-second))))) + +(define check-classifier-rewrite + `(let* ((plain (fetch "/v1/messages" + ,(req-with-options "false" %req-classifier-fields))) + (explicit (fetch "/v1/messages" + ,(req-with-options "false" %req-thinking-fields))) + (tokens (fetch "/v1/messages/count_tokens" + ,%count-tokens-request))) + (and (body-contains? plain "200") + (body-contains? plain "\"type\":\"disabled\"") + (not (body-contains? plain "reasoning_effort")) + (not (body-contains? plain "output_config")) + (body-contains? explicit "\"type\":\"enabled\"") + (body-contains? explicit "budget_tokens") + (body-contains? tokens "200") + (body-contains? tokens "reasoning_effort") + (body-contains? tokens "output_config") + (not (body-contains? tokens "thinking"))))) + +(define check-relay-and-fail-fast + `(let ((other (fetch "/other" + "{\"stream\":false,\"model\":\"x\",\"max_tokens\":1}"))) + (and (body-contains? other "401") + (body-contains? other "authentication_error") + (stop-service 'fake-upstream) + (let ((down (fetch "/v1/messages" + ,%stream-request))) + (and (body-contains? down "502") + (body-contains? down "upstream unavailable") + (< (cadr down) + (* 2 one-second)) + (start-service 'fake-upstream) + (wait-for-port ,fake-upstream-port)))))) + +(define check-claude-reply + `(let* ((env (string-append + "HOME=/tmp/claude-e2e ANTHROPIC_API_KEY=test-key " + "ANTHROPIC_BASE_URL=http://127.0.0.1:" + (number->string ,%home-proxy-port) " DISABLE_TELEMETRY=1 " + "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 ")) + (args " --bare --model claude-sonnet-4-6 -p hello < /dev/null") + (result (run-as-test-user (string-append env + ,%claude args)))) + (and (car result) + (string-contains (cadr result) "Hello from fake") #t))) + +(define check-pi-version + `(let ((result (run-as-test-user (string-append "HOME=/tmp/pi-e2e " + ,%pi " --version")))) + (and (car result) + (string-contains (cadr result) + ,(package-version pi)) #t))) + +(define %proxy-assertions + (list (cons "home: claude-proxy: accepts connections" + `(wait-for-port ,%home-proxy-port)) + (cons "home: claude-proxy: Claude Code -p gets a reply" + check-claude-reply) + (cons "home: claude-proxy: health and incremental SSE" + check-proxy-health-and-stream) + (cons "home: claude-proxy: thinking rewrite only for classifier calls" + check-classifier-rewrite) + (cons "home: claude-proxy: upstream errors relay and fail fast" + check-relay-and-fail-fast) + (cons "home: pi runs" check-pi-version))) diff --git a/tests/vps-system.scm b/tests/vps/system.scm similarity index 65% rename from tests/vps-system.scm rename to tests/vps/system.scm index aca765c..5b228e0 100644 --- a/tests/vps-system.scm +++ b/tests/vps/system.scm @@ -3,27 +3,24 @@ ;; ;; 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 simple-service - modify-services)) - #:use-module (gnu services shepherd) - #:use-module (gnu services base) - #:use-module (gnu services ssh) - #:use-module (gnu services networking) +(define-module (tests vps system) + #:use-module (conf vps sshd) #:use-module (gnu packages guile) #:use-module (gnu packages ssh) #:use-module (gnu packages tls) - #:use-module (conf vps sshd) + #:use-module ((gnu services) + #:select ((delete . remove-service) modify-services service + simple-service)) + #:use-module (gnu services base) + #:use-module (gnu services networking) + #:use-module (gnu services shepherd) + #:use-module (gnu services ssh) + #:use-module (gnu system) #:use-module (guix gexp) + #:use-module (tests base) #:use-module (vps-system) - #:export (%vps-test-os %test-user daemons-test-cases sshd-test-cases - fake-upstream-port claude-proxy-test-ca-dir)) - -(define %test-user - "dak") + #:export (%vps-test-os claude-proxy-test-ca-dir daemons-test-case + fake-upstream-port sshd-test-case)) (define %sshd-login-keypair (computed-file "sshd-login-keypair" @@ -35,7 +32,7 @@ (unless (zero? (apply system* keygen args)) (exit 1)))))) -(define %sshd-key-auth-body +(define check-ssh-key-auth `(let ((ssh ,(file-append openssh "/bin/ssh")) (key "/tmp/login-key")) (copy-file ,(file-append %sshd-login-keypair "/login-key") key) @@ -48,28 +45,23 @@ (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 (sshd-test-case vm) + #~(begin + (marionette-eval '(start-service 'ssh-daemon) + #$vm) + (wait-for-tcp-port #$%sshd-port + #$vm) + #$(assert-vm-true vm "sshd: shell command via key auth" + check-ssh-key-auth))) + +;; QEMU's e1000 NIC is ens3; static addressing avoids a DHCP hang. (define %static-networking - (service static-networking-service-type - (list %static-network-config-to-avoid-qemu-dhcp-hang))) + (let ((address (network-address (device "ens3") + (value "10.0.2.15/24")))) + (service static-networking-service-type + (list (static-networking (inherit %qemu-static-networking) + (addresses (list address)) + (name-servers '())))))) (define fake-tls-cert (local-file "claude-proxy-test-cert.pem")) @@ -82,10 +74,10 @@ 19998) (define (fake-upstream-script cert key port) - (with-imported-modules '((tests fake-llm)) + (with-imported-modules '((tests vps fake-llm)) (with-extensions (list guile-gnutls guile-json-4) #~(begin - (use-modules (tests fake-llm)) + (use-modules (tests vps fake-llm)) (run-fake-llm #$cert #$key #$port))))) @@ -132,23 +124,20 @@ (string-contains (get-string-all port) ,%nonguix-signing-key))))) -(define (daemons-test-cases marionette) +(define (daemons-test-case vm) #~(begin - (test-assert "services: resolv.conf is installed" - (marionette-eval '(file-exists? "/etc/resolv.conf") - #$marionette)) + #$(assert-vm-true vm "services: resolv.conf is installed" + '(file-exists? "/etc/resolv.conf")) (test-assert "services: DNS and NTP are up" (and (wait-for-tcp-port 53 - #$marionette) + #$vm) (marionette-eval '(start-service 'ntpd) - #$marionette))) - (test-assert "services: nonguix substitutes are authorized" - (marionette-eval '#$%nonguix-acl-test - #$marionette)))) + #$vm))) + #$(assert-vm-true vm "services: nonguix substitutes are authorized" + %nonguix-acl-test))) (define %vps-test-os - (marionette-operating-system (operating-system - (inherit vps-operating-system) - (services - %test-services)) - #:imported-modules '((gnu services herd)))) + (operating-system + (inherit vps-operating-system) + (services + %test-services))) -- 2.47.3