Files
devvm/internal/cli/script_test.go
T
w3cjandClaude Opus 5.5 fcbea2c744 feat(docker): network policy, and build confinement on Docker Desktop
A Docker VM with network policy sits alone with a relay container on an
internal network of its own (inhibit_ipv4). The relay, devvm's own Linux
build in an empty imported image (read-only, every capability dropped),
copies the VM's connections to a unix socket of devvm's proxy on the
host, so tokens, the Keychain, the CA key and agents' logins never enter
Docker. On and off is docker network connect/disconnect at the next
start. Desktop builds run on a network and relay of their own, through a
host proxy allowing any host on ports 80 and 443.

- every Docker VM drops NET_RAW; it's in the config hash, so containers
  are made again from their commit
- confined: the start's ID, the VM alone on its network with the relay
  by its recorded container ID, the network still internal; a foreign
  member is docker_network_shared, at start and on removal
- proxy: -listen-unix, -relay/-relay-to; a lifeline pid file written
  after the proxy started whose process has gone ends it
- macOS fetches the release's Linux build on first use, checked against
  a SHA-256 the two-pass release.sh bakes in; builds without the pin use
  devvm-linux-<arch> beside devvm or refuse
- the build proxy is stopped through the process devvm started
- reviewed: design notes and threat check in design.md; e2e on the
  Linux engine and Desktop: dropped capabilities, network members and
  options, no IPv6 beyond the bridge's link-local, no DNS query leaving
  (captured, with an unconfined control), direct connections refused

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 05:08:03 -07:00

253 lines
8.7 KiB
Go

