Files
devvm/testdata/script/link.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

240 lines
9.5 KiB
Plaintext

# devvm link: a folder's VM's links to the ports other VMs publish, kept in
# the VMs' state on the host. A link records its target's name and ID, its
# port and the client's address for that target; nothing carries a
# connection here.
# The group shows once its commands exist.
exec devvm --help
stdout '^Links between VMs:\n group .*\n link '
# No VM yet.
project app
! exec devvm link
stderr '^devvm: no_vm_link dir=\S*/app$'
! exec devvm link publish
stderr '^devvm: no_vm_link dir=\S*/app$'
# Usage errors, before devvm looks for the VM.
! exec devvm link nope
stderr '^devvm: no_subcommand command=devvm link commands=add, publish, rm, unpublish given=nope$'
! exec devvm link add db
stderr '^devvm: arg_missing args=TARGET PORT command=devvm link add$'
stderr '^usage_help command=devvm link add$'
! exec devvm link add db 0
stderr '^devvm: link_port_invalid value="0"$'
! exec devvm link add db 5432-5433
stderr '^devvm: link_port_invalid value="5432-5433"$'
! exec devvm link rm db http
stderr '^devvm: link_port_invalid value="http"$'
! exec devvm link add db 5432 --as Bad_Name
stderr '^devvm: link_name_invalid name="Bad_Name"$'
! exec devvm link add db 5432 --as 123
stderr '^devvm: link_name_invalid name="123"$'
! exec devvm link publish all
stderr '^devvm: link_ports_invalid value="all"$'
! exec devvm link unpublish none
stderr '^devvm: link_ports_invalid value="none"$'
! exec devvm link publish --json 5432
stderr '^devvm: json_shows_only command=devvm link publish$'
! exec devvm link add --json db 5432
stderr '^devvm: flag_unknown command=devvm link add flag=--json$'
! stderr no_vm_link
# The VMs: app, the client; db, a target; two folders called api.
exec devvm --no-network-policy --no-git-protect
project db
exec devvm --no-network-policy --no-git-protect
mkdir $WORK/x/api
cd $WORK/x/api
exec devvm --no-network-policy --no-git-protect
exec devvm link publish 5432
mkdir $WORK/y/api
cd $WORK/y/api
exec devvm --no-network-policy --no-git-protect
exec sh -c 'cat "$VMS"/*/state'
stdout '^id=[0-9a-f]{32}$'
vm_name APP app
vm_name DB db
# publish: none at first, then ports and ranges, as devvm ports takes
# them.
cd $WORK/db
exec devvm link publish
stdout '\Anone\n\z'
exec devvm link publish 5432
stdout '^==> published ports=5432 vm=devvm-db-[0-9a-f]{14}$'
exec devvm link publish 8000-8010,5432
stdout '^==> published ports=5432,8000-8010 vm=devvm-db-[0-9a-f]{14}$'
exec devvm link publish
stdout '\A5432,8000-8010\n\z'
exec devvm link publish --json
stdout '\A{\n "version": 1,\n "ports": "5432,8000-8010"\n}\n\z'
exec cat $DB_dir/state
stdout '^publish=5432,8000-8010$'
# Refusals: an unpublished port, the VM itself, no VM, a folder name two
# VMs have.
cd $WORK/app
exec devvm link
stdout '\ANAME +PORT +TARGET +FOLDER +STATUS +APPLIES\n\z'
exec devvm link --json
stdout '\A{\n "version": 1,\n "links": \[\]\n}\n\z'
! exec devvm link add db 6379
stderr '^devvm: link_not_published port=6379 vm=devvm-db-[0-9a-f]{14}$'
! exec devvm link add app 5432
stderr '^devvm: link_self vm=devvm-app-[0-9a-f]{14}$'
! exec devvm link add . 5432
stderr '^devvm: link_self vm=devvm-app-[0-9a-f]{14}$'
! exec devvm link add nope 5432
stderr '^devvm: link_unknown_target target=nope$'
! exec devvm link add ../nope 5432
stderr '^devvm: link_unknown_target target=../nope$'
! exec devvm link add api 5432
stderr '^devvm: link_ambiguous_target target=api vms=devvm-api-[0-9a-f]{14} in \S*/api, devvm-api-[0-9a-f]{14} in \S*/api$'
exec cat $APP_dir/state
! stdout '^links='
# add: by folder name, its address the first, then the same target's
# other ports by path or VM name, sharing it.
exec devvm link add db 5432
stdout '^==> link_added name=db port=5432 target=devvm-db-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
stderr '^link_trust target=devvm-db-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
exec devvm link add db 5432
stdout '^==> link_exists name=db port=5432 vm=devvm-app-[0-9a-f]{14}$'
exec devvm link add ../db 8000
stdout '^==> link_added name=db port=8000 '
exec devvm link add $DB 8005
stdout '^==> link_added name=db port=8005 '
exec sh -c 'id=$(sed -n "s/^id=//p" "$DB_dir/state"); grep -x "links=db,5432,$DB,$id,127.77.0.1 db,8000,$DB,$id,127.77.0.1 db,8005,$DB,$id,127.77.0.1" "$APP_dir/state"'
# A target has one name in a client, and a name one target; localhost
# and the client's own hostname are refused.
! exec devvm link add db 8001 --as pg
stderr '^devvm: link_target_named name=db target=devvm-db-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
exec devvm link add db 8001 --as db
stdout '^==> link_added name=db port=8001 '
! exec devvm link add ../x/api 5432 --as db
stderr '^devvm: link_name_taken name=db target=devvm-db-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
! exec devvm link add ../x/api 5432 --as app
stderr '^devvm: link_name_host name=app vm=devvm-app-[0-9a-f]{14}$'
! exec devvm link add ../x/api 5432 --as localhost
stderr '^devvm: link_name_host name=localhost vm=devvm-app-[0-9a-f]{14}$'
# Another target gets the next address.
exec devvm link add ../x/api 5432
stdout '^==> link_added name=api port=5432 target=devvm-api-[0-9a-f]{14} '
exec cat $APP_dir/state
stdout '^links=api,5432,devvm-api-[0-9a-f]{14},[0-9a-f]{32},127\.77\.0\.2 db,5432,'
# The list: each target's folder and status, and whether the link
# applies.
exec devvm link
stdout '\ANAME +PORT +TARGET +FOLDER +STATUS +APPLIES\n'
stdout '^api +5432 +devvm-api-[0-9a-f]{14} +\S*/x/api +Running +yes$'
stdout '^db +5432 +devvm-db-[0-9a-f]{14} +\S*/db +Running +yes$'
stdout '^db +8001 +devvm-db-[0-9a-f]{14} +\S*/db +Running +yes$'
exec devvm link --json
stdout '\A{\n "version": 1,\n "links": \[\n {\n "name": "api",\n "port": 5432,\n "target": "devvm-api-[0-9a-f]{14}",\n "target_folder": "\S*/x/api",\n "target_status": "Running",\n "applies": true,\n "reason": ""\n },\n'
! stdout '127\.77'
# Network policy: a client with it links only to targets with it, so it
# can't have it while it links to one without, and a target keeps it while
# a client with it links to it.
! exec devvm network on
stderr '^devvm: network_on_linked targets=devvm-api-[0-9a-f]{14}, devvm-db-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
cd $WORK/db
exec devvm network on
cd $WORK/x/api
exec devvm network on
cd $WORK/y/api
exec devvm link publish 5432
cd $WORK/app
exec devvm network on
! exec devvm link add ../y/api 5432
stderr '^devvm: link_policy_mismatch target=devvm-api-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
# db got network policy while it ran, so it runs outside its proxy until
# it starts again: add warns, refusing nothing, and so does publishing a
# port that a client with network policy links to.
exec devvm link add db 8002
stdout '^==> link_added name=db port=8002 '
stderr '^devvm: link_target_unenforced target=devvm-db-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
exec devvm link add db 8002
stdout '^==> link_exists name=db port=8002 '
stderr '^devvm: link_target_unenforced target=devvm-db-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
cd $WORK/db
exec devvm link unpublish 8000-8010
exec devvm link publish 7000
! stderr link_target_unenforced
exec devvm link publish 8000-8010
stderr -count=1 '^devvm: link_target_unenforced target=devvm-db-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
exec devvm link unpublish 7000
# Without network policy, the client isn't warned.
cd $WORK/app
exec devvm network off
exec devvm link add db 8002
! stderr link_target_unenforced
exec devvm network on
cd $WORK/db
! exec devvm network off
stderr '^devvm: network_off_linked vm=devvm-db-[0-9a-f]{14} vms=devvm-app-[0-9a-f]{14}$'
cd $WORK/app
exec devvm network off
cd $WORK/db
exec devvm network off
cd $WORK/x/api
exec devvm network off
cd $WORK/db
# unpublish: each as listed; a link to a withdrawn port no longer applies.
! exec devvm link unpublish 8001
stderr '^devvm: port_not_published port=8001 ports=5432,8000-8010$'
exec devvm link unpublish 8000-8010
stdout '^==> unpublished ports=8000-8010 vm=devvm-db-[0-9a-f]{14}$'
stderr '^unpublished_linked vm=devvm-db-[0-9a-f]{14} vms=devvm-app-[0-9a-f]{14}$'
exec devvm link publish
stdout '\A5432\n\z'
exec devvm link unpublish 5432
stderr '^unpublished_linked vm=devvm-db-[0-9a-f]{14} vms=devvm-app-[0-9a-f]{14}$'
exec devvm link publish 7000
exec devvm link unpublish 7000
! stderr unpublished_linked
exec devvm link publish
stdout '\Anone\n\z'
exec devvm link publish 5432
cd $WORK/app
exec devvm link
stdout '^db +8000 +devvm-db-[0-9a-f]{14} +\S*/db +Running +unpublished$'
stdout '^db +5432 +devvm-db-[0-9a-f]{14} +\S*/db +Running +yes$'
# rm: by the link's name, its target VM's name or what add takes.
exec devvm link rm db 8000
stdout '^==> link_removed name=db port=8000 vm=devvm-app-[0-9a-f]{14}$'
exec devvm link rm ../db 8001
stdout '^==> link_removed name=db port=8001 '
exec devvm link rm $DB 8002
stdout '^==> link_removed name=db port=8002 '
exec devvm link rm api 5432
stdout '^==> link_removed name=api port=5432 '
! exec devvm link rm db 9999
stderr '^devvm: link_not_found port=9999 target=db vm=devvm-app-[0-9a-f]{14}$'
exec devvm link
! stdout '^api '
! stdout ' 800[012] '
# A target deleted takes every link to it with it, and names the clients
# that lost one; made again in its folder, it's another VM, which the
# client doesn't link to.
cd $WORK/db
exec devvm rm --force --no-export
stdout '^==> links_target_gone count=1 vm=devvm-db-[0-9a-f]{14} vms=devvm-app-[0-9a-f]{14}$'
cd $WORK/app
exec devvm link
stdout '\ANAME +PORT +TARGET +FOLDER +STATUS +APPLIES\n\z'
exec cat $APP_dir/state
stdout '^links=$'
cd $WORK/db
exec devvm --no-network-policy --no-git-protect
exec devvm link publish 5432
cd $WORK/app
exec devvm link
stdout '\ANAME +PORT +TARGET +FOLDER +STATUS +APPLIES\n\z'