MCPcopy Create free account
hub / github.com/docker/docker-agent / TestExecutorDispatchesToCustomHandler

Function TestExecutorDispatchesToCustomHandler

pkg/hooks/handler_test.go:45–94  ·  view source on GitHub ↗

TestExecutorDispatchesToCustomHandler shows the smallest end-to-end use of the new pluggability: a custom HookType backed by an in-process Go Handler runs through the same executor pipeline as a "command" hook, and its pre-parsed Output (a deny verdict here) drives the aggregated Result.

(t *testing.T)

Source from the content-addressed store, hash-verified

43// and its pre-parsed Output (a deny verdict here) drives the aggregated
44// Result.
45func TestExecutorDispatchesToCustomHandler(t *testing.T) {
46 t.Parallel()
47
48 const customType HookType = "builtin-test"
49
50 rec := &recordingHandler{
51 out: &Output{
52 Decision: "block",
53 Reason: "denied by builtin handler",
54 },
55 }
56
57 registry := NewRegistry()
58 registry.Register(customType, func(_ HandlerEnv, _ Hook) (Handler, error) {
59 return rec, nil
60 })
61
62 config := &Config{
63 PreToolUse: []MatcherConfig{
64 {
65 Matcher: "*",
66 Hooks: []Hook{
67 {Type: customType, Timeout: 5},
68 },
69 },
70 },
71 }
72
73 exec := NewExecutorWithRegistry(config, t.TempDir(), nil, registry)
74 input := &Input{
75 SessionID: "test-session",
76 ToolName: "shell",
77 ToolUseID: "test-id",
78 }
79
80 result, err := exec.Dispatch(t.Context(), EventPreToolUse, input)
81 require.NoError(t, err)
82
83 // The custom handler ran and saw a properly serialized Input on stdin.
84 assert.Equal(t, int32(1), rec.calls.Load())
85 var got Input
86 require.NoError(t, json.Unmarshal(rec.capturedInput(), &got))
87 assert.Equal(t, EventPreToolUse, got.HookEventName)
88 assert.Equal(t, "shell", got.ToolName)
89
90 // The pre-parsed Output drove the aggregated Result, so the call is
91 // denied with the handler-supplied reason.
92 assert.False(t, result.Allowed)
93 assert.Contains(t, result.Message, "denied by builtin handler")
94}
95
96// TestExecutorUnregisteredTypeIsRejected ensures the registry is the only
97// way to plug in a handler: an unknown HookType is surfaced as a hook

Callers

nothing calls this directly

Calls 7

RegisterMethod · 0.95
capturedInputMethod · 0.95
NewExecutorWithRegistryFunction · 0.85
ContextMethod · 0.80
LoadMethod · 0.80
NewRegistryFunction · 0.70
DispatchMethod · 0.65

Tested by

no test coverage detected