mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
- backend.Backend gains Problems(ctx) []Problem (Key, Fix, Msg): all that a backend needs and the host lacks. Check is the first of them, so every command that needs the backend stops at the first one. - Lima's Problems (check.go): host OS, arm64/amd64 (arch), macOS 13+ (macos, via sysctl kern.osproductversion: platform.MacOSVersion), Lima missing/broken/older than 2.0 (lima; brew install/reinstall/ upgrade lima when Homebrew is there), on Linux qemu-system-<arch> and qemu-img (qemu; apt-get, dnf, pacman or zypper's command), /dev/kvm missing (kvm) or not openable (kvm-access; sudo usermod -aG kvm USER), Lima's global configs (lima-config). DEVVM_KVM swaps /dev/kvm for tests. - devvm doctor [--backend NAME] [--fixes]: a features table (FEATURE, one column per backend: native, emulated, unreviewed, none; Support gains String), Docker found or not, then doctor_ok or each problem (the last one fails). --fixes prints KEY<tab>FIX<tab>MESSAGE lines and succeeds either way. - Messages: lima_missing_fix, lima_broken_fix, lima_old_fix, qemu_missing_fix, lima_arch_unsupported, macos_old, no_kvm, kvm_access, docker_found, docker_missing, doctor_ok, doctor_failed; kvm and ok are initialisms in the generator. - Tests: TestProblems (23 cases), TestWriteFeatures, TestWriteFixes, TestMacOSVersion, Support's String in TestSupport; doctor.txtar; the script sandbox sets DEVVM_KVM; e2e images checks doctor. - Converted: cli.bats preflight (arm64 and x86_64), doctor (macOS without Lima, Linux without QEMU, x86_64 Linux, limactl that doesn't run), the doctor half of "all there", doctor's half of the Lima global configs case. - Docs: new install.md (requirements, checking them, --fixes keys), lima.md requirements, backends.md, commands.md, contributing.md, README; CLI reference regenerated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
167 lines
5.2 KiB
Go
167 lines
5.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"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)
|
|
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){
|
|
"stub_calls": stubCalls,
|
|
"wait_file": waitFile,
|
|
},
|
|
})
|
|
}
|
|
|
|
// buildDevvm builds cmd/devvm into a temporary folder, with copies of the
|
|
// files devvm reads next to itself, and returns that folder.
|
|
func buildDevvm(t *testing.T) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
cmd := exec.CommandContext(t.Context(), "go", "install", "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)
|
|
}
|
|
repo := filepath.Join("..", "..")
|
|
data, err := os.ReadFile(filepath.Join(repo, "base.yaml")) //nolint:gosec // the repo's own file
|
|
if err == nil {
|
|
err = os.WriteFile(filepath.Join(dir, "base.yaml"), data, 0o600) //nolint:gosec // the test's own folder
|
|
}
|
|
for _, d := range []string{"layers", "features", "tools", "guest", "oci"} {
|
|
if err == nil {
|
|
err = os.CopyFS(filepath.Join(dir, d), os.DirFS(filepath.Join(repo, d)))
|
|
}
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
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 // the test's own folder
|
|
_ = 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 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
|
|
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])
|
|
}
|
|
|
|
// 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)
|
|
}
|