Files
archie/internal/infrastructure/openshell/runtime_test.go
T
sam a1ec538a87 feat(openshell): task agents can run as OpenShell sandboxes (#1864)
Container runtime policies gain a runtime (docker or openshell) and the
OpenShell gateway's address, workspace and token, set on the container
runtime page and applied on restart. The OpenShell backend creates a
sandbox per task with the worktree and volumes bound in, reports when
the agent exits or outlives its maximum uptime, deletes the sandbox on
release, and sweeps this instance's leftovers at boot. Storage volumes
are created attachable to OpenShell sandboxes.
2026-11-14 03:07:57 +11:00

109 lines
3.4 KiB
Go

package openshell
import (
"context"
"slices"
"strings"
"testing"
"time"
v1 "github.com/ai-openshell/sdk-go/openshell/v1"
"github.com/ai-openshell/sdk-go/openshell/v1/fake"
)
// TestTaskSandboxEndsWithItsTask guards the sandbox lifecycle: a task sees its
// sandbox end however it ends (the agent exits, or it outlives its maximum
// uptime), and the sandbox is gone from the gateway once the task is done, so
// sandboxes never pile up.
func TestTaskSandboxEndsWithItsTask(t *testing.T) {
tests := []struct {
name string
maxUptime time.Duration
agentExits bool
wantExit bool
wantDetail string
}{
{name: "released while running"},
{name: "agent exits", agentExits: true, wantExit: true, wantDetail: "exit code 3"},
{name: "outlives its maximum uptime", maxUptime: 30 * time.Millisecond, wantExit: true, wantDetail: "maximum uptime"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewClient()
defer client.Close()
rt := New(client, Options{Instance: "archie", Poll: 5 * time.Millisecond, MaxUptime: func() time.Duration { return tt.maxUptime }})
sb, err := rt.Acquire(t.Context(), "agent:test", nil, []string{"NATS_URL=nats://nats.example:4222"})
if err != nil {
t.Fatal(err)
}
if tt.agentExits {
got, err := client.Sandboxes().Get(t.Context(), "default", sb.name)
if err != nil {
t.Fatal(err)
}
code := int32(3)
got.Status.ExitCode = &code
got.Status.Phase = v1.SandboxError
client.AddSandbox("default", got)
}
wait := 2 * time.Second
if !tt.wantExit {
wait = 50 * time.Millisecond
}
select {
case <-sb.Exited():
if !tt.wantExit {
t.Fatalf("sandbox reported an exit (%q) while still running", sb.ExitDetail())
}
if !strings.Contains(sb.ExitDetail(), tt.wantDetail) {
t.Fatalf("exit detail = %q, want it to say %q", sb.ExitDetail(), tt.wantDetail)
}
case <-time.After(wait):
if tt.wantExit {
t.Fatal("sandbox exit never reported")
}
}
rt.Release(t.Context(), sb)
if _, err := client.Sandboxes().Get(t.Context(), "default", sb.name); !v1.IsNotFound(err) {
t.Fatalf("sandbox still on the gateway after release: err=%v", err)
}
})
}
}
// TestSweepRemovesOnlyThisInstancesSandboxes guards instances sharing a
// gateway: a restarted daemon removes the sandboxes it left, never another
// instance's or ones archie did not create.
func TestSweepRemovesOnlyThisInstancesSandboxes(t *testing.T) {
client := fake.NewClient()
defer client.Close()
for name, labels := range map[string]map[string]string{
"archie-agent-1": {instanceLabel: "archie"},
"staging-agent-1": {instanceLabel: "staging"},
"someone-else": nil,
} {
client.AddSandbox("default", &v1.Sandbox{Name: name, Spec: v1.SandboxSpec{Template: &v1.SandboxTemplate{Labels: labels}}})
}
rt := New(client, Options{Instance: "archie"})
if err := rt.Sweep(context.Background()); err != nil {
t.Fatal(err)
}
pager, err := client.Sandboxes().List("default", v1.ListOptions{})
if err != nil {
t.Fatal(err)
}
left, err := pager.All(context.Background())
if err != nil {
t.Fatal(err)
}
var names []string
for _, sb := range left {
names = append(names, sb.Name)
}
slices.Sort(names)
if want := []string{"someone-else", "staging-agent-1"}; !slices.Equal(names, want) {
t.Fatalf("sandboxes left after sweep = %v, want %v", names, want)
}
}