Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/router/session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ func (m *manager) createSandbox(ctx context.Context, namespace string, name stri

// Construct Sandbox Info from response
sandbox := &types.SandboxInfo{
// Auto-created sessions come directly from WorkloadManager response, which
// currently does not include kind. Default to Sandbox so Router signs JWT
// when forwarding to sandbox runtimes (e.g. PicoD).
Kind: types.SandboxKind,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field is missing set, because we cannot know whether it is sandbox or sandboxclaim now.

Do you see any problem without it?

SandboxID: res.SandboxID,
Name: res.SandboxName,
SessionID: res.SessionID,
Expand Down
115 changes: 68 additions & 47 deletions pkg/router/session_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,37 +154,74 @@ func TestGetSandboxBySession_NotFound(t *testing.T) {
}
}

func assertCreateSandboxRequest(t *testing.T, r *http.Request, wantPath, wantKind, wantNamespace, wantName string) {
t.Helper()

if r.Method != http.MethodPost {
t.Errorf("expected POST request, got %s", r.Method)
}
if r.URL.Path != wantPath {
t.Errorf("expected path %s, got %s", wantPath, r.URL.Path)
}

body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("failed to read request body: %v", err)
}

var req types.CreateSandboxRequest
if err := json.Unmarshal(body, &req); err != nil {
t.Fatalf("failed to unmarshal request: %v", err)
}
if req.Kind != wantKind {
t.Errorf("expected kind %s, got %s", wantKind, req.Kind)
}
if req.Name != wantName {
t.Errorf("expected name %s, got %s", wantName, req.Name)
}
if req.Namespace != wantNamespace {
t.Errorf("expected namespace %s, got %s", wantNamespace, req.Namespace)
}
}

func writeCreateSandboxResponse(w http.ResponseWriter, resp types.CreateSandboxResponse) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(resp)
}

func assertAgentRuntimeCreateSandboxResult(t *testing.T, sandbox *types.SandboxInfo) {
t.Helper()

if sandbox == nil {
t.Fatalf("expected non-nil sandbox")
}
if sandbox.SessionID != "new-session-123" {
t.Errorf("expected SessionID new-session-123, got %s", sandbox.SessionID)
}
if sandbox.SandboxID != "sandbox-456" {
t.Errorf("expected SandboxID sandbox-456, got %s", sandbox.SandboxID)
}
if sandbox.Name != "sandbox-test" {
t.Errorf("expected Name sandbox-test, got %s", sandbox.Name)
}
if sandbox.Kind != types.SandboxKind {
t.Errorf("expected Kind %s, got %s", types.SandboxKind, sandbox.Kind)
}
if len(sandbox.EntryPoints) != 1 {
t.Fatalf("expected 1 entry point, got %d", len(sandbox.EntryPoints))
}
if sandbox.EntryPoints[0].Endpoint != "10.0.0.1:9000" {
t.Errorf("expected endpoint 10.0.0.1:9000, got %s", sandbox.EntryPoints[0].Endpoint)
}
}

// ---- tests: GetSandboxBySession with empty sessionID (sandbox creation path) ----

func TestGetSandboxBySession_CreateSandbox_AgentRuntime_Success(t *testing.T) {
// Mock workload manager server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify request method and path
if r.Method != http.MethodPost {
t.Errorf("expected POST request, got %s", r.Method)
}
if r.URL.Path != "/v1/agent-runtime" {
t.Errorf("expected path /v1/agent-runtime, got %s", r.URL.Path)
}

// Verify request body
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("failed to read request body: %v", err)
}
var req types.CreateSandboxRequest
if err := json.Unmarshal(body, &req); err != nil {
t.Fatalf("failed to unmarshal request: %v", err)
}
if req.Kind != types.AgentRuntimeKind {
t.Errorf("expected kind %s, got %s", types.AgentRuntimeKind, req.Kind)
}
if req.Name != "test-runtime" {
t.Errorf("expected name test-runtime, got %s", req.Name)
}
if req.Namespace != "default" {
t.Errorf("expected namespace default, got %s", req.Namespace)
}
assertCreateSandboxRequest(t, r, "/v1/agent-runtime", types.AgentRuntimeKind, "default", "test-runtime")

// Send successful response
resp := types.CreateSandboxResponse{
Expand All @@ -195,9 +232,7 @@ func TestGetSandboxBySession_CreateSandbox_AgentRuntime_Success(t *testing.T) {
{Endpoint: "10.0.0.1:9000", Protocol: "http", Path: "/"},
},
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(resp)
writeCreateSandboxResponse(w, resp)
}))
defer mockServer.Close()

Expand All @@ -212,24 +247,7 @@ func TestGetSandboxBySession_CreateSandbox_AgentRuntime_Success(t *testing.T) {
if err != nil {
t.Fatalf("GetSandboxBySession unexpected error: %v", err)
}
if sandbox == nil {
t.Fatalf("expected non-nil sandbox")
}
if sandbox.SessionID != "new-session-123" {
t.Errorf("expected SessionID new-session-123, got %s", sandbox.SessionID)
}
if sandbox.SandboxID != "sandbox-456" {
t.Errorf("expected SandboxID sandbox-456, got %s", sandbox.SandboxID)
}
if sandbox.Name != "sandbox-test" {
t.Errorf("expected Name sandbox-test, got %s", sandbox.Name)
}
if len(sandbox.EntryPoints) != 1 {
t.Fatalf("expected 1 entry point, got %d", len(sandbox.EntryPoints))
}
if sandbox.EntryPoints[0].Endpoint != "10.0.0.1:9000" {
t.Errorf("expected endpoint 10.0.0.1:9000, got %s", sandbox.EntryPoints[0].Endpoint)
}
assertAgentRuntimeCreateSandboxResult(t, sandbox)
}

func TestGetSandboxBySession_CreateSandbox_SetsAuthHeaderFromFile(t *testing.T) {
Expand Down Expand Up @@ -404,6 +422,9 @@ func TestGetSandboxBySession_CreateSandbox_CodeInterpreter_Success(t *testing.T)
if sandbox.SessionID != "ci-session-789" {
t.Errorf("expected SessionID ci-session-789, got %s", sandbox.SessionID)
}
if sandbox.Kind != types.SandboxKind {
t.Errorf("expected Kind %s, got %s", types.SandboxKind, sandbox.Kind)
}
}

func TestGetSandboxBySession_CreateSandbox_UnsupportedKind(t *testing.T) {
Expand Down
Loading
Loading