This guide explains how to run firecracker-python unit tests in a Docker container with KVM access.
Before running tests in Docker, ensure you have the following installed on your host system:
- Docker (version 20.10 or later)
- Docker Compose (version 1.29 or later)
- KVM support on your host system
- Access to
/dev/kvmdevice
Check if your system supports KVM:
# Check if KVM module is loaded
lsmod | grep kvm
# Check if /dev/kvm exists
ls -l /dev/kvm
# Check if you have access to /dev/kvm
test -r /dev/kvm && test -w /dev/kvm && echo "KVM accessible" || echo "KVM not accessible"If you don't have access to /dev/kvm, you can fix it with:
# Temporary fix (lost on reboot)
sudo chmod 666 /dev/kvm
# Permanent fix (add your user to kvm group)
sudo usermod -aG kvm $USER
# Then log out and log back inThe easiest way to run tests is using the provided script:
# Run all tests
./scripts/run-tests-docker.sh
# Run specific tests
./scripts/run-tests-docker.sh test_parse_ports
# Run tests with verbose output
./scripts/run-tests-docker.sh -v
# Run tests with coverage report
./scripts/run-tests-docker.sh -c
# Start a shell in the container
./scripts/run-tests-docker.sh -sYou can also use Docker Compose directly:
# Build the Docker image
docker compose -f docker-compose.test.yml build
# Run all tests
docker compose -f docker-compose.test.yml run --rm firecracker-test uv run pytest tests/
# Run specific tests
docker compose -f docker-compose.test.yml run --rm firecracker-test uv run pytest -k test_parse_ports tests/
# Run tests with verbose output
docker compose -f docker-compose.test.yml run --rm firecracker-test uv run pytest -v tests/
# Run tests with coverage
docker compose -f docker-compose.test.yml run --rm firecracker-test uv run pytest --cov=firecracker --cov-report=term-missing tests/
# Start a shell in the container
docker compose -f docker-compose.test.yml run --rm firecracker-test /bin/bashIf you prefer to use Docker directly:
# Build the Docker image
docker build -f Dockerfile.test -t firecracker-python-test .
# Run all tests
docker run --rm \
--device /dev/kvm:/dev/kvm \
--device /dev/net/tun:/dev/net/tun \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(pwd):/workspace \
--network host \
firecracker-python-test \
uv run pytest tests/
# Run specific tests
docker run --rm \
--device /dev/kvm:/dev/kvm \
--device /dev/net/tun:/dev/net/tun \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(pwd):/workspace \
--network host \
firecracker-python-test \
uv run pytest -k test_parse_ports tests/
# Start a shell in the container
docker run --rm -it \
--device /dev/kvm:/dev/kvm \
--device /dev/net/tun:/dev/net/tun \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(pwd):/workspace \
--network host \
firecracker-python-test \
/bin/bashThe test container includes:
- KVM Access: Direct access to
/dev/kvmfor running Firecracker VMs - Docker-in-Docker: Ability to run Docker containers inside the test container (for Docker image tests)
- Host Networking: Uses host network mode for easier network testing
- Firecracker Binary: Pre-installed Firecracker v1.9.0 binary
- Python Environment: Complete Python environment with all dependencies
- Persistent Volumes:
firecracker-data: Stores Firecracker VM datafirecracker-snapshots: Stores VM snapshotsuv-cache: Caches Python packages for faster rebuilds
The test suite includes several categories of tests:
These tests can run without KVM access:
# Port parsing tests
./scripts/run-tests-docker.sh test_parse_ports
# Memory size conversion tests
./scripts/run-tests-docker.sh test_convert_memory_size
# Snapshot symlink tests
./scripts/run-tests-docker.sh test_prepare_snapshot_rootfs_symlinkThese tests require KVM access to create and manage actual Firecracker VMs:
# VM creation tests
./scripts/run-tests-docker.sh test_vmm_create
# VM deletion tests
./scripts/run-tests-docker.sh test_vmm_delete
# Network tests
./scripts/run-tests-docker.sh test_network_overlap_checkThese tests require Docker to be available inside the container:
# Docker image validation tests
./scripts/run-tests-docker.sh test_is_valid_docker_image
# Docker download tests
./scripts/run-tests-docker.sh test_download_docker
# Docker export tests
./scripts/run-tests-docker.sh test_export_docker_imageIf you get a permission error:
# Check current permissions
ls -l /dev/kvm
# Fix permissions temporarily
sudo chmod 666 /dev/kvm
# Or add your user to the kvm group permanently
sudo usermod -aG kvm $USER
# Then log out and log back inIf KVM is not available on your system:
# Check if virtualization is supported
lscpu | grep Virtualization
# If not supported, you can still run unit tests that don't require KVM
./scripts/run-tests-docker.sh test_parse_portsIf Docker-in-Docker is not working:
# Check if Docker socket is mounted correctly
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock docker ps
# If you get permission errors, check Docker socket permissions
ls -l /var/run/docker.sock
sudo chmod 666 /var/run/docker.sock # Temporary fixIf the container won't start:
# Check Docker logs
docker compose -f docker-compose.test.yml logs
# Check if Firecracker binary is accessible
docker run --rm firecracker-python-test firecracker --version
# Check if KVM is accessible inside container
docker run --rm --device /dev/kvm:/dev/kvm firecracker-python-test ls -l /dev/kvmKeep the container running in the background:
# Start container in detached mode
./scripts/run-tests-docker.sh -d -k
# Connect to running container
docker exec -it firecracker-python-test /bin/bash
# Run tests in running container
docker exec -it firecracker-python-test uv run pytest tests/
# Stop container
docker compose -f docker-compose.test.yml downRun custom pytest commands:
# Start shell in container
./scripts/run-tests-docker.sh -s
# Inside container, run custom commands
uv run pytest tests/ -v --tb=short
uv run pytest tests/ -x # Stop on first failure
uv run pytest tests/ --lf # Re-run failed testsDebug failing tests:
# Run with verbose output and short traceback
./scripts/run-tests-docker.sh -v
# Run with pdb debugger
docker compose -f docker-compose.test.yml run --rm firecracker-test \
uv run pytest --pdb tests/
# Run specific test with verbose output
./scripts/run-tests-docker.sh -v test_parse_ports_with_integerFor CI/CD pipelines, you can use the Docker setup:
# Example GitHub Actions workflow
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Build test image
run: docker compose -f docker-compose.test.yml build
- name: Run tests
run: docker compose -f docker-compose.test.yml run --rm firecracker-test uv run pytest tests/Remove Docker resources:
# Stop and remove containers
docker compose -f docker-compose.test.yml down
# Remove volumes (this will delete all Firecracker data)
docker compose -f docker-compose.test.yml down -v
# Remove the Docker image
docker rmi firecracker-python-test
# Clean up everything
docker compose -f docker-compose.test.yml down -v && docker rmi firecracker-python-test