Files
archie/internal/infrastructure/openshell/runtime_test.go
T
sam 2096ae8267 feat(openshell): each sandbox's network policy admits only what its task needs (#1864, #1967)
Runtimes now take a task launch: image, mounts, env, the daemon's NATS
and State Store with the task's credentials, its model providers and the
hosts its tools may reach. Docker addresses them through the egress relay
as before. An OpenShell sandbox reaches NATS and the State Store over
plain TCP, the model providers with their credential bound, and the
task's remote MCP hosts and the allowed package registries; services
addressed by IP or loopback are named by the configured services host,
since sandbox policy admits only DNS names.
2026-11-14 03:24:37 +11:00

111 lines
3.5 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"
"github.com/samcharles93/archie-core/internal/infrastructure/egress"
)
// 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(), Launch{Image: "agent:test", Worker: egress.Endpoints{NATS: "nats://nats.example.internal: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)
}
}