mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
devvm agent save refused a VM whose ~/.config/opencode held a bun.lock (which opencode's plugins' installs write), a package-lock.json, a .gitignore or a .DS_Store: the save's script sent every entry of that folder but node_modules and FolderOwn, while the host's listedPath refuses those files (fileSetting), so it saved nothing. notSettings is now their one list, as patterns path.Match and the shell's case read alike: fileSetting leaves them out and the script sends a file of those names no more, only a folder that isn't a link, which is a setting. TestAgentSaveScriptRuns plants them and a folder named old.lock, and failed with agent_save_unsafe before the fix. opencode's HistoryOwn names its native client's plugin approvals, in its state folder (cli-rs's plugin/src/loader/approval.rs and tui/src/plugins/host.rs, opencode v2.4.1): plugin-approvals.json, its .lock and the .plugin-approvals.json.<pid>.<n>.tmp it's written through, and plugin-staged, the approved trees' copies, which it reuses by their hash's name without hashing them again, so a hostile VM's export could approve a project plugin for the VM that imports it. That client doesn't ship in 2.4.1. TestHistoryExportRuns and TestHistoryImportRuns plant them, with plugin-notes.json as the control, and failed on fedora without the entries. The history scripts' framing (onInputCmd) now says why on its stderr, which goes to the VM's log, when its input was cut short: TestOnInputCutShort checks it for every cut, an empty input included, and none for a whole one, and failed before. TestHistoryScriptsOnInput holds each script under 128 KiB, Linux's limit on one argument, which bash -c "$s" passes it as. The CLI scripts match the framing's new start. Docs: the save's and the store's table say which files stay in the VM; the import's and How it works' lists name the plugin approvals; the release page gets opencode's lock files in a save and the approvals; the changelog's opencode block gets the save's fix, and the hardening line names approved plugins. The review's optional wording: the design note's `*` matches every resource, and the plain-password steps set this machine's password first. Co-Authored-By: Claude Opus 5.5 <[email protected]>
640 lines
22 KiB
Go
640 lines
22 KiB
Go
package vm
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/gitprotect"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/netpolicy"
|
|
"github.com/devvmhq/devvm/internal/paths"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// inputScript is the script a fake Exec got on its input (onInput) and
|
|
// the rest of that input, or "" for any other command. It reads that
|
|
// input.
|
|
func inputScript(argv []string, o backend.ExecOpts) (script string, rest []byte) {
|
|
if !slices.Equal(argv, []string{"bash", "-c", onInputCmd}) || o.Stdin == nil {
|
|
return "", nil
|
|
}
|
|
b, _ := io.ReadAll(o.Stdin)
|
|
n, after, _ := strings.Cut(string(b), "\n")
|
|
size, err := strconv.Atoi(n)
|
|
if err != nil || size > len(after) {
|
|
return "", nil
|
|
}
|
|
return after[:size], []byte(after[size:])
|
|
}
|
|
|
|
// exportScript is the export of agents' history that a fake Exec got on
|
|
// its input, or "" for any other command. It reads that input.
|
|
func exportScript(argv []string, o backend.ExecOpts) string {
|
|
if s, _ := inputScript(argv, o); strings.Contains(s, "tar -czf") {
|
|
return s
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// The export's and the import's scripts go on the VM's bash's input,
|
|
// never in its arguments, which ssh's shared connection on macOS fails to
|
|
// pass at their length; the import's export follows its script.
|
|
func TestHistoryScriptsOnInput(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
existingVM(t, e, Options{Agents: &[]string{"claude"}, Network: "open"})
|
|
var scripts []string
|
|
var rest []byte
|
|
f.ExecFunc = func(_ vmname.VM, argv []string, o backend.ExecOpts) error {
|
|
if s, r := inputScript(argv, o); s != "" {
|
|
scripts, rest = append(scripts, s), r
|
|
}
|
|
for _, a := range argv {
|
|
if len(a) > 4096 {
|
|
t.Errorf("an argument of %d bytes: %.80q", len(a), a)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
file := filepath.Join(t.TempDir(), "h.tar.gz")
|
|
if err := (Lifecycle{e}).Export(t.Context(), file); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if err := os.WriteFile(file, []byte("the export"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := (Lifecycle{e}).Import(t.Context(), file); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
want := []string{strings.TrimRight(historyExport(), "\n"), strings.TrimRight(historyImport(), "\n")}
|
|
if !slices.Equal(scripts, want) || string(rest) != "the export" {
|
|
t.Errorf("the scripts on the input: %d, rest %q", len(scripts), rest)
|
|
}
|
|
// In the VM, onInputCmd passes each to bash -c as one argument, which
|
|
// Linux holds under 128 KiB (MAX_ARG_STRLEN).
|
|
for _, s := range want {
|
|
if len(s) >= 128<<10 {
|
|
t.Errorf("a script of %d bytes: %.80q", len(s), s)
|
|
}
|
|
}
|
|
}
|
|
|
|
// onInput's command runs its script, with the rest of its input as the
|
|
// script's, only when it read the whole script: an input cut short, even
|
|
// to a whole command, runs nothing, and says why on its stderr, which
|
|
// goes to the VM's log.
|
|
func TestOnInputCutShort(t *testing.T) {
|
|
script := "mkdir ran\ncat > rest\n"
|
|
argv, in := onInput(script, strings.NewReader("the rest"))
|
|
b, err := io.ReadAll(in)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
full := string(b)
|
|
for _, n := range []int{len(full), 0, 1, len(full) / 2, strings.Index(full, "cat")} {
|
|
dir := t.TempDir()
|
|
var stderr strings.Builder
|
|
cmd := exec.CommandContext(t.Context(), argv[0], argv[1:]...) //nolint:gosec // devvm's own command, its script on its input
|
|
cmd.Dir, cmd.Stdin, cmd.Stderr = dir, strings.NewReader(full[:n]), &stderr
|
|
err := cmd.Run()
|
|
_, ran := os.Stat(filepath.Join(dir, "ran"))
|
|
if n == len(full) {
|
|
if err != nil || ran != nil || readFile(t, filepath.Join(dir, "rest")) != "the rest" || stderr.Len() > 0 {
|
|
t.Errorf("whole: %v, ran %v, stderr %q", err, ran, stderr.String())
|
|
}
|
|
continue
|
|
}
|
|
if err == nil || ran == nil {
|
|
t.Errorf("cut at %d of %d: %v, it ran", n, len(full), err)
|
|
}
|
|
if !strings.Contains(stderr.String(), "devvm: The script on the input was cut short") {
|
|
t.Errorf("cut at %d of %d: stderr %q", n, len(full), stderr.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
// rm asks first in a terminal and refuses without one, unless --force;
|
|
// --no-export deletes without the export.
|
|
func TestRmConfirms(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
lc := Lifecycle{e}
|
|
vm := existingVM(t, e, Options{Agents: &[]string{"claude"}, Network: "open"}).Name().String()
|
|
if err := lc.Rm(t.Context(), RmOptions{}); msg.NamesOf(err) != "no_input answer=--force" {
|
|
t.Errorf("no terminal: %v", msg.NamesOf(err))
|
|
}
|
|
asked := answers(t, e, false)
|
|
if err := lc.Rm(t.Context(), RmOptions{}); err != nil || !slices.Equal(*asked, []string{"rm_confirm vm=" + vm}) {
|
|
t.Errorf("declined: %v, asked %q", msg.NamesOf(err), *asked)
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; !ok {
|
|
t.Fatal("deleted when declined")
|
|
}
|
|
exported := false
|
|
f.ExecFunc = func(_ vmname.VM, argv []string, o backend.ExecOpts) error {
|
|
exported = exported || exportScript(argv, o) != ""
|
|
return nil
|
|
}
|
|
if err := lc.Rm(t.Context(), RmOptions{Force: true, NoExport: true}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; ok || exported || len(*asked) != 1 {
|
|
t.Errorf("--force --no-export: deleted %v, exported %v, asked %q", !ok, exported, *asked)
|
|
}
|
|
}
|
|
|
|
// rm exports an agent VM's history first; a failed export keeps the VM.
|
|
// One made for an earlier folder at this path is deleted only after
|
|
// asking, without an export.
|
|
func TestRm(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
lc := Lifecycle{e}
|
|
st := existingVM(t, e, Options{Agents: &[]string{"claude"}, Network: "open"})
|
|
vm := st.Name().String()
|
|
f.ExecFunc = func(_ vmname.VM, argv []string, o backend.ExecOpts) error {
|
|
if exportScript(argv, o) != "" {
|
|
return errors.New("tar failed")
|
|
}
|
|
return nil
|
|
}
|
|
if err := lc.Rm(t.Context(), RmOptions{Force: true}); msg.NamesOf(err) != "export_failed_kept command=devvm rm vm="+vm {
|
|
t.Errorf("Rm = %v", msg.NamesOf(err))
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; !ok {
|
|
t.Fatal("deleted after a failed export")
|
|
}
|
|
if es, _ := os.ReadDir(e.Dirs.Exports()); len(es) > 0 {
|
|
t.Errorf("exports %v", es)
|
|
}
|
|
var script string
|
|
f.ExecFunc = func(_ vmname.VM, argv []string, o backend.ExecOpts) error {
|
|
if s := exportScript(argv, o); s != "" {
|
|
script = s
|
|
_, err := o.Stdout.Write([]byte("tarball"))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
if err := lc.Rm(t.Context(), RmOptions{Force: true}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if !strings.Contains(script, "ls -d .claude .claude.json .codex") || !strings.Contains(script, "--exclude=.claude/.credentials.json") {
|
|
t.Errorf("export script %q", script)
|
|
}
|
|
es, _ := os.ReadDir(e.Dirs.Exports())
|
|
if len(es) != 2 || !strings.HasPrefix(es[1].Name(), vm+"-") || !strings.HasSuffix(es[1].Name(), ".tar.gz") {
|
|
t.Fatalf("exports %v", es)
|
|
}
|
|
export := filepath.Join(e.Dirs.Exports(), es[1].Name())
|
|
if data, _ := os.ReadFile(export); string(data) != "tarball" { //nolint:gosec // the test's own export
|
|
t.Errorf("export = %q", data)
|
|
}
|
|
// The file beside it names its folder, for devvm history, and its
|
|
// tier, for an import.
|
|
if data, _ := os.ReadFile(paths.ExportFolder(export)); string(data) != e.Dir+"\nopen\n" {
|
|
t.Errorf("folder = %q, want %q", data, e.Dir)
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; ok {
|
|
t.Error("not deleted")
|
|
}
|
|
if _, err := os.Stat(e.Dirs.VM(vmname.VM(vm)).Dir); !errors.Is(err, os.ErrNotExist) {
|
|
t.Errorf("its folder: %v", err)
|
|
}
|
|
|
|
// Another folder at the same path.
|
|
st = existingVM(t, e, Options{Agents: &[]string{"claude"}, Network: "open"})
|
|
if err := set(t, st, keyFolder, "0:0"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
asked := answers(t, e, false)
|
|
if err := lc.Rm(t.Context(), RmOptions{Force: true, NoExport: true}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; !ok || !slices.Equal(*asked, []string{"delete_other_folder_vm vm=" + vm + " was=" + e.Dir}) {
|
|
t.Fatalf("declined: asked %q", *asked)
|
|
}
|
|
answers(t, e, true)
|
|
script = ""
|
|
if err := lc.Rm(t.Context(), RmOptions{Force: true}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; ok || script != "" {
|
|
t.Errorf("confirmed: deleted %v, export %q", !ok, script)
|
|
}
|
|
}
|
|
|
|
// streamForever is a VM's export that never ends: it writes until a
|
|
// write fails, its stderr too, as a VM's root can.
|
|
func streamForever(argv []string, o backend.ExecOpts) error {
|
|
if exportScript(argv, o) == "" {
|
|
return nil
|
|
}
|
|
chunk := []byte(strings.Repeat("x", 64<<10))
|
|
for {
|
|
for range 8 {
|
|
if _, err := o.Stderr.Write(chunk); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if _, err := o.Stdout.Write(chunk); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
// An export over the most devvm keeps is stopped and kept nowhere: devvm
|
|
// history export fails, and rm and a remake warn and go on, though the
|
|
// VM keeps writing. What its stderr adds to the run log is capped too.
|
|
func TestExportTooLarge(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
var log bytes.Buffer
|
|
fake(e).Log = &log
|
|
e.HistoryMax = 1 << 20
|
|
lc := Lifecycle{e}
|
|
vm := existingVM(t, e, Options{Agents: &[]string{"claude"}, Network: "open"}).Name().String()
|
|
f.ExecFunc = func(_ vmname.VM, argv []string, o backend.ExecOpts) error { return streamForever(argv, o) }
|
|
file := filepath.Join(t.TempDir(), "h.tar.gz")
|
|
if err := lc.Export(t.Context(), file); msg.NamesOf(err) != "history_too_large vm="+vm {
|
|
t.Errorf("Export = %v", msg.NamesOf(err))
|
|
}
|
|
if es, _ := os.ReadDir(filepath.Dir(file)); len(es) != 0 {
|
|
t.Errorf("left %v", es)
|
|
}
|
|
if log.Len() > vmLogMax+len(vmLogCut) || !strings.Contains(log.String(), vmLogCut) {
|
|
t.Errorf("run log of %d bytes", log.Len())
|
|
}
|
|
|
|
var warned []string
|
|
fake(e).OnWarn = func(m msg.M) { warned = append(warned, m.Names()) }
|
|
if err := lc.Rm(t.Context(), RmOptions{Force: true}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; ok || !slices.Equal(warned, []string{"history_too_large vm=" + vm}) {
|
|
t.Errorf("rm: deleted %v, warned %q", !ok, warned)
|
|
}
|
|
if es, _ := os.ReadDir(e.Dirs.Exports()); len(es) != 0 {
|
|
t.Errorf("exports %v", es)
|
|
}
|
|
}
|
|
|
|
// devvm history import warns of an export from a VM on a less strict
|
|
// tier, then imports it: by the tier the file beside an export in the
|
|
// exports folder records, and as open for one without, or elsewhere,
|
|
// where the file beside it isn't devvm's. An open VM gets no warning.
|
|
func TestImportWarnsOfTier(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
f.ExecFunc = noDirect
|
|
vm := existingVM(t, e, Options{Agents: &[]string{"claude"}, Network: "allowlist"}).Name().String()
|
|
x := Exports{Dirs: e.Dirs}
|
|
write := func(dir, name string, tier netpolicy.Tier) string {
|
|
p := filepath.Join(dir, name)
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte("x"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tier != "" {
|
|
if err := x.writeFolder(p, e.Dir, tier); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return p
|
|
}
|
|
exports := e.Dirs.Exports()
|
|
open := write(exports, vm+"-20260101-000000.tar.gz", netpolicy.TierOpen)
|
|
internet := write(exports, vm+"-20260102-000000.tar.gz", netpolicy.TierInternet)
|
|
none := write(exports, vm+"-20260103-000000.tar.gz", netpolicy.TierNone)
|
|
same := write(exports, vm+"-20260104-000000.tar.gz", netpolicy.TierAllowlist)
|
|
unknown := write(exports, vm+"-20260105-000000.tar.gz", "")
|
|
mine := write(t.TempDir(), "mine.tar.gz", netpolicy.TierNone)
|
|
var warned []string
|
|
fake(e).OnWarn = func(m msg.M) { warned = append(warned, m.Names()) }
|
|
lc := Lifecycle{e}
|
|
for file, want := range map[string]string{
|
|
open: "history_import_less_strict file=" + open + " from=open tier=allowlist vm=" + vm,
|
|
internet: "history_import_less_strict file=" + internet + " from=internet tier=allowlist vm=" + vm,
|
|
none: "",
|
|
same: "",
|
|
unknown: "history_import_tier_unknown file=" + unknown + " tier=allowlist vm=" + vm,
|
|
mine: "history_import_tier_unknown file=" + mine + " tier=allowlist vm=" + vm,
|
|
} {
|
|
warned = nil
|
|
if err := lc.Import(t.Context(), file); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if got := strings.Join(warned, "; "); got != want {
|
|
t.Errorf("%s: warned %q, want %q", filepath.Base(file), got, want)
|
|
}
|
|
}
|
|
|
|
e2, f2, _ := upEnv(t, allNative())
|
|
f2.ExecFunc = noDirect
|
|
existingVM(t, e2, Options{Agents: &[]string{"claude"}, Network: "open"})
|
|
fake(e2).OnWarn = func(m msg.M) { t.Errorf("open VM: warned %s", m.Names()) }
|
|
if err := (Lifecycle{e2}).Import(t.Context(), mine); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
}
|
|
|
|
// rm VM checks again once it has asked: a folder that came back while it
|
|
// asked keeps its VM, and so does an agent VM stopped meanwhile, which
|
|
// its export would start.
|
|
func TestRmVMChecksAgain(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
lc := Lifecycle{e}
|
|
vm := existingVM(t, e, Options{Agents: &[]string{"claude"}, Network: "open"}).Name().String()
|
|
dir := e.Dir
|
|
if err := os.Remove(dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Dir = t.TempDir()
|
|
answers(t, e, true)
|
|
fake(e).OnAsk = func(msg.M) bool { return os.Mkdir(dir, 0o700) == nil }
|
|
if err := lc.RmVM(t.Context(), vm, RmOptions{}); msg.NamesOf(err) != "rm_vm_folder_there dir="+dir+" vm="+vm {
|
|
t.Errorf("a folder back: %v", msg.NamesOf(err))
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; !ok {
|
|
t.Fatal("deleted though its folder came back")
|
|
}
|
|
if err := os.Remove(dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake(e).OnAsk = func(msg.M) bool {
|
|
in := f.Instances[vmname.VM(vm)]
|
|
in.Status = backend.StatusStopped
|
|
f.Instances[vmname.VM(vm)] = in
|
|
return true
|
|
}
|
|
if err := lc.RmVM(t.Context(), vm, RmOptions{}); msg.NamesOf(err) != "rm_gone_stopped dir="+dir+" vm="+vm {
|
|
t.Errorf("stopped meanwhile: %v", msg.NamesOf(err))
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; !ok {
|
|
t.Error("deleted though it stopped")
|
|
}
|
|
}
|
|
|
|
// rm VM refuses a VM whose state records a folder whose VM it isn't (a
|
|
// state changed by hand) as one whose folder is there: the folder it
|
|
// names, this one, keeps its own VM, which rm VM never deletes for it.
|
|
func TestRmVMFolderOfAnother(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
web := e.Dir
|
|
own := existingVM(t, e, Options{Network: "open"}).Name().String()
|
|
subfolder(t, e, filepath.Dir(web), "api")
|
|
e.Folder = FolderHere
|
|
if err := (Launch{e}).Up(t.Context(), Options{Network: "open"}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
other, err := (finder{e}).target(t.Context(), need{})
|
|
if err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if err := set(t, other.st, keyDir, web); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Dir, e.Folder = web, FolderAuto
|
|
err = (Lifecycle{e}).RmVM(t.Context(), other.f.Name.String(), RmOptions{Force: true, NoExport: true})
|
|
if msg.NamesOf(err) != "rm_vm_folder_there dir="+web+" vm="+other.f.Name.String() {
|
|
t.Errorf("rm VM of another folder's state: %v", msg.NamesOf(err))
|
|
}
|
|
for _, vm := range []string{own, other.f.Name.String()} {
|
|
if _, ok := f.Instances[vmname.VM(vm)]; !ok {
|
|
t.Errorf("%s deleted", vm)
|
|
}
|
|
}
|
|
}
|
|
|
|
// export saves the history to the exports folder or a file of the user's,
|
|
// which a failed export leaves as it was; the VM is kept either way.
|
|
func TestExport(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
lc := Lifecycle{e}
|
|
vm := existingVM(t, e, Options{Network: "open"}).Name().String()
|
|
tarball := "tarball"
|
|
f.ExecFunc = func(_ vmname.VM, argv []string, o backend.ExecOpts) error {
|
|
if exportScript(argv, o) != "" {
|
|
if tarball == "" {
|
|
_, _ = o.Stdout.Write([]byte("partial"))
|
|
return errors.New("tar failed")
|
|
}
|
|
_, err := o.Stdout.Write([]byte(tarball))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
// An exports folder open to others is the user's alone after an
|
|
// export.
|
|
if err := os.MkdirAll(e.Dirs.Exports(), 0o755); err != nil { //nolint:gosec // the test's own folder
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(e.Dirs.Exports(), 0o755); err != nil { //nolint:gosec // the test's own folder
|
|
t.Fatal(err)
|
|
}
|
|
if err := lc.Export(t.Context(), ""); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
es, _ := os.ReadDir(e.Dirs.Exports())
|
|
if len(es) != 2 || !strings.HasPrefix(es[1].Name(), vm+"-") || !strings.HasSuffix(es[1].Name(), ".tar.gz") {
|
|
t.Fatalf("exports %v", es)
|
|
}
|
|
for path, mode := range map[string]os.FileMode{
|
|
e.Dirs.Exports(): 0o700, filepath.Join(e.Dirs.Exports(), es[1].Name()): 0o600, filepath.Join(e.Dirs.Exports(), es[0].Name()): 0o600,
|
|
} {
|
|
if fi, err := os.Stat(path); err != nil || fi.Mode().Perm() != mode {
|
|
t.Errorf("%s: %v, want %v (%v)", path, fi.Mode().Perm(), mode, err)
|
|
}
|
|
}
|
|
|
|
// A folder of the user's keeps its mode.
|
|
mine := t.TempDir()
|
|
if err := os.Chmod(mine, 0o755); err != nil { //nolint:gosec // the test's own folder
|
|
t.Fatal(err)
|
|
}
|
|
if err := lc.Export(t.Context(), filepath.Join(mine, "h.tar.gz")); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if fi, _ := os.Stat(mine); fi.Mode().Perm() != 0o755 {
|
|
t.Errorf("the user's folder: %v", fi.Mode().Perm())
|
|
}
|
|
|
|
dir := filepath.Join(t.TempDir(), "new")
|
|
file := filepath.Join(dir, "h.tar.gz")
|
|
read := func() string {
|
|
data, _ := os.ReadFile(file) //nolint:gosec // the test's own folder
|
|
return string(data)
|
|
}
|
|
if err := lc.Export(t.Context(), file); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if data := read(); data != "tarball" {
|
|
t.Errorf("export = %q", data)
|
|
}
|
|
tarball = ""
|
|
err := lc.Export(t.Context(), file)
|
|
if le, ok := errors.AsType[*backend.LoggedError](err); !ok || msg.NamesOf(le.Msg) != "history_not_exported vm="+vm {
|
|
t.Errorf("failed: %v", msg.NamesOf(err))
|
|
}
|
|
if data := read(); data != "tarball" {
|
|
t.Errorf("after a failed export = %q", data)
|
|
}
|
|
if es, _ := os.ReadDir(dir); len(es) != 1 {
|
|
t.Errorf("left %v", es)
|
|
}
|
|
if _, ok := f.Instances[vmname.VM(vm)]; !ok {
|
|
t.Error("deleted")
|
|
}
|
|
}
|
|
|
|
// An export starts a stopped VM as a launch does: its .git locked again,
|
|
// network policy's self-check run (it stops a VM that gets out directly),
|
|
// and a VM whose state has both network policy and the docker layer
|
|
// refused.
|
|
func TestExportStartsAsLaunch(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
gitEnv(t)
|
|
gitIn(t, e.Dir, "init", "-q")
|
|
var locks []string
|
|
e.Protector = func(dir string) *gitprotect.Protector {
|
|
return gitprotect.New(dir, e.Dirs.Home, gitprotect.Flags{Chflags: func(_ context.Context, flags, path string) error {
|
|
locks = append(locks, flags+" "+path)
|
|
return nil
|
|
}}, &msg.Fake{})
|
|
}
|
|
direct := false
|
|
f.ExecFunc = func(_ vmname.VM, argv []string, o backend.ExecOpts) error {
|
|
switch {
|
|
case argv[0] == "curl" && !direct:
|
|
return errors.New("curl: (7) Failed to connect")
|
|
case exportScript(argv, o) != "":
|
|
_, err := o.Stdout.Write([]byte("tarball"))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
st := existingVM(t, e, Options{Network: "allowlist"})
|
|
vm := st.Name()
|
|
lc := Lifecycle{e}
|
|
stop := func() {
|
|
t.Helper()
|
|
if err := f.Stop(t.Context(), vm); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Proxy.Stop(vm)
|
|
}
|
|
running := func() bool { return f.Instances[vm].Status == backend.StatusRunning }
|
|
|
|
stop()
|
|
locks = nil
|
|
if err := lc.Export(t.Context(), ""); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if !slices.Contains(locks, "uchg "+filepath.Join(e.Dir, ".git", "config")) {
|
|
t.Errorf("locked %q", locks)
|
|
}
|
|
|
|
// Running, then stopped: either way the self-check stops it.
|
|
direct = true
|
|
if err := lc.Export(t.Context(), ""); msg.NamesOf(err) != "net_direct_route vm="+vm.String() || running() {
|
|
t.Errorf("running, gets out directly: %v, running %v", msg.NamesOf(err), running())
|
|
}
|
|
stop()
|
|
if err := lc.Export(t.Context(), ""); msg.NamesOf(err) != "net_direct_route vm="+vm.String() || running() {
|
|
t.Errorf("gets out directly: %v, running %v", msg.NamesOf(err), running())
|
|
}
|
|
direct = false
|
|
}
|
|
|
|
// adopt gives the folder the VM made for an earlier one at its path,
|
|
// after asking; it's then opened, its .git locked as new.
|
|
func TestAdopt(t *testing.T) {
|
|
e, _, _ := upEnv(t, allNative())
|
|
lc, l := Lifecycle{e}, Launch{e}
|
|
st := existingVM(t, e, Options{Network: "open"})
|
|
if err := lc.Adopt(t.Context(), "", false); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if err := set(t, st, keyFolder, "0:0"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := l.Up(t.Context(), Options{}); msg.NamesOf(err) != "vm_other_folder vm="+st.Name().String()+" was="+e.Dir {
|
|
t.Errorf("Up = %v", msg.NamesOf(err))
|
|
}
|
|
asked := answers(t, e, false)
|
|
if err := lc.Adopt(t.Context(), "", false); err != nil || len(*asked) != 1 {
|
|
t.Fatalf("declined: %v, asked %q", msg.NamesOf(err), *asked)
|
|
}
|
|
answers(t, e, true)
|
|
if err := lc.Adopt(t.Context(), "", false); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if err := l.Up(t.Context(), Options{}); err != nil {
|
|
t.Errorf("Up adopted = %v", msg.NamesOf(err))
|
|
}
|
|
// --force adopts without asking, and without a terminal.
|
|
if err := set(t, st, keyFolder, "0:0"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fake(e).Terminal = false
|
|
if err := lc.Adopt(t.Context(), "", false); msg.NamesOf(err) != "vm_other_folder_adopt vm="+st.Name().String()+" was="+e.Dir {
|
|
t.Errorf("no terminal: %v", msg.NamesOf(err))
|
|
}
|
|
asked = answers(t, e, false)
|
|
fake(e).Terminal = false
|
|
if err := lc.Adopt(t.Context(), "", true); err != nil || len(*asked) != 0 {
|
|
t.Fatalf("--force: %v, asked %q", msg.NamesOf(err), *asked)
|
|
}
|
|
if err := l.Up(t.Context(), Options{}); err != nil {
|
|
t.Errorf("Up adopted with --force = %v", msg.NamesOf(err))
|
|
}
|
|
}
|
|
|
|
// On Linux hosts, set-id files in the folder are named when a VM is made
|
|
// and once it's stopped, but not at a launch into the running VM: the
|
|
// walk is slow. Those in .git aren't.
|
|
func TestSetIDOnStop(t *testing.T) {
|
|
e, _, _ := upEnv(t, allNative())
|
|
e.OS = "linux"
|
|
if err := os.MkdirAll(filepath.Join(e.Dir, ".git"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, p := range []string{"tool", filepath.Join(".git", "hook")} {
|
|
p = filepath.Join(e.Dir, p)
|
|
if err := os.WriteFile(p, nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(p, os.ModeSetuid|0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
want := "setid_files files=" + filepath.Join(e.Dir, "tool")
|
|
var warned msg.Recorder
|
|
fake(e).OnWarn = warned.Add
|
|
existingVM(t, e, Options{Network: "open"})
|
|
if !slices.Equal(warned.Names(), []string{want}) {
|
|
t.Errorf("a new VM: warned %q, want %s", warned.Names(), want)
|
|
}
|
|
warned.Reset()
|
|
if err := (Launch{e}).Up(t.Context(), Options{}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if slices.Contains(warned.Names(), want) {
|
|
t.Errorf("a launch into the running VM named the set-id files")
|
|
}
|
|
warned.Reset()
|
|
if err := (Lifecycle{e}).Stop(t.Context()); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if !slices.Contains(warned.Names(), want) {
|
|
t.Errorf("stop: warned %q, want %s", warned.Names(), want)
|
|
}
|
|
}
|