-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonit_query_test.go
More file actions
319 lines (284 loc) · 9.6 KB
/
Copy pathmonit_query_test.go
File metadata and controls
319 lines (284 loc) · 9.6 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"
)
// TestMonitQueryDiagnose_RequestShape verifies the wire-level request body
// matches the documented /monit/query/diagnose contract: ds_type, ds_name,
// time_range:{start,end}, input:{query}, operation, optional limits.
func TestMonitQueryDiagnose_RequestShape(t *testing.T) {
t.Parallel()
input := &MonitQueryDiagnoseInput{
DsType: "prometheus",
DsName: "prom-prod",
TimeStart: 1700000000,
TimeEnd: 1700000900,
Operation: "metric_trends",
Input: MonitQueryDiagnoseQuery{Query: `rate(http_requests_total[5m])`},
MaxLogsScanned: 20000,
MaxPatterns: 30,
TimeoutSeconds: 28,
}
gotPath := ""
var gotBody map[string]any
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
if err := json.NewDecoder(r.Body).Decode(&gotBody); err != nil {
t.Fatalf("decode request body: %v", err)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
"operation": "metric_trends",
"results": []any{},
},
})
}))
defer ts.Close()
client, err := NewClient("test-key", WithBaseURL(ts.URL))
if err != nil {
t.Fatalf("NewClient: %v", err)
}
if _, err := client.MonitQueryDiagnose(context.Background(), input); err != nil {
t.Fatalf("MonitQueryDiagnose: %v", err)
}
if gotPath != "/monit/query/diagnose" {
t.Fatalf("path = %q, want /monit/query/diagnose", gotPath)
}
wantBody := map[string]any{
"ds_type": "prometheus",
"ds_name": "prom-prod",
"time_range": map[string]any{
"start": float64(1700000000),
"end": float64(1700000900),
},
"input": map[string]any{
"query": "rate(http_requests_total[5m])",
},
"operation": "metric_trends",
"max_logs_scanned": float64(20000),
"max_patterns": float64(30),
"timeout_seconds": float64(28),
}
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)
}
}
// TestMonitQueryDiagnose_OmitsZeroOptionals verifies optional caps + operation
// are omitted when unset, leaving the server defaults in effect.
func TestMonitQueryDiagnose_OmitsZeroOptionals(t *testing.T) {
t.Parallel()
input := &MonitQueryDiagnoseInput{
DsType: "victorialogs",
DsName: "vl-prod",
TimeStart: 1700000000,
TimeEnd: 1700000900,
Input: MonitQueryDiagnoseQuery{Query: "_msg:ERROR"},
}
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{"operation": "log_patterns", "results": []any{}}})
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
if _, err := client.MonitQueryDiagnose(context.Background(), input); err != nil {
t.Fatalf("MonitQueryDiagnose: %v", err)
}
if _, ok := gotBody["operation"]; ok {
t.Errorf("operation should be omitted when zero, got %v", gotBody["operation"])
}
if _, ok := gotBody["max_logs_scanned"]; ok {
t.Errorf("max_logs_scanned should be omitted when zero")
}
if _, ok := gotBody["max_patterns"]; ok {
t.Errorf("max_patterns should be omitted when zero")
}
if _, ok := gotBody["timeout_seconds"]; ok {
t.Errorf("timeout_seconds should be omitted when zero")
}
tr, ok := gotBody["time_range"].(map[string]any)
if !ok {
t.Fatalf("time_range must be sent even with operation omitted, got %v", gotBody["time_range"])
}
if tr["start"] != float64(1700000000) || tr["end"] != float64(1700000900) {
t.Errorf("time_range payload = %v, want start=1700000000 end=1700000900", tr)
}
}
// TestMonitQueryDiagnose_Roundtrip confirms a canned envelope decodes through
// MonitQueryDiagnoseOutput with Results preserved as RawMessage.
func TestMonitQueryDiagnose_Roundtrip(t *testing.T) {
t.Parallel()
canned := map[string]any{
"data": map[string]any{
"operation": "log_patterns",
"results": []map[string]any{
{"signature": "abc123", "sample_count": 42, "first_seen": 1700000010},
},
},
}
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.MonitQueryDiagnose(context.Background(), &MonitQueryDiagnoseInput{
DsType: "loki",
DsName: "loki-prod",
TimeStart: 1700000000,
TimeEnd: 1700000900,
Input: MonitQueryDiagnoseQuery{Query: `{app="api"} |= "error"`},
})
if err != nil {
t.Fatalf("MonitQueryDiagnose: %v", err)
}
if out.Operation != "log_patterns" {
t.Errorf("operation = %q, want log_patterns", out.Operation)
}
var results []map[string]any
if err := json.Unmarshal(out.Results, &results); err != nil {
t.Fatalf("decode results: %v (raw=%s)", err, string(out.Results))
}
if len(results) != 1 || results[0]["signature"] != "abc123" {
t.Errorf("results payload mismatch: %#v", results)
}
}
// TestMonitQueryDiagnose_ServerError surfaces request-level errors via the
// dataEnvelope wrapper.
func TestMonitQueryDiagnose_ServerError(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": "BadRequest",
"message": "time range exceeds 6h",
},
})
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
_, err := client.MonitQueryDiagnose(context.Background(), &MonitQueryDiagnoseInput{
DsType: "prometheus",
DsName: "prom",
TimeStart: 1,
TimeEnd: 100000,
Input: MonitQueryDiagnoseQuery{Query: "up"},
})
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "BadRequest") || !strings.Contains(err.Error(), "time range exceeds 6h") {
t.Errorf("unexpected error: %v", err)
}
}
// TestMonitQueryRows_RequestShape verifies request marshalling for
// /monit/query/rows: ds_type, ds_name, expr, args (string→string).
func TestMonitQueryRows_RequestShape(t *testing.T) {
t.Parallel()
input := &MonitQueryRowsInput{
DsType: "mysql",
DsName: "mysql-prod",
Expr: "SELECT 1",
Args: map[string]string{"host": "db-1", "schema": "public"},
}
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": []any{}})
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
if _, err := client.MonitQueryRows(context.Background(), input); err != nil {
t.Fatalf("MonitQueryRows: %v", err)
}
if gotPath != "/monit/query/rows" {
t.Fatalf("path = %q, want /monit/query/rows", gotPath)
}
wantBody := map[string]any{
"ds_type": "mysql",
"ds_name": "mysql-prod",
"expr": "SELECT 1",
"args": map[string]any{
"host": "db-1",
"schema": "public",
},
}
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)
}
}
// TestMonitQueryRows_OmitsEmptyArgs leaves out the args key when unset.
func TestMonitQueryRows_OmitsEmptyArgs(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": []any{}})
}))
defer ts.Close()
client, _ := NewClient("test-key", WithBaseURL(ts.URL))
if _, err := client.MonitQueryRows(context.Background(), &MonitQueryRowsInput{
DsType: "prometheus",
DsName: "prom",
Expr: "up",
}); err != nil {
t.Fatalf("MonitQueryRows: %v", err)
}
if _, ok := gotBody["args"]; ok {
t.Errorf("args should be omitted when empty, got %v", gotBody["args"])
}
}
// TestMonitQueryRows_Roundtrip ensures the response payload is captured as
// RawMessage so callers can shape rows per datasource.
func TestMonitQueryRows_Roundtrip(t *testing.T) {
t.Parallel()
canned := map[string]any{
"data": []map[string]any{
{
"fields": map[string]any{"col_1": "name", "col_2": "value"},
"values": map[string]any{"name": "a", "value": 1},
},
},
}
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.MonitQueryRows(context.Background(), &MonitQueryRowsInput{
DsType: "mysql",
DsName: "mysql-prod",
Expr: "SELECT 1",
})
if err != nil {
t.Fatalf("MonitQueryRows: %v", err)
}
var rows []map[string]any
if err := json.Unmarshal(out.Data, &rows); err != nil {
t.Fatalf("decode rows: %v (raw=%s)", err, string(out.Data))
}
if len(rows) != 1 {
t.Fatalf("expected 1 row, got %d", len(rows))
}
fields, _ := rows[0]["fields"].(map[string]any)
if fields["col_1"] != "name" {
t.Errorf("fields mismatch: %#v", rows[0])
}
}