Skip to content

Commit 7b5430b

Browse files
committed
test(router): add session-target mismatch test coverage and fix lint
- handlers_test: add expectedBodyCode assertion to TestHandleInvoke_ErrorPaths/session-target-mismatch to verify the SESSION_TARGET_MISMATCH code is present in the JSON response. - session_manager_test: migrate all sessionTargetMatches fixtures to use WorkloadKind/WorkloadName fields; fix TestGetSandboxBySession_ TargetMatch_EmptyFields to assert conflict error (fail-closed); extract agentRuntimeCreateHandler helper to bring TestGetSandboxBySession_CreateSandbox_AgentRuntime_Success cyclomatic complexity below gocyclo threshold (was 17, now <=15). - sandbox_helper_test: populate WorkloadKind/WorkloadName in sandboxEntry fixtures and assert they are propagated to SandboxInfo. Signed-off-by: KinshukSS2 <kinshuk380@gmail.com>
1 parent ea1d53f commit 7b5430b

3 files changed

Lines changed: 104 additions & 69 deletions

File tree

pkg/router/handlers_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package router
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"errors"
2223
"fmt"
2324
"net"
@@ -180,9 +181,10 @@ func TestHandleInvoke_ErrorPaths(t *testing.T) {
180181
defer teardownEnv()
181182

182183
tests := []struct {
183-
name string
184-
err error
185-
expectedCode int
184+
name string
185+
err error
186+
expectedCode int
187+
expectedBodyCode string
186188
}{
187189
{
188190
name: "session manager generic error",
@@ -195,9 +197,10 @@ func TestHandleInvoke_ErrorPaths(t *testing.T) {
195197
expectedCode: http.StatusNotFound,
196198
},
197199
{
198-
name: "session target mismatch",
199-
err: api.NewSessionTargetMismatchError("session-1", "default", "test-agent", types.AgentRuntimeKind),
200-
expectedCode: http.StatusConflict,
200+
name: "session target mismatch",
201+
err: api.NewSessionTargetMismatchError("session-1", "default", "test-agent", types.AgentRuntimeKind),
202+
expectedCode: http.StatusConflict,
203+
expectedBodyCode: "SESSION_TARGET_MISMATCH",
201204
},
202205
{
203206
name: "agent runtime not found",
@@ -233,6 +236,15 @@ func TestHandleInvoke_ErrorPaths(t *testing.T) {
233236
if w.Code != tt.expectedCode {
234237
t.Fatalf("expected status %d, got %d", tt.expectedCode, w.Code)
235238
}
239+
if tt.expectedBodyCode != "" {
240+
var resp map[string]interface{}
241+
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
242+
t.Fatalf("failed to decode response: %v", err)
243+
}
244+
if resp["code"] != tt.expectedBodyCode {
245+
t.Fatalf("expected code %q, got %v", tt.expectedBodyCode, resp["code"])
246+
}
247+
}
236248
t.Logf("Response body: %s", w.Body.String())
237249
})
238250
}

pkg/router/session_manager_test.go

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ func TestGetSandboxBySession_Success(t *testing.T) {
105105
SandboxID: "sandbox-1",
106106
Name: "test",
107107
SandboxNamespace: "default",
108+
WorkloadKind: types.AgentRuntimeKind,
109+
WorkloadName: "test",
108110
EntryPoints: []types.SandboxEntryPoint{
109111
{Endpoint: "10.0.0.1:9000"},
110112
},
@@ -159,9 +161,9 @@ func TestGetSandboxBySession_TargetMismatch(t *testing.T) {
159161
r := &fakeStoreClient{
160162
sandbox: &types.SandboxInfo{
161163
SessionID: "sess-1",
162-
Kind: types.AgentRuntimeKind,
164+
WorkloadKind: types.AgentRuntimeKind,
163165
SandboxNamespace: "default",
164-
Name: "other-runtime",
166+
WorkloadName: "other-runtime",
165167
},
166168
}
167169
m := &manager{
@@ -181,9 +183,9 @@ func TestGetSandboxBySession_TargetMatch(t *testing.T) {
181183
r := &fakeStoreClient{
182184
sandbox: &types.SandboxInfo{
183185
SessionID: "sess-1",
184-
Kind: types.AgentRuntimeKind,
186+
WorkloadKind: types.AgentRuntimeKind,
185187
SandboxNamespace: "default",
186-
Name: "test-runtime",
188+
WorkloadName: "test-runtime",
187189
},
188190
}
189191
m := &manager{
@@ -207,8 +209,11 @@ func TestGetSandboxBySession_TargetMatch_EmptyFields(t *testing.T) {
207209
}
208210

209211
_, err := m.GetSandboxBySession(context.Background(), "sess-1", "default", "test-runtime", types.AgentRuntimeKind)
210-
if err != nil {
211-
t.Fatalf("expected no error, got %v", err)
212+
if err == nil {
213+
t.Fatalf("expected conflict error for missing session metadata")
214+
}
215+
if !apierrors.IsConflict(err) {
216+
t.Fatalf("expected conflict error, got %v", err)
212217
}
213218
}
214219

@@ -229,8 +234,8 @@ func TestSessionTargetMatches(t *testing.T) {
229234
{
230235
name: "perfect match with all fields populated",
231236
sandbox: &types.SandboxInfo{
232-
Kind: types.AgentRuntimeKind,
233-
Name: "test-agent",
237+
WorkloadKind: types.AgentRuntimeKind,
238+
WorkloadName: "test-agent",
234239
SandboxNamespace: "default",
235240
},
236241
sessionID: "sess-1",
@@ -242,8 +247,8 @@ func TestSessionTargetMatches(t *testing.T) {
242247
{
243248
name: "perfect match with CodeInterpreter kind",
244249
sandbox: &types.SandboxInfo{
245-
Kind: types.CodeInterpreterKind,
246-
Name: "my-ci",
250+
WorkloadKind: types.CodeInterpreterKind,
251+
WorkloadName: "my-ci",
247252
SandboxNamespace: "ai-namespace",
248253
},
249254
sessionID: "sess-2",
@@ -256,8 +261,8 @@ func TestSessionTargetMatches(t *testing.T) {
256261
{
257262
name: "Kind mismatch",
258263
sandbox: &types.SandboxInfo{
259-
Kind: types.AgentRuntimeKind,
260-
Name: "test-agent",
264+
WorkloadKind: types.AgentRuntimeKind,
265+
WorkloadName: "test-agent",
261266
SandboxNamespace: "default",
262267
},
263268
sessionID: "sess-3",
@@ -269,8 +274,8 @@ func TestSessionTargetMatches(t *testing.T) {
269274
{
270275
name: "Name mismatch",
271276
sandbox: &types.SandboxInfo{
272-
Kind: types.AgentRuntimeKind,
273-
Name: "test-agent",
277+
WorkloadKind: types.AgentRuntimeKind,
278+
WorkloadName: "test-agent",
274279
SandboxNamespace: "default",
275280
},
276281
sessionID: "sess-4",
@@ -282,8 +287,8 @@ func TestSessionTargetMatches(t *testing.T) {
282287
{
283288
name: "Namespace mismatch",
284289
sandbox: &types.SandboxInfo{
285-
Kind: types.AgentRuntimeKind,
286-
Name: "test-agent",
290+
WorkloadKind: types.AgentRuntimeKind,
291+
WorkloadName: "test-agent",
287292
SandboxNamespace: "default",
288293
},
289294
sessionID: "sess-5",
@@ -302,72 +307,72 @@ func TestSessionTargetMatches(t *testing.T) {
302307
kind: types.AgentRuntimeKind,
303308
expects: false,
304309
},
305-
// Empty fields in sandbox (should match as they're treated as wildcards)
310+
// Empty fields in sandbox (should fail closed)
306311
{
307-
name: "all sandbox fields empty - wildcard matching",
312+
name: "all sandbox fields empty",
308313
sandbox: &types.SandboxInfo{
309-
Kind: "",
310-
Name: "",
314+
WorkloadKind: "",
315+
WorkloadName: "",
311316
SandboxNamespace: "",
312317
},
313318
sessionID: "sess-7",
314319
namespace: "default",
315320
workloadName: "test-agent",
316321
kind: types.AgentRuntimeKind,
317-
expects: true,
322+
expects: false,
318323
},
319324
{
320-
name: "only Kind populated in sandbox",
325+
name: "only workload kind populated in sandbox",
321326
sandbox: &types.SandboxInfo{
322-
Kind: types.AgentRuntimeKind,
323-
Name: "",
327+
WorkloadKind: types.AgentRuntimeKind,
328+
WorkloadName: "",
324329
SandboxNamespace: "",
325330
},
326331
sessionID: "sess-8",
327332
namespace: "default",
328333
workloadName: "test-agent",
329334
kind: types.AgentRuntimeKind,
330-
expects: true,
335+
expects: false,
331336
},
332337
{
333-
name: "Kind empty in sandbox but kind matches request",
338+
name: "workload kind empty in sandbox",
334339
sandbox: &types.SandboxInfo{
335-
Kind: "",
336-
Name: "test-agent",
340+
WorkloadKind: "",
341+
WorkloadName: "test-agent",
337342
SandboxNamespace: "default",
338343
},
339344
sessionID: "sess-9",
340345
namespace: "default",
341346
workloadName: "test-agent",
342347
kind: types.AgentRuntimeKind,
343-
expects: true,
348+
expects: false,
344349
},
345350
// Edge cases
346351
{
347352
name: "empty string kind in request",
348353
sandbox: &types.SandboxInfo{
349-
Kind: "",
350-
Name: "test-agent",
354+
WorkloadKind: "",
355+
WorkloadName: "test-agent",
351356
SandboxNamespace: "default",
352357
},
353358
sessionID: "sess-10",
354359
namespace: "default",
355360
workloadName: "test-agent",
356361
kind: "",
357-
expects: true,
362+
expects: false,
358363
},
359364
{
360365
name: "multiple empty fields in sandbox",
361366
sandbox: &types.SandboxInfo{
362-
Kind: types.AgentRuntimeKind,
363-
Name: "",
367+
WorkloadKind: types.AgentRuntimeKind,
368+
WorkloadName: "",
364369
SandboxNamespace: "",
365370
},
366371
sessionID: "sess-11",
367372
namespace: "prod",
368373
workloadName: "my-workload",
369374
kind: types.AgentRuntimeKind,
370-
expects: true,
375+
expects: false,
371376
},
372377
}
373378

@@ -377,7 +382,7 @@ func TestSessionTargetMatches(t *testing.T) {
377382
if result != tt.expects {
378383
t.Errorf("sessionTargetMatches returned %v, expected %v", result, tt.expects)
379384
if tt.sandbox != nil {
380-
t.Logf(" Sandbox: Kind=%q Name=%q Namespace=%q", tt.sandbox.Kind, tt.sandbox.Name, tt.sandbox.SandboxNamespace)
385+
t.Logf(" Sandbox: WorkloadKind=%q WorkloadName=%q Namespace=%q", tt.sandbox.WorkloadKind, tt.sandbox.WorkloadName, tt.sandbox.SandboxNamespace)
381386
}
382387
t.Logf(" Request: Kind=%q Name=%q Namespace=%q SessionID=%q", tt.kind, tt.workloadName, tt.namespace, tt.sessionID)
383388
}
@@ -387,18 +392,18 @@ func TestSessionTargetMatches(t *testing.T) {
387392

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

390-
func TestGetSandboxBySession_CreateSandbox_AgentRuntime_Success(t *testing.T) {
391-
// Mock workload manager server
392-
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
393-
// Verify request method and path
395+
// agentRuntimeCreateHandler is a reusable mock HTTP handler for the
396+
// /v1/agent-runtime endpoint, extracted to reduce cyclomatic complexity of its
397+
// parent test.
398+
func agentRuntimeCreateHandler(t *testing.T) http.HandlerFunc {
399+
t.Helper()
400+
return func(w http.ResponseWriter, r *http.Request) {
394401
if r.Method != http.MethodPost {
395402
t.Errorf("expected POST request, got %s", r.Method)
396403
}
397404
if r.URL.Path != "/v1/agent-runtime" {
398405
t.Errorf("expected path /v1/agent-runtime, got %s", r.URL.Path)
399406
}
400-
401-
// Verify request body
402407
body, err := io.ReadAll(r.Body)
403408
if err != nil {
404409
t.Fatalf("failed to read request body: %v", err)
@@ -416,8 +421,6 @@ func TestGetSandboxBySession_CreateSandbox_AgentRuntime_Success(t *testing.T) {
416421
if req.Namespace != "default" {
417422
t.Errorf("expected namespace default, got %s", req.Namespace)
418423
}
419-
420-
// Send successful response
421424
resp := types.CreateSandboxResponse{
422425
SessionID: "new-session-123",
423426
SandboxID: "sandbox-456",
@@ -429,7 +432,11 @@ func TestGetSandboxBySession_CreateSandbox_AgentRuntime_Success(t *testing.T) {
429432
w.Header().Set("Content-Type", "application/json")
430433
w.WriteHeader(http.StatusOK)
431434
_ = json.NewEncoder(w).Encode(resp)
432-
}))
435+
}
436+
}
437+
438+
func TestGetSandboxBySession_CreateSandbox_AgentRuntime_Success(t *testing.T) {
439+
mockServer := httptest.NewServer(agentRuntimeCreateHandler(t))
433440
defer mockServer.Close()
434441

435442
r := &fakeStoreClient{}

0 commit comments

Comments
 (0)