-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonit_agent_test.go
More file actions
319 lines (289 loc) · 9.78 KB
/
Copy pathmonit_agent_test.go
File metadata and controls
319 lines (289 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package flashduty
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
// TestMonitAgentCatalog_RequestShape verifies the wire-level request body for
// /monit/tools/catalog: target_locator + optional target_kind.
func TestMonitAgentCatalog_RequestShape(t *testing.T) {
t.Parallel()
gotPath := ""
var gotBody map[string]any
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
_ = json.NewDecoder(r.Body).Decode(&gotBody)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{"tools": []any{}},
})
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
if _, err := client.MonitAgentCatalog(context.Background(), &MonitAgentCatalogInput{
TargetLocator: "host:web-01",
TargetKind: "host",
}); err != nil {
t.Fatalf("MonitAgentCatalog: %v", err)
}
if gotPath != "/monit/tools/catalog" {
t.Fatalf("path = %q, want /monit/tools/catalog", gotPath)
}
wantBody := map[string]any{
"target_locator": "host:web-01",
"target_kind": "host",
}
if !reflect.DeepEqual(gotBody, wantBody) {
gotJSON, _ := json.MarshalIndent(gotBody, "", " ")
wantJSON, _ := json.MarshalIndent(wantBody, "", " ")
t.Fatalf("request body mismatch\n got:\n%s\nwant:\n%s", gotJSON, wantJSON)
}
}
// TestMonitAgentCatalog_OmitsTargetKind drops the optional target_kind when
// the agent should infer it.
func TestMonitAgentCatalog_OmitsTargetKind(t *testing.T) {
t.Parallel()
var gotBody map[string]any
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = json.NewDecoder(r.Body).Decode(&gotBody)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"tools": []any{}}})
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
if _, err := client.MonitAgentCatalog(context.Background(), &MonitAgentCatalogInput{
TargetLocator: "mysql:prod-master",
}); err != nil {
t.Fatalf("MonitAgentCatalog: %v", err)
}
if _, ok := gotBody["target_kind"]; ok {
t.Errorf("target_kind should be omitted when empty, got %v", gotBody["target_kind"])
}
}
// TestMonitAgentCatalog_Roundtrip ensures tool entries decode through
// MonitAgentTool with the input_schema preserved as RawMessage.
func TestMonitAgentCatalog_Roundtrip(t *testing.T) {
t.Parallel()
canned := map[string]any{
"data": map[string]any{
"tools": []map[string]any{
{
"name": "ps_top",
"description": "List top processes by CPU",
"input_schema": map[string]any{
"type": "object",
"properties": map[string]any{
"limit": map[string]any{"type": "integer"},
},
},
},
},
},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(canned)
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
out, err := client.MonitAgentCatalog(context.Background(), &MonitAgentCatalogInput{
TargetLocator: "host:web-01",
})
if err != nil {
t.Fatalf("MonitAgentCatalog: %v", err)
}
if len(out.Tools) != 1 {
t.Fatalf("expected 1 tool, got %d", len(out.Tools))
}
tool := out.Tools[0]
if tool.Name != "ps_top" || tool.Description != "List top processes by CPU" {
t.Errorf("tool basics mismatch: %#v", tool)
}
var schema map[string]any
if err := json.Unmarshal(tool.InputSchema, &schema); err != nil {
t.Fatalf("decode input_schema: %v", err)
}
if schema["type"] != "object" {
t.Errorf("input_schema mismatch: %v", schema)
}
}
// TestMonitAgentInvoke_RequestShape verifies the wire-level request body for
// /monit/tools/invoke: target_locator, optional target_kind, tools[]{tool,params}.
func TestMonitAgentInvoke_RequestShape(t *testing.T) {
t.Parallel()
gotPath := ""
var gotBody map[string]any
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
_ = json.NewDecoder(r.Body).Decode(&gotBody)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{"results": []any{}},
})
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
if _, err := client.MonitAgentInvoke(context.Background(), &MonitAgentInvokeInput{
TargetLocator: "host:web-01",
TargetKind: "host",
Tools: []MonitAgentInvokeTool{
{Tool: "ps_top", Params: json.RawMessage(`{"limit":5}`)},
{Tool: "disk_usage", Params: json.RawMessage(`{"path":"/"}`)},
},
}); err != nil {
t.Fatalf("MonitAgentInvoke: %v", err)
}
if gotPath != "/monit/tools/invoke" {
t.Fatalf("path = %q, want /monit/tools/invoke", gotPath)
}
wantBody := map[string]any{
"target_locator": "host:web-01",
"target_kind": "host",
"tools": []any{
map[string]any{
"tool": "ps_top",
"params": map[string]any{"limit": float64(5)},
},
map[string]any{
"tool": "disk_usage",
"params": map[string]any{"path": "/"},
},
},
}
if !reflect.DeepEqual(gotBody, wantBody) {
gotJSON, _ := json.MarshalIndent(gotBody, "", " ")
wantJSON, _ := json.MarshalIndent(wantBody, "", " ")
t.Fatalf("request body mismatch\n got:\n%s\nwant:\n%s", gotJSON, wantJSON)
}
}
// TestMonitAgentInvoke_OmitsTargetKindAndAllowsNullParams covers two edges:
// target_kind omitted when empty, and Params left nil (omitted entirely so
// the server applies tool defaults).
func TestMonitAgentInvoke_OmitsTargetKindAndAllowsNullParams(t *testing.T) {
t.Parallel()
var gotBody map[string]any
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = json.NewDecoder(r.Body).Decode(&gotBody)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"results": []any{}}})
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
if _, err := client.MonitAgentInvoke(context.Background(), &MonitAgentInvokeInput{
TargetLocator: "mysql:prod-master",
Tools: []MonitAgentInvokeTool{
{Tool: "show_status"},
},
}); err != nil {
t.Fatalf("MonitAgentInvoke: %v", err)
}
if _, ok := gotBody["target_kind"]; ok {
t.Errorf("target_kind should be omitted, got %v", gotBody["target_kind"])
}
tools, _ := gotBody["tools"].([]any)
if len(tools) != 1 {
t.Fatalf("expected 1 tool, got %d", len(tools))
}
tool0, _ := tools[0].(map[string]any)
if _, ok := tool0["params"]; ok {
t.Errorf("params should be omitted when nil, got %v", tool0["params"])
}
}
// TestMonitAgentInvoke_Roundtrip ensures per-tool results decode through
// MonitAgentInvokeResult, preserving per-tool data + error so callers can
// distinguish the three error layers (HTTP, request-level, per-tool).
func TestMonitAgentInvoke_Roundtrip(t *testing.T) {
t.Parallel()
canned := map[string]any{
"data": map[string]any{
"results": []map[string]any{
{
"tool": "ps_top",
"data": map[string]any{
"summary": "top processes: chrome (cpu 25%), java (cpu 18%)",
"data": map[string]any{
"rows": []map[string]any{
{"pid": 1, "cpu": 25.0, "cmd": "chrome"},
},
},
},
},
{
"tool": "ssh_exec",
"error": "command not allowed",
},
},
},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(canned)
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
out, err := client.MonitAgentInvoke(context.Background(), &MonitAgentInvokeInput{
TargetLocator: "host:web-01",
Tools: []MonitAgentInvokeTool{
{Tool: "ps_top"},
{Tool: "ssh_exec"},
},
})
if err != nil {
t.Fatalf("MonitAgentInvoke: %v", err)
}
if len(out.Results) != 2 {
t.Fatalf("expected 2 results, got %d", len(out.Results))
}
// First tool succeeded — data populated, error empty.
if out.Results[0].Tool != "ps_top" {
t.Errorf("results[0].tool = %q, want ps_top", out.Results[0].Tool)
}
if out.Results[0].Error != "" {
t.Errorf("results[0].error should be empty, got %q", out.Results[0].Error)
}
if len(out.Results[0].Data) == 0 {
t.Fatalf("results[0].data should be non-empty")
}
var first map[string]any
if err := json.Unmarshal(out.Results[0].Data, &first); err != nil {
t.Fatalf("decode results[0].data: %v", err)
}
if first["summary"] != "top processes: chrome (cpu 25%), java (cpu 18%)" {
t.Errorf("results[0] summary mismatch: %v", first["summary"])
}
// Second tool failed — error populated, data empty/null.
if out.Results[1].Tool != "ssh_exec" || out.Results[1].Error != "command not allowed" {
t.Errorf("results[1] mismatch: %#v", out.Results[1])
}
}
// TestMonitAgentInvoke_RequestLevelError verifies request-level errors
// (target_unavailable, ambiguous_target_kind, …) surface via the dataEnvelope
// error field and abort before per-tool results are touched.
func TestMonitAgentInvoke_RequestLevelError(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"error": map[string]any{
"code": "ambiguous_target_kind",
"message": "specify target_kind explicitly",
},
})
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
_, err := client.MonitAgentInvoke(context.Background(), &MonitAgentInvokeInput{
TargetLocator: "ambiguous",
Tools: []MonitAgentInvokeTool{{Tool: "ps_top"}},
})
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "ambiguous_target_kind") {
t.Errorf("unexpected error: %v", err)
}
}