From 6483c65fd7deda17f37ca6f37973424f95822810 Mon Sep 17 00:00:00 2001 From: Seamus Abshere Date: Mon, 16 Nov 2026 12:44:51 -0500 Subject: [PATCH 1/4] feat: mint Vault child tokens, resolve host_env, run prepare steps and host suites Co-authored-by: Cursor --- README.md | 63 ++++++- cmd/aviary/main.go | 91 +++++++++- cmd/aviary/main_test.go | 34 +++- internal/compose/compose.go | 39 ++++- internal/config/config.go | 132 +++++++++++++- internal/config/config_test.go | 51 ++++++ internal/stack/stack.go | 178 ++++++++++++++++--- internal/stack/stack_test.go | 87 ++++++++++ internal/vault/vault.go | 302 +++++++++++++++++++++++++++++++++ internal/vault/vault_test.go | 164 ++++++++++++++++++ 10 files changed, 1107 insertions(+), 34 deletions(-) create mode 100644 internal/vault/vault.go create mode 100644 internal/vault/vault_test.go diff --git a/README.md b/README.md index 24769b1..f9bf02d 100644 --- a/README.md +++ b/README.md @@ -166,16 +166,51 @@ path, so a worktree at the same path gets the same ports on every machine. aviary skips ports another stack holds or something on the machine is listening on. A worktree keeps its ports until `aviary rm` or `aviary gc`. -### Required environment +### Required environment, Vault tokens, and host files ```yaml +vault: + addr: https://vault.example.com + ttl: 8h + +host_env: + GOOGLE_CREDENTIALS_HOST_PATH: + path: ~/.config/gcloud/application_default_credentials.json + required: true + hint: ask the user to run `gcloud auth application-default login` + require_env: [VAULT_TOKEN] ``` Commands that create containers (`up`, `test`, `init`, `run`, `build`, and -`aviary compose up` or `run`) refuse to start while any of these is unset or -empty, and name every missing one. The host environment passes through to -compose, so compose files can interpolate them. +`aviary compose up` or `run`) resolve credentials in this order before calling +compose: + +1. **`vault`**: if `VAULT_TOKEN` is set in the environment and accepted by + `lookup-self`, aviary uses it as-is. Otherwise it mints a non-renewable + child token from `~/.vault-token`, caches it under + `$XDG_STATE_HOME/aviary/vault-token.json` (`0600`), and reuses it across + worktrees and shells until less than half of `ttl` remains. An expired + `VAULT_TOKEN` in an agent shell falls back to `~/.vault-token` automatically. + `VAULT_TOKEN` is passed to compose subprocesses and never printed by + `aviary env`. +2. **`host_env`**: each unset variable is filled with the expanded `path` when + that file exists on the host. When `required: true` and the file is missing, + aviary stops before touching Docker and prints `hint`. +3. **`require_env`**: refuses to start while any listed variable is still unset + or empty, naming every missing one. + +### Prepare steps + +```yaml +prepare: + - command: [node, scripts/copy-api-artifacts.mts, --all] +``` + +Commands that run code from the worktree (`up`, `init`, `run`, and any suite +without `prepare: false`) run `prepare` on the host in the worktree root first, +so gitignored build/codegen inputs exist in fresh worktrees without an extra +step. ### Definitions outside the worktree @@ -231,14 +266,25 @@ suites: needs: [postgres] command: [npm, run, test:migrations] init: false + worker-host: + host: true + workdir: src/worker + init: false + needs: [postgres] + env: + TEST_DATABASE_URL: postgresql://postgres@127.0.0.1:${AVIARY_PORT_POSTGRES}/postgres + command: [uv, run, pytest] ``` `aviary test api`: -1. runs the init steps, each as `docker compose run --rm `, +1. runs `prepare` on the host in the worktree root (skipped with `prepare: false`); +2. runs the init steps, each as `docker compose run --rm `, if init has not run for this stack yet (skipped with `init: false`); -2. starts `postgres` and `redis` and waits until they are healthy; -3. runs `npm test` in a fresh `api` container. +3. starts `postgres` and `redis` and waits until they are healthy; +4. runs `npm test` in a fresh `api` container (or directly on the host in + `workdir` when `host: true`, with `env` interpolated against the stack's + `AVIARY_*` variables). Arguments after the suite name, with or without a `--`, are appended to its command, so `aviary test api -- --grep users` and `aviary test api --help` @@ -289,6 +335,9 @@ Run from anywhere inside a worktree. `aviary --help` describes each. - `aviary down` stops it and keeps its volumes; `--volumes` removes them too. - `aviary test [args...]` runs a suite; with no suite it lists them. - `aviary init [--force]` runs the init steps by hand. +- `aviary pull [service...]` pulls the stack's images across every profile + (using `gcloud` Application Default Credentials automatically when present so + GCR pulls never hang on an interactive reauth prompt). - `aviary run [command...]` runs a one-off command in a fresh container and exits with its status. - `aviary exec ` runs a command in a running container. diff --git a/cmd/aviary/main.go b/cmd/aviary/main.go index 526f12f..8f7d569 100644 --- a/cmd/aviary/main.go +++ b/cmd/aviary/main.go @@ -9,6 +9,7 @@ import ( "io/fs" "maps" "os" + "os/exec" "path/filepath" "slices" "strings" @@ -18,6 +19,7 @@ import ( "github.com/spf13/cobra" "github.com/faradayio/aviary/internal/compose" + "github.com/faradayio/aviary/internal/config" "github.com/faradayio/aviary/internal/registry" "github.com/faradayio/aviary/internal/stack" "github.com/faradayio/aviary/internal/workspace" @@ -50,6 +52,8 @@ type app struct { listProjects func() ([]compose.Project, error) downProject func(name string) error runCompose func(c compose.Stack, args ...string) error + runHost func(dir string, extraEnv map[string]string, command []string) error + vaultToken func(cfg *config.Vault, project string) (string, error) branch func(root string) string } @@ -59,6 +63,7 @@ func newApp() *app { listProjects: compose.ListProjects, downProject: compose.DownProject, runCompose: compose.Stack.Run, + runHost: compose.RunHost, branch: workspace.Branch, } } @@ -86,11 +91,12 @@ func (a *app) rootCmd() *cobra.Command { root.PersistentFlags().StringVar(&a.config, "config", "", "directory holding aviary.yml when the worktree has none (default $AVIARY_CONFIG)") root.AddCommand( - a.passthrough("up [--build] [service...]", "Start the stack, or the named services, and wait until healthy", startsContainers, "up", "--detach", "--wait"), + a.upCmd(), a.downCmd(), a.testCmd(), a.initCmd(), - a.passthrough("run [command...]", "Run a one-off command in a fresh container", startsContainers, "run", "--rm"), + a.pullCmd(), + a.runCmd(), a.passthrough("exec ", "Run a command in a running container", inspectsOnly, "exec"), a.passthrough("ps", "List this stack's containers", inspectsOnly, "ps"), a.passthrough("logs [service...]", "Show container logs", inspectsOnly, "logs"), @@ -134,9 +140,90 @@ func (a *app) loadStack(opts stack.Options) (*stack.Stack, error) { return nil, err } opts.Runner = a.runCompose + opts.HostRunner = a.runHost + opts.VaultToken = a.vaultToken return stack.Load(cwd, a.configDir(), opts) } +func (a *app) upCmd() *cobra.Command { + cmd := a.passthrough("up [--build] [service...]", "Start the stack, or the named services, and wait until healthy", startsContainers, "up", "--detach", "--wait") + cmd.RunE = func(cmd *cobra.Command, args []string) error { + s, err := a.loadStack(startsContainers) + if err != nil { + return err + } + if err := s.Prepare(); err != nil { + return err + } + return a.runCompose(s.Compose(), append([]string{"up", "--detach", "--wait"}, args...)...) + } + return cmd +} + +// pullCmd pulls images across every profile without registering the stack or +// checking require_env. For an unregistered stack, s.Entry.Ports is empty, so +// compose files that guard port mappings with `${AVIARY_PORT_X:?...}` would +// refuse to parse; fill any unallocated configured port with its BasePort +// placeholder for interpolation only. +func (a *app) pullCmd() *cobra.Command { + return &cobra.Command{ + Use: "pull [service...]", + Short: "Pull the stack's images across every profile", + Long: `Pull the stack's images across every profile (or for the named services). +When gcloud Application Default Credentials are available on the host, +CLOUDSDK_AUTH_ACCESS_TOKEN is set from them so Docker's gcloud credential +helper works unattended without an interactive gcloud reauthentication prompt.`, + DisableFlagParsing: true, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) > 0 && (args[0] == "--help" || args[0] == "-h") { + return cmd.Help() + } + s, err := a.loadStack(inspectsOnly) + if err != nil { + return err + } + c := s.Compose() + c.Profiles = []string{"*"} + base := registry.BasePort(s.Workspace.Project()) + for i, name := range s.Config.Ports { + key := "AVIARY_PORT_" + strings.ToUpper(name) + if c.Env[key] == "" { + c.Env[key] = fmt.Sprint(base + i) + } + } + if os.Getenv("CLOUDSDK_AUTH_ACCESS_TOKEN") == "" { + if tok := gcloudADCToken(); tok != "" { + c.Env["CLOUDSDK_AUTH_ACCESS_TOKEN"] = tok + } + } + return a.runCompose(c, append([]string{"pull", "--quiet"}, args...)...) + }, + } +} + +func gcloudADCToken() string { + out, err := exec.Command("gcloud", "auth", "application-default", "print-access-token").Output() + if err != nil { + return "" + } + return strings.TrimSpace(string(out)) +} + +func (a *app) runCmd() *cobra.Command { + cmd := a.passthrough("run [command...]", "Run a one-off command in a fresh container", startsContainers, "run", "--rm") + cmd.RunE = func(cmd *cobra.Command, args []string) error { + s, err := a.loadStack(startsContainers) + if err != nil { + return err + } + if err := s.Prepare(); err != nil { + return err + } + return a.runCompose(s.Compose(), append([]string{"run", "--rm"}, args...)...) + } + return cmd +} + // passthrough makes a command that forwards its arguments to one compose // subcommand, after sub and its defaults, with the stack's project, files, // profiles and ports applied. An empty sub forwards the arguments as the diff --git a/cmd/aviary/main_test.go b/cmd/aviary/main_test.go index f46a63f..c099510 100644 --- a/cmd/aviary/main_test.go +++ b/cmd/aviary/main_test.go @@ -144,7 +144,7 @@ func TestHelpListsEveryCommand(t *testing.T) { if err != nil { t.Fatal(err) } - for _, name := range []string{"up", "down", "test", "init", "run", "exec", "ps", "logs", "build", "compose", "env", "ls", "gc", "rm", "--config"} { + for _, name := range []string{"up", "down", "test", "init", "pull", "run", "exec", "ps", "logs", "build", "compose", "env", "ls", "gc", "rm", "--config"} { if !strings.Contains(out, " "+name+" ") { t.Errorf("help does not list %s:\n%s", name, out) } @@ -741,6 +741,38 @@ func TestComposeDownVolumesMarksTheStackUninitialized(t *testing.T) { } } +func TestPullSuppliesPlaceholderPortsWithoutRegisteringOrRequiringEnv(t *testing.T) { + root := tempDir(t) + writeDefinition(t, root, ` +compose: {files: [compose.yaml]} +ports: [postgres, extapiweb_http] +require_env: [AVIARY_TEST_MISSING] +`) + t.Setenv("AVIARY_TEST_MISSING", "") + t.Chdir(root) + h := newHarness(t) + + if _, err := h.run(t, "pull", "sojourner"); err != nil { + t.Fatalf("aviary pull failed on an unregistered stack: %v", err) + } + if _, err := os.Stat(h.reg.Path()); !os.IsNotExist(err) { + t.Fatalf("aviary pull registered the stack (stat err %v)", err) + } + if len(h.composed) != 1 { + t.Fatalf("compose calls = %v, want 1", h.composed) + } + call := h.composed[0] + if !slices.Equal(call.stack.Profiles, []string{"*"}) { + t.Errorf("pull profiles = %v, want [*]", call.stack.Profiles) + } + if !slices.Equal(call.args, []string{"pull", "--quiet", "sojourner"}) { + t.Errorf("pull args = %v, want [pull --quiet sojourner]", call.args) + } + if call.stack.Env["AVIARY_PORT_EXTAPIWEB_HTTP"] == "" { + t.Errorf("pull env missing placeholder AVIARY_PORT_EXTAPIWEB_HTTP: %v", call.stack.Env) + } +} + func TestComposeSubcommandSkipsGlobalFlags(t *testing.T) { for _, tc := range []struct { args []string diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 6c9e674..7bdd950 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -76,10 +76,30 @@ func (s Stack) Args(sub ...string) []string { return append(argv, sub...) } +// isTerminal reports whether f is a character device (TTY). +func isTerminal(f *os.File) bool { + if f == nil { + return false + } + fi, err := f.Stat() + if err != nil { + return false + } + return fi.Mode()&os.ModeCharDevice != 0 +} + // environ is the inherited environment followed by Env in key order. exec -// keeps the last value of a repeated key, so Env wins over the host. +// keeps the last value of a repeated key, so Env wins over the host. When +// stderr is not a terminal (coding agents, CI, redirected logs) and the caller +// has not set COMPOSE_PROGRESS, it defaults to "quiet" so compose's container +// wait spinners do not bury test output. func (s Stack) environ() []string { env := os.Environ() + if os.Getenv("COMPOSE_PROGRESS") == "" && !isTerminal(os.Stderr) { + if _, ok := s.Env["COMPOSE_PROGRESS"]; !ok { + env = append(env, "COMPOSE_PROGRESS=quiet") + } + } for _, k := range slices.Sorted(maps.Keys(s.Env)) { env = append(env, k+"="+s.Env[k]) } @@ -151,6 +171,23 @@ func runSignals(cmd *exec.Cmd) error { return cmd.Wait() } +// RunHost executes a command directly on the host in dir with extraEnv merged +// over the inherited environment, forwarding signals and returning non-zero +// statuses as *ExitError. +func RunHost(dir string, extraEnv map[string]string, command []string) error { + cmd := exec.Command(command[0], command[1:]...) + cmd.Dir = dir + env := os.Environ() + for _, k := range slices.Sorted(maps.Keys(extraEnv)) { + env = append(env, k+"="+extraEnv[k]) + } + cmd.Env = env + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return exitError(runSignals(cmd)) +} + // exitError turns a non-zero exit into *ExitError. A process killed by a // signal has no status to pass on (ExitCode is -1), so it becomes a plain // error naming the signal, which the CLI prints before exiting 1. diff --git a/internal/config/config.go b/internal/config/config.go index e572c14..82a54ec 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -8,10 +8,12 @@ import ( "bytes" "fmt" "maps" + "net/url" "os" "path/filepath" "regexp" "slices" + "time" "gopkg.in/yaml.v3" ) @@ -27,6 +29,17 @@ type Config struct { // aviary calls compose, so a missing credential fails with a clear // message instead of a service boot failure. RequireEnv []string `yaml:"require_env"` + // Vault, when set, mints and caches a child VAULT_TOKEN from the + // developer's ~/.vault-token when VAULT_TOKEN is absent or expired. + Vault *Vault `yaml:"vault"` + // HostEnv fills unset host environment variables from standard host + // files (such as Google Application Default Credentials) before + // require_env and compose run. + HostEnv map[string]HostFile `yaml:"host_env"` + // Prepare lists host commands run in the worktree root before init and + // before suites that do not opt out, such as copying gitignored API + // artifacts into a fresh worktree. + Prepare []HostStep `yaml:"prepare"` // Init seeds a fresh stack from scratch. It runs once after the stack is // created and again after aviary removes its volumes (`down --volumes` // or `rm`). @@ -42,6 +55,31 @@ type Config struct { Snapshot *Snapshot `yaml:"snapshot"` } +type Vault struct { + // Addr is the Vault server URL, such as https://vault2.faraday.ai. + Addr string `yaml:"addr"` + // TTL is the Go duration requested for minted child tokens (default 8h). + TTL string `yaml:"ttl"` +} + +type HostFile struct { + // Path is a host file path (`~/` expands to the user's home directory) + // assigned to the environment variable when the variable is unset on + // the host and the file exists. + Path string `yaml:"path"` + // Required fails commands that start containers when the variable is + // unset and Path does not exist. + Required bool `yaml:"required"` + // Hint is appended to the error when Required is true and Path is + // missing, telling the user or agent what login command creates it. + Hint string `yaml:"hint"` +} + +// HostStep is a command run on the host in the worktree root. +type HostStep struct { + Command []string `yaml:"command"` +} + type Compose struct { // Files are compose files, relative to the directory holding aviary.yml. Files []string `yaml:"files"` @@ -69,6 +107,19 @@ type Suite struct { // Init is false for suites that run against an empty database and // should not trigger the stack's init steps. Init *bool `yaml:"init"` + // Prepare is false for suites that do not need the stack's host + // prepare steps to run first. + Prepare *bool `yaml:"prepare"` + // Host is true when Command runs directly on the host in Workdir (relative + // to the worktree root) instead of `docker compose run` inside Service, + // with the stack's AVIARY_* environment variables and Suite.Env set. + Host bool `yaml:"host"` + // Workdir is the host working directory, relative to the worktree root, + // when Host is true. Defaults to the worktree root. + Workdir string `yaml:"workdir"` + // Env holds extra environment variables passed to the suite's command + // after `${VAR}` interpolation against the stack's environment. + Env map[string]string `yaml:"env"` } type Snapshot struct { @@ -90,6 +141,11 @@ func (s Suite) WantsInit() bool { return s.Init == nil || *s.Init } +// WantsPrepare reports whether the suite should run the host prepare steps first. +func (s Suite) WantsPrepare() bool { + return s.Prepare == nil || *s.Prepare +} + var ( portName = regexp.MustCompile(`^[a-z][a-z0-9_]*$`) envName = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`) @@ -139,6 +195,24 @@ func (c *Config) validate() error { return fmt.Errorf("require_env name %q must match %s", name, envName) } } + if c.Vault != nil { + if err := c.Vault.validate(); err != nil { + return fmt.Errorf("vault: %w", err) + } + } + for _, name := range slices.Sorted(maps.Keys(c.HostEnv)) { + if !envName.MatchString(name) { + return fmt.Errorf("host_env name %q must match %s", name, envName) + } + if err := c.HostEnv[name].validate(); err != nil { + return fmt.Errorf("host_env[%q]: %w", name, err) + } + } + for i, step := range c.Prepare { + if err := step.validate(); err != nil { + return fmt.Errorf("prepare[%d]: %w", i, err) + } + } for i, step := range c.Init { if err := step.validate(); err != nil { return fmt.Errorf("init[%d]: %w", i, err) @@ -160,6 +234,37 @@ func (c *Config) validate() error { return nil } +func (v Vault) validate() error { + if v.Addr == "" { + return fmt.Errorf("needs addr") + } + u, err := url.Parse(v.Addr) + if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" { + return fmt.Errorf("addr %q must be an http or https URL", v.Addr) + } + if v.TTL != "" { + d, err := time.ParseDuration(v.TTL) + if err != nil || d <= 0 { + return fmt.Errorf("ttl %q must be a positive Go duration (such as 8h)", v.TTL) + } + } + return nil +} + +func (f HostFile) validate() error { + if f.Path == "" { + return fmt.Errorf("needs path") + } + return nil +} + +func (s HostStep) validate() error { + if len(s.Command) == 0 { + return fmt.Errorf("needs a command") + } + return noEmpty("command", s.Command) +} + func (s Step) validate() error { if s.Service == "" { return fmt.Errorf("needs a service") @@ -171,8 +276,31 @@ func (s Step) validate() error { } func (s Suite) validate() error { - if err := (Step{Service: s.Service, Command: s.Command}).validate(); err != nil { - return err + if s.Host { + if s.Service != "" { + return fmt.Errorf("host suite must not set service") + } + if len(s.Command) == 0 { + return fmt.Errorf("needs a command") + } + if err := noEmpty("command", s.Command); err != nil { + return err + } + if filepath.IsAbs(s.Workdir) { + return fmt.Errorf("workdir %q must be relative to the worktree root", s.Workdir) + } + } else { + if s.Workdir != "" { + return fmt.Errorf("workdir is only for host suites") + } + if err := (Step{Service: s.Service, Command: s.Command}).validate(); err != nil { + return err + } + } + for _, name := range slices.Sorted(maps.Keys(s.Env)) { + if !envName.MatchString(name) { + return fmt.Errorf("env name %q must match %s", name, envName) + } } if err := noEmpty("needs", s.Needs); err != nil { return err diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 9ec19c2..c8e557e 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -90,6 +90,49 @@ func TestLoadReadsTheFullSchema(t *testing.T) { } } +func TestLoadReadsVaultHostEnvPrepareAndHostSuites(t *testing.T) { + cfg, err := Load(write(t, `compose: + files: [compose.yaml] +vault: + addr: https://vault2.faraday.ai + ttl: 8h +host_env: + FDY_GOOGLE_APPLICATION_CREDENTIALS_HOST_PATH: + path: ~/.config/gcloud/application_default_credentials.json + required: true + hint: run gcloud auth application-default login +prepare: + - command: [node, scripts/copy-api-artifacts.mts, --all] +suites: + investigator: + host: true + workdir: src/investigator + init: false + prepare: false + needs: [postgres] + env: + INVESTIGATOR_TEST_DATABASE_URL: postgresql://postgres@127.0.0.1:${AVIARY_PORT_POSTGRES}/postgres + command: [uv, run, pytest] +`)) + if err != nil { + t.Fatal(err) + } + if cfg.Vault == nil || cfg.Vault.Addr != "https://vault2.faraday.ai" || cfg.Vault.TTL != "8h" { + t.Errorf("vault = %+v", cfg.Vault) + } + hf := cfg.HostEnv["FDY_GOOGLE_APPLICATION_CREDENTIALS_HOST_PATH"] + if !hf.Required || hf.Hint != "run gcloud auth application-default login" { + t.Errorf("host_env = %+v", hf) + } + if len(cfg.Prepare) != 1 || !reflect.DeepEqual(cfg.Prepare[0].Command, []string{"node", "scripts/copy-api-artifacts.mts", "--all"}) { + t.Errorf("prepare = %+v", cfg.Prepare) + } + inv := cfg.Suites["investigator"] + if !inv.Host || inv.Workdir != "src/investigator" || inv.WantsInit() || inv.WantsPrepare() { + t.Errorf("investigator suite = %+v", inv) + } +} + func TestFullSchemaRoundTrips(t *testing.T) { cfg, err := Load(write(t, fullExample)) if err != nil { @@ -202,6 +245,14 @@ func TestLoadRejectsBadInput(t *testing.T) { "require_env starting with a digit": {base + "require_env: [1TOKEN]\n", `require_env name "1TOKEN"`}, "require_env empty name": {base + "require_env: [\"\"]\n", `require_env name ""`}, "require_env with an equals sign": {base + "require_env: [A=B]\n", `require_env name "A=B"`}, + "vault without addr": {base + "vault: {ttl: 8h}\n", "vault: needs addr"}, + "vault with bad addr": {base + "vault: {addr: vault2.faraday.ai}\n", "must be an http or https URL"}, + "vault with negative ttl": {base + "vault: {addr: https://vault.test, ttl: -1h}\n", "positive Go duration"}, + "host_env without path": {base + "host_env:\n FOO: {required: true}\n", `host_env["FOO"]: needs path`}, + "prepare without command": {base + "prepare:\n - {}\n", "prepare[0]: needs a command"}, + "host suite with service": {base + "suites:\n s: {host: true, service: api, command: [t]}\n", "host suite must not set service"}, + "container suite with workdir": {base + "suites:\n s: {service: api, workdir: src/x, command: [t]}\n", "workdir is only for host suites"}, + "host suite with abs workdir": {base + "suites:\n s: {host: true, workdir: /abs, command: [t]}\n", "must be relative"}, } for name, c := range cases { _, err := Load(write(t, c.body)) diff --git a/internal/stack/stack.go b/internal/stack/stack.go index 9acaf82..2ecd0c6 100644 --- a/internal/stack/stack.go +++ b/internal/stack/stack.go @@ -18,6 +18,7 @@ import ( "github.com/faradayio/aviary/internal/compose" "github.com/faradayio/aviary/internal/config" "github.com/faradayio/aviary/internal/registry" + "github.com/faradayio/aviary/internal/vault" "github.com/faradayio/aviary/internal/workspace" ) @@ -30,17 +31,23 @@ type Stack struct { // registered. Entry registry.Entry registry *registry.Registry + // resolvedEnv holds credentials and host file paths resolved during + // Load (Vault token, host_env paths), added to compose invocations + // without appearing in `aviary env` output. + resolvedEnv map[string]string // runner is Options.Runner. runner func(c compose.Stack, args ...string) error + // hostRunner is Options.HostRunner. + hostRunner func(dir string, extraEnv map[string]string, command []string) error // stderr receives aviary's progress lines for init steps. stderr io.Writer } // Options adjusts how Load builds a stack. type Options struct { - // SkipRequireEnv loads the stack without checking require_env, for - // commands that never start or run a container and so never hand the - // required variables to one. + // SkipRequireEnv loads the stack without resolving vault/host_env or + // checking require_env, for commands that never start or run a + // container and so never hand the required variables to one. SkipRequireEnv bool // NoAllocate reads the stack's registry entry instead of allocating, // for commands that only inspect or stop a stack and so must not @@ -53,14 +60,19 @@ type Options struct { // defaults to compose.Stack.Run; tests replace it to record argv // instead of calling Docker. Runner func(c compose.Stack, args ...string) error + // HostRunner runs a command on the host in dir. It defaults to + // compose.RunHost; tests replace it to record host commands. + HostRunner func(dir string, extraEnv map[string]string, command []string) error + // VaultToken overrides Vault token minting during tests. When nil and + // cfg.Vault is set, vault.NewClient(cfg.Vault).Token is called. + VaultToken func(cfg *config.Vault, project string) (string, error) } // Load builds the stack for the worktree containing cwd, allocating any host // ports it does not have yet unless opts.NoAllocate is set. configDir is the // external definition directory from --config or AVIARY_CONFIG, or empty when -// neither is set. Unless opts.SkipRequireEnv is set, every name in -// require_env must be set in the host environment; otherwise Load fails -// listing all the missing names, before touching the registry. +// neither is set. Unless opts.SkipRequireEnv is set, vault, host_env and +// require_env are resolved and checked before touching the registry. func Load(cwd, configDir string, opts Options) (*Stack, error) { ws, err := workspace.Detect(cwd, configDir) if err != nil { @@ -70,9 +82,26 @@ func Load(cwd, configDir string, opts Options) (*Stack, error) { if err != nil { return nil, err } + defFile := filepath.Join(ws.Definition, config.FileName) + resolvedEnv := map[string]string{} if !opts.SkipRequireEnv { - if err := checkRequiredEnv(cfg.RequireEnv); err != nil { - return nil, fmt.Errorf("%s: %w", filepath.Join(ws.Definition, config.FileName), err) + if cfg.Vault != nil { + mint := opts.VaultToken + if mint == nil { + mint = defaultVaultToken + } + token, err := mint(cfg.Vault, ws.Project()) + if err != nil { + return nil, fmt.Errorf("%s: %w", defFile, err) + } + resolvedEnv["VAULT_ADDR"] = cfg.Vault.Addr + resolvedEnv["VAULT_TOKEN"] = token + } + if err := resolveHostEnv(cfg.HostEnv, resolvedEnv); err != nil { + return nil, fmt.Errorf("%s: %w", defFile, err) + } + if err := checkRequiredEnv(cfg.RequireEnv, resolvedEnv); err != nil { + return nil, fmt.Errorf("%s: %w", defFile, err) } } reg := opts.Registry @@ -95,15 +124,75 @@ func Load(cwd, configDir string, opts Options) (*Stack, error) { if runner == nil { runner = compose.Stack.Run } - return &Stack{Config: cfg, Workspace: ws, Entry: entry, registry: reg, runner: runner, stderr: os.Stderr}, nil + hostRunner := opts.HostRunner + if hostRunner == nil { + hostRunner = compose.RunHost + } + return &Stack{ + Config: cfg, + Workspace: ws, + Entry: entry, + registry: reg, + resolvedEnv: resolvedEnv, + runner: runner, + hostRunner: hostRunner, + stderr: os.Stderr, + }, nil +} + +func defaultVaultToken(cfg *config.Vault, project string) (string, error) { + client, err := vault.NewClient(cfg) + if err != nil { + return "", err + } + return client.Token(project) +} + +func resolveHostEnv(specs map[string]config.HostFile, out map[string]string) error { + for _, name := range slices.Sorted(maps.Keys(specs)) { + if os.Getenv(name) != "" || out[name] != "" { + continue + } + spec := specs[name] + expanded, err := expandHome(spec.Path) + if err != nil { + return err + } + if info, err := os.Stat(expanded); err == nil && !info.IsDir() { + out[name] = expanded + continue + } + if spec.Required { + msg := fmt.Sprintf("host_env %s: %s does not exist", name, expanded) + if spec.Hint != "" { + msg += " (" + spec.Hint + ")" + } + return fmt.Errorf("%s", msg) + } + } + return nil +} + +func expandHome(path string) (string, error) { + if path == "~" || strings.HasPrefix(path, "~/") { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + if path == "~" { + return home, nil + } + return filepath.Join(home, path[2:]), nil + } + return path, nil } // checkRequiredEnv treats an empty variable as missing: an empty credential // fails the same way an unset one does, only later and less clearly. -func checkRequiredEnv(names []string) error { +func checkRequiredEnv(names []string, resolved map[string]string) error { var missing []string for _, name := range names { - if os.Getenv(name) == "" { + if os.Getenv(name) == "" && resolved[name] == "" { missing = append(missing, name) } } @@ -113,8 +202,10 @@ func checkRequiredEnv(names []string) error { return nil } -// Env is the environment aviary adds for compose interpolation and for -// processes that want to reach the stack from the host. +// Env is the public environment aviary adds for compose interpolation and for +// processes that want to reach the stack from the host (`aviary env`). Secrets +// such as VAULT_TOKEN are deliberately excluded so `aviary env` never prints +// credentials. func (s *Stack) Env() map[string]string { env := map[string]string{ "COMPOSE_PROJECT_NAME": s.Workspace.Project(), @@ -131,6 +222,20 @@ func (s *Stack) Env() map[string]string { return env } +func (s *Stack) composeEnv(extra map[string]string) map[string]string { + env := s.Env() + maps.Copy(env, s.resolvedEnv) + for k, v := range extra { + env[k] = os.Expand(v, func(name string) string { + if val, ok := env[name]; ok { + return val + } + return os.Getenv(name) + }) + } + return env +} + // EnvLines renders Env as KEY=VALUE lines sorted by key. func (s *Stack) EnvLines() []string { return s.envLines(func(k, v string) string { return k + "=" + v }) @@ -156,7 +261,7 @@ func (s *Stack) envLines(line func(k, v string) string) []string { } // Compose is the compose invocation for this stack with the stack-wide -// profiles. +// profiles and any resolved credentials. func (s *Stack) Compose() compose.Stack { return compose.Stack{ Project: s.Workspace.Project(), @@ -164,7 +269,7 @@ func (s *Stack) Compose() compose.Stack { Definition: s.Workspace.Definition, Files: s.Config.Compose.Files, Profiles: s.Config.Compose.Profiles, - Env: s.Env(), + Env: s.composeEnv(nil), } } @@ -176,6 +281,17 @@ func (s *Stack) run(extraProfiles []string, args ...string) error { return s.runner(c, args...) } +// Prepare runs the host prepare steps from aviary.yml in the worktree root. +func (s *Stack) Prepare() error { + for i, step := range s.Config.Prepare { + desc := strings.Join(step.Command, " ") + if err := s.hostRunner(s.Workspace.Root, s.composeEnv(nil), step.Command); err != nil { + return fmt.Errorf("prepare step %d (%s) failed: %v", i+1, desc, err) + } + } + return nil +} + // Init seeds the stack's volumes by running each init step in a fresh // container, in order. Unless force is set, it is a no-op once init has run // since the stack was created or since aviary last removed its volumes @@ -196,6 +312,9 @@ func (s *Stack) Init(force bool) error { if s.Entry.InitializedAt != nil && !force { return nil } + if err := s.Prepare(); err != nil { + return err + } if err := s.runSteps("init", s.Config.Init); err != nil { return err } @@ -230,11 +349,11 @@ func seconds(d time.Duration) string { return fmt.Sprintf("%.1fs", d.Seconds()) } -// Test runs the named suite: init first if the suite wants it, then its -// needs brought up and healthy, then the suite's command in a fresh -// container with extra appended (a leading "--" is dropped). The stack-wide -// and suite profiles apply to both compose calls. A suite that fails returns -// its status as a *compose.ExitError. +// Test runs the named suite: prepare steps if wanted, init if wanted, then its +// needs brought up and healthy, then the suite's command in a fresh container +// (or on the host when suite.Host is true) with extra appended (a leading "--" +// is dropped). The stack-wide and suite profiles apply to both compose calls. +// A suite that fails returns its status as a *compose.ExitError. func (s *Stack) Test(name string, extra []string) error { suite, ok := s.Config.Suites[name] if !ok { @@ -244,6 +363,11 @@ func (s *Stack) Test(name string, extra []string) error { } return fmt.Errorf("no suite %q; available: %s", name, strings.Join(names, ", ")) } + if suite.WantsPrepare() { + if err := s.Prepare(); err != nil { + return err + } + } if suite.WantsInit() { if err := s.Init(false); err != nil { return err @@ -258,7 +382,19 @@ func (s *Stack) Test(name string, extra []string) error { if len(extra) > 0 && extra[0] == "--" { extra = extra[1:] } - args := slices.Concat([]string{"run", "--rm", suite.Service}, suite.Command, extra) + if suite.Host { + dir := s.Workspace.Root + if suite.Workdir != "" { + dir = filepath.Join(dir, suite.Workdir) + } + return s.hostRunner(dir, s.composeEnv(suite.Env), slices.Concat(suite.Command, extra)) + } + envFlags := make([]string, 0, len(suite.Env)*2) + resolvedSuiteEnv := s.composeEnv(suite.Env) + for _, k := range slices.Sorted(maps.Keys(suite.Env)) { + envFlags = append(envFlags, "--env", k+"="+resolvedSuiteEnv[k]) + } + args := slices.Concat([]string{"run", "--rm"}, envFlags, []string{suite.Service}, suite.Command, extra) return s.run(suite.Profiles, args...) } diff --git a/internal/stack/stack_test.go b/internal/stack/stack_test.go index 58b063b..c96794e 100644 --- a/internal/stack/stack_test.go +++ b/internal/stack/stack_test.go @@ -728,3 +728,90 @@ func TestRemoveVolumesWaitsForARunningInit(t *testing.T) { t.Error("volumes removed after init finished, but the stack is still marked initialized") } } + +func TestVaultAndHostEnvPassToComposeWithoutLeakingToEnvOutput(t *testing.T) { + root := tempDir(t) + adc := filepath.Join(root, "adc.json") + if err := os.WriteFile(adc, []byte("{}"), 0o600); err != nil { + t.Fatal(err) + } + writeDefinition(t, root, `compose: + files: [compose.yaml] +ports: [postgres] +require_env: [VAULT_TOKEN, ADC_PATH] +vault: + addr: https://vault2.faraday.ai +host_env: + ADC_PATH: + path: `+adc+` + required: true +`) + unset(t, "VAULT_TOKEN") + unset(t, "ADC_PATH") + + s, err := Load(root, "", Options{ + Registry: testRegistry(t), + VaultToken: func(cfg *config.Vault, project string) (string, error) { + return "secret-child-token", nil + }, + }) + if err != nil { + t.Fatal(err) + } + if _, leaked := s.Env()["VAULT_TOKEN"]; leaked { + t.Error("s.Env() leaked VAULT_TOKEN") + } + c := s.Compose() + if c.Env["VAULT_TOKEN"] != "secret-child-token" || c.Env["VAULT_ADDR"] != "https://vault2.faraday.ai" || c.Env["ADC_PATH"] != adc { + t.Errorf("Compose().Env = %v", c.Env) + } +} + +func TestPrepareAndHostSuiteExpandPortVariables(t *testing.T) { + noInit := false + cfg := &config.Config{ + Compose: config.Compose{Files: []string{"compose.yaml"}}, + Ports: []string{"postgres"}, + Prepare: []config.HostStep{{Command: []string{"node", "scripts/copy-api-artifacts.mts", "--all"}}}, + Suites: map[string]config.Suite{ + "investigator": { + Host: true, + Workdir: "src/investigator", + Needs: []string{"postgres"}, + Init: &noInit, + Env: map[string]string{ + "INVESTIGATOR_TEST_DATABASE_URL": "postgresql://postgres@127.0.0.1:${AVIARY_PORT_POSTGRES}/postgres", + }, + Command: []string{"uv", "run", "pytest"}, + }, + }, + } + s, rec := newStack(t, cfg) + type hostCall struct { + dir string + env map[string]string + cmd []string + } + var hostCalls []hostCall + s.hostRunner = func(dir string, extraEnv map[string]string, command []string) error { + hostCalls = append(hostCalls, hostCall{dir, maps.Clone(extraEnv), slices.Clone(command)}) + return nil + } + + if err := s.Test("investigator", []string{"--", "-k", "runs"}); err != nil { + t.Fatal(err) + } + assertCalls(t, rec, argv(nil, "up", "--detach", "--wait", "postgres")) + if len(hostCalls) != 2 { + t.Fatalf("hostCalls = %+v, want prepare + suite", hostCalls) + } + if hostCalls[0].dir != "/work/wt" || !slices.Equal(hostCalls[0].cmd, []string{"node", "scripts/copy-api-artifacts.mts", "--all"}) { + t.Errorf("prepare call = %+v", hostCalls[0]) + } + wantPort := strconv.Itoa(s.Entry.Ports["postgres"]) + if hostCalls[1].dir != "/work/wt/src/investigator" || + hostCalls[1].env["INVESTIGATOR_TEST_DATABASE_URL"] != "postgresql://postgres@127.0.0.1:"+wantPort+"/postgres" || + !slices.Equal(hostCalls[1].cmd, []string{"uv", "run", "pytest", "-k", "runs"}) { + t.Errorf("suite call = %+v", hostCalls[1]) + } +} diff --git a/internal/vault/vault.go b/internal/vault/vault.go new file mode 100644 index 0000000..5108488 --- /dev/null +++ b/internal/vault/vault.go @@ -0,0 +1,302 @@ +// Package vault mints and caches scoped child Vault tokens from a developer's +// local Vault login (`~/.vault-token`) or an ambient VAULT_TOKEN, so stacks +// never need manual token exports in every shell. +package vault + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "strings" + "time" + + "github.com/gofrs/flock" + + "github.com/faradayio/aviary/internal/config" +) + +// DefaultTTL is the lifetime requested for child tokens when aviary.yml omits +// vault.ttl. +const DefaultTTL = 8 * time.Hour + +// Client talks to one Vault server and keeps a locked on-disk cache of minted +// child tokens. +type Client struct { + Addr string + TTL time.Duration + CacheDir string + HomeDir string + HTTPClient *http.Client + Now func() time.Time +} + +type cachedToken struct { + Addr string `json:"addr"` + Token string `json:"token"` + ExpiresAt time.Time `json:"expires_at"` +} + +type createTokenRequest struct { + TTL string `json:"ttl"` + Renewable bool `json:"renewable"` + DisplayName string `json:"display_name"` +} + +type vaultResponse struct { + Auth *struct { + ClientToken string `json:"client_token"` + LeaseDuration int `json:"lease_duration"` + } `json:"auth"` + Data *struct { + TTL int `json:"ttl"` + } `json:"data"` + Errors []string `json:"errors"` +} + +// NewClient builds a Vault client from the aviary.yml vault section. +func NewClient(cfg *config.Vault) (*Client, error) { + cacheDir, err := defaultCacheDir() + if err != nil { + return nil, err + } + home, err := os.UserHomeDir() + if err != nil { + return nil, err + } + ttl := DefaultTTL + if cfg.TTL != "" { + parsed, err := time.ParseDuration(cfg.TTL) + if err != nil { + return nil, fmt.Errorf("vault.ttl %q: %w", cfg.TTL, err) + } + ttl = parsed + } + return &Client{ + Addr: strings.TrimRight(cfg.Addr, "/"), + TTL: ttl, + CacheDir: cacheDir, + HomeDir: home, + HTTPClient: &http.Client{Timeout: 15 * time.Second}, + Now: time.Now, + }, nil +} + +func defaultCacheDir() (string, error) { + dir := os.Getenv("XDG_STATE_HOME") + if !filepath.IsAbs(dir) { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + dir = filepath.Join(home, ".local", "state") + } + return filepath.Join(dir, "aviary"), nil +} + +// Token returns a live Vault token for services to read secrets with. +// +// Resolution order: +// 1. A non-empty VAULT_TOKEN in env that Vault accepts on lookup-self (used as +// is, for CI or explicit overrides). If VAULT_TOKEN is set in env AND +// `~/.vault-token` exists, an expired env token falls through to minting +// from `~/.vault-token` because agent shells frequently inherit stale +// VAULT_TOKEN exports. If no `~/.vault-token` exists, an invalid env token +// fails immediately. +// 2. A cached child token on disk for this Vault server with more than half +// of c.TTL remaining and accepted by lookup-self. +// 3. A fresh non-renewable child token minted from `~/.vault-token` and saved +// to the cache with mode 0600. +func (c *Client) Token(project string) (string, error) { + envToken := strings.TrimSpace(os.Getenv("VAULT_TOKEN")) + homeToken := c.readHomeToken() + + if envToken != "" { + if ok, err := c.validToken(envToken); err != nil { + return "", err + } else if ok { + return envToken, nil + } + if homeToken == "" { + return "", fmt.Errorf("VAULT_TOKEN in the environment is expired or invalid, and %s does not exist; run `VAULT_ADDR=%s vault login -method=oidc`", c.homeTokenPath(), c.Addr) + } + } + + if err := os.MkdirAll(c.CacheDir, 0o700); err != nil { + return "", err + } + cachePath := filepath.Join(c.CacheDir, "vault-token.json") + lock := flock.New(cachePath + ".lock") + if err := lock.Lock(); err != nil { + return "", fmt.Errorf("lock %s: %w", lock.Path(), err) + } + defer lock.Unlock() + + if cached, ok := c.loadCached(cachePath); ok { + if valid, err := c.validToken(cached.Token); err != nil { + return "", err + } else if valid { + return cached.Token, nil + } + } + + if homeToken == "" { + return "", fmt.Errorf("no Vault login found at %s; ask the user to run `VAULT_ADDR=%s vault login -method=oidc`", c.homeTokenPath(), c.Addr) + } + + minted, err := c.mintChild(homeToken, project) + if err != nil { + return "", err + } + if err := c.saveCached(cachePath, minted); err != nil { + return "", err + } + return minted.Token, nil +} + +func (c *Client) homeTokenPath() string { + return filepath.Join(c.HomeDir, ".vault-token") +} + +func (c *Client) readHomeToken() string { + raw, err := os.ReadFile(c.homeTokenPath()) + if err != nil { + return "" + } + return strings.TrimSpace(string(raw)) +} + +func (c *Client) loadCached(path string) (cachedToken, bool) { + raw, err := os.ReadFile(path) + if err != nil { + return cachedToken{}, false + } + var tok cachedToken + if err := json.Unmarshal(raw, &tok); err != nil { + return cachedToken{}, false + } + if tok.Addr != c.Addr || tok.Token == "" { + return cachedToken{}, false + } + // Refresh once less than half the desired TTL remains so a long test suite + // does not start with a token about to expire. + if !c.Now().Add(c.TTL / 2).Before(tok.ExpiresAt) { + return cachedToken{}, false + } + return tok, true +} + +func (c *Client) saveCached(path string, tok cachedToken) error { + raw, err := json.MarshalIndent(tok, "", " ") + if err != nil { + return err + } + tmp := path + ".tmp" + if err := writeSyncedSecret(tmp, raw); err != nil { + return err + } + if err := os.Rename(tmp, path); err != nil { + return err + } + return syncDir(filepath.Dir(path)) +} + +func writeSyncedSecret(path string, raw []byte) error { + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) + if err != nil { + return err + } + if _, err := f.Write(raw); err != nil { + f.Close() + return err + } + if err := f.Sync(); err != nil { + f.Close() + return err + } + return f.Close() +} + +func syncDir(dir string) error { + d, err := os.Open(dir) + if err != nil { + return err + } + defer d.Close() + return d.Sync() +} + +func (c *Client) validToken(token string) (bool, error) { + req, err := http.NewRequest(http.MethodGet, c.Addr+"/v1/auth/token/lookup-self", nil) + if err != nil { + return false, err + } + req.Header.Set("X-Vault-Token", token) + resp, err := c.HTTPClient.Do(req) + if err != nil { + return false, fmt.Errorf("reaching Vault at %s: %w", c.Addr, err) + } + defer resp.Body.Close() + _, _ = io.Copy(io.Discard, resp.Body) + if resp.StatusCode == http.StatusOK { + return true, nil + } + if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { + return false, nil + } + return false, fmt.Errorf("Vault lookup-self at %s returned HTTP %d", c.Addr, resp.StatusCode) +} + +func (c *Client) mintChild(parentToken, project string) (cachedToken, error) { + body, err := json.Marshal(createTokenRequest{ + TTL: fmt.Sprintf("%ds", int(c.TTL.Seconds())), + Renewable: false, + DisplayName: "aviary-" + project, + }) + if err != nil { + return cachedToken{}, err + } + req, err := http.NewRequest(http.MethodPost, c.Addr+"/v1/auth/token/create", bytes.NewReader(body)) + if err != nil { + return cachedToken{}, err + } + req.Header.Set("X-Vault-Token", parentToken) + req.Header.Set("Content-Type", "application/json") + resp, err := c.HTTPClient.Do(req) + if err != nil { + return cachedToken{}, fmt.Errorf("reaching Vault at %s: %w", c.Addr, err) + } + defer resp.Body.Close() + raw, err := io.ReadAll(resp.Body) + if err != nil { + return cachedToken{}, err + } + var parsed vaultResponse + _ = json.Unmarshal(raw, &parsed) + if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { + detail := strings.Join(parsed.Errors, "; ") + if detail == "" { + detail = fmt.Sprintf("HTTP %d", resp.StatusCode) + } + return cachedToken{}, fmt.Errorf("Vault login at %s is expired or cannot create child tokens (%s); ask the user to run `VAULT_ADDR=%s vault login -method=oidc`", c.homeTokenPath(), detail, c.Addr) + } + if resp.StatusCode != http.StatusOK { + return cachedToken{}, fmt.Errorf("Vault token create at %s returned HTTP %d: %s", c.Addr, resp.StatusCode, strings.TrimSpace(string(raw))) + } + if parsed.Auth == nil || parsed.Auth.ClientToken == "" { + return cachedToken{}, errors.New("Vault token create returned an empty client_token") + } + lease := c.TTL + if parsed.Auth.LeaseDuration > 0 { + lease = time.Duration(parsed.Auth.LeaseDuration) * time.Second + } + return cachedToken{ + Addr: c.Addr, + Token: parsed.Auth.ClientToken, + ExpiresAt: c.Now().UTC().Add(lease), + }, nil +} diff --git a/internal/vault/vault_test.go b/internal/vault/vault_test.go new file mode 100644 index 0000000..be0526d --- /dev/null +++ b/internal/vault/vault_test.go @@ -0,0 +1,164 @@ +package vault + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "sync/atomic" + "testing" + "time" +) + +type fakeVault struct { + server *httptest.Server + valid map[string]bool + minted atomic.Int32 + mintTTL int +} + +func newFakeVault(t *testing.T, valid map[string]bool) *fakeVault { + t.Helper() + fv := &fakeVault{valid: valid, mintTTL: 28800} + fv.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + tok := r.Header.Get("X-Vault-Token") + switch r.URL.Path { + case "/v1/auth/token/lookup-self": + if fv.valid[tok] { + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"data":{"ttl":3600}}`)) + return + } + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte(`{"errors":["permission denied"]}`)) + case "/v1/auth/token/create": + if !fv.valid[tok] { + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte(`{"errors":["invalid parent token"]}`)) + return + } + var req createTokenRequest + _ = json.NewDecoder(r.Body).Decode(&req) + if req.Renewable || !strings.HasPrefix(req.DisplayName, "aviary-") { + w.WriteHeader(http.StatusBadRequest) + return + } + n := fv.minted.Add(1) + child := "child-token-" + string(rune('0'+n)) + fv.valid[child] = true + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(map[string]any{ + "auth": map[string]any{ + "client_token": child, + "lease_duration": fv.mintTTL, + }, + }) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(fv.server.Close) + return fv +} + +func testClient(t *testing.T, fv *fakeVault, now time.Time) *Client { + t.Helper() + return &Client{ + Addr: fv.server.URL, + TTL: 8 * time.Hour, + CacheDir: t.TempDir(), + HomeDir: t.TempDir(), + HTTPClient: fv.server.Client(), + Now: func() time.Time { return now }, + } +} + +func TestTokenMintsFromHomeTokenAndCachesAcrossCalls(t *testing.T) { + t.Setenv("VAULT_TOKEN", "") + fv := newFakeVault(t, map[string]bool{"parent-token": true}) + now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC) + c := testClient(t, fv, now) + if err := os.WriteFile(filepath.Join(c.HomeDir, ".vault-token"), []byte("parent-token\n"), 0o600); err != nil { + t.Fatal(err) + } + + tok1, err := c.Token("aviary-fdy-abc123") + if err != nil { + t.Fatal(err) + } + if tok1 != "child-token-1" || fv.minted.Load() != 1 { + t.Fatalf("tok1 = %q, minted = %d", tok1, fv.minted.Load()) + } + + // Still more than half TTL remaining: reuses the cached child token. + c.Now = func() time.Time { return now.Add(3 * time.Hour) } + tok2, err := c.Token("aviary-fdy-abc123") + if err != nil { + t.Fatal(err) + } + if tok2 != tok1 || fv.minted.Load() != 1 { + t.Fatalf("tok2 = %q, minted = %d; want cached token reused", tok2, fv.minted.Load()) + } + + // Past half TTL: mints a fresh child token. + c.Now = func() time.Time { return now.Add(5 * time.Hour) } + tok3, err := c.Token("aviary-fdy-abc123") + if err != nil { + t.Fatal(err) + } + if tok3 != "child-token-2" || fv.minted.Load() != 2 { + t.Fatalf("tok3 = %q, minted = %d; want renewed child token", tok3, fv.minted.Load()) + } +} + +func TestStaleEnvVaultTokenFallsBackToHomeToken(t *testing.T) { + t.Setenv("VAULT_TOKEN", "expired-agent-shell-token") + fv := newFakeVault(t, map[string]bool{"parent-token": true}) + c := testClient(t, fv, time.Now().UTC()) + if err := os.WriteFile(filepath.Join(c.HomeDir, ".vault-token"), []byte("parent-token"), 0o600); err != nil { + t.Fatal(err) + } + + tok, err := c.Token("aviary-fdy-abc123") + if err != nil { + t.Fatal(err) + } + if tok != "child-token-1" { + t.Fatalf("tok = %q, want minted child token", tok) + } +} + +func TestValidEnvVaultTokenWinsWithoutMinting(t *testing.T) { + t.Setenv("VAULT_TOKEN", "ci-provided-token") + fv := newFakeVault(t, map[string]bool{"ci-provided-token": true}) + c := testClient(t, fv, time.Now().UTC()) + + tok, err := c.Token("aviary-fdy-abc123") + if err != nil { + t.Fatal(err) + } + if tok != "ci-provided-token" || fv.minted.Load() != 0 { + t.Fatalf("tok = %q, minted = %d; want env token used directly", tok, fv.minted.Load()) + } +} + +func TestMissingOrExpiredLoginInstructsVaultLogin(t *testing.T) { + t.Setenv("VAULT_TOKEN", "") + fv := newFakeVault(t, map[string]bool{}) + c := testClient(t, fv, time.Now().UTC()) + + _, err := c.Token("aviary-fdy-abc123") + if err == nil || !strings.Contains(err.Error(), "vault login -method=oidc") { + t.Fatalf("missing ~/.vault-token err = %v, want oidc login hint", err) + } + + if err := os.WriteFile(filepath.Join(c.HomeDir, ".vault-token"), []byte("expired-parent"), 0o600); err != nil { + t.Fatal(err) + } + _, err = c.Token("aviary-fdy-abc123") + if err == nil || !strings.Contains(err.Error(), "vault login -method=oidc") { + t.Fatalf("expired ~/.vault-token err = %v, want oidc login hint", err) + } +} From b57cd71d2152e13cfaa8ba5511a4b6dfb4810a96 Mon Sep 17 00:00:00 2001 From: Seamus Abshere Date: Mon, 16 Nov 2026 13:31:37 -0500 Subject: [PATCH 2/4] fix: trust cached Vault tokens offline, prepare once under the stack lock - Use a cached child token by its recorded expiry without asking Vault, and reuse it until half of the lease Vault granted has passed, so a login near its end no longer mints a token on every command. - Pass VAULT_TOKEN through unchecked when there is no ~/.vault-token to fall back to; warn when Vault grants less than vault.ttl. - Name the shared child token "aviary" and say plainly that it carries every policy of the parent login. - Run prepare steps at most once per command and under the stack lock, so `aviary test` no longer prepares twice and parallel commands never race. - Pass container suite env through compose's environment with `--env KEY`, keeping values out of the process list; `$$` is a literal `$`, and env values no longer depend on map order. - `aviary pull` only runs gcloud when an ADC file exists. - README and `aviary test --help` cover pull, prepare, host suites and Vault. Co-authored-by: Cursor --- README.md | 32 +++++++++---- cmd/aviary/main.go | 26 +++++++++-- internal/stack/stack.go | 55 ++++++++++++++++------ internal/stack/stack_test.go | 69 ++++++++++++++++++++++++++- internal/vault/vault.go | 80 ++++++++++++++++++-------------- internal/vault/vault_test.go | 90 ++++++++++++++++++++++++++++++++---- 6 files changed, 281 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index f9bf02d..e86afa1 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ the first or has to be started by hand with different settings. | One stack per machine, fixed host ports | One stack per worktree, a port set per worktree | | Writes into the checkout (`pods/`, mounted sources) | Writes nothing into the worktree; sources are bind-mounted by your compose file | | Pods, targets and overlays | Compose profiles and extra compose files | -| Mints a Vault token per service | Checks that the variables in `require_env` are set and passes them through | +| Mints a Vault token per service | Mints one Vault token shared by every stack, and checks `require_env` | | Build and deploy path for production | Local development and testing only | cage remains the deploy tool. aviary replaces it only for running a repository @@ -186,12 +186,18 @@ Commands that create containers (`up`, `test`, `init`, `run`, `build`, and `aviary compose up` or `run`) resolve credentials in this order before calling compose: -1. **`vault`**: if `VAULT_TOKEN` is set in the environment and accepted by - `lookup-self`, aviary uses it as-is. Otherwise it mints a non-renewable - child token from `~/.vault-token`, caches it under +1. **`vault`**: a `VAULT_TOKEN` already in the environment wins. When + `~/.vault-token` also exists, aviary first checks the variable with + `lookup-self` and falls back to `~/.vault-token` if Vault rejects it, since + agent shells often inherit stale exports. Otherwise aviary mints a + non-renewable child token from `~/.vault-token`, caches it under `$XDG_STATE_HOME/aviary/vault-token.json` (`0600`), and reuses it across - worktrees and shells until less than half of `ttl` remains. An expired - `VAULT_TOKEN` in an agent shell falls back to `~/.vault-token` automatically. + worktrees and shells until half of its lease has passed. aviary trusts the + cached token's recorded expiry without asking Vault, so commands work + offline while it lasts; after `vault token revoke`, delete the cache file. + The child token has every policy your login has, and Vault caps its lease + at your login's remaining time, so aviary warns when Vault grants less + than `ttl`. `VAULT_ADDR` is set to `vault.addr`, overriding the shell's. `VAULT_TOKEN` is passed to compose subprocesses and never printed by `aviary env`. 2. **`host_env`**: each unset variable is filled with the expanded `path` when @@ -210,7 +216,9 @@ prepare: Commands that run code from the worktree (`up`, `init`, `run`, and any suite without `prepare: false`) run `prepare` on the host in the worktree root first, so gitignored build/codegen inputs exist in fresh worktrees without an extra -step. +step. The steps run at most once per command and never in two aviary commands +in one worktree at once. Init runs them even for a suite with +`prepare: false`, because init steps may need their output. ### Definitions outside the worktree @@ -283,8 +291,12 @@ suites: if init has not run for this stack yet (skipped with `init: false`); 3. starts `postgres` and `redis` and waits until they are healthy; 4. runs `npm test` in a fresh `api` container (or directly on the host in - `workdir` when `host: true`, with `env` interpolated against the stack's - `AVIARY_*` variables). + `workdir` when `host: true`). + +A suite's `env` is interpolated against the stack's `AVIARY_*` variables and +resolved credentials, then the host environment; `$$` is a literal `$`. For +container suites the values reach compose through its environment, not its +command line, so they never show up in `ps`. Arguments after the suite name, with or without a `--`, are appended to its command, so `aviary test api -- --grep users` and `aviary test api --help` @@ -509,7 +521,7 @@ and those of similar compose wrappers, map like this: | `exec`, `shell` | `aviary exec ` | | `test` | `aviary test `, with the suites named in `aviary.yml` | | `logs`, `build` | `aviary logs`, `aviary build` | -| `pull` | `aviary compose pull` | +| `pull` | `aviary pull [service...]` | | init, seed | `aviary init`; `aviary down --volumes` first to start from empty volumes | | source mount, unmount | none: the compose files bind-mount the worktree's sources, so each stack always runs its own code | | targets, environments | more files under `compose.files`, or [profiles](#profiles) | diff --git a/cmd/aviary/main.go b/cmd/aviary/main.go index 8f7d569..6cf6bae 100644 --- a/cmd/aviary/main.go +++ b/cmd/aviary/main.go @@ -53,7 +53,7 @@ type app struct { downProject func(name string) error runCompose func(c compose.Stack, args ...string) error runHost func(dir string, extraEnv map[string]string, command []string) error - vaultToken func(cfg *config.Vault, project string) (string, error) + vaultToken func(cfg *config.Vault) (string, error) branch func(root string) string } @@ -201,7 +201,21 @@ helper works unattended without an interactive gcloud reauthentication prompt.`, } } +// gcloudADCToken prints an access token from gcloud's Application Default +// Credentials, or returns empty when there are none, without starting gcloud +// on machines that never ran `gcloud auth application-default login`. func gcloudADCToken() string { + configDir := os.Getenv("CLOUDSDK_CONFIG") + if configDir == "" { + home, err := os.UserHomeDir() + if err != nil { + return "" + } + configDir = filepath.Join(home, ".config", "gcloud") + } + if _, err := os.Stat(filepath.Join(configDir, "application_default_credentials.json")); err != nil { + return "" + } out, err := exec.Command("gcloud", "auth", "application-default", "print-access-token").Output() if err != nil { return "" @@ -340,10 +354,12 @@ func (a *app) testCmd() *cobra.Command { return &cobra.Command{ Use: "test [suite [-- args...]]", Short: "Run a named test suite, starting what it needs first", - Long: `Run a named test suite from aviary.yml. The stack's init steps run first if -the suite wants them and they have not run yet, the services the suite needs -are started and waited on, and the suite's command runs in a fresh container -with any extra args appended. The exit status is the suite's own. + Long: `Run a named test suite from aviary.yml. The prepare steps run on the host +unless the suite sets prepare: false, the init steps run if the suite wants +them and they have not run yet, the services the suite needs are started and +waited on, and the suite's command runs in a fresh container (or on the host +for host: true suites) with any extra args appended. The exit status is the +suite's own. With no suite, list the suites aviary.yml defines.`, // Arguments after the suite name go to the suite's command, so cobra diff --git a/internal/stack/stack.go b/internal/stack/stack.go index 2ecd0c6..6754baf 100644 --- a/internal/stack/stack.go +++ b/internal/stack/stack.go @@ -39,6 +39,8 @@ type Stack struct { runner func(c compose.Stack, args ...string) error // hostRunner is Options.HostRunner. hostRunner func(dir string, extraEnv map[string]string, command []string) error + // prepared is set once this Stack has run the prepare steps. + prepared bool // stderr receives aviary's progress lines for init steps. stderr io.Writer } @@ -65,7 +67,7 @@ type Options struct { HostRunner func(dir string, extraEnv map[string]string, command []string) error // VaultToken overrides Vault token minting during tests. When nil and // cfg.Vault is set, vault.NewClient(cfg.Vault).Token is called. - VaultToken func(cfg *config.Vault, project string) (string, error) + VaultToken func(cfg *config.Vault) (string, error) } // Load builds the stack for the worktree containing cwd, allocating any host @@ -90,7 +92,7 @@ func Load(cwd, configDir string, opts Options) (*Stack, error) { if mint == nil { mint = defaultVaultToken } - token, err := mint(cfg.Vault, ws.Project()) + token, err := mint(cfg.Vault) if err != nil { return nil, fmt.Errorf("%s: %w", defFile, err) } @@ -140,12 +142,12 @@ func Load(cwd, configDir string, opts Options) (*Stack, error) { }, nil } -func defaultVaultToken(cfg *config.Vault, project string) (string, error) { +func defaultVaultToken(cfg *config.Vault) (string, error) { client, err := vault.NewClient(cfg) if err != nil { return "", err } - return client.Token(project) + return client.Token() } func resolveHostEnv(specs map[string]config.HostFile, out map[string]string) error { @@ -222,12 +224,19 @@ func (s *Stack) Env() map[string]string { return env } +// composeEnv is Env plus the resolved credentials, plus extra with each value +// interpolated against those (not against other extra values) and then the +// host environment. `$$` is a literal `$`, as in compose files. func (s *Stack) composeEnv(extra map[string]string) map[string]string { - env := s.Env() - maps.Copy(env, s.resolvedEnv) + base := s.Env() + maps.Copy(base, s.resolvedEnv) + env := maps.Clone(base) for k, v := range extra { env[k] = os.Expand(v, func(name string) string { - if val, ok := env[name]; ok { + if name == "$" { + return "$" + } + if val, ok := base[name]; ok { return val } return os.Getenv(name) @@ -281,14 +290,31 @@ func (s *Stack) run(extraProfiles []string, args ...string) error { return s.runner(c, args...) } -// Prepare runs the host prepare steps from aviary.yml in the worktree root. +// Prepare runs the host prepare steps from aviary.yml in the worktree root, +// under the stack's lock so two aviary commands in one worktree never run +// them at once. func (s *Stack) Prepare() error { + unlock, err := s.registry.LockProject(s.Workspace.Project()) + if err != nil { + return err + } + defer unlock() + return s.prepareLocked() +} + +// prepareLocked runs the prepare steps at most once per Stack, so a command +// that prepares and then inits runs them once. The caller holds the lock. +func (s *Stack) prepareLocked() error { + if s.prepared { + return nil + } for i, step := range s.Config.Prepare { desc := strings.Join(step.Command, " ") if err := s.hostRunner(s.Workspace.Root, s.composeEnv(nil), step.Command); err != nil { return fmt.Errorf("prepare step %d (%s) failed: %v", i+1, desc, err) } } + s.prepared = true return nil } @@ -312,7 +338,7 @@ func (s *Stack) Init(force bool) error { if s.Entry.InitializedAt != nil && !force { return nil } - if err := s.Prepare(); err != nil { + if err := s.prepareLocked(); err != nil { return err } if err := s.runSteps("init", s.Config.Init); err != nil { @@ -389,13 +415,16 @@ func (s *Stack) Test(name string, extra []string) error { } return s.hostRunner(dir, s.composeEnv(suite.Env), slices.Concat(suite.Command, extra)) } + // `--env KEY` with no value makes compose read the value from its own + // environment, so values never appear in the process list. envFlags := make([]string, 0, len(suite.Env)*2) - resolvedSuiteEnv := s.composeEnv(suite.Env) for _, k := range slices.Sorted(maps.Keys(suite.Env)) { - envFlags = append(envFlags, "--env", k+"="+resolvedSuiteEnv[k]) + envFlags = append(envFlags, "--env", k) } - args := slices.Concat([]string{"run", "--rm"}, envFlags, []string{suite.Service}, suite.Command, extra) - return s.run(suite.Profiles, args...) + c := s.Compose() + c.Profiles = slices.Concat(c.Profiles, suite.Profiles) + c.Env = s.composeEnv(suite.Env) + return s.runner(c, slices.Concat([]string{"run", "--rm"}, envFlags, []string{suite.Service}, suite.Command, extra)...) } // SuiteNames returns the stack's suite names, sorted. diff --git a/internal/stack/stack_test.go b/internal/stack/stack_test.go index c96794e..befae60 100644 --- a/internal/stack/stack_test.go +++ b/internal/stack/stack_test.go @@ -751,7 +751,7 @@ host_env: s, err := Load(root, "", Options{ Registry: testRegistry(t), - VaultToken: func(cfg *config.Vault, project string) (string, error) { + VaultToken: func(cfg *config.Vault) (string, error) { return "secret-child-token", nil }, }) @@ -767,6 +767,73 @@ host_env: } } +func TestSuiteThatWantsPrepareAndInitPreparesOnce(t *testing.T) { + cfg := &config.Config{ + Compose: config.Compose{Files: []string{"compose.yaml"}}, + Prepare: []config.HostStep{{Command: []string{"make", "artifacts"}}}, + Init: []config.Step{{Service: "migrate", Command: []string{"rake", "db:reset"}}}, + Suites: map[string]config.Suite{ + "api": {Service: "api", Command: []string{"npm", "test"}}, + }, + } + s, rec := newStack(t, cfg) + var prepares int + s.hostRunner = func(dir string, extraEnv map[string]string, command []string) error { + prepares++ + return nil + } + + if err := s.Test("api", nil); err != nil { + t.Fatal(err) + } + if prepares != 1 { + t.Errorf("prepare ran %d times, want 1", prepares) + } + assertCalls(t, rec, + argv(nil, "run", "--rm", "migrate", "rake", "db:reset"), + argv(nil, "run", "--rm", "api", "npm", "test"), + ) +} + +func TestContainerSuiteEnvTravelsInComposeEnvironmentNotArgv(t *testing.T) { + cfg := &config.Config{ + Compose: config.Compose{Files: []string{"compose.yaml"}}, + Ports: []string{"postgres"}, + Suites: map[string]config.Suite{ + "api": { + Service: "api", + Init: new(bool), + Prepare: new(bool), + Env: map[string]string{ + "DATABASE_URL": "postgresql://postgres@postgres:${AVIARY_PORT_POSTGRES}/db", + "PRICE": "$$5", + }, + Command: []string{"npm", "test"}, + }, + }, + } + s, _ := newStack(t, cfg) + var gotArgs []string + var gotEnv map[string]string + s.runner = func(c compose.Stack, args ...string) error { + gotArgs, gotEnv = args, c.Env + return nil + } + + if err := s.Test("api", nil); err != nil { + t.Fatal(err) + } + if want := []string{"run", "--rm", "--env", "DATABASE_URL", "--env", "PRICE", "api", "npm", "test"}; !slices.Equal(gotArgs, want) { + t.Errorf("args = %q, want %q", gotArgs, want) + } + if want := "postgresql://postgres@postgres:" + strconv.Itoa(s.Entry.Ports["postgres"]) + "/db"; gotEnv["DATABASE_URL"] != want { + t.Errorf("DATABASE_URL = %q, want %q", gotEnv["DATABASE_URL"], want) + } + if gotEnv["PRICE"] != "$5" { + t.Errorf("PRICE = %q, want $5", gotEnv["PRICE"]) + } +} + func TestPrepareAndHostSuiteExpandPortVariables(t *testing.T) { noInit := false cfg := &config.Config{ diff --git a/internal/vault/vault.go b/internal/vault/vault.go index 5108488..17dae78 100644 --- a/internal/vault/vault.go +++ b/internal/vault/vault.go @@ -1,6 +1,8 @@ -// Package vault mints and caches scoped child Vault tokens from a developer's -// local Vault login (`~/.vault-token`) or an ambient VAULT_TOKEN, so stacks -// never need manual token exports in every shell. +// Package vault mints and caches short-lived child Vault tokens from a +// developer's local Vault login (`~/.vault-token`) or an ambient VAULT_TOKEN, +// so stacks never need manual token exports in every shell. A child token +// carries every policy of its parent; it differs only in expiring sooner and +// being revoked with the parent. package vault import ( @@ -33,14 +35,30 @@ type Client struct { HomeDir string HTTPClient *http.Client Now func() time.Time + // Stderr receives a warning when Vault grants a child token a shorter + // lease than TTL, usually because the parent login is about to expire. + Stderr io.Writer } +// cachedToken is a minted child token as saved on disk, shared by every +// worktree that uses the same Vault server. type cachedToken struct { Addr string `json:"addr"` Token string `json:"token"` + IssuedAt time.Time `json:"issued_at"` ExpiresAt time.Time `json:"expires_at"` } +// fresh reports whether less than half of the token's granted lease has +// passed, so a long test suite does not start with a token about to expire. +// It trusts the recorded expiry instead of asking Vault, so commands work +// offline while the token lasts. Measuring against the granted lease, not the +// requested TTL, keeps a short lease (from a parent login near its end) in +// use for half its life instead of minting a new token on every command. +func (t cachedToken) fresh(now time.Time) bool { + return now.Before(t.IssuedAt.Add(t.ExpiresAt.Sub(t.IssuedAt) / 2)) +} + type createTokenRequest struct { TTL string `json:"ttl"` Renewable bool `json:"renewable"` @@ -52,9 +70,6 @@ type vaultResponse struct { ClientToken string `json:"client_token"` LeaseDuration int `json:"lease_duration"` } `json:"auth"` - Data *struct { - TTL int `json:"ttl"` - } `json:"data"` Errors []string `json:"errors"` } @@ -83,6 +98,7 @@ func NewClient(cfg *config.Vault) (*Client, error) { HomeDir: home, HTTPClient: &http.Client{Timeout: 15 * time.Second}, Now: time.Now, + Stderr: os.Stderr, }, nil } @@ -101,29 +117,27 @@ func defaultCacheDir() (string, error) { // Token returns a live Vault token for services to read secrets with. // // Resolution order: -// 1. A non-empty VAULT_TOKEN in env that Vault accepts on lookup-self (used as -// is, for CI or explicit overrides). If VAULT_TOKEN is set in env AND -// `~/.vault-token` exists, an expired env token falls through to minting -// from `~/.vault-token` because agent shells frequently inherit stale -// VAULT_TOKEN exports. If no `~/.vault-token` exists, an invalid env token -// fails immediately. -// 2. A cached child token on disk for this Vault server with more than half -// of c.TTL remaining and accepted by lookup-self. -// 3. A fresh non-renewable child token minted from `~/.vault-token` and saved +// 1. A non-empty VAULT_TOKEN in env, for CI or explicit overrides. With no +// `~/.vault-token` to fall back to, it is used as is without asking +// Vault. Otherwise it is used only if Vault accepts it on lookup-self, +// because agent shells often inherit stale VAULT_TOKEN exports. +// 2. A fresh cached child token on disk for this Vault server (see +// cachedToken.fresh), used without asking Vault. +// 3. A new non-renewable child token minted from `~/.vault-token` and saved // to the cache with mode 0600. -func (c *Client) Token(project string) (string, error) { +func (c *Client) Token() (string, error) { envToken := strings.TrimSpace(os.Getenv("VAULT_TOKEN")) homeToken := c.readHomeToken() if envToken != "" { + if homeToken == "" { + return envToken, nil + } if ok, err := c.validToken(envToken); err != nil { return "", err } else if ok { return envToken, nil } - if homeToken == "" { - return "", fmt.Errorf("VAULT_TOKEN in the environment is expired or invalid, and %s does not exist; run `VAULT_ADDR=%s vault login -method=oidc`", c.homeTokenPath(), c.Addr) - } } if err := os.MkdirAll(c.CacheDir, 0o700); err != nil { @@ -137,18 +151,14 @@ func (c *Client) Token(project string) (string, error) { defer lock.Unlock() if cached, ok := c.loadCached(cachePath); ok { - if valid, err := c.validToken(cached.Token); err != nil { - return "", err - } else if valid { - return cached.Token, nil - } + return cached.Token, nil } if homeToken == "" { return "", fmt.Errorf("no Vault login found at %s; ask the user to run `VAULT_ADDR=%s vault login -method=oidc`", c.homeTokenPath(), c.Addr) } - minted, err := c.mintChild(homeToken, project) + minted, err := c.mintChild(homeToken) if err != nil { return "", err } @@ -179,12 +189,7 @@ func (c *Client) loadCached(path string) (cachedToken, bool) { if err := json.Unmarshal(raw, &tok); err != nil { return cachedToken{}, false } - if tok.Addr != c.Addr || tok.Token == "" { - return cachedToken{}, false - } - // Refresh once less than half the desired TTL remains so a long test suite - // does not start with a token about to expire. - if !c.Now().Add(c.TTL / 2).Before(tok.ExpiresAt) { + if tok.Addr != c.Addr || tok.Token == "" || !tok.fresh(c.Now()) { return cachedToken{}, false } return tok, true @@ -251,11 +256,11 @@ func (c *Client) validToken(token string) (bool, error) { return false, fmt.Errorf("Vault lookup-self at %s returned HTTP %d", c.Addr, resp.StatusCode) } -func (c *Client) mintChild(parentToken, project string) (cachedToken, error) { +func (c *Client) mintChild(parentToken string) (cachedToken, error) { body, err := json.Marshal(createTokenRequest{ TTL: fmt.Sprintf("%ds", int(c.TTL.Seconds())), Renewable: false, - DisplayName: "aviary-" + project, + DisplayName: "aviary", }) if err != nil { return cachedToken{}, err @@ -294,9 +299,16 @@ func (c *Client) mintChild(parentToken, project string) (cachedToken, error) { if parsed.Auth.LeaseDuration > 0 { lease = time.Duration(parsed.Auth.LeaseDuration) * time.Second } + // Vault also caps leases at a role's max TTL, so a short lease does not + // prove the login is ending. + if lease < c.TTL && c.Stderr != nil { + fmt.Fprintf(c.Stderr, "aviary: Vault granted a %s token instead of %s; if your Vault login at %s is about to expire, run `VAULT_ADDR=%s vault login -method=oidc`\n", lease.Round(time.Second), c.TTL, c.homeTokenPath(), c.Addr) + } + issuedAt := c.Now().UTC() return cachedToken{ Addr: c.Addr, Token: parsed.Auth.ClientToken, - ExpiresAt: c.Now().UTC().Add(lease), + IssuedAt: issuedAt, + ExpiresAt: issuedAt.Add(lease), }, nil } diff --git a/internal/vault/vault_test.go b/internal/vault/vault_test.go index be0526d..908f28d 100644 --- a/internal/vault/vault_test.go +++ b/internal/vault/vault_test.go @@ -16,6 +16,7 @@ type fakeVault struct { server *httptest.Server valid map[string]bool minted atomic.Int32 + lookups atomic.Int32 mintTTL int } @@ -26,6 +27,7 @@ func newFakeVault(t *testing.T, valid map[string]bool) *fakeVault { tok := r.Header.Get("X-Vault-Token") switch r.URL.Path { case "/v1/auth/token/lookup-self": + fv.lookups.Add(1) if fv.valid[tok] { w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte(`{"data":{"ttl":3600}}`)) @@ -41,7 +43,7 @@ func newFakeVault(t *testing.T, valid map[string]bool) *fakeVault { } var req createTokenRequest _ = json.NewDecoder(r.Body).Decode(&req) - if req.Renewable || !strings.HasPrefix(req.DisplayName, "aviary-") { + if req.Renewable || req.DisplayName != "aviary" { w.WriteHeader(http.StatusBadRequest) return } @@ -84,7 +86,7 @@ func TestTokenMintsFromHomeTokenAndCachesAcrossCalls(t *testing.T) { t.Fatal(err) } - tok1, err := c.Token("aviary-fdy-abc123") + tok1, err := c.Token() if err != nil { t.Fatal(err) } @@ -94,7 +96,7 @@ func TestTokenMintsFromHomeTokenAndCachesAcrossCalls(t *testing.T) { // Still more than half TTL remaining: reuses the cached child token. c.Now = func() time.Time { return now.Add(3 * time.Hour) } - tok2, err := c.Token("aviary-fdy-abc123") + tok2, err := c.Token() if err != nil { t.Fatal(err) } @@ -104,7 +106,7 @@ func TestTokenMintsFromHomeTokenAndCachesAcrossCalls(t *testing.T) { // Past half TTL: mints a fresh child token. c.Now = func() time.Time { return now.Add(5 * time.Hour) } - tok3, err := c.Token("aviary-fdy-abc123") + tok3, err := c.Token() if err != nil { t.Fatal(err) } @@ -121,7 +123,7 @@ func TestStaleEnvVaultTokenFallsBackToHomeToken(t *testing.T) { t.Fatal(err) } - tok, err := c.Token("aviary-fdy-abc123") + tok, err := c.Token() if err != nil { t.Fatal(err) } @@ -135,7 +137,7 @@ func TestValidEnvVaultTokenWinsWithoutMinting(t *testing.T) { fv := newFakeVault(t, map[string]bool{"ci-provided-token": true}) c := testClient(t, fv, time.Now().UTC()) - tok, err := c.Token("aviary-fdy-abc123") + tok, err := c.Token() if err != nil { t.Fatal(err) } @@ -149,7 +151,7 @@ func TestMissingOrExpiredLoginInstructsVaultLogin(t *testing.T) { fv := newFakeVault(t, map[string]bool{}) c := testClient(t, fv, time.Now().UTC()) - _, err := c.Token("aviary-fdy-abc123") + _, err := c.Token() if err == nil || !strings.Contains(err.Error(), "vault login -method=oidc") { t.Fatalf("missing ~/.vault-token err = %v, want oidc login hint", err) } @@ -157,8 +159,80 @@ func TestMissingOrExpiredLoginInstructsVaultLogin(t *testing.T) { if err := os.WriteFile(filepath.Join(c.HomeDir, ".vault-token"), []byte("expired-parent"), 0o600); err != nil { t.Fatal(err) } - _, err = c.Token("aviary-fdy-abc123") + _, err = c.Token() if err == nil || !strings.Contains(err.Error(), "vault login -method=oidc") { t.Fatalf("expired ~/.vault-token err = %v, want oidc login hint", err) } } + +func TestCachedTokenIsUsedWithoutAskingVault(t *testing.T) { + t.Setenv("VAULT_TOKEN", "") + fv := newFakeVault(t, map[string]bool{"parent-token": true}) + now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC) + c := testClient(t, fv, now) + if err := os.WriteFile(filepath.Join(c.HomeDir, ".vault-token"), []byte("parent-token"), 0o600); err != nil { + t.Fatal(err) + } + if _, err := c.Token(); err != nil { + t.Fatal(err) + } + + fv.server.Close() + c.Now = func() time.Time { return now.Add(time.Hour) } + tok, err := c.Token() + if err != nil { + t.Fatalf("cached token needed Vault: %v", err) + } + if tok != "child-token-1" || fv.lookups.Load() != 0 { + t.Fatalf("tok = %q, lookups = %d; want cached token and no lookups", tok, fv.lookups.Load()) + } +} + +func TestShortLeaseWarnsAndIsReusedForHalfItsLife(t *testing.T) { + t.Setenv("VAULT_TOKEN", "") + fv := newFakeVault(t, map[string]bool{"parent-token": true}) + fv.mintTTL = 3600 + now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC) + c := testClient(t, fv, now) + var stderr strings.Builder + c.Stderr = &stderr + if err := os.WriteFile(filepath.Join(c.HomeDir, ".vault-token"), []byte("parent-token"), 0o600); err != nil { + t.Fatal(err) + } + + if _, err := c.Token(); err != nil { + t.Fatal(err) + } + if !strings.Contains(stderr.String(), "granted a 1h0m0s token instead of 8h0m0s") { + t.Errorf("stderr = %q, want a warning about the 1h lease", stderr.String()) + } + c.Now = func() time.Time { return now.Add(20 * time.Minute) } + if _, err := c.Token(); err != nil { + t.Fatal(err) + } + if fv.minted.Load() != 1 { + t.Errorf("minted = %d; want the 1h token reused at 20 minutes", fv.minted.Load()) + } + c.Now = func() time.Time { return now.Add(40 * time.Minute) } + if _, err := c.Token(); err != nil { + t.Fatal(err) + } + if fv.minted.Load() != 2 { + t.Errorf("minted = %d; want a new token past half the 1h lease", fv.minted.Load()) + } +} + +func TestEnvTokenWithoutHomeLoginPassesThroughUnchecked(t *testing.T) { + t.Setenv("VAULT_TOKEN", "ci-provided-token") + fv := newFakeVault(t, map[string]bool{}) + c := testClient(t, fv, time.Now().UTC()) + fv.server.Close() + + tok, err := c.Token() + if err != nil { + t.Fatal(err) + } + if tok != "ci-provided-token" { + t.Fatalf("tok = %q, want the env token as is", tok) + } +} From 58bc81e359dc475e66c9724fcee725ee894b8972 Mon Sep 17 00:00:00 2001 From: Seamus Abshere Date: Mon, 16 Nov 2026 13:59:42 -0500 Subject: [PATCH 3/4] feat: say whether init will run before a test suite starts A first init can sit for several minutes with no output, which looks like a hang. Co-authored-by: Cursor --- README.md | 2 +- cmd/aviary/main.go | 13 +++++----- internal/stack/stack.go | 14 +++++++++++ internal/stack/stack_test.go | 47 ++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e86afa1..565d591 100644 --- a/README.md +++ b/README.md @@ -284,7 +284,7 @@ suites: command: [uv, run, pytest] ``` -`aviary test api`: +`aviary test api` prints one line first, naming the suite and whether init will run or was skipped. The first init in a worktree can take several minutes. Then it: 1. runs `prepare` on the host in the worktree root (skipped with `prepare: false`); 2. runs the init steps, each as `docker compose run --rm `, diff --git a/cmd/aviary/main.go b/cmd/aviary/main.go index 6cf6bae..1df2bc1 100644 --- a/cmd/aviary/main.go +++ b/cmd/aviary/main.go @@ -354,12 +354,13 @@ func (a *app) testCmd() *cobra.Command { return &cobra.Command{ Use: "test [suite [-- args...]]", Short: "Run a named test suite, starting what it needs first", - Long: `Run a named test suite from aviary.yml. The prepare steps run on the host -unless the suite sets prepare: false, the init steps run if the suite wants -them and they have not run yet, the services the suite needs are started and -waited on, and the suite's command runs in a fresh container (or on the host -for host: true suites) with any extra args appended. The exit status is the -suite's own. + Long: `Run a named test suite from aviary.yml. It first prints whether init will +run; the first init in a worktree can take several minutes. The prepare steps +run on the host unless the suite sets prepare: false, the init steps run if +the suite wants them and they have not run yet, the services the suite needs +are started and waited on, and the suite's command runs in a fresh container +(or on the host for host: true suites) with any extra args appended. The exit +status is the suite's own. With no suite, list the suites aviary.yml defines.`, // Arguments after the suite name go to the suite's command, so cobra diff --git a/internal/stack/stack.go b/internal/stack/stack.go index 6754baf..b9681f7 100644 --- a/internal/stack/stack.go +++ b/internal/stack/stack.go @@ -380,6 +380,9 @@ func seconds(d time.Duration) string { // (or on the host when suite.Host is true) with extra appended (a leading "--" // is dropped). The stack-wide and suite profiles apply to both compose calls. // A suite that fails returns its status as a *compose.ExitError. +// +// The first thing it prints is whether init will run. A first init can sit +// for several minutes before any other output. func (s *Stack) Test(name string, extra []string) error { suite, ok := s.Config.Suites[name] if !ok { @@ -389,6 +392,7 @@ func (s *Stack) Test(name string, extra []string) error { } return fmt.Errorf("no suite %q; available: %s", name, strings.Join(names, ", ")) } + s.announceTest(name, suite) if suite.WantsPrepare() { if err := s.Prepare(); err != nil { return err @@ -427,6 +431,16 @@ func (s *Stack) Test(name string, extra []string) error { return s.runner(c, slices.Concat([]string{"run", "--rm"}, envFlags, []string{suite.Service}, suite.Command, extra)...) } +// announceTest names the suite and says whether init will run, before prepare +// or init, so a quiet first run is not mistaken for a hang. +func (s *Stack) announceTest(name string, suite config.Suite) { + if suite.WantsInit() && s.Entry.InitializedAt == nil { + fmt.Fprintf(s.stderr, "aviary: test %s: init running (the first init in a worktree can take several minutes)\n", name) + return + } + fmt.Fprintf(s.stderr, "aviary: test %s: init skipped\n", name) +} + // SuiteNames returns the stack's suite names, sorted. func (s *Stack) SuiteNames() []string { return slices.Sorted(maps.Keys(s.Config.Suites)) diff --git a/internal/stack/stack_test.go b/internal/stack/stack_test.go index befae60..82bb658 100644 --- a/internal/stack/stack_test.go +++ b/internal/stack/stack_test.go @@ -541,6 +541,53 @@ func suiteConfig() *config.Config { return cfg } +func TestTestAnnouncesWhetherInitWillRun(t *testing.T) { + t.Run("running", func(t *testing.T) { + s, _ := newStack(t, suiteConfig()) + var stderr bytes.Buffer + s.stderr = &stderr + if err := s.Test("web", nil); err != nil { + t.Fatal(err) + } + const want = "aviary: test web: init running (the first init in a worktree can take several minutes)\n" + if !strings.Contains(stderr.String(), want) { + t.Errorf("stderr = %q, want it to contain %q", stderr.String(), want) + } + }) + + t.Run("already initialized", func(t *testing.T) { + s, rec := newStack(t, suiteConfig()) + if err := s.markInitialized(true); err != nil { + t.Fatal(err) + } + var stderr bytes.Buffer + s.stderr = &stderr + if err := s.Test("web", nil); err != nil { + t.Fatal(err) + } + if !strings.Contains(stderr.String(), "aviary: test web: init skipped\n") || strings.Contains(stderr.String(), "several minutes") { + t.Errorf("stderr = %q, want init skipped and no first-init warning", stderr.String()) + } + for _, call := range rec.calls { + if slices.Contains(call, "db:reset") { + t.Errorf("init ran even though it was already recorded: %q", call) + } + } + }) + + t.Run("suite opts out", func(t *testing.T) { + s, _ := newStack(t, suiteConfig()) + var stderr bytes.Buffer + s.stderr = &stderr + if err := s.Test("acceptance", nil); err != nil { + t.Fatal(err) + } + if !strings.Contains(stderr.String(), "aviary: test acceptance: init skipped\n") || strings.Contains(stderr.String(), "several minutes") { + t.Errorf("stderr = %q, want init skipped and no first-init warning", stderr.String()) + } + }) +} + func TestTestRunsInitThenNeedsThenTheSuite(t *testing.T) { s, rec := newStack(t, suiteConfig()) From 9d553cfaf42095445eb004a98f21ea6f4384b995 Mon Sep 17 00:00:00 2001 From: Seamus Abshere Date: Mon, 16 Nov 2026 15:00:42 -0500 Subject: [PATCH 4/4] feat: volume snapshots, suite workdir path stripping, and Docker Desktop warnings Co-authored-by: Cursor --- README.md | 38 +- cmd/aviary/integration_test.go | 117 ++++++ cmd/aviary/main.go | 8 + internal/compose/compose.go | 27 +- internal/config/config.go | 145 ++++++- internal/config/config_test.go | 47 ++- internal/dockerdesktop/settings.go | 92 ++++ internal/dockerdesktop/settings_test.go | 62 +++ internal/snapshot/snapshot.go | 532 ++++++++++++++++++++++++ internal/snapshot/snapshot_test.go | 189 +++++++++ internal/stack/stack.go | 154 ++++++- internal/stack/stack_test.go | 209 ++++++++++ 12 files changed, 1593 insertions(+), 27 deletions(-) create mode 100644 internal/dockerdesktop/settings.go create mode 100644 internal/dockerdesktop/settings_test.go create mode 100644 internal/snapshot/snapshot.go create mode 100644 internal/snapshot/snapshot_test.go diff --git a/README.md b/README.md index 565d591..150e75d 100644 --- a/README.md +++ b/README.md @@ -300,8 +300,42 @@ command line, so they never show up in `ps`. Arguments after the suite name, with or without a `--`, are appended to its command, so `aviary test api -- --grep users` and `aviary test api --help` -reach the test runner. aviary exits with the suite's status. `aviary test` -alone lists the suites; `aviary test -h` prints the command's help. +reach the test runner. When a suite sets `workdir` (either a `host: true` suite +or a container suite whose working directory mounts that package), path +arguments pointing inside `workdir` from the worktree root or caller directory +are rewritten relative to `workdir` (`aviary test api -- src/api/test/foo.ts` +passes `test/foo.ts`), preserving `:line` and `::test` selectors. aviary exits +with the suite's status. `aviary test` alone lists the suites; `aviary test -h` +prints the command's help. + +### Volume snapshots + +When seeding a database takes minutes, `snapshot` caches the seeded volumes +under `$XDG_CACHE_HOME/aviary/snapshots` (`~/.cache/aviary/snapshots` by +default) so later worktrees restore them in seconds: + +```yaml +after_restore: + - service: migrate + command: [rake, db:migrate] +snapshot: + volumes: [postgres] + key_files: [db/seeds.rb, db/reference_data.sql] + stamp_files: [db/structure.sql] +``` + +The snapshot key hashes the volume names, the images mounting them, the `init` +steps, and every file or directory in `key_files`. The stamp hashes +`stamp_files`. On `init` (unless `aviary init --force` is passed): + +- **Exact `(key, stamp)` hit**: stops the stack's containers, restores the + volumes, and skips both `init` and `after_restore`. +- **Same `key`, different `stamp`** (when `after_restore` is non-empty): + restores the newest cached snapshot for `key`, runs `after_restore`, stops + the stack to flush database state, and saves the migrated snapshot under the + new `stamp`. +- **Cache miss** (or `aviary init --force`): runs `init`, stops the stack to + flush database state, and saves the snapshot under `(key, stamp)`. Init runs once per stack, so a new worktree runs it from scratch. Step times go to stderr. A failing step stops the sequence, names the step, and leaves diff --git a/cmd/aviary/integration_test.go b/cmd/aviary/integration_test.go index d37d15e..812c659 100644 --- a/cmd/aviary/integration_test.go +++ b/cmd/aviary/integration_test.go @@ -164,6 +164,123 @@ func TestMinimalExampleEndToEnd(t *testing.T) { } } +func TestSnapshotSaveRestoreAndAfterRestoreEndToEnd(t *testing.T) { + if out, err := exec.Command("docker", "compose", "version").CombinedOutput(); err != nil { + t.Fatalf("the integration test needs docker compose: %v\n%s", err, out) + } + + tmp := t.TempDir() + bin := filepath.Join(tmp, "aviary") + if out, err := exec.Command("go", "build", "-o", bin, ".").CombinedOutput(); err != nil { + t.Fatalf("go build: %v\n%s", err, out) + } + env := append(withoutAviaryVars(os.Environ()), + "XDG_STATE_HOME="+filepath.Join(tmp, "state"), + "XDG_CACHE_HOME="+filepath.Join(tmp, "cache"), + ) + + const aviaryYml = `compose: + files: [compose.yaml] +ports: [postgres, redis] +init: + - service: client + command: [psql, -f, /src/seed.sql] +after_restore: + - service: client + command: [psql, -f, /src/migrate.sql] +snapshot: + volumes: [postgres] + key_files: [seed.sql] + stamp_files: [schema.sql] +suites: + db: + service: client + needs: [postgres] + command: [psql, -tAc, "SELECT count(*) FROM seeded"] +` + + makeWorktree := func(name, schema, migrate string) string { + dir := filepath.Join(tmp, name) + if err := os.CopyFS(dir, os.DirFS(filepath.Join("..", "..", "examples", "minimal"))); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "aviary.yml"), []byte(aviaryYml), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "seed.sql"), []byte("CREATE TABLE seeded (id int); INSERT INTO seeded VALUES (1), (2);\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "schema.sql"), []byte(schema), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "migrate.sql"), []byte(migrate), 0o644); err != nil { + t.Fatal(err) + } + return dir + } + + runIn := func(dir string, args ...string) (stdout, stderr string) { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) + defer cancel() + cmd := exec.CommandContext(ctx, bin, args...) + cmd.Dir = dir + cmd.Env = env + var outBuf, errBuf bytes.Buffer + cmd.Stdout = &outBuf + cmd.Stderr = &errBuf + if err := cmd.Run(); err != nil { + t.Fatalf("aviary %s in %s: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), dir, err, &outBuf, &errBuf) + } + return outBuf.String(), errBuf.String() + } + + wt1 := makeWorktree("snap1", "-- v1\n", "SELECT 1;\n") + wt2 := makeWorktree("snap2", "-- v1\n", "SELECT 1;\n") + wt3 := makeWorktree("snap3", "-- v2\n", "INSERT INTO seeded VALUES (3);\n") + + for _, dir := range []string{wt1, wt2, wt3} { + out, _ := runIn(dir, "env", "--json") + var e map[string]string + if err := json.Unmarshal([]byte(out), &e); err != nil { + t.Fatal(err) + } + proj := e["AVIARY_PROJECT"] + t.Cleanup(func() { _ = compose.DownProject(proj) }) + } + + // First worktree seeds from scratch and saves the snapshot. + out1, err1 := runIn(wt1, "test", "db") + if got := lastLine(out1); got != "2" { + t.Fatalf("wt1 test db = %q, want 2", got) + } + if !strings.Contains(err1, "saved snapshot") { + t.Errorf("wt1 stderr missing saved snapshot:\n%s", err1) + } + + // Second worktree on the same schema restores without running init steps. + out2, err2 := runIn(wt2, "test", "db") + if got := lastLine(out2); got != "2" { + t.Fatalf("wt2 test db = %q, want 2", got) + } + if !strings.Contains(err2, "restoring snapshot") || strings.Contains(err2, "init: client psql") { + t.Errorf("wt2 stderr should restore snapshot without running init:\n%s", err2) + } + + // Third worktree on a newer schema restores and runs after_restore. + out3, err3 := runIn(wt3, "test", "db") + if got := lastLine(out3); got != "3" { + t.Fatalf("wt3 test db = %q, want 3", got) + } + if !strings.Contains(err3, "after_restore") { + t.Errorf("wt3 stderr should run after_restore:\n%s", err3) + } + + for _, dir := range []string{wt1, wt2, wt3} { + runIn(dir, "rm") + } +} + // withoutAviaryVars drops variables that would point the binary at a // different definition or registry than the test's. func withoutAviaryVars(environ []string) []string { diff --git a/cmd/aviary/main.go b/cmd/aviary/main.go index 1df2bc1..2867b46 100644 --- a/cmd/aviary/main.go +++ b/cmd/aviary/main.go @@ -152,6 +152,7 @@ func (a *app) upCmd() *cobra.Command { if err != nil { return err } + s.WarnDockerDesktop() if err := s.Prepare(); err != nil { return err } @@ -230,6 +231,7 @@ func (a *app) runCmd() *cobra.Command { if err != nil { return err } + s.WarnDockerDesktop() if err := s.Prepare(); err != nil { return err } @@ -256,6 +258,9 @@ Every argument is passed to docker compose unchanged, so aviary flags such as if err != nil { return err } + if !opts.SkipRequireEnv { + s.WarnDockerDesktop() + } return a.runCompose(s.Compose(), slices.Concat(sub, args)...) }, } @@ -277,6 +282,9 @@ func (a *app) composeCmd() *cobra.Command { if err != nil { return err } + if !opts.SkipRequireEnv { + s.WarnDockerDesktop() + } run := func() error { return a.runCompose(s.Compose(), args...) } if sub == "down" && removesVolumes(rest) { return s.RemoveVolumes(run) diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 7bdd950..fe206ff 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -256,8 +256,9 @@ type Resolved struct { } type ResolvedService struct { - Image string `json:"image"` - Volumes []ResolvedMount `json:"volumes"` + Image string `json:"image"` + Platform string `json:"platform"` + Volumes []ResolvedMount `json:"volumes"` } type ResolvedMount struct { @@ -295,11 +296,29 @@ func (r *Resolved) ImagesMounting(volume string) []string { var images []string for _, svc := range r.Services { for _, m := range svc.Volumes { - if m.Type == "volume" && m.Source == volume { + if m.Type == "volume" && m.Source == volume && svc.Image != "" { images = append(images, svc.Image) } } } slices.Sort(images) - return images + return slices.Compact(images) +} + +// ArchiveContainer returns the image and platform of a service that mounts the +// named volume, picking the first service by name so the choice is +// deterministic. +func (r *Resolved) ArchiveContainer(volume string) (image, platform string) { + for _, name := range slices.Sorted(maps.Keys(r.Services)) { + svc := r.Services[name] + if svc.Image == "" { + continue + } + for _, m := range svc.Volumes { + if m.Type == "volume" && m.Source == volume { + return svc.Image, svc.Platform + } + } + } + return "", "" } diff --git a/internal/config/config.go b/internal/config/config.go index 82a54ec..eafbaca 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -13,6 +13,7 @@ import ( "path/filepath" "regexp" "slices" + "strings" "time" "gopkg.in/yaml.v3" @@ -114,8 +115,10 @@ type Suite struct { // to the worktree root) instead of `docker compose run` inside Service, // with the stack's AVIARY_* environment variables and Suite.Env set. Host bool `yaml:"host"` - // Workdir is the host working directory, relative to the worktree root, - // when Host is true. Defaults to the worktree root. + // Workdir is the suite's package directory, relative to the worktree root. + // When Host is true, Command runs in Workdir on the host. For every suite + // with Workdir set, aviary strips the package directory prefix from path + // arguments passed to `aviary test` so paths work from the repo root too. Workdir string `yaml:"workdir"` // Env holds extra environment variables passed to the suite's command // after `${VAR}` interpolation against the stack's environment. @@ -228,8 +231,35 @@ func (c *Config) validate() error { return fmt.Errorf("suite %q: %w", name, err) } } - if c.Snapshot != nil && len(c.Snapshot.Volumes) == 0 { - return fmt.Errorf("snapshot.volumes must name at least one volume") + if c.Snapshot != nil { + if err := c.Snapshot.validate(); err != nil { + return fmt.Errorf("snapshot: %w", err) + } + } + return nil +} + +func (s Snapshot) validate() error { + if len(s.Volumes) == 0 { + return fmt.Errorf("volumes must name at least one volume") + } + if err := noEmpty("volumes", s.Volumes); err != nil { + return err + } + if err := validRelativePaths("key_files", s.KeyFiles); err != nil { + return err + } + return validRelativePaths("stamp_files", s.StampFiles) +} + +func validRelativePaths(field string, paths []string) error { + for i, p := range paths { + if p == "" { + return fmt.Errorf("%s[%d] is empty", field, i) + } + if !filepath.IsLocal(p) { + return fmt.Errorf("%s[%d] %q must be relative to the worktree root", field, i, p) + } } return nil } @@ -286,17 +316,14 @@ func (s Suite) validate() error { if err := noEmpty("command", s.Command); err != nil { return err } - if filepath.IsAbs(s.Workdir) { - return fmt.Errorf("workdir %q must be relative to the worktree root", s.Workdir) - } } else { - if s.Workdir != "" { - return fmt.Errorf("workdir is only for host suites") - } if err := (Step{Service: s.Service, Command: s.Command}).validate(); err != nil { return err } } + if s.Workdir != "" && s.Workdir != "." && !filepath.IsLocal(s.Workdir) { + return fmt.Errorf("workdir %q must be relative to the worktree root", s.Workdir) + } for _, name := range slices.Sorted(maps.Keys(s.Env)) { if !envName.MatchString(name) { return fmt.Errorf("env name %q must match %s", name, envName) @@ -308,6 +335,104 @@ func (s Suite) validate() error { return noEmpty("profiles", s.Profiles) } +// patternValueFlags are common test-runner flags whose following argument is a +// filter expression rather than a file path. +var patternValueFlags = map[string]bool{ + "-e": true, "--example": true, + "-g": true, "--grep": true, + "-k": true, "-m": true, "-t": true, +} + +// NormalizeArgs rewrites path arguments in extra that point inside s.Workdir +// (from root, cwd, or an absolute path) so they are relative to s.Workdir. +// Arguments already relative to s.Workdir, flags, and filter patterns are +// returned unchanged. +func (s Suite) NormalizeArgs(root, cwd string, extra []string) []string { + workdir := strings.TrimSuffix(filepath.ToSlash(filepath.Clean(s.Workdir)), "/") + if s.Workdir == "" || workdir == "." { + return extra + } + suiteDir := filepath.Join(root, filepath.FromSlash(workdir)) + out := make([]string, len(extra)) + skipNext := false + for i, arg := range extra { + if skipNext { + out[i] = arg + skipNext = false + continue + } + if strings.HasPrefix(arg, "-") { + out[i] = arg + skipNext = patternValueFlags[arg] + continue + } + out[i] = normalizeSuiteArg(suiteDir, cwd, workdir, arg) + } + return out +} + +func normalizeSuiteArg(suiteDir, cwd, workdir, arg string) string { + if arg == "" || strings.Contains(arg, "://") { + return arg + } + pathPart, suffix := splitSelector(arg) + if pathPart == "" { + return arg + } + if pathExists(filepath.Join(suiteDir, pathPart)) { + return arg + } + if filepath.IsAbs(pathPart) { + if rel, ok := relWithin(suiteDir, pathPart); ok { + return filepath.ToSlash(rel) + suffix + } + return arg + } + if cwd != "" && cwd != suiteDir { + candidate := filepath.Join(cwd, pathPart) + if pathExists(candidate) { + if rel, ok := relWithin(suiteDir, candidate); ok { + return filepath.ToSlash(rel) + suffix + } + } + } + trimmed := strings.TrimPrefix(filepath.ToSlash(pathPart), "./") + if trimmed == workdir || trimmed == workdir+"/" { + return "." + suffix + } + if strings.HasPrefix(trimmed, workdir+"/") { + return strings.TrimPrefix(trimmed, workdir+"/") + suffix + } + return arg +} + +func splitSelector(arg string) (pathPart, suffix string) { + idx := strings.IndexByte(arg, ':') + if idx <= 0 { + return arg, "" + } + return arg[:idx], arg[idx:] +} + +func pathExists(path string) bool { + _, err := os.Lstat(path) + return err == nil +} + +func relWithin(base, target string) (string, bool) { + rel, err := filepath.Rel(base, target) + if err != nil { + return "", false + } + if rel == "." { + return ".", true + } + if !filepath.IsLocal(rel) { + return "", false + } + return rel, true +} + func noEmpty(field string, values []string) error { for i, v := range values { if v == "" { diff --git a/internal/config/config_test.go b/internal/config/config_test.go index c8e557e..3b44eb0 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -239,8 +239,9 @@ func TestLoadRejectsBadInput(t *testing.T) { "suite without command": {base + "suites:\n api:\n service: api\n", `suite "api": needs a command`}, "suite with an empty need": {base + "suites:\n api: {service: api, command: [t], needs: [db, \"\"]}\n", `suite "api": needs[1] is empty`}, "suite with an empty profile": {base + "suites:\n api: {service: api, command: [t], profiles: [\"\"]}\n", `suite "api": profiles[0] is empty`}, - "snapshot without volumes": {base + "snapshot:\n key_files: [seeds/]\n", "snapshot.volumes"}, - "snapshot with empty volumes": {base + "snapshot:\n volumes: []\n", "snapshot.volumes"}, + "snapshot without volumes": {base + "snapshot:\n key_files: [seeds/]\n", "snapshot: volumes"}, + "snapshot with empty volumes": {base + "snapshot:\n volumes: []\n", "snapshot: volumes"}, + "snapshot with abs key_file": {base + "snapshot:\n volumes: [pg]\n key_files: [/etc/passwd]\n", "must be relative"}, "require_env with a dash": {base + "require_env: [VAULT-TOKEN]\n", `require_env name "VAULT-TOKEN"`}, "require_env starting with a digit": {base + "require_env: [1TOKEN]\n", `require_env name "1TOKEN"`}, "require_env empty name": {base + "require_env: [\"\"]\n", `require_env name ""`}, @@ -251,7 +252,7 @@ func TestLoadRejectsBadInput(t *testing.T) { "host_env without path": {base + "host_env:\n FOO: {required: true}\n", `host_env["FOO"]: needs path`}, "prepare without command": {base + "prepare:\n - {}\n", "prepare[0]: needs a command"}, "host suite with service": {base + "suites:\n s: {host: true, service: api, command: [t]}\n", "host suite must not set service"}, - "container suite with workdir": {base + "suites:\n s: {service: api, workdir: src/x, command: [t]}\n", "workdir is only for host suites"}, + "suite with escaping workdir": {base + "suites:\n s: {service: api, workdir: ../outside, command: [t]}\n", "must be relative"}, "host suite with abs workdir": {base + "suites:\n s: {host: true, workdir: /abs, command: [t]}\n", "must be relative"}, } for name, c := range cases { @@ -298,3 +299,43 @@ func TestMinimalExampleLoads(t *testing.T) { t.Errorf("db should want init and smoke should not: %+v", cfg.Suites) } } + +func TestSuiteNormalizeArgsStripsPackagePrefixAndPreservesSelectors(t *testing.T) { + root := t.TempDir() + specFile := filepath.Join(root, "workers", "model_train", "tests", "citus", "test_insights.py") + if err := os.MkdirAll(filepath.Dir(specFile), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(specFile, []byte("# test\n"), 0o644); err != nil { + t.Fatal(err) + } + suite := Suite{Service: "model_train", Workdir: "workers/model_train", Command: []string{"pytest"}} + + got := suite.NormalizeArgs(root, root, []string{ + "workers/model_train/tests/citus/test_insights.py::test_bar", + "./workers/model_train/tests/citus/test_insights.py", + specFile + "::test_abs", + "tests/citus/test_insights.py", + "-k", "workers/model_train/keep_pattern", + "workers/model_train/tests/**/*.py", + }) + want := []string{ + "tests/citus/test_insights.py::test_bar", + "tests/citus/test_insights.py", + "tests/citus/test_insights.py::test_abs", + "tests/citus/test_insights.py", + "-k", "workers/model_train/keep_pattern", + "tests/**/*.py", + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("NormalizeArgs =\n got %q\nwant %q", got, want) + } + + // Invoked from an intermediate directory (`workers/`). + fromWorkers := suite.NormalizeArgs(root, filepath.Join(root, "workers"), []string{ + "model_train/tests/citus/test_insights.py:42", + }) + if !reflect.DeepEqual(fromWorkers, []string{"tests/citus/test_insights.py:42"}) { + t.Fatalf("NormalizeArgs from parent subdir = %q", fromWorkers) + } +} diff --git a/internal/dockerdesktop/settings.go b/internal/dockerdesktop/settings.go new file mode 100644 index 0000000..7ad99b6 --- /dev/null +++ b/internal/dockerdesktop/settings.go @@ -0,0 +1,92 @@ +// Package dockerdesktop inspects Docker Desktop's local settings on Apple +// silicon Macs and reports configurations that make linux/amd64 containers slow +// or prone to out-of-memory kills. +package dockerdesktop + +import ( + "bytes" + "encoding/json" + "fmt" + "os" + "path/filepath" + "runtime" +) + +const MinRecommendedMemoryMiB = 8192 + +// Settings models the subset of Docker Desktop's settings-store.json that +// governs amd64 emulation and VM memory on Apple silicon. +type Settings struct { + UseVirtualizationFramework bool `json:"UseVirtualizationFramework"` + UseVirtualizationFrameworkRosetta bool `json:"UseVirtualizationFrameworkRosetta"` + MemoryMiB int `json:"MemoryMiB"` +} + +// LoadDefault reads Docker Desktop's settings-store.json on darwin/arm64. It +// returns nil when not on Apple silicon, when AVIARY_NO_DOCKER_WARNING is set, +// or when the settings file is absent or unreadable. +func LoadDefault() *Settings { + if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" { + return nil + } + if os.Getenv("AVIARY_NO_DOCKER_WARNING") != "" { + return nil + } + home, err := os.UserHomeDir() + if err != nil { + return nil + } + path := filepath.Join(home, "Library", "Group Containers", "group.com.docker", "settings-store.json") + raw, err := os.ReadFile(path) + if err != nil { + return nil + } + s, err := Parse(raw) + if err != nil { + return nil + } + return s +} + +// Parse decodes a Docker Desktop settings-store.json payload. +func Parse(raw []byte) (*Settings, error) { + var s Settings + if err := json.Unmarshal(raw, &s); err != nil { + return nil, err + } + return &s, nil +} + +// Warnings returns human-readable configuration warnings when the stack uses +// linux/amd64 images on Apple silicon without Rosetta or sufficient memory. +func (s Settings) Warnings(hasLinuxAmd64 bool) []string { + if !hasLinuxAmd64 { + return nil + } + var out []string + if !s.UseVirtualizationFramework { + out = append(out, "Docker Desktop is not using Apple Virtualization framework, so linux/amd64 containers run under slow QEMU emulation instead of Rosetta") + } else if !s.UseVirtualizationFrameworkRosetta { + out = append(out, "Docker Desktop has Rosetta emulation turned off, so linux/amd64 containers run under slow QEMU emulation") + } + if s.MemoryMiB > 0 && s.MemoryMiB < MinRecommendedMemoryMiB { + out = append(out, fmt.Sprintf("Docker Desktop has %d MiB memory allocated (< %d MiB); linux/amd64 stacks may OOM (exit 137)", s.MemoryMiB, MinRecommendedMemoryMiB)) + } + return out +} + +// ComposeFilesSpecifyLinuxAmd64 reports whether any compose file in files +// (resolved against definitionDir when relative) mentions linux/amd64. +func ComposeFilesSpecifyLinuxAmd64(definitionDir string, files []string) bool { + needle := []byte("linux/amd64") + for _, f := range files { + if !filepath.IsAbs(f) { + f = filepath.Join(definitionDir, f) + } + raw, err := os.ReadFile(f) + if err == nil && bytes.Contains(raw, needle) { + return true + } + } + return false +} diff --git a/internal/dockerdesktop/settings_test.go b/internal/dockerdesktop/settings_test.go new file mode 100644 index 0000000..f96d707 --- /dev/null +++ b/internal/dockerdesktop/settings_test.go @@ -0,0 +1,62 @@ +package dockerdesktop + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestSettingsWarnings(t *testing.T) { + healthy := Settings{ + UseVirtualizationFramework: true, + UseVirtualizationFrameworkRosetta: true, + MemoryMiB: 8192, + } + if got := healthy.Warnings(true); len(got) != 0 { + t.Errorf("healthy settings produced warnings: %v", got) + } + + dockerVMMAndLowMem, err := Parse([]byte(`{ + "UseVirtualizationFramework": false, + "UseVirtualizationFrameworkRosetta": true, + "MemoryMiB": 6144 + }`)) + if err != nil { + t.Fatal(err) + } + if got := dockerVMMAndLowMem.Warnings(false); len(got) != 0 { + t.Errorf("stack without linux/amd64 produced warnings: %v", got) + } + got := dockerVMMAndLowMem.Warnings(true) + if len(got) != 2 { + t.Fatalf("got %d warnings, want 2: %v", len(got), got) + } + if !strings.Contains(got[0], "Apple Virtualization framework") { + t.Errorf("warning[0] = %q", got[0]) + } + if !strings.Contains(got[1], "6144 MiB") { + t.Errorf("warning[1] = %q", got[1]) + } + + rosettaOff := Settings{UseVirtualizationFramework: true, UseVirtualizationFrameworkRosetta: false, MemoryMiB: 12288} + if w := rosettaOff.Warnings(true); len(w) != 1 || !strings.Contains(w[0], "Rosetta") { + t.Errorf("rosetta off warnings = %v", w) + } +} + +func TestComposeFilesSpecifyLinuxAmd64(t *testing.T) { + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, "a.yaml"), []byte("services:\n db:\n image: postgres:16\n"), 0o644); err != nil { + t.Fatal(err) + } + if ComposeFilesSpecifyLinuxAmd64(dir, []string{"a.yaml"}) { + t.Error("reported linux/amd64 when absent") + } + if err := os.WriteFile(filepath.Join(dir, "b.yaml"), []byte("x-fdy:\n platform: linux/amd64\n"), 0o644); err != nil { + t.Fatal(err) + } + if !ComposeFilesSpecifyLinuxAmd64(dir, []string{"a.yaml", "b.yaml"}) { + t.Error("missed linux/amd64 in b.yaml") + } +} diff --git a/internal/snapshot/snapshot.go b/internal/snapshot/snapshot.go new file mode 100644 index 0000000..62c9732 --- /dev/null +++ b/internal/snapshot/snapshot.go @@ -0,0 +1,532 @@ +// Package snapshot caches seeded Docker volumes on disk keyed by their seed +// inputs and schema stamp, so new worktrees restore a seeded database in +// seconds instead of running init from scratch. +package snapshot + +import ( + "bytes" + "compress/gzip" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "hash" + "io" + "io/fs" + "os" + "os/exec" + "path/filepath" + "slices" + "strings" + "time" + + "github.com/gofrs/flock" + + "github.com/faradayio/aviary/internal/compose" + "github.com/faradayio/aviary/internal/config" +) + +const ( + formatVersion = "aviary-snapshot-v1" + defaultStamp = "default" + manifestFile = "manifest.json" +) + +// Volume describes one Compose-managed named volume covered by a snapshot. +type Volume struct { + // Key is the logical volume name under the compose file's top-level + // `volumes:` section (such as "postgres"). + Key string + // DockerName is the project-prefixed Docker volume name (such as + // "aviary-wt-abc123_postgres"). + DockerName string + // Images lists every service image mounting Key, sorted. + Images []string + // ArchiveImage and ArchivePlatform identify the container image used to + // stream tar archives out of and into DockerName. + ArchiveImage string + ArchivePlatform string +} + +// Identity uniquely identifies what a stack's seeded volumes hold (`Key`) and +// the schema version they are at (`Stamp`). +type Identity struct { + Project string + Key string + Stamp string + Volumes []Volume +} + +// Hit describes a reusable snapshot found in the cache. +type Hit struct { + Stamp string + // Exact is true when Hit.Stamp equals Identity.Stamp, so no + // after_restore steps are needed. + Exact bool +} + +// Archiver streams volume contents between Docker volumes and compressed tar +// archives on the host filesystem. +type Archiver interface { + SaveVolume(vol Volume, archivePath string) error + RestoreVolume(project string, vol Volume, archivePath string) error +} + +// Store manages cached volume snapshots on disk. +type Store struct { + Dir string + Archiver Archiver + Now func() time.Time +} + +type manifest struct { + Key string `json:"key"` + Stamp string `json:"stamp"` + Volumes []string `json:"volumes"` + CreatedAt time.Time `json:"created_at"` +} + +// DefaultDir returns $XDG_CACHE_HOME/aviary/snapshots, falling back to +// ~/.cache/aviary/snapshots when XDG_CACHE_HOME is unset or relative. +func DefaultDir() (string, error) { + base := os.Getenv("XDG_CACHE_HOME") + if !filepath.IsAbs(base) { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + base = filepath.Join(home, ".cache") + } + return filepath.Join(base, "aviary", "snapshots"), nil +} + +// OpenDefault constructs a Store at DefaultDir backed by DockerArchiver. +func OpenDefault() (*Store, error) { + dir, err := DefaultDir() + if err != nil { + return nil, err + } + return &Store{Dir: dir, Archiver: DockerArchiver{}, Now: time.Now}, nil +} + +// BuildIdentity computes a stack's snapshot Key, Stamp, and volume bindings. +func BuildIdentity( + project, root string, + snap *config.Snapshot, + initSteps []config.Step, + resolved *compose.Resolved, +) (*Identity, error) { + names := slices.Clone(snap.Volumes) + slices.Sort(names) + names = slices.Compact(names) + + volumes := make([]Volume, 0, len(names)) + for _, key := range names { + rv, ok := resolved.Volumes[key] + if !ok || rv.Name == "" { + return nil, fmt.Errorf("snapshot volume %q is not defined in compose volumes", key) + } + images := resolved.ImagesMounting(key) + archiveImage, archivePlatform := resolved.ArchiveContainer(key) + if archiveImage == "" { + return nil, fmt.Errorf("snapshot volume %q is not mounted by any service with an image", key) + } + volumes = append(volumes, Volume{ + Key: key, + DockerName: rv.Name, + Images: images, + ArchiveImage: archiveImage, + ArchivePlatform: archivePlatform, + }) + } + + keyHash := sha256.New() + writeToken(keyHash, formatVersion) + writeToken(keyHash, "volumes") + for _, v := range volumes { + writeToken(keyHash, v.Key) + for _, img := range v.Images { + writeToken(keyHash, img) + } + writeToken(keyHash, "") + } + writeToken(keyHash, "init") + for _, step := range initSteps { + writeToken(keyHash, step.Service) + for _, arg := range step.Command { + writeToken(keyHash, arg) + } + writeToken(keyHash, "") + } + writeToken(keyHash, "key_files") + if err := hashPaths(keyHash, root, snap.KeyFiles); err != nil { + return nil, fmt.Errorf("snapshot key_files: %w", err) + } + + stamp := defaultStamp + if len(snap.StampFiles) > 0 { + stampHash := sha256.New() + writeToken(stampHash, formatVersion) + if err := hashPaths(stampHash, root, snap.StampFiles); err != nil { + return nil, fmt.Errorf("snapshot stamp_files: %w", err) + } + stamp = hex.EncodeToString(stampHash.Sum(nil))[:16] + } + + return &Identity{ + Project: project, + Key: hex.EncodeToString(keyHash.Sum(nil))[:16], + Stamp: stamp, + Volumes: volumes, + }, nil +} + +func writeToken(h hash.Hash, s string) { + _, _ = fmt.Fprintf(h, "%d:%s\n", len(s), s) +} + +func hashPaths(h hash.Hash, root string, relPaths []string) error { + sorted := slices.Clone(relPaths) + slices.Sort(sorted) + for _, rel := range sorted { + cleanRel := filepath.ToSlash(filepath.Clean(rel)) + full := filepath.Join(root, filepath.FromSlash(cleanRel)) + info, err := os.Lstat(full) + if err != nil { + return fmt.Errorf("%s: %w", cleanRel, err) + } + if !info.IsDir() { + if err := hashEntry(h, cleanRel, full, info); err != nil { + return err + } + continue + } + err = filepath.WalkDir(full, func(p string, d fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if d.IsDir() { + return nil + } + fi, err := d.Info() + if err != nil { + return err + } + entryRel, err := filepath.Rel(root, p) + if err != nil { + return err + } + return hashEntry(h, filepath.ToSlash(entryRel), p, fi) + }) + if err != nil { + return err + } + } + return nil +} + +func hashEntry(h hash.Hash, slashRel, full string, info fs.FileInfo) error { + writeToken(h, slashRel) + if info.Mode()&os.ModeSymlink != 0 { + target, err := os.Readlink(full) + if err != nil { + return err + } + writeToken(h, "symlink:"+target) + return nil + } + f, err := os.Open(full) + if err != nil { + return err + } + defer f.Close() + writeToken(h, fmt.Sprintf("file:%d", info.Size())) + if _, err := io.Copy(h, f); err != nil { + return err + } + _, _ = h.Write([]byte("\n")) + return nil +} + +func (s *Store) now() time.Time { + if s.Now != nil { + return s.Now() + } + return time.Now() +} + +func (s *Store) lockKey(key string) (func() error, error) { + if err := os.MkdirAll(s.Dir, 0o755); err != nil { + return nil, err + } + lock := flock.New(filepath.Join(s.Dir, key+".lock")) + if err := lock.Lock(); err != nil { + return nil, fmt.Errorf("lock %s: %w", lock.Path(), err) + } + return lock.Unlock, nil +} + +func (s *Store) stampDir(key, stamp string) string { + return filepath.Join(s.Dir, key, stamp) +} + +func (s *Store) archivePath(key, stamp, volumeKey string) string { + return filepath.Join(s.stampDir(key, stamp), volumeKey+".tar.gz") +} + +// Lookup finds a cached snapshot for id. An exact (Key, Stamp) match wins; when +// none exists and allowDifferentStamp is true, the newest valid snapshot under +// id.Key is returned with Exact=false so the caller can run after_restore. +func (s *Store) Lookup(id *Identity, allowDifferentStamp bool) (*Hit, error) { + unlock, err := s.lockKey(id.Key) + if err != nil { + return nil, err + } + defer unlock() + + if _, ok := s.readValidManifest(id, id.Stamp); ok { + return &Hit{Stamp: id.Stamp, Exact: true}, nil + } + if !allowDifferentStamp { + return nil, nil + } + entries, err := os.ReadDir(filepath.Join(s.Dir, id.Key)) + if os.IsNotExist(err) { + return nil, nil + } + if err != nil { + return nil, err + } + var best *manifest + for _, entry := range entries { + if !entry.IsDir() { + continue + } + m, ok := s.readValidManifest(id, entry.Name()) + if !ok { + continue + } + if best == nil || m.CreatedAt.After(best.CreatedAt) { + mCopy := m + best = &mCopy + } + } + if best == nil { + return nil, nil + } + return &Hit{Stamp: best.Stamp, Exact: false}, nil +} + +func (s *Store) readValidManifest(id *Identity, stamp string) (manifest, bool) { + dir := s.stampDir(id.Key, stamp) + raw, err := os.ReadFile(filepath.Join(dir, manifestFile)) + if err != nil { + return manifest{}, false + } + var m manifest + if err := json.Unmarshal(raw, &m); err != nil { + return manifest{}, false + } + if m.Key != id.Key || m.Stamp != stamp { + return manifest{}, false + } + for _, v := range id.Volumes { + if !slices.Contains(m.Volumes, v.Key) { + return manifest{}, false + } + if fi, err := os.Stat(s.archivePath(id.Key, stamp, v.Key)); err != nil || fi.IsDir() { + return manifest{}, false + } + } + return m, true +} + +// Restore populates each of id's Docker volumes from the snapshot at stamp. +func (s *Store) Restore(id *Identity, stamp string) error { + unlock, err := s.lockKey(id.Key) + if err != nil { + return err + } + defer unlock() + + for _, vol := range id.Volumes { + archive := s.archivePath(id.Key, stamp, vol.Key) + if err := s.Archiver.RestoreVolume(id.Project, vol, archive); err != nil { + return fmt.Errorf("restoring volume %s: %w", vol.Key, err) + } + } + return nil +} + +// Save archives each of id's Docker volumes under (id.Key, id.Stamp) and writes +// manifest.json atomically once every archive is on disk. +func (s *Store) Save(id *Identity) error { + unlock, err := s.lockKey(id.Key) + if err != nil { + return err + } + defer unlock() + + dir := s.stampDir(id.Key, id.Stamp) + if err := os.MkdirAll(dir, 0o755); err != nil { + return err + } + manifestPath := filepath.Join(dir, manifestFile) + _ = os.Remove(manifestPath) + + volKeys := make([]string, 0, len(id.Volumes)) + for _, vol := range id.Volumes { + archive := s.archivePath(id.Key, id.Stamp, vol.Key) + if err := s.Archiver.SaveVolume(vol, archive); err != nil { + return fmt.Errorf("saving volume %s: %w", vol.Key, err) + } + volKeys = append(volKeys, vol.Key) + } + + raw, err := json.MarshalIndent(manifest{ + Key: id.Key, + Stamp: id.Stamp, + Volumes: volKeys, + CreatedAt: s.now().UTC(), + }, "", " ") + if err != nil { + return err + } + return writeAtomic(manifestPath, raw) +} + +func writeAtomic(path string, raw []byte) error { + tmp := path + ".tmp" + f, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) + if err != nil { + return err + } + if _, err := f.Write(raw); err != nil { + f.Close() + _ = os.Remove(tmp) + return err + } + if err := f.Sync(); err != nil { + f.Close() + _ = os.Remove(tmp) + return err + } + if err := f.Close(); err != nil { + _ = os.Remove(tmp) + return err + } + return os.Rename(tmp, path) +} + +// DockerArchiver streams tar archives to and from Docker volumes using a +// one-off container of the service image that mounts the volume, compressing +// with gzip on the host. +type DockerArchiver struct{} + +func (DockerArchiver) SaveVolume(vol Volume, archivePath string) error { + tmp := archivePath + ".tmp" + outFile, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) + if err != nil { + return err + } + cleanup := func() { + outFile.Close() + _ = os.Remove(tmp) + } + + gz, err := gzip.NewWriterLevel(outFile, gzip.BestSpeed) + if err != nil { + cleanup() + return err + } + + args := []string{"run", "--rm"} + if vol.ArchivePlatform != "" { + args = append(args, "--platform", vol.ArchivePlatform) + } + args = append(args, + "--user", "0:0", + "--entrypoint", "tar", + "-v", vol.DockerName+":/volume:ro", + vol.ArchiveImage, + "-cf", "-", "-C", "/volume", ".", + ) + var stderr bytes.Buffer + cmd := exec.Command("docker", args...) + cmd.Stdout = gz + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + cleanup() + return fmt.Errorf("docker run tar: %w (%s)", err, strings.TrimSpace(stderr.String())) + } + if err := gz.Close(); err != nil { + cleanup() + return err + } + if err := outFile.Sync(); err != nil { + cleanup() + return err + } + if err := outFile.Close(); err != nil { + _ = os.Remove(tmp) + return err + } + return os.Rename(tmp, archivePath) +} + +func (DockerArchiver) RestoreVolume(project string, vol Volume, archivePath string) error { + if err := ensureComposeVolume(project, vol); err != nil { + return err + } + inFile, err := os.Open(archivePath) + if err != nil { + return err + } + defer inFile.Close() + + gz, err := gzip.NewReader(inFile) + if err != nil { + return err + } + defer gz.Close() + + args := []string{"run", "--rm", "-i"} + if vol.ArchivePlatform != "" { + args = append(args, "--platform", vol.ArchivePlatform) + } + args = append(args, + "--user", "0:0", + "--entrypoint", "sh", + "-v", vol.DockerName+":/volume", + vol.ArchiveImage, + "-c", "find /volume -mindepth 1 -delete && exec tar -xpf - -C /volume", + ) + var stderr bytes.Buffer + cmd := exec.Command("docker", args...) + cmd.Stdin = gz + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("docker run untar: %w (%s)", err, strings.TrimSpace(stderr.String())) + } + return nil +} + +// ensureComposeVolume creates vol.DockerName with Docker Compose's ownership +// labels when it does not exist yet, so `docker compose up` adopts it without +// warning and `aviary rm` / `aviary gc` removes it by project label. +func ensureComposeVolume(project string, vol Volume) error { + if err := exec.Command("docker", "volume", "inspect", vol.DockerName).Run(); err == nil { + return nil + } + var stderr bytes.Buffer + cmd := exec.Command("docker", "volume", "create", + "--label", "com.docker.compose.project="+project, + "--label", "com.docker.compose.volume="+vol.Key, + vol.DockerName, + ) + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("docker volume create %s: %w (%s)", vol.DockerName, err, strings.TrimSpace(stderr.String())) + } + return nil +} diff --git a/internal/snapshot/snapshot_test.go b/internal/snapshot/snapshot_test.go new file mode 100644 index 0000000..706ed7c --- /dev/null +++ b/internal/snapshot/snapshot_test.go @@ -0,0 +1,189 @@ +package snapshot + +import ( + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/faradayio/aviary/internal/compose" + "github.com/faradayio/aviary/internal/config" +) + +type memoryArchiver struct { + volumes map[string]string +} + +func newMemoryArchiver() *memoryArchiver { + return &memoryArchiver{volumes: map[string]string{}} +} + +func (m *memoryArchiver) SaveVolume(vol Volume, archivePath string) error { + return os.WriteFile(archivePath, []byte(m.volumes[vol.DockerName]), 0o600) +} + +func (m *memoryArchiver) RestoreVolume(project string, vol Volume, archivePath string) error { + raw, err := os.ReadFile(archivePath) + if err != nil { + return err + } + m.volumes[vol.DockerName] = string(raw) + return nil +} + +func resolvedFixture(project, image string) *compose.Resolved { + return &compose.Resolved{ + Services: map[string]compose.ResolvedService{ + "postgres": { + Image: image, + Platform: "linux/amd64", + Volumes: []compose.ResolvedMount{{Type: "volume", Source: "postgres"}}, + }, + }, + Volumes: map[string]compose.ResolvedVolume{ + "postgres": {Name: project + "_postgres"}, + }, + } +} + +func TestBuildIdentityChangesOnSeedImageInitOrStampChanges(t *testing.T) { + root := t.TempDir() + seedDir := filepath.Join(root, "seeds") + if err := os.MkdirAll(seedDir, 0o755); err != nil { + t.Fatal(err) + } + seedFile := filepath.Join(seedDir, "users.sql") + schemaFile := filepath.Join(root, "structure.sql") + if err := os.WriteFile(seedFile, []byte("INSERT INTO users VALUES (1);\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(schemaFile, []byte("CREATE TABLE users (id int);\n"), 0o644); err != nil { + t.Fatal(err) + } + snap := &config.Snapshot{ + Volumes: []string{"postgres"}, + KeyFiles: []string{"seeds"}, + StampFiles: []string{"structure.sql"}, + } + steps := []config.Step{{Service: "db", Command: []string{"psql", "-f", "seeds/users.sql"}}} + + base, err := BuildIdentity("aviary-a", root, snap, steps, resolvedFixture("aviary-a", "postgres:17")) + if err != nil { + t.Fatal(err) + } + otherWorktree, err := BuildIdentity("aviary-b", root, snap, steps, resolvedFixture("aviary-b", "postgres:17")) + if err != nil { + t.Fatal(err) + } + if base.Key != otherWorktree.Key || base.Stamp != otherWorktree.Stamp { + t.Fatalf("two projects with identical files produced different keys/stamps: %+v vs %+v", base, otherWorktree) + } + + // Schema change alters Stamp, keeping Key intact so after_restore can run. + if err := os.WriteFile(schemaFile, []byte("CREATE TABLE users (id int, name text);\n"), 0o644); err != nil { + t.Fatal(err) + } + migrated, err := BuildIdentity("aviary-a", root, snap, steps, resolvedFixture("aviary-a", "postgres:17")) + if err != nil { + t.Fatal(err) + } + if migrated.Key != base.Key || migrated.Stamp == base.Stamp { + t.Fatalf("schema change: key %s (was %s), stamp %s (was %s)", migrated.Key, base.Key, migrated.Stamp, base.Stamp) + } + + // Image upgrade alters Key. + newImage, err := BuildIdentity("aviary-a", root, snap, steps, resolvedFixture("aviary-a", "postgres:18")) + if err != nil { + t.Fatal(err) + } + if newImage.Key == base.Key { + t.Fatal("mounting image change did not change Key") + } + + // Seed file edit alters Key. + if err := os.WriteFile(seedFile, []byte("INSERT INTO users VALUES (1), (2);\n"), 0o644); err != nil { + t.Fatal(err) + } + newSeed, err := BuildIdentity("aviary-a", root, snap, steps, resolvedFixture("aviary-a", "postgres:17")) + if err != nil { + t.Fatal(err) + } + if newSeed.Key == base.Key { + t.Fatal("seed file change did not change Key") + } +} + +func TestBuildIdentityRejectsUnknownOrUnmountedVolume(t *testing.T) { + root := t.TempDir() + _, err := BuildIdentity("aviary-a", root, &config.Snapshot{Volumes: []string{"missing"}}, nil, resolvedFixture("aviary-a", "postgres:17")) + if err == nil || !strings.Contains(err.Error(), `"missing"`) { + t.Fatalf("expected missing volume error, got %v", err) + } +} + +func TestStoreSaveLookupAndRestore(t *testing.T) { + arch := newMemoryArchiver() + now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC) + store := &Store{ + Dir: t.TempDir(), + Archiver: arch, + Now: func() time.Time { return now }, + } + + idV1 := &Identity{ + Project: "aviary-one", + Key: "seedkey1", + Stamp: "stamp_v1", + Volumes: []Volume{{Key: "postgres", DockerName: "aviary-one_postgres", ArchiveImage: "postgres:17"}}, + } + arch.volumes["aviary-one_postgres"] = "seeded-rows-v1" + if err := store.Save(idV1); err != nil { + t.Fatal(err) + } + + // Exact hit in a second worktree on the same schema. + wt2V1 := &Identity{ + Project: "aviary-two", + Key: "seedkey1", + Stamp: "stamp_v1", + Volumes: []Volume{{Key: "postgres", DockerName: "aviary-two_postgres", ArchiveImage: "postgres:17"}}, + } + hit, err := store.Lookup(wt2V1, false) + if err != nil || hit == nil || !hit.Exact || hit.Stamp != "stamp_v1" { + t.Fatalf("Lookup exact = %+v, %v", hit, err) + } + if err := store.Restore(wt2V1, hit.Stamp); err != nil { + t.Fatal(err) + } + if got := arch.volumes["aviary-two_postgres"]; got != "seeded-rows-v1" { + t.Fatalf("restored volume = %q, want seeded-rows-v1", got) + } + + // Newer schema misses without after_restore, hits newest stamp with after_restore. + now = now.Add(time.Minute) + idV2 := &Identity{ + Project: "aviary-one", + Key: "seedkey1", + Stamp: "stamp_v2", + Volumes: []Volume{{Key: "postgres", DockerName: "aviary-one_postgres", ArchiveImage: "postgres:17"}}, + } + arch.volumes["aviary-one_postgres"] = "seeded-rows-v2" + if err := store.Save(idV2); err != nil { + t.Fatal(err) + } + + wt3V3 := &Identity{ + Project: "aviary-three", + Key: "seedkey1", + Stamp: "stamp_v3", + Volumes: []Volume{{Key: "postgres", DockerName: "aviary-three_postgres", ArchiveImage: "postgres:17"}}, + } + if miss, err := store.Lookup(wt3V3, false); err != nil || miss != nil { + t.Fatalf("Lookup without after_restore = %+v, %v; want nil", miss, err) + } + hit, err = store.Lookup(wt3V3, true) + if err != nil || hit == nil || hit.Exact || hit.Stamp != "stamp_v2" { + t.Fatalf("Lookup with after_restore = %+v, %v; want non-exact stamp_v2", hit, err) + } +} diff --git a/internal/stack/stack.go b/internal/stack/stack.go index b9681f7..4524f53 100644 --- a/internal/stack/stack.go +++ b/internal/stack/stack.go @@ -17,7 +17,9 @@ import ( "github.com/faradayio/aviary/internal/compose" "github.com/faradayio/aviary/internal/config" + "github.com/faradayio/aviary/internal/dockerdesktop" "github.com/faradayio/aviary/internal/registry" + "github.com/faradayio/aviary/internal/snapshot" "github.com/faradayio/aviary/internal/vault" "github.com/faradayio/aviary/internal/workspace" ) @@ -25,6 +27,9 @@ import ( type Stack struct { Config *config.Config Workspace workspace.Workspace + // Cwd is the caller's working directory, used to rewrite repo-relative + // test path arguments against a suite's Workdir. + Cwd string // Entry is this stack's registry entry: its host ports and whether // aviary has recorded that its init steps ran. It is the zero Entry, // with no ports, when Load ran with NoAllocate for a stack that is not @@ -37,8 +42,15 @@ type Stack struct { resolvedEnv map[string]string // runner is Options.Runner. runner func(c compose.Stack, args ...string) error + // resolver is Options.Resolver. + resolver func(c compose.Stack) (*compose.Resolved, error) // hostRunner is Options.HostRunner. hostRunner func(dir string, extraEnv map[string]string, command []string) error + // snapshots caches seeded volumes when Config.Snapshot is set. + snapshots *snapshot.Store + // dockerSettings holds local Docker Desktop settings on Apple silicon. + dockerSettings *dockerdesktop.Settings + warnedDocker bool // prepared is set once this Stack has run the prepare steps. prepared bool // stderr receives aviary's progress lines for init steps. @@ -62,9 +74,14 @@ type Options struct { // defaults to compose.Stack.Run; tests replace it to record argv // instead of calling Docker. Runner func(c compose.Stack, args ...string) error + // Resolver resolves `docker compose config --format json` for snapshot + // identity calculation. Defaults to compose.Stack.Config. + Resolver func(c compose.Stack) (*compose.Resolved, error) // HostRunner runs a command on the host in dir. It defaults to // compose.RunHost; tests replace it to record host commands. HostRunner func(dir string, extraEnv map[string]string, command []string) error + // SnapshotStore overrides the default XDG snapshot cache during tests. + SnapshotStore *snapshot.Store // VaultToken overrides Vault token minting during tests. When nil and // cfg.Vault is set, vault.NewClient(cfg.Vault).Token is called. VaultToken func(cfg *config.Vault) (string, error) @@ -126,22 +143,56 @@ func Load(cwd, configDir string, opts Options) (*Stack, error) { if runner == nil { runner = compose.Stack.Run } + resolver := opts.Resolver + if resolver == nil { + resolver = compose.Stack.Config + } hostRunner := opts.HostRunner if hostRunner == nil { hostRunner = compose.RunHost } + callerDir := cwd + if abs, err := filepath.Abs(cwd); err == nil { + if resolved, err := filepath.EvalSymlinks(abs); err == nil { + callerDir = resolved + } else { + callerDir = abs + } + } + var dockerSettings *dockerdesktop.Settings + if !opts.SkipRequireEnv { + dockerSettings = dockerdesktop.LoadDefault() + } return &Stack{ - Config: cfg, - Workspace: ws, - Entry: entry, - registry: reg, - resolvedEnv: resolvedEnv, - runner: runner, - hostRunner: hostRunner, - stderr: os.Stderr, + Config: cfg, + Workspace: ws, + Cwd: callerDir, + Entry: entry, + registry: reg, + resolvedEnv: resolvedEnv, + runner: runner, + resolver: resolver, + hostRunner: hostRunner, + snapshots: opts.SnapshotStore, + dockerSettings: dockerSettings, + stderr: os.Stderr, }, nil } +// WarnDockerDesktop prints one-line warnings to stderr at most once when +// linux/amd64 compose services run on Apple silicon without Rosetta or enough +// VM memory. +func (s *Stack) WarnDockerDesktop() { + if s.warnedDocker || s.dockerSettings == nil { + return + } + s.warnedDocker = true + hasAmd64 := dockerdesktop.ComposeFilesSpecifyLinuxAmd64(s.Workspace.Definition, s.Config.Compose.Files) + for _, w := range s.dockerSettings.Warnings(hasAmd64) { + fmt.Fprintf(s.stderr, "aviary: warning: %s\n", w) + } +} + func defaultVaultToken(cfg *config.Vault) (string, error) { client, err := vault.NewClient(cfg) if err != nil { @@ -338,15 +389,94 @@ func (s *Stack) Init(force bool) error { if s.Entry.InitializedAt != nil && !force { return nil } + s.WarnDockerDesktop() if err := s.prepareLocked(); err != nil { return err } + if s.Config.Snapshot != nil { + return s.initWithSnapshot(force) + } if err := s.runSteps("init", s.Config.Init); err != nil { return err } return s.markInitialized(true) } +func (s *Stack) initWithSnapshot(force bool) error { + resolveCompose := s.Compose() + resolveCompose.Profiles = []string{"*"} + resolveFn := s.resolver + if resolveFn == nil { + resolveFn = compose.Stack.Config + } + resolved, err := resolveFn(resolveCompose) + if err != nil { + return fmt.Errorf("resolving compose config for snapshot: %w", err) + } + id, err := snapshot.BuildIdentity(s.Workspace.Project(), s.Workspace.Root, s.Config.Snapshot, s.Config.Init, resolved) + if err != nil { + return err + } + store := s.snapshots + if store == nil { + store, err = snapshot.OpenDefault() + if err != nil { + return err + } + } + + if !force { + hit, err := store.Lookup(id, len(s.Config.AfterRestore) > 0) + if err != nil { + return err + } + if hit != nil { + restoreStart := time.Now() + if hit.Exact { + fmt.Fprintf(s.stderr, "aviary: init: restoring snapshot %s\n", hit.Stamp) + } else { + fmt.Fprintf(s.stderr, "aviary: init: restoring snapshot %s (schema changed; after_restore will run)\n", hit.Stamp) + } + if err := s.run(nil, "down", "--remove-orphans"); err != nil { + return fmt.Errorf("stopping stack before snapshot restore: %v", err) + } + if err := store.Restore(id, hit.Stamp); err != nil { + return err + } + fmt.Fprintf(s.stderr, "aviary: init: snapshot restored in %s\n", seconds(time.Since(restoreStart))) + if !hit.Exact { + if err := s.runSteps("after_restore", s.Config.AfterRestore); err != nil { + return err + } + if err := s.checkpointAndSaveSnapshot(store, id); err != nil { + return err + } + } + return s.markInitialized(true) + } + } + + if err := s.runSteps("init", s.Config.Init); err != nil { + return err + } + if err := s.checkpointAndSaveSnapshot(store, id); err != nil { + return err + } + return s.markInitialized(true) +} + +func (s *Stack) checkpointAndSaveSnapshot(store *snapshot.Store, id *snapshot.Identity) error { + if err := s.run(nil, "down", "--remove-orphans"); err != nil { + return fmt.Errorf("stopping stack before saving snapshot: %v", err) + } + saveStart := time.Now() + if err := store.Save(id); err != nil { + return err + } + fmt.Fprintf(s.stderr, "aviary: init: saved snapshot %s in %s\n", id.Stamp, seconds(time.Since(saveStart))) + return nil +} + // runSteps runs steps in order and stops at the first failure. It reports // each step as it starts, and its wall-clock time and the total as they // succeed. The error names the step and is deliberately not a @@ -403,6 +533,9 @@ func (s *Stack) Test(name string, extra []string) error { return err } } + if len(suite.Needs) > 0 || !suite.Host { + s.WarnDockerDesktop() + } if len(suite.Needs) > 0 { args := append([]string{"up", "--detach", "--wait"}, suite.Needs...) if err := s.run(suite.Profiles, args...); err != nil { @@ -412,6 +545,11 @@ func (s *Stack) Test(name string, extra []string) error { if len(extra) > 0 && extra[0] == "--" { extra = extra[1:] } + callerDir := s.Cwd + if callerDir == "" { + callerDir = s.Workspace.Root + } + extra = suite.NormalizeArgs(s.Workspace.Root, callerDir, extra) if suite.Host { dir := s.Workspace.Root if suite.Workdir != "" { diff --git a/internal/stack/stack_test.go b/internal/stack/stack_test.go index 82bb658..05b23fe 100644 --- a/internal/stack/stack_test.go +++ b/internal/stack/stack_test.go @@ -20,7 +20,9 @@ import ( "github.com/faradayio/aviary/internal/compose" "github.com/faradayio/aviary/internal/config" + "github.com/faradayio/aviary/internal/dockerdesktop" "github.com/faradayio/aviary/internal/registry" + "github.com/faradayio/aviary/internal/snapshot" "github.com/faradayio/aviary/internal/workspace" ) @@ -929,3 +931,210 @@ func TestPrepareAndHostSuiteExpandPortVariables(t *testing.T) { t.Errorf("suite call = %+v", hostCalls[1]) } } + +type fakeArchiver struct { + volumes map[string]string +} + +func (f *fakeArchiver) SaveVolume(vol snapshot.Volume, archivePath string) error { + return os.WriteFile(archivePath, []byte(f.volumes[vol.DockerName]), 0o600) +} + +func (f *fakeArchiver) RestoreVolume(project string, vol snapshot.Volume, archivePath string) error { + raw, err := os.ReadFile(archivePath) + if err != nil { + return err + } + f.volumes[vol.DockerName] = string(raw) + return nil +} + +func TestSnapshotSavesOnFirstInitRestoresExactAndRunsAfterRestoreOnStampChange(t *testing.T) { + arch := &fakeArchiver{volumes: map[string]string{}} + store := &snapshot.Store{Dir: t.TempDir(), Archiver: arch} + reg := testRegistry(t) + + makeWorktree := func(name, schema string) (*Stack, *recorder) { + root := tempDir(t) + if err := os.WriteFile(filepath.Join(root, "seed.sql"), []byte("INSERT 1;\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(root, "structure.sql"), []byte(schema), 0o644); err != nil { + t.Fatal(err) + } + cfg := &config.Config{ + Compose: config.Compose{Files: []string{"compose.yaml"}}, + Init: []config.Step{{Service: "diamond", Command: []string{"rake", "db:reset"}}}, + AfterRestore: []config.Step{{Service: "diamond", Command: []string{"rake", "db:migrate"}}}, + Snapshot: &config.Snapshot{ + Volumes: []string{"postgres"}, + KeyFiles: []string{"seed.sql"}, + StampFiles: []string{"structure.sql"}, + }, + } + ws := workspace.Workspace{Root: root, Definition: root, Slug: name} + entry, err := reg.Allocate(ws.Project(), ws.Root, "", nil) + if err != nil { + t.Fatal(err) + } + rec := &recorder{} + s := &Stack{ + Config: cfg, + Workspace: ws, + Cwd: root, + Entry: entry, + registry: reg, + snapshots: store, + stderr: io.Discard, + runner: func(c compose.Stack, args ...string) error { + if slices.Equal(args, []string{"run", "--rm", "diamond", "rake", "db:reset"}) { + arch.volumes[c.Project+"_postgres"] = "seeded-v1" + } + if slices.Equal(args, []string{"run", "--rm", "diamond", "rake", "db:migrate"}) { + arch.volumes[c.Project+"_postgres"] += "+migrated-v2" + } + return rec.run(c, args...) + }, + resolver: func(c compose.Stack) (*compose.Resolved, error) { + return &compose.Resolved{ + Services: map[string]compose.ResolvedService{ + "postgres": { + Image: "postgis/postgis:17-3.5-alpine", + Volumes: []compose.ResolvedMount{{Type: "volume", Source: "postgres"}}, + }, + }, + Volumes: map[string]compose.ResolvedVolume{ + "postgres": {Name: c.Project + "_postgres"}, + }, + }, nil + }, + } + return s, rec + } + + // First worktree misses the cache, runs init, checkpoints down, and saves. + wt1, rec1 := makeWorktree("wt1-111111", "CREATE TABLE t (id int);\n") + if err := wt1.Init(false); err != nil { + t.Fatal(err) + } + if len(rec1.calls) != 2 || !slices.Contains(rec1.calls[0], "db:reset") || !slices.Contains(rec1.calls[1], "down") { + t.Fatalf("wt1 calls = %q, want db:reset then down", rec1.calls) + } + + // Second worktree on the same schema restores directly with zero init steps. + wt2, rec2 := makeWorktree("wt2-222222", "CREATE TABLE t (id int);\n") + if err := wt2.Init(false); err != nil { + t.Fatal(err) + } + if len(rec2.calls) != 1 || !slices.Contains(rec2.calls[0], "down") { + t.Fatalf("wt2 calls = %q, want only down before restore", rec2.calls) + } + if got := arch.volumes["aviary-wt2-222222_postgres"]; got != "seeded-v1" { + t.Fatalf("wt2 volume = %q, want seeded-v1", got) + } + + // Third worktree on a newer schema restores v1, runs after_restore, and saves v2. + wt3, rec3 := makeWorktree("wt3-333333", "CREATE TABLE t (id int, name text);\n") + if err := wt3.Init(false); err != nil { + t.Fatal(err) + } + if len(rec3.calls) != 3 || !slices.Contains(rec3.calls[1], "db:migrate") { + t.Fatalf("wt3 calls = %q, want down, db:migrate, down", rec3.calls) + } + if got := arch.volumes["aviary-wt3-333333_postgres"]; got != "seeded-v1+migrated-v2" { + t.Fatalf("wt3 volume = %q, want seeded-v1+migrated-v2", got) + } + + // Fourth worktree on that same newer schema now hits v2 directly without after_restore. + wt4, rec4 := makeWorktree("wt4-444444", "CREATE TABLE t (id int, name text);\n") + if err := wt4.Init(false); err != nil { + t.Fatal(err) + } + if len(rec4.calls) != 1 || !slices.Contains(rec4.calls[0], "down") { + t.Fatalf("wt4 calls = %q, want exact hit without after_restore", rec4.calls) + } + if got := arch.volumes["aviary-wt4-444444_postgres"]; got != "seeded-v1+migrated-v2" { + t.Fatalf("wt4 volume = %q, want seeded-v1+migrated-v2", got) + } +} + +func TestTestStripsSuiteWorkdirPrefixAndWarnsDockerDesktopOnlyForContainers(t *testing.T) { + root := tempDir(t) + if err := os.WriteFile(filepath.Join(root, "compose.yaml"), []byte("x-fdy:\n platform: linux/amd64\n"), 0o644); err != nil { + t.Fatal(err) + } + noInit := false + noPrepare := false + cfg := &config.Config{ + Compose: config.Compose{Files: []string{"compose.yaml"}}, + Suites: map[string]config.Suite{ + "popsicle": { + Host: true, + Workdir: "src/popsicle", + Init: &noInit, + Prepare: &noPrepare, + Command: []string{"pnpm", "test"}, + }, + "model_train-file": { + Service: "model_train", + Workdir: "workers/model_train", + Init: &noInit, + Prepare: &noPrepare, + Command: []string{"uv", "run", "pytest"}, + }, + }, + } + ws := workspace.Workspace{Root: root, Definition: root, Slug: "wt-abc123"} + reg := testRegistry(t) + entry, err := reg.Allocate(ws.Project(), ws.Root, "", nil) + if err != nil { + t.Fatal(err) + } + var stderr bytes.Buffer + var hostCmd []string + var composeArgs []string + s := &Stack{ + Config: cfg, + Workspace: ws, + Cwd: root, + Entry: entry, + registry: reg, + stderr: &stderr, + dockerSettings: &dockerdesktop.Settings{ + UseVirtualizationFramework: false, + UseVirtualizationFrameworkRosetta: true, + MemoryMiB: 6144, + }, + hostRunner: func(dir string, extraEnv map[string]string, command []string) error { + hostCmd = command + return nil + }, + runner: func(c compose.Stack, args ...string) error { + composeArgs = args + return nil + }, + } + + // Pure host suite with no container needs emits no Docker Desktop warning. + if err := s.Test("popsicle", []string{"--", "src/popsicle/src/js/Foo.test.tsx"}); err != nil { + t.Fatal(err) + } + if !slices.Equal(hostCmd, []string{"pnpm", "test", "src/js/Foo.test.tsx"}) { + t.Errorf("popsicle host command = %q", hostCmd) + } + if strings.Contains(stderr.String(), "warning:") { + t.Errorf("pure host suite emitted Docker warning: %s", stderr.String()) + } + + // Container suite strips prefix and emits the Rosetta + memory warnings. + stderr.Reset() + if err := s.Test("model_train-file", []string{"--", "workers/model_train/tests/citus/test_insights.py::test_x"}); err != nil { + t.Fatal(err) + } + if !slices.Equal(composeArgs, []string{"run", "--rm", "model_train", "uv", "run", "pytest", "tests/citus/test_insights.py::test_x"}) { + t.Errorf("container suite args = %q", composeArgs) + } + if !strings.Contains(stderr.String(), "Apple Virtualization framework") || !strings.Contains(stderr.String(), "6144 MiB") { + t.Errorf("container suite stderr missing Docker Desktop warnings:\n%s", stderr.String()) + } +}