Files
devvm/testdata/script/clone.txtar
T
w3cjandClaude Opus 5.5 d8a7d4de7f refactor(state): a folder per VM, keyed by its project folder's hash
Every file devvm keeps for a project VM is in one folder, vms/<hash>/,
where <hash> is the 14-hex hash of the project folder's path that the VM's
name ends with: its state, its own rules (policy), its backend's files
(backend/: record.json, key, known_hosts, proxy, ports and the port
forwarder's ports.*), what its runs leave (run/: proxy.*, sandbox.sb,
started, lifeline.pid, secrets.*, links.*) and its logs (logs/:
network.jsonl, proxy.log, links.log). Build VMs keep the same layout in
run/builds/<name>/. The top-level run/ holds only what isn't one VM's or
must be short: the hashed control sockets, links' link-<hash>.sock/.ctl,
the QEMU wrapper and the ports lock.

A folder's VM is found by its path's hash (state.ReadHash), with the name
and backend its state records, so devvm --backend docker then devvm in
the same folder opens the one VM, and a backend's name limit applies to
new VMs only. The state records the VM's name and its ssh alias, chosen
once at creation (sshconfig.ChooseAlias) and never recomputed. Links'
targets match recorded names, aliases and folders (project.Targets).

One teardown (vmFolders.teardown) replaces Lifecycle.forget,
undoCreate's own list, Leftovers.Forget's, Proxy.Forget and space's
leftoverFiles: it stops the proxy and link session, then the pieces
outside the folder (clone remote or .git locks, other VMs' links and the
group, host keys, the backend's leftovers), removes the folder, and
writes the ssh config. Each backend's Delete and Forget remove the VM's
backend/ folder (vmssh.Forwards.Forget). A new VM made where an earlier
one's folder was left clears it first. devvm space counts an orphan's
whole folder as leftover state and the other VMs' logs as network logs.

Path builders move into paths: Dirs.VMDir, Dirs.VM and paths.VM's files
(secrets' BindingsFile/PolicyFile, netpolicy's run, EventLog, ProxyLog,
LinkList, VMPolicyFile, Lima's profileFile, startedFile, forwards.file,
vmssh.Forwards.file, links' LinksLog/linksPID, Docker's vmFile and
lifeline), ToolLockConfig, ToolLockChecked and ToolsCheckLog; the
user's cache folder for Lima's and smolvm's caches is Dirs.UserCache,
from HOME, not os.UserCacheDir. Dirs.VMState, LimaVMs, SmolvmVM and
DockerVM go. vmname.HashOf and IsHash, project.Hash.

The proxy takes the links' sockets' folder (-link-sockets, devvm's run
folder) apart from the links' list, which is in the VM's run folder.

Tests: a VM made and removed (rm, gc --orphans) leaves nothing under
vms/, and with network policy has no file named for it outside its
folder (TestTeardownLeavesNothing); a new VM over an old folder starts
clean; lookup finds a VM made on another backend with a long folder name
and makes no second one (TestLookupOtherBackend, and in docker.txtar);
the alias stays put when a second VM shares the folder name
(TestChooseAlias, ssh.txtar); every socket fits under a 35-character
home with the longest VM name (TestSocketsFit); space counts an
orphan's whole folder (TestLeftoverFolders); paths.VM's files; state by
hash and name; the proxy refuses a socket in the list's own folder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 08:16:39 -07:00

175 lines
7.1 KiB
Plaintext

# --clone: the VM gets its own clone of the repo instead of the folder
# mounted. The host's branches are pushed into it over devvm's ssh config,
# the repo gets a devvm remote, and nothing of .git is locked. rm checks
# the clone for commits the repo hasn't fetched and uncommitted changes,
# in the clone and its worktrees, keeps fetched work as local branches and removes the remote. (The stubs
# keep the VM's files in
# $STUB_DIR/vmfs.)
env GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t GIT_COMMITTER_EMAIL=t@t
mkdir $WORK/web/sub
cd $WORK/web
# Not a repo, not its top folder, no commits, HEAD detached: refused
# before anything is built.
! exec devvm --clone --no-network-policy
stderr '^devvm: clone_not_top dir=/\S*/web$'
exec git init -q -b main
cd sub
! exec devvm --clone --no-network-policy
stderr '^devvm: clone_not_top dir=/\S*/web/sub$'
cd ..
! exec devvm --clone --no-network-policy
stderr '^devvm: clone_no_commits$'
exec git commit -q --allow-empty -m one
exec git checkout -q --detach
! exec devvm --clone --no-network-policy
stderr '^devvm: clone_detached$'
exec git checkout -q main
exec git branch other
stub_calls
! stdout 'limactl (create|clone)'
! exists $VMS
# A new VM: nothing mounted but the shared tools; its clone made at the
# folder's path, every branch pushed into it through devvm's ssh config,
# main checked out; the remote added. Not a terminal: the Include line
# isn't added, and devvm says how to fetch until it is. Uncommitted
# changes aren't copied, and devvm says so.
exec sh -c 'echo x > tracked && git add tracked && git commit -qm two && echo y > tracked'
stub_reset
exec devvm --clone --no-network-policy
stdout '^==> copying_branches$'
stdout '^==> clone_ready branch=main remote=devvm$'
stdout '^ssh_include_needed line=Include /\S*/data/devvm/ssh_config$'
stderr '^devvm: ssh_include_yourself$'
stderr '^devvm: fetch_until file=/\S*/data/devvm/ssh_config remote=devvm$'
stderr '^devvm: uncommitted_not_copied branch=main dir=/\S*/web remote=devvm$'
! stderr 'git_protect|ssh_remote_hint'
stub_calls
stdout '^limactl clone --tty=false devvm-img-[0-9a-f]{12} devvm-web-[0-9a-f]{14} --mount-none --set \.mounts = \[\{"location":"[^"]*/data/devvm/tools","mountPoint":"/mnt/devvm-tools","writable":false\}\] '
stdout '^ssh -F /\S*/data/devvm/ssh_config -o BatchMode=yes -o StrictHostKeyChecking=yes -o UserKnownHostsFile=/\S*/data/devvm/known_hosts -o GlobalKnownHostsFile=/dev/null -o HostKeyAlias=devvm-web-[0-9a-f]{14} -o ForwardAgent=no -o ClearAllForwardings=yes devvm-web-[0-9a-f]{14} git-receive-pack ''/\S*/web''$'
stdout '^limactl shell --workdir /\S*/web devvm-web-[0-9a-f]{14}\n\z'
! stdout chflags
vm_name WEB web
catg $WEB_dir/state
stdout '^mode=clone$'
stdout '^clone_remote=devvm$'
stdout '^seeded=yes$'
! stdout git_protect
exec git remote get-url devvm
stdout '^devvm-web-[0-9a-f]{14}:/\S*/web$'
exec sh -c 'c="$STUB_DIR/vmfs$(pwd -P)"; git -C "$c" branch --show-current && git -C "$c" branch --format="%(refname:short)" && cat "$c/tracked"'
cmp stdout $WORK/vm-branches
! exists $HOME/.ssh/config
# Opened again: not copied again. --clone on it is fine.
stub_reset
exec devvm --clone
! stdout 'copying_branches|clone_ready'
stub_calls
! stdout '^ssh '
# rm: nothing unfetched, nothing uncommitted (the host's own uncommitted
# change was never copied): deleted, and the remote removed.
exec git checkout -q -- tracked
exec devvm rm --force
stdout '^==> deleted vm=devvm-web-[0-9a-f]{14}$'
stdout '^==> removed_remote remote=devvm$'
! exec git remote get-url devvm
# devvm is another repo's: the next free name is devvm-<folder>. The VM's
# own commit isn't fetched: without a terminal rm refuses and keeps it.
rm $STUB_DIR/vmfs
exec git remote add devvm https://example.com/other.git
exec devvm --clone --no-network-policy
stdout '^==> clone_ready branch=main remote=devvm-web$'
exec git remote get-url devvm
stdout '^https://example.com/other.git$'
exec sh -c 'git -C "$STUB_DIR/vmfs$(pwd -P)" commit -q --allow-empty -m from-vm'
! exec devvm rm --force
stdout '^unfetched_commits refs="main"$'
stderr '^devvm: no_terminal_fetch remote=devvm-web$'
vm_name WEB web
exec git remote get-url devvm-web
# Fetched: rm then finds uncommitted changes in the clone, shows them,
# and without a terminal keeps the VM.
exec git fetch -q devvm-web
exec git log --format=%s -1 devvm-web/main
stdout '^from-vm$'
exec sh -c 'echo z > "$STUB_DIR/vmfs$(pwd -P)/new"'
! exec devvm rm --force
! stdout unfetched_commits
stdout '^clone_dirty$'
stdout '^\?\? new$'
stderr '^devvm: no_terminal_delete$'
exec git remote get-url devvm-web
# A clean clone with a worktree in it that has uncommitted changes: rm
# shows them under the worktree's path, and without a terminal keeps the
# VM.
exec sh -c 'c="$STUB_DIR/vmfs$(pwd -P)"; rm "$c/new" && git -C "$c" worktree add -q .worktrees/a && echo .worktrees/ >> "$c/.git/info/exclude" && echo w > "$c/.worktrees/a/w"'
! exec devvm rm --force
! stdout 'unfetched_commits|clone_dirty$'
stdout '^clone_worktree_dirty path=/\S*/web/\.worktrees/a$'
stdout '^\?\? w$'
stderr '^devvm: no_terminal_delete$'
# --force never skips the checks: with the worktree's change still there,
# rm keeps the VM. A check that can't read the clone keeps it too, naming
# --no-clone-check, which deletes it unchecked after a warning, the
# worktree's change with it; a remote the user pointed elsewhere stays.
! exec devvm rm --force --no-export
stderr '^devvm: no_terminal_delete$'
exec sh -c 'mv "$STUB_DIR/vmfs" "$STUB_DIR/vmfs.away"'
! exec devvm rm --force
stderr '^devvm: clone_check_failed vm=devvm-web-[0-9a-f]{14}$'
exec sh -c 'mv "$STUB_DIR/vmfs.away" "$STUB_DIR/vmfs"'
exec git remote set-url devvm-web https://example.com/mine.git
exec devvm rm --force --no-clone-check
stderr '^devvm: clone_check_skipped vm=devvm-web-[0-9a-f]{14}$'
! stdout 'unfetched_commits|clone_dirty|clone_worktree_dirty'
stdout '^==> deleted vm=devvm-web-[0-9a-f]{14}$'
! stdout removed_remote
exec git remote get-url devvm-web
stdout '^https://example.com/mine.git$'
exec git remote remove devvm-web
exec git remote remove devvm
# Copying fails (ssh can't reach it): the VM stays unseeded, and the next
# launch copies again.
rm $STUB_DIR/vmfs
exec sh -c 'touch "$STUB_DIR/ssh-fails"'
! exec devvm --clone --no-network-policy
stderr '^devvm: clone_seed_failed vm=devvm-web-[0-9a-f]{14}$'
! exec git remote get-url devvm
rm $STUB_DIR/ssh-fails
exec devvm
stdout '^==> clone_ready branch=main remote=devvm$'
# rm --force: the VM's work fetched only into devvm/* is kept as new local
# branches under saved/devvm, then the remote goes with its
# remote-tracking branches.
exec sh -c 'git -C "$STUB_DIR/vmfs$(pwd -P)" commit -q --allow-empty -m from-vm-2'
exec git fetch -q devvm
exec devvm rm --force --no-export
stdout '^==> saved_branches branches="saved/devvm/main"$'
stdout '^==> removed_remote remote=devvm$'
exec git log --format=%s -1 saved/devvm/main
stdout '^from-vm-2$'
exec git for-each-ref refs/remotes
! stdout .
exec git branch -D saved/devvm/main
# A VM that mounts the folder: --clone on it is refused.
exec devvm --no-network-policy --no-git-protect
! exec devvm --clone
stderr '^devvm: clone_needs_new_vm vm=devvm-web-[0-9a-f]{14}$'
exec devvm rm --force --no-export
-- vm-branches --
main
main
other
x