mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
When a boot numbered a project folder's disk anew and its volume couldn't be read at that launch (diskutil failing or slow), recordFolder recorded the new device with the old volume, and from then on the identity matched byte for byte, so no launch compared the volume again: a copy of the disk with a volume ID of its own, mounted at the folder's path later, would have kept the VM. It now records the folder unchecked (state.VM.SetFolderUnchecked, vm.json's folder_unchecked), so each launch looks the volume up again; Env.keepsVolume compares it while the record is unchecked, as for a renumbered one, refusing another volume (vm_other_folder), and the first lookup that finds the recorded volume clears the mark (folderRecord). A move matches an unchecked record only on its volume (here.same), and a move whose lookup fails keeps the recorded volume, bound to the inode and birth time a move keeps, unchecked (recordMove). On Linux, where the volume is read in the process (volumeFree), every launch compares it, so a Btrfs snapshot or a ZFS clone mounted at the folder's path is another folder even on the original's device; after a volume's UUID changes, devvm adopt gives the folder its VM again. The finder's sameFolder and madeFor share one body (madeForOpen), and the folder is opened once: platform.SameOpenFolder is SameFolder for a folder StatFolder already read, platform.RecordedAs is its exact match, and keepsVolume, recordFolder and here.same take that read rather than open the folder again (here's id and unique are its stat's). madeFor, which the checks that ask which VM mounts a folder use, falls back to the folder's stat when it can't be opened, as when code in a VM that mounts it took away its read permission, so no VM hides from clone_repo_mounted or project_in_vm_folder that way. recordFolder compares again under the state's lock with the same folderRecord. The structure test's folder-ids rule now finds a map literal keyed by a folder's identity whose type is a named map type of the module's, a generic one included, or elided in a map's or a slice's literal whose elements are maps. Docs: a Btrfs rollback at the folder's path, or a volume's new UUID, needs devvm adopt; a Mac volume that ignores ownership makes every folder yours to the offer's check; the unchecked mark and Linux's check at every launch (internals, overview's vm.json fields). Tests: TestUncheckedOnAnotherVolume, TestKeepsVolume, TestRecordFolder (unchecked, then checked), TestRecordFolderUnderLock, TestRecordMoveVolume, TestMovedFrom (unchecked), TestMountedByUnreadableParent, TestSameFolder (SameOpenFolder, RecordedAs), folder-ids' named, generic and elided map cases, and fixture_v010_renumbered's unchecked steps on 0.1.0's state. Co-Authored-By: Claude Opus 5.5 <[email protected]>
489 lines
19 KiB
Go
489 lines
19 KiB
Go
package vm
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/gitprotect"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/platform"
|
|
"github.com/devvmhq/devvm/internal/project"
|
|
"github.com/devvmhq/devvm/internal/state"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// A move records the path the agents' state is under: the first move's
|
|
// old path, kept through more moves until a setup follows it, and none
|
|
// once the folder is back there.
|
|
func TestMovedTo(t *testing.T) {
|
|
for _, c := range []struct {
|
|
last movedInVM
|
|
was, dir string
|
|
want movedInVM
|
|
}{
|
|
{movedInVM{}, "/a", "/b", movedInVM{From: "/a"}},
|
|
{movedInVM{From: "/a"}, "/b", "/c", movedInVM{From: "/a"}},
|
|
{movedInVM{From: "/a"}, "/b", "/a", movedInVM{}},
|
|
{movedInVM{}, "/a", "/a", movedInVM{}},
|
|
} {
|
|
if got := movedTo(c.last, c.was, c.dir); got != c.want {
|
|
t.Errorf("movedTo(%+v, %s, %s) = %+v, want %+v", c.last, c.was, c.dir, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A VM's folder moved to h when it's h's folder itself (its identity, the
|
|
// device in it included), unique and this user's, at another path, with
|
|
// nothing left at the old one; its creation finished and it mounts its
|
|
// folder. A copy, a folder made again, an inode number used again and
|
|
// another user's folder never match.
|
|
func TestMovedFrom(t *testing.T) {
|
|
e, _, _ := upEnv(t, allNative())
|
|
made := existingVM(t, e, Options{Network: "open"})
|
|
clone, err := state.ReadVM(e.Dirs, made.Name()) // its own sections
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := setField(clone, keyClone, "clone"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
unfinished := *made
|
|
unfinished.Created = time.Time{}
|
|
h := here{dir: "/new/web", mine: true, stat: platform.FolderStat{ID: made.Folder(), Unique: true}}
|
|
gone := func(*state.VM, string) bool { return true }
|
|
there := func(*state.VM, string) bool { return false }
|
|
other := func(st *state.VM, id string) *state.VM { c := *st; c.SetFolder(id, ""); return &c }
|
|
at := func(st *state.VM, dir string) *state.VM { c := *st; c.Dir = dir; return &c }
|
|
dev, _, _ := strings.Cut(made.Folder(), ":")
|
|
for _, tc := range []struct {
|
|
name string
|
|
h here
|
|
st *state.VM
|
|
away func(*state.VM, string) bool
|
|
want bool
|
|
}{
|
|
{"moved: the same folder, the old path gone", h, made, gone, true},
|
|
{"the old folder still there", h, made, there, false},
|
|
{"a copy: another inode", h, other(made, dev+":999999999"), gone, false},
|
|
{"the same inode on another device", h, other(made, "1"+made.Folder()), gone, false},
|
|
{"an identity that may be a deleted folder's", here{dir: h.dir, mine: true, stat: platform.FolderStat{ID: h.stat.ID}}, made, gone, false},
|
|
{"another user's folder", here{dir: h.dir, stat: h.stat}, made, gone, false},
|
|
{"no identity", here{dir: h.dir, mine: true, stat: platform.FolderStat{Unique: true}}, other(made, ""), gone, false},
|
|
{"already this folder's", h, at(made, h.dir), gone, false},
|
|
{"no folder recorded", h, at(made, ""), gone, false},
|
|
{"a --clone VM", h, clone, gone, false},
|
|
{"its creation didn't finish", h, &unfinished, gone, false},
|
|
} {
|
|
if got := tc.h.movedFrom(tc.st, tc.away); got != tc.want {
|
|
t.Errorf("%s: movedFrom = %v, want %v", tc.name, got, tc.want)
|
|
}
|
|
}
|
|
|
|
// Moved, and its disk numbered anew since: recorded on device 1<dev>, on volume A, found on
|
|
// dev, on volume A, which is looked up only once the rest matches.
|
|
_, rest, _ := strings.Cut(made.Folder(), ":")
|
|
renumbered := func(vol string) *state.VM { c := *made; c.SetFolder("1"+made.Folder(), vol); return &c }
|
|
unchecked := func(vol string) *state.VM { c := *made; c.SetFolderUnchecked(made.Folder(), vol); return &c }
|
|
asked := 0
|
|
on := func(vol string, ro bool) here {
|
|
v := h
|
|
v.stat = platform.FolderStat{ID: made.Folder(), Unique: true, ReadOnly: ro}
|
|
v.volume = func() string { asked++; return vol }
|
|
return v
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
h here
|
|
st *state.VM
|
|
want bool
|
|
asked int
|
|
}{
|
|
{"moved and renumbered: its volume as recorded", on("uuid:A", false), renumbered("uuid:A:" + rest), true, 1},
|
|
{"renumbered onto another volume", on("uuid:B", false), renumbered("uuid:A:" + rest), false, 1},
|
|
{"renumbered, on a read-only volume or a snapshot", on("uuid:A", true), renumbered("uuid:A:" + rest), false, 0},
|
|
{"a volume bound to another folder, as an older devvm's move leaves it", on("uuid:A", false), renumbered("uuid:A:1:" + rest), false, 0},
|
|
{"renumbered, with no volume recorded", on("uuid:A", false), renumbered(""), false, 0},
|
|
// Recorded unchecked: the identity as recorded matches only on the volume recorded.
|
|
{"unchecked, on its volume", on("uuid:A", false), unchecked("uuid:A:" + rest), true, 1},
|
|
{"unchecked, on another volume", on("uuid:B", false), unchecked("uuid:A:" + rest), false, 1},
|
|
} {
|
|
asked = 0
|
|
if got := tc.h.movedFrom(tc.st, gone); got != tc.want || asked != tc.asked {
|
|
t.Errorf("%s: movedFrom = %v, asked %d, want %v, %d", tc.name, got, asked, tc.want, tc.asked)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A move whose volume lookup failed keeps the volume recorded, bound to the
|
|
// folder's inode and birth time, which a move keeps, unchecked, so a later
|
|
// launch checks it; one that read the volume records it, checked; one to
|
|
// another folder, whose inode differs, records none.
|
|
func TestRecordMoveVolume(t *testing.T) {
|
|
e, _, _ := upEnv(t, allNative())
|
|
st := existingVM(t, e, Options{Network: "open"})
|
|
id := st.Folder()
|
|
bound := platform.VolumeOf("uuid:A", id)
|
|
other := "1:999:1.0"
|
|
for _, tc := range []struct {
|
|
name, id, vol, wantVol string
|
|
unchecked, wantUnchecked bool
|
|
}{
|
|
{"unchecked, its volume unread", id, "", bound, true, true},
|
|
{"checked, its volume unread", id, "", bound, false, true},
|
|
{"its volume read", id, platform.VolumeOf("uuid:A", id), bound, true, false},
|
|
{"another folder, its volume unread", other, "", "", false, false},
|
|
} {
|
|
if err := st.Update(t.Context(), func(st *state.VM) error {
|
|
if tc.unchecked {
|
|
st.SetFolderUnchecked(id, bound)
|
|
} else {
|
|
st.SetFolder(id, bound)
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cur, err := state.ReadVM(e.Dirs, st.Name())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := recordMove(t.Context(), cur, cur.Dir, tc.id, tc.vol, false); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
got, err := state.ReadVM(e.Dirs, st.Name())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.FolderVolume() != tc.wantVol || got.FolderUnchecked() != tc.wantUnchecked {
|
|
t.Errorf("%s: volume %q, unchecked %v, want %q, %v", tc.name, got.FolderVolume(), got.FolderUnchecked(), tc.wantVol, tc.wantUnchecked)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A folder moved away when nothing is at its old path on its disk, or a
|
|
// symlink there leads to where it is now; never when a folder (made
|
|
// again, or the same) is there, or a symlink leads elsewhere.
|
|
func TestMovedAway(t *testing.T) {
|
|
root, err := filepath.EvalSymlinks(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
to, elsewhere := filepath.Join(root, "new"), filepath.Join(root, "other")
|
|
for _, d := range []string{to, elsewhere} {
|
|
if err := os.Mkdir(d, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
id, err := platform.FolderID(to)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link := func(name, target string) string {
|
|
p := filepath.Join(root, name)
|
|
if err := os.Symlink(target, p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|
|
for _, tc := range []struct {
|
|
name, dir string
|
|
want bool
|
|
}{
|
|
{"nothing at the old path", filepath.Join(root, "old"), true},
|
|
{"a folder at the old path", elsewhere, false},
|
|
{"a symlink to where it is now", link("tonew", to), true},
|
|
{"a symlink elsewhere", link("toother", elsewhere), false},
|
|
{"a symlink to nothing", link("dangling", filepath.Join(root, "none")), false},
|
|
} {
|
|
st := vmAt(tc.dir, id, "")
|
|
if got := awayAs(st, to, func(st *state.VM) FolderState { return (&Env{}).FolderOf(t.Context(), st) }); got != tc.want {
|
|
t.Errorf("%s: awayAs = %v, want %v", tc.name, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// moveFolder renames the folder e runs in to name, beside it, and runs
|
|
// there; it returns the old path and the new.
|
|
func moveFolder(t *testing.T, e *Env, name string) (was, now string) {
|
|
t.Helper()
|
|
was, now = e.Dir, filepath.Join(filepath.Dir(e.Dir), name)
|
|
if err := os.Rename(was, now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Dir = now
|
|
return was, now
|
|
}
|
|
|
|
// A folder moved or renamed keeps its VM, only after a yes in a terminal:
|
|
// without one devvm refuses, naming devvm adopt; a no leaves the VM where
|
|
// it was and the folder gets a new one; a yes records the folder in the
|
|
// VM's state, from which the finder finds it, and its next start mounts
|
|
// the folder where it is now, a running VM restarted for it after
|
|
// asking. A new folder at the old path gets a VM of its own, by its next
|
|
// name.
|
|
func TestMoveHere(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
st := existingVM(t, e, Options{Network: "open"})
|
|
vm := st.Name()
|
|
was, now := moveFolder(t, e, "site")
|
|
fd := finder{e}
|
|
if _, err := fd.target(t.Context(), need{}); msg.NamesOf(err) != "vm_moved_here vm="+vm.String()+" was="+was {
|
|
t.Fatalf("no terminal: %v", msg.NamesOf(err))
|
|
}
|
|
asked := answers(t, e, false)
|
|
tg, err := fd.target(t.Context(), need{})
|
|
if err != nil || tg.exists || tg.f.Name == vm || !slices.Equal(*asked, []string{"move_vm vm=" + vm.String() + " was=" + was}) {
|
|
t.Fatalf("a no: %+v, %v, asked %q", tg, msg.NamesOf(err), *asked)
|
|
}
|
|
if got, _ := state.ReadVM(e.Dirs, vm); got.Dir != was {
|
|
t.Errorf("a no moved it: %s", got.Dir)
|
|
}
|
|
var infos []string
|
|
fake(e).OnInfo = func(m msg.M) { infos = append(infos, m.Names()) }
|
|
answers(t, e, true)
|
|
if tg, err = fd.target(t.Context(), need{}); err != nil || tg.f.Name != vm || !tg.moved || tg.f.Dir != now {
|
|
t.Fatalf("a yes: %+v, %v", tg, msg.NamesOf(err))
|
|
}
|
|
if !slices.Contains(infos, "vm_adopted dir="+now+" vm="+vm.String()) {
|
|
t.Errorf("said %q", infos)
|
|
}
|
|
moved, err := state.ReadVM(e.Dirs, vm)
|
|
if id, _ := platform.FolderID(now); err != nil || moved.Dir != now || moved.Folder() != id {
|
|
t.Fatalf("its state: %+v, %v", moved, err)
|
|
}
|
|
if m := sectionOf[movedInVM](moved, movedSection); m.From != was {
|
|
t.Errorf("recorded it moved from %q, want %s", m.From, was)
|
|
}
|
|
// Found by the folder its state records, asking nothing.
|
|
asked = answers(t, e, false)
|
|
if tg, err = fd.target(t.Context(), need{has: hasMade}); err != nil || tg.f.Name != vm || tg.moved || len(*asked) != 0 {
|
|
t.Fatalf("found again: %+v, %v, asked %q", tg, msg.NamesOf(err), *asked)
|
|
}
|
|
// It ran, mounting the old path: the move stopped it, and its next
|
|
// start mounts the new one.
|
|
if in := f.Instances[vm]; in.Status != backend.StatusStopped || !slices.Contains(infos, "stopped vm="+vm.String()) {
|
|
t.Errorf("moved while running: %s, said %q", in.Status, infos)
|
|
}
|
|
if err := (Launch{e}).Up(t.Context(), Options{}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if w := call(f, "Start").Args[1].(backend.Want); w.Project != now {
|
|
t.Errorf("started with %q mounted, want %s", w.Project, now)
|
|
}
|
|
// Its setup moved the agents' state from the old path, and the
|
|
// record is gone.
|
|
if got := staged(t, f)["moved"]; got == "" || got != string(claudeMoved(was, now)) {
|
|
t.Errorf("staged moved = %q", got)
|
|
}
|
|
if st, _ := state.ReadVM(e.Dirs, vm); sectionOf[movedInVM](st, movedSection) != (movedInVM{}) {
|
|
t.Errorf("still recorded: %+v", sectionOf[movedInVM](st, movedSection))
|
|
}
|
|
// Running with the old path mounted all the same: up restarts it for
|
|
// the new one, after asking, yes by default.
|
|
in := f.Instances[vm]
|
|
in.Project = was
|
|
f.Instances[vm] = in
|
|
asked = answers(t, e, true)
|
|
if err := (Launch{e}).Up(t.Context(), Options{}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if !slices.Contains(*asked, "restart_now count=1 vm="+vm.String()) || f.Instances[vm].Project != now {
|
|
t.Errorf("asked %q, mounts %q", *asked, f.Instances[vm].Project)
|
|
}
|
|
if got, ok := staged(t, f)["moved"]; ok {
|
|
t.Errorf("moved again: %q", got)
|
|
}
|
|
// A new folder at the old path: a VM of its own, by its next name.
|
|
if err := os.Mkdir(was, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Dir = was
|
|
if tg, err = fd.target(t.Context(), need{}); err != nil || tg.exists || tg.st.Exists() {
|
|
t.Fatalf("the old path: %+v, %v", tg, msg.NamesOf(err))
|
|
}
|
|
second, _ := project.Nth(was, f.NameMax(), 1)
|
|
if tg.f.Name != second.Name {
|
|
t.Errorf("the old path's new VM is %s, want %s", tg.f.Name, second.Name)
|
|
}
|
|
if err := (Launch{e}).Up(t.Context(), Options{Network: "open"}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
for dir, want := range map[string]vmname.VM{was: second.Name, now: vm} {
|
|
e.Dir = dir
|
|
if tg, err := fd.target(t.Context(), need{has: hasMade}); err != nil || tg.f.Name != want {
|
|
t.Errorf("%s's VM: %+v, %v, want %s", dir, tg, msg.NamesOf(err), want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A folder replaced at the new path while devvm asked isn't the one asked
|
|
// about: nothing moves.
|
|
func TestMoveFolderChanged(t *testing.T) {
|
|
e, _, _ := upEnv(t, allNative())
|
|
vm := existingVM(t, e, Options{Network: "open"}).Name()
|
|
was, now := moveFolder(t, e, "site")
|
|
answers(t, e, true)
|
|
fake(e).OnAsk = func(msg.M) bool {
|
|
if err := os.Rename(now, now+".x"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return os.Mkdir(now, 0o700) == nil
|
|
}
|
|
if _, err := (finder{e}).target(t.Context(), need{}); msg.NamesOf(err) != "move_folder_changed dir="+now+" vm="+vm.String() {
|
|
t.Errorf("replaced while asked: %v", msg.NamesOf(err))
|
|
}
|
|
if st, _ := state.ReadVM(e.Dirs, vm); st.Dir != was {
|
|
t.Errorf("moved to %s", st.Dir)
|
|
}
|
|
}
|
|
|
|
// A folder moved where devvm makes no new VM, inside devvm's own config
|
|
// folder, gets no VM moved to it, even with devvm adopt --force.
|
|
func TestMoveUnsafe(t *testing.T) {
|
|
e, _, _ := upEnv(t, allNative())
|
|
st := existingVM(t, e, Options{Network: "open"})
|
|
was := e.Dir
|
|
now := filepath.Join(e.Dirs.Config, "site")
|
|
if err := os.MkdirAll(e.Dirs.Config, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Rename(was, now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Dir = now
|
|
if err := (Lifecycle{e}).Adopt(t.Context(), "", true); msg.IDOf(err) != "unsafe_folder_inside" {
|
|
t.Errorf("adopt --force inside the config folder: %v", msg.NamesOf(err))
|
|
}
|
|
if got, _ := state.ReadVM(e.Dirs, st.Name()); got.Dir != was {
|
|
t.Errorf("moved to %s", got.Dir)
|
|
}
|
|
}
|
|
|
|
// devvm adopt --force moves a VM whose folder moved here without asking,
|
|
// a terminal or not; adopt alone asks, as any command does.
|
|
func TestAdoptMoved(t *testing.T) {
|
|
e, _, _ := upEnv(t, allNative())
|
|
vm := existingVM(t, e, Options{Network: "open"}).Name()
|
|
_, now := moveFolder(t, e, "site")
|
|
lc := Lifecycle{e}
|
|
if err := lc.Adopt(t.Context(), "", false); msg.IDOf(err) != "vm_moved_here" {
|
|
t.Errorf("adopt without a terminal: %v", msg.NamesOf(err))
|
|
}
|
|
if err := lc.Adopt(t.Context(), "", true); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if st, _ := state.ReadVM(e.Dirs, vm); st.Dir != now {
|
|
t.Errorf("adopt --force: %s's folder is %s", vm, st.Dir)
|
|
}
|
|
}
|
|
|
|
// Two VMs made for one folder at paths it had before: devvm moves
|
|
// neither, and warns.
|
|
func TestMoveHereSeveral(t *testing.T) {
|
|
e, _, _ := upEnv(t, allNative())
|
|
first := existingVM(t, e, Options{Network: "open"})
|
|
// The first one declined at the folder's second path, which got a
|
|
// VM of its own; then the folder moved again.
|
|
moveFolder(t, e, "two")
|
|
answers(t, e, false)
|
|
if err := (Launch{e}).Up(t.Context(), Options{Network: "open"}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
moveFolder(t, e, "three")
|
|
var warned []string
|
|
fake(e).OnWarn = func(m msg.M) { warned = append(warned, msg.IDOf(m)) }
|
|
tg, err := (finder{e}).target(t.Context(), need{})
|
|
if err != nil || tg.exists || !slices.Contains(warned, "vms_moved_here") {
|
|
t.Errorf("several: %+v, %v, warned %q", tg, msg.NamesOf(err), warned)
|
|
}
|
|
if st, _ := state.ReadVM(e.Dirs, first.Name()); st.Dir == e.Dir {
|
|
t.Error("moved one of several")
|
|
}
|
|
}
|
|
|
|
// devvm adopt VM moves a VM whose folder is gone to this folder, a copy
|
|
// of it (another folder), after asking, its .git protection started
|
|
// again; it refuses a VM whose folder is there, a --clone VM, and a
|
|
// folder with a VM of its own.
|
|
func TestAdoptVM(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
st := existingVM(t, e, Options{Network: "open"})
|
|
vm, web := st.Name(), e.Dir
|
|
change(t, st, func(st *state.VM) error {
|
|
g, err := gitprotect.Of(st)
|
|
g.Made = gitprotect.Made{Protected: true, GitID: "1:2"}
|
|
return errors.Join(err, gitprotect.Set(st, g))
|
|
})
|
|
cp := filepath.Join(filepath.Dir(web), "copy")
|
|
if err := os.Mkdir(cp, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Dir = cp
|
|
lc := Lifecycle{e}
|
|
if err := lc.Adopt(t.Context(), vm.String(), true); msg.NamesOf(err) != "adopt_folder_there dir="+web+" vm="+vm.String() {
|
|
t.Errorf("its folder there: %v", msg.NamesOf(err))
|
|
}
|
|
if err := os.RemoveAll(web); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := lc.Adopt(t.Context(), vm.String(), false); msg.NamesOf(err) != "adopt_move_refused vm="+vm.String() {
|
|
t.Errorf("no terminal: %v", msg.NamesOf(err))
|
|
}
|
|
asked := answers(t, e, false)
|
|
if err := lc.Adopt(t.Context(), vm.String(), false); err != nil || !slices.Equal(*asked, []string{"adopt_move_vm vm=" + vm.String() + " was=" + web}) {
|
|
t.Errorf("a no: %v, asked %q", msg.NamesOf(err), *asked)
|
|
}
|
|
if got, _ := state.ReadVM(e.Dirs, vm); got.Dir != web {
|
|
t.Errorf("a no moved it to %s", got.Dir)
|
|
}
|
|
answers(t, e, true)
|
|
if err := lc.Adopt(t.Context(), vm.String(), false); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
got, _ := state.ReadVM(e.Dirs, vm)
|
|
g, _ := gitprotect.Of(got)
|
|
if id, _ := platform.FolderID(cp); got.Dir != cp || got.Folder() != id || g.Made.Protected || g.Made.GitID != "" {
|
|
t.Errorf("adopted: %s %s, .git %+v", got.Dir, got.Folder(), g.Made)
|
|
}
|
|
// Here already: nothing to do.
|
|
if err := lc.Adopt(t.Context(), vm.String(), true); err != nil {
|
|
t.Errorf("adopt of this folder's VM: %v", msg.NamesOf(err))
|
|
}
|
|
// Another VM to a folder with a VM of its own; a --clone VM.
|
|
other := filepath.Join(filepath.Dir(web), "other")
|
|
if err := os.Mkdir(other, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Dir = other
|
|
if err := (Launch{e}).Up(t.Context(), Options{Network: "open"}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
ovm, _ := (finder{e}).folderVM(other)
|
|
e.Dir = cp
|
|
if err := lc.Adopt(t.Context(), ovm.String(), true); msg.NamesOf(err) != "adopt_folder_there dir="+other+" vm="+ovm.String() {
|
|
t.Errorf("another's: %v", msg.NamesOf(err))
|
|
}
|
|
if err := os.RemoveAll(other); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := lc.Adopt(t.Context(), ovm.String(), true); msg.NamesOf(err) != "adopt_has_vm vm="+vm.String() {
|
|
t.Errorf("a folder with its own: %v", msg.NamesOf(err))
|
|
}
|
|
ost, _ := state.ReadVM(e.Dirs, ovm)
|
|
if err := set(t, ost, keyClone, "clone"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := lc.Adopt(t.Context(), ovm.String(), true); msg.NamesOf(err) != "adopt_clone dir="+other+" vm="+ovm.String() {
|
|
t.Errorf("a --clone VM: %v", msg.NamesOf(err))
|
|
}
|
|
if _, ok := f.Instances[ovm]; !ok {
|
|
t.Error("refused VM deleted")
|
|
}
|
|
}
|