Skip to content

Tests

This page tells you how the project does tests and how you add a test for a change. It is for a person who changes the code or the scripts.

The project has three levels of proof:

  1. Tests that run on your computer without a container engine. make test runs them all.
  2. Break-it checks. They show that a test finds a defect.
  3. Proof scripts. They run on a real engine on a real machine.

development.md gives the tools and the commands to build.

Kind Where What the code under test talks to Command
Go unit tests of the manager internal/sandbox The test engine go test ./internal/sandbox/
Go tests of the engine client internal/engine/docker_test.go A stand-in for the Docker Engine API on a Unix socket go test ./internal/engine/
Go tests of the helper internal/guest Real processes and real files on your computer go test ./internal/guest/
Go tests of the GPU allocator internal/gpu A shell script that is a stand-in for nvidia-smi go test ./internal/gpu/
Go tests of the firewall rules internal/hostfw A stand-in for iptables that records each call go test ./internal/hostfw/
API tests internal/api An HTTP server on the manager and the test engine go test ./internal/api/
Tests of the Go client, the tools and the MCP server internal/client, internal/tools, internal/mcp A daemon from internal/apitest go test ./internal/client/ ./internal/tools/ ./internal/mcp/
CLI tests cmd/boxfork A daemon from internal/apitest go test ./cmd/boxfork/
Python SDK tests sdk/python/tests bin/boxfork-testserver make test-python
TypeScript SDK tests sdk/typescript/test bin/boxfork-testserver make test-typescript
Installer tests scripts/install_test.sh Stand-ins for Docker, gVisor, the NVIDIA tools, systemd and the network make test-install
Proof scripts scripts/proof A real daemon on a real engine See “Proof scripts” below

make test runs all rows but the last row. make test-go runs go vet ./... and then go test -race -count=1 ./.... The flag -race finds data races. The flag -count=1 stops Go from the use of a cached result.

No CI service runs these tests. Run make test before you send a change.

The test engine is the type Fake in internal/engine/enginetest/fake.go. It implements the Engine interface in memory.

The test engine has no isolation. When the manager runs the helper in a container, the test engine calls guest.Main in the test process. The input and the output go through pipes, as they go through the engine on a real machine. Thus the tests use the real protocol, the real helper code and real processes.

The test engine answers four helper commands itself: gpus, procs, dns and own. On a real machine these commands read the GPUs, the process table, the resolver file and the owner of a directory. On your computer the real commands read or change your system.

A test controls the test engine with these fields and methods:

Control Effect
Fail["Create"] = err, SetFail The operation returns the error and does nothing.
Lose["Remove"] = true The operation does its work and then returns an error. This is an answer that the network lost.
Stop(id) The container stops, as after a crash.
BrokenRuntimes The engine lists a runtime but cannot start a container with it.
IgnoreRuntime The engine accepts each runtime name and uses its default runtime.
IgnoreFilter List returns all containers and ignores the label.
OpenNetwork The network does not isolate the sandboxes.
OnCommit, OnRemove The test runs code in the middle of a commit or before a removal.
ExecHook, ExecIn The test replaces the helper for one call. This is the stand-in for a helper that lies or stops.
GPUsSeen, Procs, DNSAnswer, OwnAnswer The test gives the answer of the helper commands that the test engine answers itself.
Containers(), Execs(), DNSCalls(), OwnCalls() The test reads what the manager asked the engine to do.

The test engine cannot show these things. Each of them needs a real engine:

  • The isolation of gVisor, and the limits on memory, CPU and processes.
  • The network: no traffic between sandboxes, the name servers, the firewall rules.
  • A real commit of a container and a real image.
  • A real GPU.
  • Time. All timings on this project come from the proof scripts.

The function setup in internal/sandbox/manager_test.go makes one environment for a test. It makes a test engine, a clock, a GPU allocator with a number of stand-in GPUs, and a manager with a state file in t.TempDir(). A test changes the configuration with a function: setup(t, 1, func(c *Config) { c.MaxSandboxes = 2 }).

A test calls the periodic work directly. The loop Manager.Watch does not run in a test. A test moves the clock and then calls Sweep, Reclaim, Reconcile or FillPools.

A test of a restart makes a second manager on the same state file and the same test engine. TestRestartRecovery is the example.

startFakeAPI in internal/engine/docker_test.go starts an HTTP server on a Unix socket. The server records each request. A test then compares the request with the request that a real engine expects. These tests prove what the daemon sends. They do not prove what Docker does with it.

newServer in internal/api/server_test.go starts the real handler on httptest.NewServer. The tests send HTTP requests and read the status, the error code and the body.

