Testing
This guide covers running tests and building test VMs for Capsa development.
Running Tests
Unit Tests
bash
# Linux
cargo test-linux --lib
# macOS
cargo test-macos --libIntegration Tests
Integration tests require actual hypervisor access:
bash
# Linux (requires /dev/kvm)
cargo test-linux -- --test-threads=1
# macOS (requires Virtualization.framework entitlements)
cargo test-macos -- --test-threads=1Single Thread
Integration tests must run with --test-threads=1 to avoid resource contention. Multiple VMs competing for the same ports or sockets will cause failures.
Running Specific Tests
bash
# Run a specific integration test
cargo test-linux --test boot_test
# Run tests matching a name
cargo test-linux test_consoleDoc Tests
bash
cargo test-linux --doc
cargo test-macos --docLinting
bash
# Check formatting
cargo fmt --all -- --check
# Run clippy
cargo clippy-linux -- -D warnings # Linux
cargo clippy-macos -- -D warnings # macOSBuilding Test VMs
Test VMs are NixOS-based minimal systems built specifically for testing Capsa features.
Building
bash
# For x86_64 (Intel/AMD Linux, Intel Mac)
nix-build nix/test-vms -A x86_64 -o result-vms
# For aarch64 (Apple Silicon)
nix-build nix/test-vms -A aarch64 -o result-vmsThis creates a result-vms symlink containing:
kernel- Linux kernel (bzImage format)initrd- Initial ramdisk with test utilitiesdisk.img- Root filesystem (for disk tests)
Available Test VMs
The test VM system provides several configurations:
| VM | Description |
|---|---|
default | Universal VM with networking, disk, and vsock support |
with-disk | Default + pre-created disk image |
uefi | UEFI-bootable VM for testing UEFI boot |
Using Test VMs in Code
rust
use capsa::test_utils::{test_vm, BOOT_SUCCESS_MESSAGE};
use std::time::Duration;
#[tokio::test]
async fn my_test() {
let vm = test_vm("default")
.cpus(2)
.memory_mb(512)
.build()
.await
.expect("Failed to build VM");
let console = vm.console().await.unwrap();
console
.wait_for_timeout(BOOT_SUCCESS_MESSAGE, Duration::from_secs(30))
.await
.unwrap();
// Your test here...
}Troubleshooting
KVM Not Available (Linux)
Error: Backend unavailable: KVM device /dev/kvm not accessibleSolutions:
- Ensure KVM module is loaded:
sudo modprobe kvm_intelorkvm_amd - Check permissions:
sudo chmod 666 /dev/kvm(or use udev rules) - Add user to kvm group:
sudo usermod -aG kvm $USER
Entitlement Errors (macOS)
Error: The operation couldn't be completed. (Virtualization error -1)Solution: Ensure you're running within the dev shell which provides codesign-run for automatic signing.
VZD Not Found (macOS)
Error: capsa-apple-vzd not foundSolutions:
- Set
CAPSA_VZD_PATHenvironment variable - Build vzd:
cargo build -p capsa-apple-vzd - Run within the dev shell
Test Timeouts
If tests hang or timeout:
- Ensure you're using
--test-threads=1 - Check that test VMs are built (
result-vmsexists) - Verify hypervisor access (
/dev/kvmon Linux)