package cli
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"crypto/sha256"
"encoding/hex"
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
"github.com/rogpeppe/go-internal/testscript"
)
// TestScript runs testdata/script/*.txtar against a built devvm, in a sandbox.
func TestScript(t *testing.T) {
bin := buildDevvm(t)
release := linuxRelease(t)
stubs, err := filepath.Abs(filepath.Join("..", "..", "testdata", "stubs"))
if err != nil {
t.Fatal(err)
}
testscript.Run(t, testscript.Params{
Dir: filepath.Join("..", "..", "testdata", "script"),
RequireExplicitExec: true,
RequireUniqueNames: true,
Setup: func(env *testscript.Env) error {
return sandbox(env, bin, stubs)
},
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
"linux_release": release.serve,
"listen_any": listenAny,
"stub_calls": stubCalls,
"wait_file": waitFile,
},
})
}
// linuxBuild is a release's Linux archive, for the Linux build devvm
// fetches: the test build of devvm, built for Linux on this machine's
// processor as the test build itself was, and the devvm in it's SHA-256.
type linuxBuild struct {
archive []byte
sum string
}
// linuxRelease builds it.
func linuxRelease(t *testing.T) linuxBuild {
t.Helper()
dir := t.TempDir()
out := filepath.Join(dir, "devvm")
cmd := exec.CommandContext(t.Context(), "go", "build", "-o", out, //nolint:gosec // the test's own folder
"-ldflags", "-X github.com/w3cj/devvm/internal/backend/registry.unreviewed=allowed", "github.com/w3cj/devvm/cmd/devvm")
cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=linux", "GOARCH="+runtime.GOARCH)
if b, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("building devvm for Linux: %v\n%s", err, b)
}
data, err := os.ReadFile(out) //nolint:gosec // the test's own build
if err != nil {
t.Fatal(err)
}
sum := sha256.Sum256(data)
var b bytes.Buffer
gz := gzip.NewWriter(&b)
tw := tar.NewWriter(gz)
if err := tw.WriteHeader(&tar.Header{Name: "devvm-0.0.0-linux-" + runtime.GOARCH + "/devvm", Mode: 0o755, Size: int64(len(data)), Typeflag: tar.TypeReg}); err != nil {
t.Fatal(err)
}
if _, err := tw.Write(data); err != nil {
t.Fatal(err)
}
if err := tw.Close(); err != nil {
t.Fatal(err)
}
if err := gz.Close(); err != nil {
t.Fatal(err)
}
return linuxBuild{archive: b.Bytes(), sum: hex.EncodeToString(sum[:])}
}
// serve serves the archive on a loopback port until the script ends, and
// points the test build at it as a release's Linux build: its address in
// DEVVM_LINUX_BUILD_URL, and in DEVVM_LINUX_BUILD_SHA256 the devvm in it's
// SHA-256 (match), or another's (mismatch).
func (l linuxBuild) serve(ts *testscript.TestScript, neg bool, args []string) {
if neg || len(args) != 1 || args[0] != "match" && args[0] != "mismatch" {
ts.Fatalf("usage: linux_release match|mismatch")
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(l.archive) }))
ts.Defer(srv.Close)
sum := l.sum
if args[0] == "mismatch" {
sum = strings.Repeat("0", 64)
}
ts.Setenv("DEVVM_LINUX_BUILD_URL", srv.URL+"/devvm-0.0.0-linux-"+runtime.GOARCH+".tar.gz")
ts.Setenv("DEVVM_LINUX_BUILD_SHA256", sum)
ts.Setenv("LINUX_ARCH", runtime.GOARCH)
}
// buildDevvm builds cmd/devvm, its files embedded, into a temporary folder
// and returns that folder. It's a test build: DEVVM_UNREVIEWED counts.
func buildDevvm(t *testing.T) string {
t.Helper()
dir := t.TempDir()
cmd := exec.CommandContext(t.Context(), "go", "install",
"-ldflags", "-X github.com/w3cj/devvm/internal/backend/registry.unreviewed=allowed", "github.com/w3cj/devvm/cmd/devvm")
cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOBIN="+dir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("building devvm: %v\n%s", err, out)
}
return dir
}
// sandbox points every folder devvm uses into a new temporary folder, and puts
// devvm and the stubs first on PATH.
func sandbox(env *testscript.Env, bin, stubs string) error {
// /tmp keeps Lima's socket paths short.
root, err := os.MkdirTemp("/tmp", "dvt.")
if err != nil {
return err
}
env.Defer(func() {
// VMs' proxies follow a stub hostagent that's gone before they
// look: stop them with the sandbox.
_ = exec.CommandContext(context.Background(), "pkill", "-f", "_proxy -listen .* -lifeline "+root+"/").Run() //nolint:gosec,forbidigo // the test's own folder; a cleanup, after the scripts' contexts end
// And smolvm VMs' port forwarders.
_ = exec.CommandContext(context.Background(), "pkill", "-f", "_smolvm-ports -vm .* -lifeline "+root+"/").Run() //nolint:gosec,forbidigo // the test's own folder; a cleanup, after the scripts' contexts end
// And Docker VMs'.
_ = exec.CommandContext(context.Background(), "pkill", "-f", "_docker-ports -vm .* -key "+root+"/").Run() //nolint:gosec,forbidigo // the test's own folder; a cleanup, after the scripts' contexts end
_ = os.RemoveAll(root)
})
dirs := map[string]string{
"HOME": "home",
"XDG_DATA_HOME": "data",
"XDG_CONFIG_HOME": "config",
"DEVVM_LIMA_HOME": "lima",
"LIMA_HOME": "lima",
"STUB_DIR": "stub",
}
for name, dir := range dirs {
path := filepath.Join(root, dir)
if err := os.MkdirAll(path, 0o750); err != nil {
return err
}
env.Setenv(name, path)
}
log := filepath.Join(root, "stub", "calls")
if err := os.WriteFile(log, nil, 0o600); err != nil {
return err
}
env.Setenv("STUB_LOG", log)
// Base's features are chosen (none), so nothing asks.
if err := os.MkdirAll(filepath.Join(root, "config", "devvm"), 0o700); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(root, "config", "devvm", "base-features"), nil, 0o600); err != nil {
return err
}
// Ubuntu's cloud images: one dated release in a folder, never the
// network.
rel := filepath.Join(root, "ubuntu", "release-20260101")
if err := os.MkdirAll(rel, 0o750); err != nil {
return err
}
sums := strings.Repeat("1", 64) + " *ubuntu-26.04-server-cloudimg-arm64.img\n" +
strings.Repeat("2", 64) + " *ubuntu-26.04-server-cloudimg-amd64.img\n"
if err := os.WriteFile(filepath.Join(rel, "SHA256SUMS"), []byte(sums), 0o600); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(rel, "SHA256SUMS.gpg"), []byte("signature\n"), 0o600); err != nil {
return err
}
env.Setenv("DEVVM_UBUNTU_URL", "file://"+filepath.Join(root, "ubuntu"))
// Linux's /dev/kvm: a file anyone can open, so the host's doesn't
// matter.
kvm := filepath.Join(root, "stub", "kvm")
if err := os.WriteFile(kvm, nil, 0o600); err != nil {
return err
}
env.Setenv("DEVVM_KVM", kvm)
env.Setenv("DEVVM_SECRET_SERVICE", "devvm-test")
// The stub smolvm's machines run on this process, which outlives each
// command, as smolvm's own does.
env.Setenv("SMOLVM_STUB_PID", strconv.Itoa(os.Getpid()))
// The shared tools' versions: the stub mise, which resolves the
// fixture lock.
env.Setenv("DEVVM_HOST_MISE", filepath.Join(stubs, "mise"))
env.Setenv("MISE_STUB_LOCK", filepath.Join(filepath.Dir(stubs), "mise.lock"))
env.Setenv("DEVVM_BIN", bin) // for scripts that set their own PATH
repo, err := filepath.Abs(filepath.Join("..", ".."))
if err != nil {
return err
}
env.Setenv("DEVVM_REPO", repo) // for the repo's own scripts (scripts/, assets/)
env.Setenv("PATH", strings.Join([]string{bin, stubs, env.Getenv("PATH")}, string(os.PathListSeparator)))
return nil
}
// waitFile waits up to 10 seconds for a file to exist, for a background
// command to get somewhere.
func waitFile(ts *testscript.TestScript, neg bool, args []string) {
if neg || len(args) != 1 {
ts.Fatalf("usage: wait_file FILE")
}
for range 200 {
if _, err := os.Stat(ts.MkAbs(args[0])); err == nil {
return
}
time.Sleep(50 * time.Millisecond)
}
ts.Fatalf("%s didn't appear", args[0])
}
// listenAny listens on a free port on every address, as a program on the
// host does, until the script ends, and sets VAR to the port.
func listenAny(ts *testscript.TestScript, neg bool, args []string) {
if neg || len(args) != 1 {
ts.Fatalf("usage: listen_any VAR")
}
l, err := net.Listen("tcp", ":0") //nolint:noctx,gosec // every address, as the program it stands for; the script's whole life
ts.Check(err)
ts.Defer(func() { _ = l.Close() })
ts.Setenv(args[0], strconv.Itoa(l.Addr().(*net.TCPAddr).Port))
}
// stubCalls writes the calls the stubs logged to stdout, one per line.
// Negated, it fails if any were logged.
func stubCalls(ts *testscript.TestScript, neg bool, args []string) {
if len(args) > 0 {
ts.Fatalf("usage: stub_calls")
}
calls := ts.ReadFile(ts.Getenv("STUB_LOG"))
if neg {
if calls != "" {
ts.Fatalf("unexpected stub calls:\n%s", calls)
}
return
}
_, err := ts.Stdout().Write([]byte(calls))
ts.Check(err)
}