The CLI tests run the test binary as the boxfork command. TestMain in cmd/boxfork/cli_test.go calls main when the variable BOXFORK_RUN_AS_CLI is 1. Thus a test gets the real exit status and the real two output streams. The daemon on the other side comes from internal/apitest.

cmd/boxfork-testserver serves the real HTTP API on the test engine. It prints one line of JSON with its address, its API key and its temporary directory. It stops when its standard input closes.

setUpModule in sdk/python/tests/test_sdk.py starts the test server one time for all tests. The class Base removes all pools, sandboxes, snapshots and volumes after each test. The commands of a sandbox run on your computer, thus each path in a test is below the temporary directory of the server.

make test-python uses -W error. A warning from Python, for example for a file that stays open, is a failure.

The TypeScript SDK tests use the same test server.

scripts/install_test.sh runs scripts/install.sh and scripts/cloud-startup.sh. Nothing is installed. Each test makes an empty “machine” in a temporary directory. The programs on the machine are stand-ins that record how the installer called them. development.md gives the details.

make test-install runs the tests with sh. If dash is installed, it runs them a second time with dash. The installer must work in a plain POSIX shell, and sh on some systems is bash.

  • The name of a Go test is a sentence that must be true: TestAGPUOutlivesItsSandboxWhileAContainerIsStillOnIt. When a test fails, its name tells you which rule the change broke.
  • One test proves one rule from start to end. It creates the sandboxes that it needs. It does not use the result of a different test.
  • A failure message says what the test saw and what it expected.
  • Sub-tests with t.Run group the cases of one rule, for example one case for each operation that fails.
  • Helper functions call t.Helper() and stop the test with t.Fatal. mustCreate is the example.
  • Tests of faults are in separate files, for example internal/sandbox/faults_test.go.
  • The name of a Python test starts with test_ in a class that groups one topic, for example TestFiles.
  • An installer test is a block between t "name" and done_t.

A test does not wait for a timeout of the daemon. It moves a clock.

The manager reads the time only from Config.Now. setup gives the manager the method Now of the type clock in internal/sandbox/manager_test.go. The clock starts at a fixed time and moves only when the test calls Advance.

short := mustCreate(t, e.m, CreateRequest{Timeout: dur(10 * time.Second)})
e.clock.Advance(9 * time.Second)
if got := e.m.Sweep(ctx); len(got) != 0 {
t.Fatalf("swept early: %v", got)
}
e.clock.Advance(time.Second)
if got := e.m.Sweep(ctx); len(got) != 1 || got[0] != short.ID {
t.Fatalf("swept %v, want %s", got, short.ID)
}

Other parts of the code have the same control:

Part Control
A daemon from internal/apitest Daemon.Advance(d) moves the clock of that daemon forward.
The kept connection It gets the clock of the manager. A test also sets helloWait and stall of the manager to a short time.
The watch loops A test does not start them. It calls one pass directly.

Two kinds of wait use real time, because a real process is on the other side:

  • A test that waits for a real process asks again each 20 milliseconds, up to a limit. procGone in internal/sandbox/kept_test.go is the example.
  • A test of a helper that does not answer sets the limit of the manager to 150 milliseconds and waits for it.

When you add code that reads the time in internal/sandbox, read it from m.cfg.Now().

A break-it check answers one question: if this safety rule breaks, does a test fail?

A test that passes does not prove that it can fail. In a break-it check you put a deliberate defect in a copy of one source file. You then run the tests against the copy. If no test fails, a test is missing.

The flag -overlay of the go command makes this possible without a change to the repository. The flag reads a JSON file that tells the compiler to read a different file in the place of a source file. The files of the repository stay as they are. No person can commit the defect by accident.

  1. Select one line that applies a safety rule.
  2. Make a scratch directory that is not in the repository.
  3. Copy the source file to the scratch directory.
  4. Put one defect in the copy. Remove the condition, or make it always false.
  5. Write the overlay file. It maps the path of the source file to the path of the copy.
  6. Run go test -overlay <overlay file> <package> from the root of the repository.
  7. If a test fails, the rule has a test. Record the name of the test.
  8. If no test fails, write the missing test. Then do steps 6 and 7 again.
  9. Delete the scratch directory.

Use one defect for one run. With two defects you cannot tell which defect a test found.

The rule: a client cannot set the variable NVIDIA_VISIBLE_DEVICES for a sandbox. The daemon sets this variable itself. On a host where the NVIDIA runtime is the default, a container with the value all gets each GPU.

Manager.Create in internal/sandbox/manager.go applies the rule with these lines:

