mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
devvm upgrade and install.sh accept a release's SHA256SUMS when any two
of three release keys signed it (CI's, the co-signer's and an offline
recovery key's), each in SHA256SUMS.<role>.minisig, checked only against
its own role's key, with the trusted comment devvm-release <version>
compared exactly; one signer counts once. While releases bridge from it,
the old release key's single SHA256SUMS.minisig (devvm <version>) still
passes alone, so 0.1 and 0.2 keep upgrading and --to/--version can
install them; install.sh now checks that key's trusted comment too.
The keys themselves aren't made yet, so packaging/keys holds none and
install.sh's three key lines are empty: a devvm built so checks each
release with the old release key alone, and a release that only the
release keys signed is refused. Releases go out as before, signed with
the old key, until the keys are made and added, all three at once.
- internal/minisign: the one owner of signature checks (moved from
internal/upgrade): ParsePublicKey, PublicKey.Check (comment always
compared), Role (CI, Cosign, Recovery), Kind (Release, Base),
Kind.Comment, SigName, KeyFile, Keys/NewKeys/Keys.Check (two of three,
distinct keys and key IDs). minisigntest/ signs for tests. Stdlib and
blake2b only (depguard), a leaf; FuzzCheck, FuzzKeysCheck,
FuzzParsePublicKey.
- The structure test's signatures rule: outside internal/minisign no
code calls ed25519/rsa/ecdsa's Verify functions or imports blake2b,
but signatureFree's files (ubuntu/pgp.go, proxy/nixcache.go).
- internal/upgrade: Source.Keys, checkSums (signatures.go); the bridge
in legacy.go (checkLegacy, legacyKey, Source.Key); FuzzSumOf.
- packaging/keys/release-{ci,cosign,recovery}.pub, none yet;
packaging.Key ("" without one). TestReleaseKeys (checkReleaseKeys:
none, or three distinct minisign keys, none the old key),
TestCheckReleaseKeys (fixture keys from minisigntest),
TestInstallScriptKeys (checkInstallScriptKeys: one line per role,
empty without keys), TestReleaseRefusesPlaceholders (no key in keys/
marked PLACEHOLDER).
- scripts/release.sh (a tag, --check, --tag; never a snapshot) needs
none or all three of packaging/keys' release keys. With none,
install.sh's release_<role>_key lines are empty or missing; with all
three, each is a minisign key, distinct, not the old release key, not
marked PLACEHOLDER (placeholder), and equal to its install.sh line.
- install.sh checks with minisign -VHQm: prehashed signatures only.
- The structure guard also flags dot imports of the verifier packages
and calls of x509's CheckSignature* method names.
- install.sh: release_<role>_key lines, download_signatures (a role's
signature only when the script has its key), check_signature,
signed_by_two, check_legacy; plainer warning without minisign, with
the commands to check by hand.
- Tests: signatures_test.go (TestFetchWithoutReleaseKeys among them),
upgrade.txtar (-legacy, -keys, and the keys built in, none),
install.txtar (stub minisign with -Q; NO_KEYS), install_minisign.txtar
(real minisign), release.txtar (none, nolines, nokeyline, old, marked
and the rest); DEVVM_RELEASE_KEYS override.
- Docs: internals/install.md, using/upgrading.md, maintaining.md's
Release keys, structure, tests, testing, SECURITY.md, CHANGELOG,
releases/0.2.0.md.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
836 lines
30 KiB
Go
836 lines
30 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rogpeppe/go-internal/testscript"
|
|
|
|
"github.com/devvmhq/devvm/internal/agents"
|
|
"github.com/devvmhq/devvm/internal/daemon"
|
|
"github.com/devvmhq/devvm/internal/image"
|
|
"github.com/devvmhq/devvm/internal/minisign"
|
|
"github.com/devvmhq/devvm/internal/paths"
|
|
"github.com/devvmhq/devvm/internal/platform"
|
|
"github.com/devvmhq/devvm/internal/ports"
|
|
"github.com/devvmhq/devvm/internal/stubtest"
|
|
"github.com/devvmhq/devvm/internal/ubuntu"
|
|
"github.com/devvmhq/devvm/internal/upgrade/upgradetest"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// testBuildFlag makes a test build (internal/testbuild), as tests/e2e
|
|
// does.
|
|
const testBuildFlag = "-X github.com/devvmhq/devvm/internal/testbuild.testBuild=yes"
|
|
|
|
// TestScript runs testdata/script/*.txtar against a built devvm, in a sandbox.
|
|
func TestScript(t *testing.T) {
|
|
bin := buildDevvm(t)
|
|
stubs := stubtest.Bin(t)
|
|
testscript.Run(t, testscript.Params{
|
|
Dir: scriptsDir(),
|
|
RequireExplicitExec: true,
|
|
RequireUniqueNames: true,
|
|
Setup: func(env *testscript.Env) error {
|
|
return sandbox(env, bin, stubs)
|
|
},
|
|
Cmds: scriptCmds(),
|
|
})
|
|
}
|
|
|
|
// scriptCmds are the commands devvm's scripts add to testscript's own.
|
|
// TestCommandLines leaves their lines out, as they aren't devvm's.
|
|
func scriptCmds() map[string]func(ts *testscript.TestScript, neg bool, args []string) {
|
|
return map[string]func(ts *testscript.TestScript, neg bool, args []string){
|
|
"catg": catGlob,
|
|
"cp_exe": cpExe,
|
|
"fixture": fixture,
|
|
"free_port": freePort,
|
|
"json_set": jsonSet,
|
|
"json_superset": jsonSuperset,
|
|
"listen_any": listenAny,
|
|
"project": project,
|
|
"release_server": releaseServer,
|
|
"renumber": renumber,
|
|
"stub_calls": stubCalls,
|
|
"stub_reset": stubReset,
|
|
"vm_name": vmName,
|
|
"vm_state": vmState,
|
|
"wait_file": waitFile,
|
|
"wait_until": waitUntil,
|
|
}
|
|
}
|
|
|
|
// scriptsDir is where the scripts are.
|
|
func scriptsDir() string { return filepath.Join("..", "..", "testdata", "script") }
|
|
|
|
// buildDevvm builds cmd/devvm, its files embedded, into a temporary folder
|
|
// and returns that folder. It's a test build: the test overrides count
|
|
// (readOverrides).
|
|
func buildDevvm(t *testing.T) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
args := []string{"install", "-ldflags", testBuildFlag}
|
|
if os.Getenv(coverDirVar) != "" {
|
|
args = append(args, coverFlags()...)
|
|
}
|
|
cmd := exec.CommandContext(t.Context(), "go", append(args, "github.com/devvmhq/devvm/cmd/devvm")...) //nolint:gosec // go install, with the test's own flags
|
|
cmd.Env = append(stubtest.GitEnv(os.Environ()), "CGO_ENABLED=0", "GOBIN="+dir) // go reads the checkout's own repo to stamp the binary
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("building devvm: %v\n%s", err, out)
|
|
}
|
|
return dir
|
|
}
|
|
|
|
// coverDirVar names the folder the CLI tests' devvm writes its coverage
|
|
// in, a folder for each script, when scripts/test.sh asks for it: devvm
|
|
// is built with -cover for
|
|
// every package of devvm's, so the CLI tests count toward each package's
|
|
// coverage, not only cli's.
|
|
const coverDirVar = "CLI_GOCOVERDIR"
|
|
|
|
// scriptCoverDir sets GOCOVERDIR, where a -cover devvm writes its coverage,
|
|
// to a folder of the script's own under CLI_GOCOVERDIR, when that's set.
|
|
// Scripts run in parallel, and devvm runs that start at once in one folder
|
|
// race on the meta-data file: Go names its temporary file by the time, to
|
|
// the microsecond on macOS, so one run's rename fails and devvm prints
|
|
// "coverage meta-data emit failed" on stderr. scripts/test.sh merges the
|
|
// folders.
|
|
func scriptCoverDir(env *testscript.Env) error {
|
|
parent := os.Getenv(coverDirVar)
|
|
if parent == "" {
|
|
return nil
|
|
}
|
|
dir, err := os.MkdirTemp(parent, "script.")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env.Setenv("GOCOVERDIR", dir)
|
|
return nil
|
|
}
|
|
|
|
// coverFlags are go build's flags for that: the test binary's own
|
|
// coverage mode (atomic with -race), as go tool covdata merges only
|
|
// data of one mode.
|
|
func coverFlags() []string {
|
|
mode := testing.CoverMode()
|
|
if mode == "" {
|
|
mode = "atomic"
|
|
}
|
|
return []string{"-cover", "-covermode=" + mode, "-coverpkg=github.com/devvmhq/devvm/..."}
|
|
}
|
|
|
|
// stopLinkSessions stops the link sessions (devvm _links) of the sandbox
|
|
// at root: each process a pid file in its run folder names, if it still
|
|
// runs as devvm _links.
|
|
func stopLinkSessions(root string) {
|
|
pids, _ := filepath.Glob(filepath.Join(root, "data", "devvm", "run", "*.links.pid"))
|
|
for _, f := range pids {
|
|
b, err := os.ReadFile(f) //nolint:gosec // the test's own folder
|
|
if err != nil {
|
|
continue
|
|
}
|
|
pid, err := strconv.Atoi(strings.TrimSpace(string(b)))
|
|
if err != nil || pid <= 0 {
|
|
continue
|
|
}
|
|
if args, err := platform.ProcessArgs(pid); err == nil && len(args) > 1 && args[1] == "_links" {
|
|
_ = daemon.Signal(pid, syscall.SIGKILL)
|
|
}
|
|
}
|
|
}
|
|
|
|
// proxyLogLines is how much of each proxy's log logProxies shows: its end.
|
|
const proxyLogLines = 40
|
|
|
|
// logProxies logs, once a script has failed, the end of each of the
|
|
// sandbox's VMs' proxy logs and its proxy's status file, before the
|
|
// sandbox goes with them: a proxy that didn't start (proxy_start_failed)
|
|
// says why there. Nothing for a script that passed, or a VM without a
|
|
// proxy log.
|
|
func logProxies(t testscript.T, root string) {
|
|
if f, ok := t.(interface{ Failed() bool }); !ok || !f.Failed() {
|
|
return
|
|
}
|
|
vms, _ := filepath.Glob(filepath.Join(root, "data", "devvm", "vms", "*"))
|
|
for _, dir := range vms {
|
|
v := paths.VM{Dir: dir}
|
|
b, err := os.ReadFile(v.ProxyLog())
|
|
if err != nil {
|
|
continue
|
|
}
|
|
lines := strings.SplitAfter(strings.TrimSuffix(string(b), "\n"), "\n")
|
|
lines = lines[max(0, len(lines)-proxyLogLines):]
|
|
status, err := os.ReadFile(v.Proxy(".status"))
|
|
if err != nil {
|
|
status = []byte("(none: " + err.Error() + ")")
|
|
}
|
|
t.Log("proxy log " + v.ProxyLog() + ", its last lines:\n" + strings.Join(lines, "") +
|
|
"\nproxy status " + v.Proxy(".status") + ":\n" + strings.TrimSuffix(string(status), "\n"))
|
|
}
|
|
}
|
|
|
|
// sandbox points every folder devvm uses into a new temporary folder, puts
|
|
// devvm and the stubs first on PATH, and leaves out git's variables that
|
|
// pick a repo (stubtest.GitEnv).
|
|
func sandbox(env *testscript.Env, bin, stubs string) error {
|
|
// /tmp keeps Lima's socket paths short.
|
|
root, err := os.MkdirTemp("/tmp", "dvt.")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The sandbox's own block of ports (sandboxPortsLo), given up once
|
|
// what it ran has stopped.
|
|
first, release, err := claimPortBlock(sandboxPortsDir(), sandboxPortsWait)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env.Defer(func() {
|
|
defer release()
|
|
logProxies(env.T(), root)
|
|
// VMs' proxies follow a stub hostagent that's gone before they
|
|
// look: stop them with the sandbox.
|
|
_ = exec.CommandContext(context.Background(), "pkill", "-f", "_proxy -config "+root+"/").Run() //nolint:gosec,forbidigo // the test's own folder; a cleanup, after the scripts' contexts end
|
|
// Their brokers stop their children; and a child whose broker a
|
|
// test killed so it kept its child.
|
|
_ = exec.CommandContext(context.Background(), "pkill", "-f", "_proxy-child -for "+root+"/").Run() //nolint:gosec,forbidigo // the test's own folder; a cleanup, after the scripts' contexts end
|
|
// And VMs' port forwarders.
|
|
_ = exec.CommandContext(context.Background(), "pkill", "-f", "_ports -vm .* -lifeline "+root+"/").Run() //nolint:gosec,forbidigo // the test's own folder; a cleanup, after the scripts' contexts end
|
|
// And client VMs' link sessions, named only by their VM: by their
|
|
// pid files, then the stub ssh's masters they and the forwarders
|
|
// leave, which outlive them as ssh's do.
|
|
stopLinkSessions(root)
|
|
_ = exec.CommandContext(context.Background(), "pkill", "-f", "[-]M -S "+root+"/").Run() //nolint:gosec,forbidigo // the test's own folder; a cleanup, after the scripts' contexts end
|
|
_ = os.RemoveAll(root)
|
|
})
|
|
|
|
dirs := map[string]string{
|
|
"HOME": "home",
|
|
"XDG_DATA_HOME": "data",
|
|
"XDG_CONFIG_HOME": "config",
|
|
"DEVVM_LIMA_HOME": "lima",
|
|
"LIMA_HOME": "lima",
|
|
"STUB_DIR": "stub",
|
|
}
|
|
for name, dir := range dirs {
|
|
path := filepath.Join(root, dir)
|
|
if err := os.MkdirAll(path, 0o750); err != nil {
|
|
return err
|
|
}
|
|
env.Setenv(name, path)
|
|
}
|
|
log := filepath.Join(root, "stub", "calls")
|
|
if err := os.WriteFile(log, nil, 0o600); err != nil {
|
|
return err
|
|
}
|
|
env.Setenv("STUB_LOG", log)
|
|
// Messages by their names, and devvm's own folders, which scripts read:
|
|
// a script that checks wording sets DEVVM_MESSAGES= itself.
|
|
env.Setenv("DEVVM_MESSAGES", "names")
|
|
for name, dir := range scriptFolders(root) {
|
|
env.Setenv(name, dir)
|
|
}
|
|
// Base's features are chosen (none), as devvm setup saves them, so
|
|
// nothing asks.
|
|
for _, dir := range []string{filepath.Join(root, "config", "devvm"), filepath.Join(root, "data", "devvm")} {
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := os.WriteFile(filepath.Join(root, "data", "devvm", "settings.json"), []byte(`{"features": []}`+"\n"), 0o600); err != nil {
|
|
return err
|
|
}
|
|
// git takes a bare repo only when it's named, as devvm doctor asks.
|
|
if err := os.WriteFile(filepath.Join(root, "home", ".gitconfig"), []byte("[safe]\n\tbareRepository = explicit\n"), 0o600); err != nil {
|
|
return err
|
|
}
|
|
// Ubuntu's cloud images: a dated release in a folder, as Ubuntu
|
|
// published and signed it (internal/ubuntu's fixture), never the
|
|
// network.
|
|
if err := os.CopyFS(filepath.Join(root, "ubuntu"), os.DirFS(filepath.Join("..", "ubuntu", "testdata", ubuntu.Series))); err != nil {
|
|
return err
|
|
}
|
|
env.Setenv("DEVVM_UBUNTU_URL", "file://"+filepath.Join(root, "ubuntu"))
|
|
if err := ownCloudImage(root); err != nil {
|
|
return err
|
|
}
|
|
// Linux's /dev/kvm: a file anyone can open, so the host's doesn't
|
|
// matter.
|
|
kvm := filepath.Join(root, "stub", "kvm")
|
|
if err := os.WriteFile(kvm, nil, 0o600); err != nil {
|
|
return err
|
|
}
|
|
env.Setenv("DEVVM_KVM", kvm)
|
|
// Linux hosts that can't pin .git in QEMU's namespace, whatever the
|
|
// host's kernel lets devvm do: a script that tests pins sets on.
|
|
env.Setenv("DEVVM_PINS", "this host's kernel refused")
|
|
// The sandbox's own ports (sandboxPortsLo), and from them its VMs'
|
|
// ssh ports: as many in place of the ports browsers refuse, which your
|
|
// own VMs may hold, and which scripts running at once would share.
|
|
sshPorts, next, err := takePorts(first, first+sandboxTaken, len(ports.SSHPorts()), false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
env.Setenv("SANDBOX_PORT_NEXT", strconv.Itoa(next))
|
|
env.Setenv("SANDBOX_PORT_END", strconv.Itoa(first+sandboxTaken))
|
|
env.Setenv("DEVVM_PORT_RANGE", strconv.Itoa(first+sandboxTaken)+"-"+strconv.Itoa(first+sandboxBlock-1))
|
|
ssh := make([]string, len(sshPorts))
|
|
for i, p := range sshPorts {
|
|
ssh[i] = strconv.Itoa(p)
|
|
}
|
|
env.Setenv("DEVVM_SSH_PORTS", strings.Join(ssh, ","))
|
|
env.Setenv("DEVVM_SECRET_SERVICE", "devvm-test")
|
|
// No launch asks GitHub for devvm's newest release: a script that
|
|
// tests the check unsets it and serves its own (release_server).
|
|
env.Setenv("DEVVM_NO_RELEASE_CHECK", "1")
|
|
// Tool images build one at a time, so the stub's calls keep their
|
|
// order: a script that tests builds side by side sets more.
|
|
env.Setenv("DEVVM_TOOL_SLOTS", "1")
|
|
// A large host, whatever runs the tests: a script that tests a small
|
|
// one sets its own.
|
|
for name, v := range fakeHost() {
|
|
env.Setenv(name, v)
|
|
}
|
|
// The shared tools' versions: the stub mise, which resolves the
|
|
// fixture lock.
|
|
repo, err := filepath.Abs(filepath.Join("..", ".."))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// The registries' lists, as devvm's messages join them, so a script
|
|
// that checks one names no toolchain or agent itself.
|
|
env.Setenv("TOOLCHAINS", strings.Join(image.Toolchains, " "))
|
|
env.Setenv("AGENTS", strings.Join(image.Agents, " "))
|
|
// The agents' lists as a choice's message names them: every agent,
|
|
// those devvm agent login logs in, and those with a key for each
|
|
// provider (--provider).
|
|
env.Setenv("AGENT_CHOICES", strings.Join(agents.Names(), ", "))
|
|
env.Setenv("LOGIN_AGENTS", strings.Join(agents.LoginAgents(), ", "))
|
|
env.Setenv("KEY_AGENTS", strings.Join(agents.Select(func(d agents.Def) bool { return d.Keys == agents.ProviderKeys }), ", "))
|
|
env.Setenv("DEVVM_HOST_MISE", filepath.Join(stubs, "mise"))
|
|
// devvm paste's clipboard tools: the stubs, in place of the system's
|
|
// (macOS's /usr/bin/osascript), so no script reaches the real clipboard.
|
|
env.Setenv("DEVVM_CLIP_TOOLS", stubs)
|
|
env.Setenv("MISE_STUB_LOCK", filepath.Join(repo, "testdata", "mise.lock"))
|
|
env.Setenv("DEVVM_BIN", bin) // for scripts that set their own PATH
|
|
if err := scriptCoverDir(env); err != nil {
|
|
return err
|
|
}
|
|
env.Setenv("DEVVM_REPO", repo) // for the repo's own scripts (scripts/, assets/)
|
|
env.Setenv("PATH", strings.Join([]string{bin, stubs, env.Getenv("PATH")}, string(os.PathListSeparator)))
|
|
// testscript starts from an environment of its own, but a script's git
|
|
// never gets git's variables that pick a repo, whatever ran the tests.
|
|
env.Vars = stubtest.GitEnv(env.Vars)
|
|
// Nor the machine's system config, which differs between machines:
|
|
// Apple's sets init.defaultBranch=main, CI runners' none.
|
|
env.Setenv("GIT_CONFIG_NOSYSTEM", "1")
|
|
return nil
|
|
}
|
|
|
|
// ownCloudImage puts this host's cloud image of the sandbox's one release
|
|
// of Ubuntu (root/ubuntu) in devvm's own folder of them, named for its
|
|
// SHA-256 in the release's checksums, as if devvm had downloaded it: the
|
|
// real one is a gigabyte, so a stand-in, which only Lima would check.
|
|
// Every base build goes the way it does after a first download, and a
|
|
// script that tests the download or the import from Lima's cache removes
|
|
// it.
|
|
func ownCloudImage(root string) error {
|
|
rels, err := filepath.Glob(filepath.Join(root, "ubuntu", "release-*"))
|
|
if err != nil || len(rels) != 1 {
|
|
return fmt.Errorf("the sandbox's Ubuntu releases: %v, %w", rels, err)
|
|
}
|
|
sums, err := os.ReadFile(filepath.Join(rels[0], "SHA256SUMS"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sum := ubuntu.Checksum(sums, ubuntu.File(runtime.GOARCH))
|
|
if sum == "" {
|
|
return fmt.Errorf("no %s in %s's checksums", ubuntu.File(runtime.GOARCH), rels[0])
|
|
}
|
|
d := paths.Dirs{Data: filepath.Join(root, "data", "devvm")}
|
|
if err := os.MkdirAll(d.UbuntuImages(), 0o700); err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(d.UbuntuImage(sum), []byte("a stand-in for Ubuntu's cloud image\n"), 0o600)
|
|
}
|
|
|
|
// scriptFolders are the variables naming devvm's folders in the sandbox
|
|
// at root: DATA its data folder, VMS its VMs' state files, RUN its run
|
|
// folder and CFG its config folder.
|
|
func scriptFolders(root string) map[string]string {
|
|
data := filepath.Join(root, "data", "devvm")
|
|
return map[string]string{
|
|
"DATA": data,
|
|
"VMS": filepath.Join(data, "vms"),
|
|
"RUN": filepath.Join(data, "run"),
|
|
"CFG": filepath.Join(root, "config", "devvm"),
|
|
}
|
|
}
|
|
|
|
// fakeHost is the host the test build reads in every script
|
|
// (platform.Host's overrides): twice the CPUs and memory base asks for,
|
|
// and more free space than any build checks for.
|
|
func fakeHost() map[string]string {
|
|
return map[string]string{
|
|
platform.TestCPUs: "8",
|
|
platform.TestMemoryGiB: "16",
|
|
platform.TestFreeGiB: "500",
|
|
// Backends' listings wait long, as the stubs are slow under load:
|
|
// a script that tests the timeout sets its own.
|
|
platform.TestListWait: "10m",
|
|
}
|
|
}
|
|
|
|
// waitFileTimeout is how long wait_file waits. A background devvm takes
|
|
// a dozen stub calls to get to the one that writes the file: about a
|
|
// second alone, but over 10 under the full suite's load, when each stub
|
|
// call (bash and its own commands) can take a second.
|
|
const waitFileTimeout = 60 * time.Second
|
|
|
|
// waitUntilTimeout is how long wait_until waits: as long as the slowest
|
|
// of what scripts wait for, a background devvm desktop's VNC server under
|
|
// the full suite's load.
|
|
const waitUntilTimeout = 90 * time.Second
|
|
|
|
// waitGiveUp is the exit status by which a wait_until script says it can't
|
|
// succeed: one no tool the scripts run gives, as grep and sed give 2 for a
|
|
// file that isn't there yet.
|
|
const waitGiveUp = 100
|
|
|
|
// cpExe copies an executable, a new one mode 0755, for the script to run. It writes
|
|
// it under syscall.ForkLock, as testscript's cp doesn't: on Linux a child
|
|
// another script forks while the copy's write descriptor is open holds it
|
|
// until that child execs, and running the copy then fails with "text file
|
|
// busy" (ETXTBSY).
|
|
func cpExe(ts *testscript.TestScript, neg bool, args []string) {
|
|
if neg || len(args) != 2 {
|
|
ts.Fatalf("usage: cp_exe SRC DST")
|
|
}
|
|
data, err := os.ReadFile(ts.MkAbs(args[0]))
|
|
ts.Check(err)
|
|
syscall.ForkLock.RLock()
|
|
defer syscall.ForkLock.RUnlock()
|
|
f, err := os.OpenFile(ts.MkAbs(args[1]), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755) //nolint:gosec // a file in the script's own folder, written in place under the lock
|
|
ts.Check(err)
|
|
_, err = f.Write(data)
|
|
if cerr := f.Close(); err == nil {
|
|
err = cerr
|
|
}
|
|
ts.Check(err)
|
|
}
|
|
|
|
// waitFile waits for a file to exist, for a background command to get
|
|
// somewhere. It fails at once when every background command has ended
|
|
// (nothing is left to write it), else after waitFileTimeout, for one that
|
|
// hangs on the way.
|
|
func waitFile(ts *testscript.TestScript, neg bool, args []string) {
|
|
if neg || len(args) != 1 {
|
|
ts.Fatalf("usage: wait_file FILE")
|
|
}
|
|
deadline := time.Now().Add(waitFileTimeout)
|
|
for time.Now().Before(deadline) {
|
|
if _, err := os.Stat(ts.MkAbs(args[0])); err == nil {
|
|
return
|
|
}
|
|
if bg := ts.BackgroundCmds(); len(bg) > 0 && !slices.ContainsFunc(bg, running) {
|
|
ts.Fatalf("%s didn't appear: the background command ended", args[0])
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
ts.Fatalf("%s didn't appear in %s", args[0], waitFileTimeout)
|
|
}
|
|
|
|
// waitUntil is the one wait of the scripts for something that isn't a
|
|
// file: it runs SCRIPT with sh -c, in the script's environment, every
|
|
// 100 ms until it succeeds, and fails after waitUntilTimeout, at once when
|
|
// every background command has ended (nothing is left to make it
|
|
// succeed), or at once when SCRIPT exits waitGiveUp, which a script's check
|
|
// that can't succeed any more gives, such as a session that ended. Its
|
|
// last run's output is the command's, so stdout and stderr can check it.
|
|
func waitUntil(ts *testscript.TestScript, neg bool, args []string) {
|
|
if neg || len(args) != 1 {
|
|
ts.Fatalf("usage: wait_until SCRIPT")
|
|
}
|
|
deadline := time.Now().Add(waitUntilTimeout)
|
|
for {
|
|
err := ts.Exec("sh", "-c", args[0])
|
|
if err == nil {
|
|
return
|
|
}
|
|
if e, ok := errors.AsType[*exec.ExitError](err); ok && e.ExitCode() == waitGiveUp {
|
|
ts.Fatalf("%s can't succeed: %v", args[0], err)
|
|
}
|
|
if bg := ts.BackgroundCmds(); len(bg) > 0 && !slices.ContainsFunc(bg, running) {
|
|
ts.Fatalf("%s didn't succeed: the background command ended", args[0])
|
|
}
|
|
if time.Now().After(deadline) {
|
|
ts.Fatalf("%s didn't succeed in %s: %v", args[0], waitUntilTimeout, err)
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
// running reports whether a background command's process hasn't been
|
|
// waited for yet. Signal 0 asks without reading the state testscript's
|
|
// own goroutine writes when it waits.
|
|
func running(cmd *exec.Cmd) bool {
|
|
return cmd.Process != nil && !errors.Is(cmd.Process.Signal(syscall.Signal(0)), os.ErrProcessDone)
|
|
}
|
|
|
|
// releaseServer serves devvm's releases until the script ends, as GitHub
|
|
// does (upgradetest): a release of each VERSION for this machine, the
|
|
// first the newest, each devvm in it a script that says its version, and
|
|
// SHA256SUMS signed as a release is while releases bridge from the old
|
|
// release key, by CI's and the co-signer's keys and by the old key, all
|
|
// keys of its own. With -legacy only the old key signs it, as 0.1 and
|
|
// 0.2's releases are, with -keys only the release keys do, and with
|
|
// -badsig other keys sign it. It points devvm upgrade at it:
|
|
// DEVVM_REPO_URL, the release keys' public halves in DEVVM_RELEASE_KEYS,
|
|
// the old key's in DEVVM_RELEASE_KEY, and its certificate in
|
|
// $WORK/release-ca.pem, DEVVM_RELEASE_CA.
|
|
func releaseServer(ts *testscript.TestScript, neg bool, args []string) {
|
|
keys := upgradetest.NewSigners()
|
|
signers := keys
|
|
shape := func(r upgradetest.Release) upgradetest.Release { return r }
|
|
for len(args) > 0 && strings.HasPrefix(args[0], "-") {
|
|
switch args[0] {
|
|
case "-badsig":
|
|
signers = upgradetest.NewSigners()
|
|
case "-legacy":
|
|
shape = upgradetest.Release.LegacyOnly
|
|
case "-keys":
|
|
shape = upgradetest.Release.KeysOnly
|
|
default:
|
|
ts.Fatalf("release_server: unknown flag %s", args[0])
|
|
}
|
|
args = args[1:]
|
|
}
|
|
if neg || len(args) == 0 {
|
|
ts.Fatalf("usage: release_server [-badsig] [-legacy|-keys] LATEST [VERSION...]")
|
|
}
|
|
s := upgradetest.Start()
|
|
ts.Defer(s.Close)
|
|
for i, v := range args {
|
|
s.Add(v, shape(upgradetest.NewRelease(signers, v, nil)), i == 0)
|
|
}
|
|
ca := ts.MkAbs("release-ca.pem")
|
|
ts.Check(os.WriteFile(ca, s.CA(), 0o600))
|
|
var pub []string
|
|
for _, r := range minisign.Roles() {
|
|
pub = append(pub, string(r)+"="+keys.Keys[r].Public())
|
|
}
|
|
ts.Setenv("DEVVM_REPO_URL", s.URL)
|
|
ts.Setenv("DEVVM_RELEASE_KEYS", strings.Join(pub, " "))
|
|
ts.Setenv("DEVVM_RELEASE_KEY", keys.Legacy.Public())
|
|
ts.Setenv("DEVVM_RELEASE_CA", ca)
|
|
}
|
|
|
|
// listenAny listens on a free port on every address, as a program on the
|
|
// host does, until the script ends, and sets VAR to the port.
|
|
func listenAny(ts *testscript.TestScript, neg bool, args []string) {
|
|
if neg || len(args) != 1 {
|
|
ts.Fatalf("usage: listen_any VAR")
|
|
}
|
|
l, err := net.Listen("tcp", ":0") //nolint:noctx,gosec // every address, as the program it stands for; the script's whole life
|
|
ts.Check(err)
|
|
ts.Defer(func() { _ = l.Close() })
|
|
ts.Setenv(args[0], strconv.Itoa(l.Addr().(*net.TCPAddr).Port))
|
|
}
|
|
|
|
// freePort sets each VAR to a different port of the sandbox's own
|
|
// (sandboxTake) nothing on the host listens on now, for a script that
|
|
// lists ports a start or a change finds free: a fixed one (5173) may be a
|
|
// program's here. -range FIRST LAST sets the two to a range of two such
|
|
// ports, LAST the one after FIRST.
|
|
func freePort(ts *testscript.TestScript, neg bool, args []string) {
|
|
usage := "usage: free_port VAR... | free_port -range FIRST LAST"
|
|
if neg || len(args) == 0 {
|
|
ts.Fatalf("%s", usage)
|
|
}
|
|
if args[0] == "-range" {
|
|
if len(args) != 3 {
|
|
ts.Fatalf("%s", usage)
|
|
}
|
|
r := sandboxTake(ts, 2, true)
|
|
ts.Setenv(args[1], strconv.Itoa(r[0]))
|
|
ts.Setenv(args[2], strconv.Itoa(r[1]))
|
|
return
|
|
}
|
|
for i, port := range freePorts(ts, len(args)) {
|
|
ts.Setenv(args[i], strconv.Itoa(port))
|
|
}
|
|
}
|
|
|
|
// freePorts returns n different ports of the sandbox's own nothing on the
|
|
// host listens on now, on any address (sandboxTake): no devvm the tests
|
|
// run, no outgoing connection and no other sandbox takes one of them
|
|
// meanwhile.
|
|
func freePorts(ts *testscript.TestScript, n int) []int { return sandboxTake(ts, n, false) }
|
|
|
|
// stubCalls writes the calls the stubs logged to stdout, one per line.
|
|
// Negated, it fails if any were logged.
|
|
func stubCalls(ts *testscript.TestScript, neg bool, args []string) {
|
|
if len(args) > 0 {
|
|
ts.Fatalf("usage: stub_calls")
|
|
}
|
|
calls := ts.ReadFile(ts.Getenv("STUB_LOG"))
|
|
if neg {
|
|
if calls != "" {
|
|
ts.Fatalf("unexpected stub calls:\n%s", calls)
|
|
}
|
|
return
|
|
}
|
|
_, err := ts.Stdout().Write([]byte(calls))
|
|
ts.Check(err)
|
|
}
|
|
|
|
// stubReset forgets the calls the stubs logged so far. It empties stdout
|
|
// and stderr too, as an exec does.
|
|
func stubReset(ts *testscript.TestScript, neg bool, args []string) {
|
|
if neg || len(args) > 0 {
|
|
ts.Fatalf("usage: stub_reset")
|
|
}
|
|
ts.Check(os.WriteFile(ts.Getenv("STUB_LOG"), nil, 0o600))
|
|
ts.Stdout()
|
|
ts.Stderr()
|
|
}
|
|
|
|
// catGlob writes the one file a glob matches to stdout, and fails when it
|
|
// matches none or more than one. Negated, it fails when the glob matches
|
|
// exactly one.
|
|
func catGlob(ts *testscript.TestScript, neg bool, args []string) {
|
|
if len(args) != 1 {
|
|
ts.Fatalf("usage: catg GLOB")
|
|
}
|
|
files, err := filepath.Glob(ts.MkAbs(args[0]))
|
|
ts.Check(err)
|
|
switch {
|
|
case neg && len(files) == 1:
|
|
ts.Fatalf("%s matches one file: %s", args[0], files[0])
|
|
case neg:
|
|
case len(files) != 1:
|
|
ts.Fatalf("%s matches %d files, not one: %q", args[0], len(files), files)
|
|
default:
|
|
_, err = ts.Stdout().Write([]byte(ts.ReadFile(files[0])))
|
|
ts.Check(err)
|
|
}
|
|
}
|
|
|
|
// jsonSet sets fields in each JSON file GLOB matches, keeping its other
|
|
// fields; a GLOB without a wildcard that matches nothing is made, an
|
|
// object with only these fields. KEY=VALUE sets a string, KEY:=JSON any
|
|
// value; a dotted KEY (net.on) is a field of an object, made when it's
|
|
// missing. It's how scripts write devvm's JSON records, a tool image's
|
|
// built time or a VM's state say, which they can't touch.
|
|
func jsonSet(ts *testscript.TestScript, neg bool, args []string) {
|
|
if neg || len(args) < 2 {
|
|
ts.Fatalf("usage: json_set GLOB KEY=VALUE...")
|
|
}
|
|
glob := ts.MkAbs(args[0])
|
|
files, err := filepath.Glob(glob)
|
|
ts.Check(err)
|
|
if len(files) == 0 && !strings.ContainsAny(args[0], "*?[") {
|
|
files = []string{glob}
|
|
}
|
|
if len(files) == 0 {
|
|
ts.Fatalf("%s matches no file", args[0])
|
|
}
|
|
for _, f := range files {
|
|
obj := map[string]any{}
|
|
if data, err := os.ReadFile(f); err == nil { //nolint:gosec // the script's own file
|
|
ts.Check(json.Unmarshal(data, &obj))
|
|
} else if !errors.Is(err, os.ErrNotExist) {
|
|
ts.Fatalf("%v", err)
|
|
}
|
|
for _, kv := range args[1:] {
|
|
k, v, ok := strings.Cut(kv, "=")
|
|
if !ok {
|
|
ts.Fatalf("usage: json_set GLOB KEY=VALUE...")
|
|
}
|
|
var val any = v
|
|
if raw, isRaw := strings.CutSuffix(k, ":"); isRaw {
|
|
k = raw
|
|
ts.Check(json.Unmarshal([]byte(v), &val))
|
|
}
|
|
// A dotted key is a field of an object, made when it's missing.
|
|
at := obj
|
|
parts := strings.Split(k, ".")
|
|
for _, p := range parts[:len(parts)-1] {
|
|
next, _ := at[p].(map[string]any)
|
|
if next == nil {
|
|
next = map[string]any{}
|
|
at[p] = next
|
|
}
|
|
at = next
|
|
}
|
|
at[parts[len(parts)-1]] = val
|
|
}
|
|
data, err := json.MarshalIndent(obj, "", " ")
|
|
ts.Check(err)
|
|
ts.Check(os.MkdirAll(filepath.Dir(f), 0o700))
|
|
ts.Check(os.WriteFile(f, append(data, '\n'), 0o600))
|
|
}
|
|
}
|
|
|
|
// renumber FILE changes a VM's state, FILE, as a boot that numbered its
|
|
// disks' devices anew leaves it, as macOS does (stubtest.Renumbered): its
|
|
// project folder's identity and each .git's (git_id) on another device,
|
|
// their inodes and birth times as they were. FOLDER_ID is the folder's
|
|
// identity before, and GIT_ID its own .git's ("" for none).
|
|
func renumber(ts *testscript.TestScript, neg bool, args []string) {
|
|
if neg || len(args) != 1 {
|
|
ts.Fatalf("usage: renumber FILE")
|
|
}
|
|
file := ts.MkAbs(args[0])
|
|
dec := json.NewDecoder(strings.NewReader(ts.ReadFile(args[0])))
|
|
dec.UseNumber()
|
|
var obj map[string]any
|
|
ts.Check(dec.Decode(&obj))
|
|
id, _ := obj["folder"].(string)
|
|
if id == "" {
|
|
ts.Fatalf("%s records no folder", args[0])
|
|
}
|
|
ts.Setenv("FOLDER_ID", id)
|
|
git, _ := obj["git"].(map[string]any)
|
|
made, _ := git["made"].(map[string]any)
|
|
gitID, _ := made["git_id"].(string)
|
|
ts.Setenv("GIT_ID", gitID)
|
|
obj["folder"] = stubtest.Renumbered(id)
|
|
var each func(v any)
|
|
each = func(v any) {
|
|
switch v := v.(type) {
|
|
case map[string]any:
|
|
for k, e := range v {
|
|
if s, ok := e.(string); ok && k == "git_id" {
|
|
v[k] = stubtest.Renumbered(s)
|
|
} else {
|
|
each(e)
|
|
}
|
|
}
|
|
case []any:
|
|
for _, e := range v {
|
|
each(e)
|
|
}
|
|
}
|
|
}
|
|
each(obj)
|
|
data, err := json.MarshalIndent(obj, "", " ")
|
|
ts.Check(err)
|
|
ts.Check(os.WriteFile(file, append(data, '\n'), 0o600))
|
|
}
|
|
|
|
// vmName sets VAR to the name of the one VM made for the folder NAME, the
|
|
// one whose state in $VMS records the name devvm-NAME-ID, and VAR_dir to
|
|
// its folder there ($VMS/ID). Negated, it fails when there's exactly one.
|
|
func vmName(ts *testscript.TestScript, neg bool, args []string) {
|
|
if len(args) != 2 {
|
|
ts.Fatalf("usage: vm_name VAR NAME")
|
|
}
|
|
found := vmsFor(ts, args[1])
|
|
switch {
|
|
case neg && len(found) == 1:
|
|
ts.Fatalf("one VM for the folder %s: %s", args[1], found[0])
|
|
case neg:
|
|
case len(found) != 1:
|
|
ts.Fatalf("%d VMs for the folder %s, not one: %q", len(found), args[1], found)
|
|
default:
|
|
ts.Setenv(args[0], found[0])
|
|
ts.Setenv(args[0]+"_dir", filepath.Join(ts.Getenv("VMS"), found[0][len(found[0])-vmname.HashLen:]))
|
|
}
|
|
}
|
|
|
|
// vmsFor returns the names of the VMs whose states in $VMS record a name
|
|
// made for the folder name.
|
|
func vmsFor(ts *testscript.TestScript, name string) []string {
|
|
states, err := filepath.Glob(filepath.Join(ts.Getenv("VMS"), "*", "vm.json"))
|
|
if err != nil {
|
|
ts.Fatalf("%v", err)
|
|
}
|
|
re := regexp.MustCompile(`^devvm-` + regexp.QuoteMeta(name) + `-[0-9a-f]+$`)
|
|
var found []string
|
|
for _, s := range states {
|
|
var st struct{ Name string }
|
|
if json.Unmarshal([]byte(ts.ReadFile(s)), &st) == nil && re.MatchString(st.Name) {
|
|
found = append(found, st.Name)
|
|
}
|
|
}
|
|
return found
|
|
}
|
|
|
|
// vmState prints the one VM state (vm.json) GLOB matches as KEY=VALUE
|
|
// lines, sorted, for scripts to match: a section's fields dotted
|
|
// (net.on=true, ports.host.ssh=23456), a list of strings space-separated,
|
|
// a list of objects by index (links.links.0.name=db), strings as they
|
|
// are and anything else as JSON.
|
|
func vmState(ts *testscript.TestScript, neg bool, args []string) {
|
|
if neg || len(args) != 1 {
|
|
ts.Fatalf("usage: vm_state GLOB")
|
|
}
|
|
files, err := filepath.Glob(ts.MkAbs(args[0]))
|
|
ts.Check(err)
|
|
if len(files) != 1 {
|
|
ts.Fatalf("%s matches %d files, not one: %q", args[0], len(files), files)
|
|
}
|
|
var v any
|
|
ts.Check(json.Unmarshal([]byte(ts.ReadFile(files[0])), &v))
|
|
var lines []string
|
|
var flat func(prefix string, v any)
|
|
flat = func(prefix string, v any) {
|
|
switch v := v.(type) {
|
|
case map[string]any:
|
|
for k, f := range v {
|
|
flat(strings.TrimPrefix(prefix+"."+k, "."), f)
|
|
}
|
|
case []any:
|
|
var words []string
|
|
for i, e := range v {
|
|
if s, ok := e.(string); ok {
|
|
words = append(words, s)
|
|
} else {
|
|
flat(prefix+"."+strconv.Itoa(i), e)
|
|
}
|
|
}
|
|
if len(words) > 0 || len(v) == 0 {
|
|
lines = append(lines, prefix+"="+strings.Join(words, " "))
|
|
}
|
|
case string:
|
|
lines = append(lines, prefix+"="+v)
|
|
default:
|
|
b, _ := json.Marshal(v)
|
|
lines = append(lines, prefix+"="+string(b))
|
|
}
|
|
}
|
|
flat("", v)
|
|
slices.Sort(lines)
|
|
_, err = ts.Stdout().Write([]byte(strings.Join(lines, "\n") + "\n"))
|
|
ts.Check(err)
|
|
}
|
|
|
|
// project makes the folder $WORK/NAME and goes to it, as a project's
|
|
// folder; with git, it's a repo with nothing committed.
|
|
func project(ts *testscript.TestScript, neg bool, args []string) {
|
|
if neg || len(args) == 0 || len(args) > 2 || len(args) == 2 && args[1] != "git" || strings.ContainsAny(args[0], `/\`) {
|
|
ts.Fatalf("usage: project NAME [git]")
|
|
}
|
|
dir := filepath.Join(ts.Getenv("WORK"), args[0])
|
|
ts.Check(os.MkdirAll(dir, 0o750))
|
|
ts.Check(ts.Chdir(dir))
|
|
if len(args) == 2 {
|
|
ts.Check(ts.Exec("git", "init", "-q"))
|
|
}
|
|
}
|