Files
devvm/testdata/script/script_cmds.txtar
T
w3cjandClaude Opus 5.5 9f6f62d28a refactor(state): vm.json, typed state in package sections, changed under the VM's lock
A VM's state is vms/<hash>/vm.json: core fields (version, id, name,
alias, image, chain, from, dir, folder, backend, backend_at, created)
and one section per package, each a json.RawMessage its owner decodes
into its own type (state.Section, state.SetSection), what was asked for
at its top level and what devvm made under "made":
net (netpolicy), git (gitprotect), ports (the new internal/ports),
agent (sshagent), tools (tools), secrets (secrets), and size, clone,
devcontainer, desktop and links (vm, internal/vm/state.go).

- state.VM.Update takes vms/<hash>/lock (platform.LockFile), reads the
  file again, applies a change and writes it once; a failed change, or
  write, writes nothing. VM.Set and its calls are gone; read-modify-write
  of lists (links, publish, secrets, tools, size, ports) happens inside
  Update. VM.Claim also holds run/claims.lock, after the VM's own;
  VM.Create writes a new VM's whole record once, its alias chosen
  (sshconfig.ChooseAlias) under the claims lock. Reads take no lock.
- created replaces "net unset" as unfinished (VM.Finished).
- internal/ports: the ports section (forward, host.ssh/proxy/desktop),
  ports.Used (was netpolicy.UsedPorts), and one Allocator for ssh, proxy
  and desktop ports under the claims lock (desktop allocated without
  one before; run/ports.lock and netpolicy's own ports key are gone).
- gitprotect: mode recorded before the backend makes the VM (NewMode;
  clone for --clone, which ls and git-protect share as gitprotect.Mode);
  made.git_id (platform.FolderID), one git_dirs list for modules and
  embedded git dirs, delegations, protected; Unlock takes the union of
  recorded and current git dirs.
- links' entries, git dirs, tools and devcontainer commands are JSON
  lists, not strings in values; the key=value parser is gone.
- vm.checkState reads every section of the folder's VM as it's found.
- registry.For drops its unused Dirs.
- Tests: TestVMRoundTrip, TestVMOtherName, TestVMID, TestVMUpdate,
  TestVMUpdateTogether (two writers on one VM and on crossed VMs),
  TestVMClaim, TestVMFailures, TestVMCorrupt, TestChooseAliasTogether,
  TestUsed, TestSSH, TestChooseTogether, TestUnlockRecorded,
  TestCheckState, TestChangesTogether, TestChangesAtOnce; testscript's
  vm_state, json_set with dotted and := keys; e2e's vm_state/vm_set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 14:47:06 -07:00

103 lines
2.8 KiB
Plaintext

# The scripts' own commands, which every other script uses.
# The sandbox shows messages by name and names devvm's folders.
exec sh -c 'test "$DEVVM_MESSAGES" = names && test "$DATA" = "$XDG_DATA_HOME/devvm" && test "$VMS" = "$DATA/vms" && test "$RUN" = "$DATA/run" && test "$CFG" = "$XDG_CONFIG_HOME/devvm"'
# stub_reset forgets the stubs' calls, and empties stdout as an exec does.
exec gpgv x
stub_calls
stdout '^gpgv x$'
stub_reset
! stdout .
! stub_calls
exec gpgv y
stub_calls
cmp stdout calls
# catg cats the one file a glob matches; a glob matching none or two
# fails it.
mkdir $VMS/0123456789abcd
cp vm.json $VMS/0123456789abcd/vm.json
catg $VMS/*/vm.json
cmp stdout vm.json
! catg $VMS/*/policy
cp vm.json $VMS/0123456789abcd/policy
! catg $VMS/0123456789abcd/*
catg $VMS/*/vm.json
cmp stdout vm.json
# vm_name sets a variable to the one VM whose state records a name made
# for a folder, and VAR_dir to its folder; a folder without state, another
# folder's VM, no VM or two fail it.
mkdir $VMS/0123456789abce
cp policy $VMS/0123456789abce/vm.json
vm_name WEB web
exec echo $WEB $WEB_dir
stdout '^devvm-web-0123456789abcd .*/vms/0123456789abcd$'
! vm_name API api
mkdir $VMS/fedcba98765432
cp other.json $VMS/fedcba98765432/vm.json
! vm_name WEB web
# vm_state prints a VM's state as sorted KEY=VALUE lines: a section's
# fields dotted, a list of strings space-separated, a list of objects by
# index, anything but a string as JSON.
vm_state $VMS/0123456789abcd/vm.json
cmp stdout flat
# json_set sets strings, any JSON with :=, and fields of objects by dotted
# keys, made when they're missing; the rest stays.
json_set $VMS/0123456789abcd/vm.json image=docker net.made.check=ok ports.host:={"ssh":2222} version:=2
vm_state $VMS/0123456789abcd/vm.json
stdout '^image=docker$'
stdout '^net.made.check=ok$'
stdout '^net.on=true$'
stdout '^ports.host.ssh=2222$'
stdout '^version=2$'
stdout '^tools.names=node claude$'
json_set $WORK/new.json a.b=c
exec cat $WORK/new.json
stdout '"a": \{\n "b": "c"\n \}'
# project makes a folder in $WORK and goes to it; with git, a repo.
project app
exec pwd
stdout '/app$'
! exists .git
project repo git
exec pwd
stdout '/repo$'
exists .git
-- calls --
gpgv y
-- vm.json --
{
"version": 1,
"id": "x",
"name": "devvm-web-0123456789abcd",
"image": "base",
"dir": "/p",
"backend": "lima",
"net": {"on": true},
"tools": {"names": ["node", "claude"]},
"links": {"links": [{"name": "db", "port": 5432}]},
"secrets": {"names": []}
}
-- flat --
backend=lima
dir=/p
id=x
image=base
links.links.0.name=db
links.links.0.port=5432
name=devvm-web-0123456789abcd
net.on=true
secrets.names=
tools.names=node claude
version=1
-- policy --
example.com
-- other.json --
{"version": 1, "id": "y", "name": "devvm-web-fedcba98765432", "image": "base", "dir": "/q", "backend": "lima"}