if k == gpuEnv {
return Sandbox{}, invalid("%s is set by boxfork; ask for GPUs with the gpu option", k)
}
  1. Go to the root of the repository.

  2. Make the scratch directory and the copy:

    Terminal window
    mkdir /tmp/breakit
    cp internal/sandbox/manager.go /tmp/breakit/manager.go
  3. In /tmp/breakit/manager.go, change the first of the three lines to this line:

    if false && k == gpuEnv {
  4. Write the file /tmp/breakit/overlay.json with this content:

    {
    "Replace": {
    "internal/sandbox/manager.go": "/tmp/breakit/manager.go"
    }
    }
  5. Run the tests of the package with the overlay:

    Terminal window
    go test -overlay /tmp/breakit/overlay.json ./internal/sandbox/
  6. Read the result. A test must fail.

  7. Make sure that the repository did not change: git status --short internal/ shows the same files as before step 2.

  8. Delete the scratch directory: rm -r /tmp/breakit.

The key on the left in the overlay file is the path of the source file. A relative path starts at the directory where you run go. You can also give an absolute path.

The test that must find this defect is TestSandboxesOnlySeeTheGPUsTheyWereGiven in internal/sandbox/manager_test.go. It asks for a sandbox with NVIDIA_VISIBLE_DEVICES=all and expects an InvalidError. With the defect, the create gives no error, and the output has this form:

--- FAIL: TestSandboxesOnlySeeTheGPUsTheyWereGiven (0.01s)
manager_test.go:1320: a caller set NVIDIA_VISIBLE_DEVICES: <nil>
FAIL
FAIL boxfork/internal/sandbox 12.3s
FAIL

The times and the line number can be different on your computer.

To run only that test, add -run:

Terminal window
go test -overlay /tmp/breakit/overlay.json -run 'TestSandboxesOnlySeeTheGPUsTheyWereGiven$' ./internal/sandbox/

If the output is ok boxfork/internal/sandbox, no test knows the rule. Write the test before you send the change.

Do a break-it check for each line that these words describe:

  • The line refuses a request: a limit, a name that is not permitted, a missing API key.
  • The line limits what the daemon reads from a sandbox: a number of bytes or a time.
  • The line makes sure that the engine did what the daemon asked: the runtime, the GPUs, the network option, the labels.
  • The line frees or keeps a resource: a GPU, a volume, a snapshot, a container that the daemon cannot remove.
  • The line decides which containers or volumes the daemon removes.

In the pull request, give the line, the defect and the name of the test that failed.

A proof script does checks on a real engine. It also measures times. The two scripts are scripts/proof/pool.sh and scripts/proof/conn.sh. Each script sends requests directly to the HTTP API with curl. Thus the start of the CLI is not in the times.

  • A Linux machine where scripts/install.sh installed the daemon. The scripts use the service boxfork and the settings file /etc/boxfork/boxfork.env.
  • Docker as the engine. The scripts use the docker command to count and to kill containers.
  • Root. The scripts restart the service and change the settings file.
  • The API key. The installer puts it in /var/lib/boxfork/api-key.
  • curl, and a date command that supports %N.
  • No sandbox and no pool at the start. The first check of each script fails if a container of boxfork runs.
  • Access to a registry for the image, ubuntu:24.04 by default.
  • For pool.sh: the default limit --max-pooled 64, and snapshots that work on the host.

Caution: Use a machine that has no sandboxes that you want to keep. The scripts restart the daemon and kill containers.

Caution: Do not stop a script before its end. Both scripts change BOXFORK_EXTRA_ARGS in the settings file and put the old value back later. If a script stops in the middle, the daemon can run with the changed settings. Then compare the settings file with your settings and run systemctl restart boxfork.

  1. Copy the directory scripts/proof to the machine.
  2. Become root.
  3. Run KEY=$(cat /var/lib/boxfork/api-key) N=20 sh pool.sh.
  4. Run KEY=$(cat /var/lib/boxfork/api-key) N=30 sh conn.sh.
  5. Read each line that starts with FAIL. Read the last line, == done, <number> failed.
  6. Keep the full output. Put it in the pull request.
Variable Default Meaning
KEY None. It is necessary. The API key.
URL http://127.0.0.1:7070 The address of the daemon.
N 20 in pool.sh, 30 in conn.sh The number of rounds for each time measurement.
IMAGE ubuntu:24.04 The image of the sandboxes.

Each check prints one line that starts with PASS or FAIL. The section == timings at the end gives the median, the 90th percentile and the maximum of each measurement.

The exit status of conn.sh is not zero if a check failed. The exit status of pool.sh is zero also when a check failed. Read its last line.

Section What it proves
Start No container of boxfork runs.
1. No pool It measures a create and the first command without a pool. This is the base value.
2. A pool of the image A pool of 4 fills. Four containers wait, and the list of sandboxes does not show them. In each round the container of the new sandbox ran before the request. The sandbox has the metadata and the timeout of the request. The pool fills again.
3. A request of a different kind A create with an environment variable does not come from the pool. The daemon starts a new container, and the pool keeps its four.
4. A burst of 8 on a pool of 4 All eight creates get a sandbox. The pool fills again. After the kills, only the four waiting sandboxes stay.
5. A pool of a snapshot A sandbox from the pool has the files of the snapshot: one small file and one file of 20 MB. The daemon refuses to delete the snapshot while the pool exists.
6. The daemon restarts After systemctl restart boxfork the two pools are full with the same eight containers. The first create after the restart gets one of them.
6b. Waiting sandboxes that die The script kills three of four waiting containers with docker kill. No one of the next four creates gets a dead sandbox. The pool fills again, and the dead containers go.
6c. The limit of the host The daemon refuses a pool, and a larger size, above --max-pooled. A new size through the id of the pool works. The daemon removes the sandboxes that are too many. A new size for a pool that does not exist gives pool_not_found.
6d. Different name servers The script adds --dns 9.9.9.9 and restarts the daemon. The next create does not get an old waiting sandbox. Its resolver file has the new server. Both pools fill with eight new containers. The script then puts the settings back.
7. Smaller pool, delete A smaller size removes the sandboxes that are too many. After the delete of both pools no container runs. The snapshot can then go.

The script counts the processes /.boxfork/guest serve in a container. One such process is one kept connection.

Section What it proves
1. The first command and the next commands Before a command, no helper serves. After one command, one helper serves. After N more commands, there is still one.
2. A command runs as before Through the kept connection a command gets the environment of the sandbox, its standard input, its exit status and its own timeout. A command that cannot start gives exit code 127. Output above the limit is cut, and the answer says truncated.
3. Twenty commands at the same time Each command gets its own answer. All of them use one connection.
4. Pause and resume A paused sandbox refuses a command with status 409. After the resume the same helper answers.
5. The helper is killed in the sandbox A command in the sandbox kills the helper with signal 9. The next command gets an answer from one new helper.
6. The helper is stopped in the sandbox A command in the sandbox sends STOP to the helper. The next command gets an answer in less than 20 seconds and does not wait without end. The command after that gets a correct answer in less than 0.5 seconds.
7. A kill while a command runs The command gets status 404.
8. A sandbox without a network The kept connection works there also.
9. Suspend and wake An idle sandbox with an open kept connection is suspended. Its container goes. A command wakes it with its files, in a new container, with a new connection.
10. The daemon restarts The helper of the old connection goes. A command gets an answer after the restart.
11. An idle connection With --connection-idle 3s the helper goes after the idle time. The sandbox then runs only its first process. The next command opens a new connection.
12. No kept connection With --connection-idle 0 no helper stays. A fixed command gives the same output with and without the kept connection. The script then puts the settings back.
End No container of boxfork stays.

performance.md gives the numbers that these scripts measured.

  • Start with a comment that says what the script proves, and give an example command.
  • Send requests to the HTTP API. Do not use the CLI for a time measurement.
  • Make sure of the state from the outside, with the docker command, and not only with the answers of the daemon.
  • Print one PASS or FAIL line for each check, and the number of failures at the end.
  • Put each setting back that the script changes.
  • End with an exit status that is not zero if a check failed.

Each change needs these three things, as applicable:

  1. Tests. A test that fails without the change. All of make test passes.
  2. Break-it checks, for each new safety rule. A rule without a test that can fail is not done.
  3. A run on a real engine, for behavior that the test engine cannot show. Use a proof script if one applies to your change. Add a section to it, or write a new script.

Do not lower a test bar to make a change pass. ../CONTRIBUTING.md gives the rule.

In the pull request, say what you ran and on which machine. Say what you did not run.

  • Nobody ran a test or a proof script on a machine with an NVIDIA GPU. All GPU tests use stand-ins.
  • Nobody ran a test or a proof script on x86_64.
  • Nobody ran the guides for AWS and Google Cloud on a real account.
  • The runs on a real engine were on Docker with gVisor on arm64, in a nested virtual machine, and on Podman. A nested virtual machine changes the times. Do not compare its numbers with numbers from a different machine.
  • Nobody ran the installer on a system without apt. Nobody ran the tools with a live model.
  • No CI service runs the tests.