Files
onyx/tools/ods/integration/stack_live_test.go
T
roshan a8033533d1 refactor(ods): unify launchers and protect shared infrastructure (#16246)
## Description

Make ODS reuse shared development infrastructure by default, with
`--isolated` for separate datasets.

- Use the same service specifications and launchers for `stack`, `web
dev`, and `backend` commands. Remove the duplicate `stack
--frontend-only` option.
- Enforce project ownership across checkouts and resolve database
commands to the actual managed database.
- Stop services before pending migrations, preserve them when already at
head, and reject manual migrations beneath managed services.
- Harden startup cancellation, shutdown grace, descendant cleanup,
terminal input, and frontend API validation. Preserve Windows foreground
commands.
- Consolidate environment helpers, protect symlinked environment files,
propagate restore errors, and update help and documentation.

This PR contains only ODS and its development-stack skill. It does not
include the plugins implementation.

## How Has This Been Tested?

- Targeted command and regression tests using fake tools and disposable
Git repositories.
- Race tests for runtime targeting, ownership, migration ordering,
process cleanup, and terminal input.
- `go vet ./...`, pre-commit checks, native builds, and Windows
cross-builds.
- The full Go suite was run. Its remaining failures are the existing
macOS devcontainer SSH-mount fixture expectations in
`TestDevcontainerCommands` and `TestRunDevRebuild`.

No real database migrations, restores, or development-stack restarts
ran. Native Windows execution remains untested.

## Additional Options

- [ ] [Optional] Please cherry-pick this PR to the latest release
version.
- [x] [Optional] Override Linear Check

<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
ODS now shares development infrastructure across worktrees by default,
with `--isolated` for separate datasets. `stack`, `web dev`, and
`backend` use the same service specs and launchers, and the duplicate
`stack --frontend-only` option is removed.

- Direct launches run the same dependency-prepare step as stack startup,
so `backend` and `web dev` work with fresh or stale Python environments.
- A stopped preview discards its former project state before retargeting
to another API owner.
- Enforces project ownership across checkouts and resolves database
commands to the managed database. New tests reject conflicting targets
before any mutation.
- Stops services before pending migrations, preserves them when already
at head, and rejects manual migrations beneath managed services.
Migration probe failures fail closed.
- Hardens startup cancellation, shutdown grace, and descendant cleanup;
ODS keeps the terminal foreground group so Ctrl-C still honors shutdown
timeouts, and tmux pane identifiers no longer change service identity.
- Database passwords reach tools through the child environment only,
never in process arguments; Windows foreground commands are preserved.
- Consolidates environment helpers, protects symlinked env files,
propagates restore errors, and refreshes help and documentation. Test
commits exercise the new migration-target and preview-launch paths.

<sup>Written for commit 14500cb051.
Summary will update on new commits.</sup>

<a
href="https://cubic.dev/pr/onyx-dot-app/onyx/pull/16246?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="View guided diff"
src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>
<a
href="https://www.cubic.dev/action/auto-fix/pr/onyx-dot-app/onyx/16246?returnTo=https%3A%2F%2Fgithub.com%2Fonyx-dot-app%2Fonyx%2Fpull%2F16246&source=description"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img
alt="Turn on auto-fix"
src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a>

<!-- End of auto-generated description by cubic. -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added the global `--isolated` option for separate development stacks;
shared infrastructure is the default, with clearer project ownership and
status information.
* Added `ods web dev` previews that can use a healthy managed API on
macOS and Linux, plus foreground startup on Windows.
* Expanded command guidance for development workflows, database
targeting, and dump formats.
* **Bug Fixes**
* Pending migrations now stop managed application services first;
healthy services remain running when no migrations are needed.
* Database operations verify project ownership and target selection
before making changes. Restore errors now fail the command, SQL restores
stop on errors, and temporary files are cleaned up after failures.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-10-19 20:07:02 +00:00

319 lines
9.1 KiB
Go

//go:build integration
package integration
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
"time"
"github.com/onyx-dot-app/onyx/tools/ods/internal/devstack"
)
const testPassword = "TestPassword123!"
type liveStack struct {
root string
binary string
client *http.Client
}
func TestStackLiveLifecycle(t *testing.T) {
a := configuredStack(t, "ODS_STACK_LIVE_ROOT_A")
b := configuredStack(t, "ODS_STACK_LIVE_ROOT_B")
if a.root == b.root {
t.Fatal("live roots must be different disposable worktrees")
}
if os.Getenv("ODS_STACK_KEEP_RUNNING") != "1" {
t.Cleanup(func() {
for _, stack := range []liveStack{a, b} {
if _, err := stack.command("stack", "down"); err != nil {
t.Errorf("cleanup %s: %v", stack.root, err)
}
}
})
}
stacks := []liveStack{a, b}
results := make([][]byte, len(stacks))
startupErrors := make([]error, len(stacks))
var startup sync.WaitGroup
for i, stack := range stacks {
startup.Go(func() {
results[i], startupErrors[i] = stack.command("stack", "up", "--profile", "minimal", "--timeout", "12m", "--json")
})
}
startup.Wait()
states := make([]devstack.Status, len(stacks))
for i := range stacks {
if startupErrors[i] != nil {
t.Fatal(startupErrors[i])
}
if err := json.Unmarshal(results[i], &states[i]); err != nil || !states[i].Ready {
t.Fatalf("worktree %s did not become ready: parse=%v status=%+v", stacks[i].root, err, states[i])
}
}
sa, sb := &states[0], &states[1]
if sa.Project == sb.Project {
t.Fatal("worktrees share a Compose project")
}
for name, port := range sa.Ports {
for otherName, otherPort := range sb.Ports {
if port > 0 && port == otherPort {
t.Errorf("worktrees share %s/%s port %d", name, otherName, port)
}
}
}
a.ready(t, sa)
b.ready(t, sb)
t.Run("databases and authentication are isolated", func(t *testing.T) {
for _, stack := range []struct {
stack liveStack
state *devstack.Status
}{{a, sa}, {b, sb}} {
stack.stack.register(t, stack.state, "admin_user@example.com")
}
a.register(t, sa, "ods-stack-a@example.com")
if status := a.login(t, sa, "ods-stack-a@example.com"); status != http.StatusNoContent {
t.Fatalf("login on owning stack: HTTP %d", status)
}
if status := b.login(t, sb, "ods-stack-a@example.com"); status < 400 || status >= 500 {
t.Fatalf("credentials leaked across stacks: HTTP %d", status)
}
})
t.Run("concurrent up preserves healthy processes and ports", func(t *testing.T) {
before := a.status(t)
var wg sync.WaitGroup
errors := make(chan error, 2)
for range 2 {
wg.Go(func() {
_, err := a.command("stack", "up", "--profile", "minimal", "--skip-prepare", "--timeout", "12m", "--json")
errors <- err
})
}
wg.Wait()
close(errors)
for err := range errors {
if err != nil {
t.Fatal(err)
}
}
after := a.status(t)
if !reflect.DeepEqual(before.Ports, after.Ports) {
t.Fatalf("healthy ports changed: %v -> %v", before.Ports, after.Ports)
}
if !reflect.DeepEqual(servicePIDs(before), servicePIDs(after)) {
t.Fatal("healthy service processes were replaced")
}
})
t.Run("stop and restart preserve data and the other stack", func(t *testing.T) {
before := a.status(t)
if _, err := a.command("stack", "stop"); err != nil {
t.Fatal(err)
}
if a.status(t).Ready {
t.Fatal("stopped stack still reports ready")
}
b.ready(t, b.status(t))
after := a.start(t)
if !reflect.DeepEqual(before.Ports, after.Ports) {
t.Fatal("restart did not reuse durable ports")
}
if status := a.login(t, after, "ods-stack-a@example.com"); status != http.StatusNoContent {
t.Fatalf("restart lost database data: HTTP %d", status)
}
})
t.Run("occupied application port moves safely", func(t *testing.T) {
before := a.status(t)
if _, err := a.command("stack", "stop"); err != nil {
t.Fatal(err)
}
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", before.Ports["web"]))
if err != nil {
t.Fatal(err)
}
defer func() { _ = listener.Close() }()
after := a.start(t)
if after.Ports["web"] == before.Ports["web"] {
t.Fatal("stack did not replace occupied port")
}
if after.Ports["api"] != before.Ports["api"] {
t.Fatal("unaffected API port changed")
}
a.ready(t, after)
b.ready(t, b.status(t))
})
t.Run("down and up preserve volumes and durable assignments", func(t *testing.T) {
before := a.status(t)
if _, err := a.command("stack", "down"); err != nil {
t.Fatal(err)
}
containers := dockerOutput(t, "ps", "--filter", "label=com.docker.compose.project="+before.Project, "--format", "{{.ID}}")
if containers != "" {
t.Fatalf("down left infrastructure containers running: %s", containers)
}
volumes := dockerOutput(t, "volume", "ls", "--filter", "label=com.docker.compose.project="+before.Project, "--format", "{{.Name}}")
if volumes == "" {
t.Fatal("down removed persistent volumes")
}
b.ready(t, b.status(t))
after := a.up(t)
if !reflect.DeepEqual(before.Ports, after.Ports) {
t.Fatal("down/up did not reuse assigned ports")
}
if status := a.login(t, after, "ods-stack-a@example.com"); status != http.StatusNoContent {
t.Fatalf("down/up lost database data: HTTP %d", status)
}
})
}
func configuredStack(t *testing.T, variable string) liveStack {
t.Helper()
root := os.Getenv(variable)
binary := os.Getenv("ODS_STACK_LIVE_BINARY")
if root == "" || binary == "" {
t.Skip("set ODS_STACK_LIVE_BINARY and both ODS_STACK_LIVE_ROOT_A/B to disposable worktrees")
}
abs, err := filepath.Abs(root)
if err != nil {
t.Fatal(err)
}
jar, err := cookiejar.New(nil)
if err != nil {
t.Fatal(err)
}
return liveStack{root: abs, binary: binary, client: &http.Client{Jar: jar, Timeout: 30 * time.Second}}
}
func (s liveStack) command(args ...string) ([]byte, error) {
if len(args) > 0 && args[0] == "stack" {
args = append([]string{"stack", "--isolated"}, args[1:]...)
}
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Minute)
defer cancel()
cmd := exec.CommandContext(ctx, s.binary, args...)
cmd.Dir = s.root
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err := cmd.Run(); err != nil {
return stdout.Bytes(), fmt.Errorf("ods %s in %s: %w\n%s", strings.Join(args, " "), s.root, err, stderr.String())
}
return stdout.Bytes(), nil
}
func (s liveStack) up(t *testing.T) *devstack.Status {
t.Helper()
return s.lifecycle(t, "up")
}
func (s liveStack) start(t *testing.T) *devstack.Status {
t.Helper()
return s.lifecycle(t, "start")
}
func (s liveStack) lifecycle(t *testing.T, action string) *devstack.Status {
t.Helper()
data, err := s.command("stack", action, "--profile", "minimal", "--timeout", "12m", "--json")
if err != nil {
t.Fatal(err)
}
var status devstack.Status
if err := json.Unmarshal(data, &status); err != nil {
t.Fatalf("invalid lifecycle JSON: %v", err)
}
if !status.Ready {
t.Fatalf("stack is not ready: %v", status.Errors)
}
return &status
}
func (s liveStack) status(t *testing.T) *devstack.Status {
t.Helper()
data, err := s.command("stack", "status", "--json")
var status devstack.Status
if parseErr := json.Unmarshal(data, &status); parseErr != nil {
t.Fatalf("invalid status JSON: %v, command: %v", parseErr, err)
}
return &status
}
func (s liveStack) ready(t *testing.T, status *devstack.Status) {
t.Helper()
response, err := s.client.Get(status.URLs["web"] + "/api/auth/type")
if err != nil {
t.Fatal(err)
}
defer func() { _ = response.Body.Close() }()
if response.StatusCode != http.StatusOK {
t.Fatalf("frontend API proxy readiness: HTTP %d", response.StatusCode)
}
}
func (s liveStack) register(t *testing.T, status *devstack.Status, email string) {
t.Helper()
data, err := json.Marshal(map[string]string{"email": email, "username": email, "password": testPassword})
if err != nil {
t.Fatal(err)
}
response, err := s.client.Post(status.URLs["web"]+"/api/auth/register", "application/json", bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
defer func() { _ = response.Body.Close() }()
body, err := io.ReadAll(response.Body)
if err != nil {
t.Fatal(err)
}
if response.StatusCode >= 300 && !bytes.Contains(body, []byte("REGISTER_USER_ALREADY_EXISTS")) {
t.Fatalf("registration: HTTP %d", response.StatusCode)
}
}
func (s liveStack) login(t *testing.T, status *devstack.Status, email string) int {
t.Helper()
response, err := s.client.PostForm(status.URLs["web"]+"/api/auth/login", url.Values{"username": {email}, "password": {testPassword}})
if err != nil {
t.Fatal(err)
}
defer func() { _ = response.Body.Close() }()
return response.StatusCode
}
func servicePIDs(status *devstack.Status) map[string]int {
pids := map[string]int{}
for _, service := range status.Services {
pids[service.Slug] = service.PID
}
return pids
}
func dockerOutput(t *testing.T, args ...string) string {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
data, err := exec.CommandContext(ctx, "docker", args...).CombinedOutput()
if err != nil {
t.Fatalf("docker %v: %v, %s", args, err, data)
}
return strings.TrimSpace(string(data))
}