master
go 537 lines 17.5 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package bind
4
5 import (
6 "context"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "testing"
11
12 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
13
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
16 )
17
18 var (
19 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
20 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
21
22 dataServerStatsJSON, _ = os.ReadFile("testdata/query-server.json")
23 dataServerStatsXML, _ = os.ReadFile("testdata/query-server.xml")
24 )
25
26 func Test_testDataIsValid(t *testing.T) {
27 for name, data := range map[string][]byte{
28 "dataConfigJSON": dataConfigJSON,
29 "dataConfigYAML": dataConfigYAML,
30 "dataServerStatsJSON": dataServerStatsJSON,
31 "dataServerStatsXML": dataServerStatsXML,
32 } {
33 require.NotNil(t, data, name)
34
35 }
36 }
37
38 func TestCollector_ConfigurationSerialize(t *testing.T) {
39 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
40 }
41
42 func TestCollector_Cleanup(t *testing.T) { New().Cleanup(context.Background()) }
43
44 func TestCollector_Init(t *testing.T) {
45 // OK
46 collr := New()
47 assert.NoError(t, collr.Init(context.Background()))
48 assert.NotNil(t, collr.bindAPIClient)
49
50 //NG
51 collr = New()
52 collr.URL = ""
53 assert.Error(t, collr.Init(context.Background()))
54 }
55
56 func TestCollector_Check(t *testing.T) {
57 ts := httptest.NewServer(
58 http.HandlerFunc(
59 func(w http.ResponseWriter, r *http.Request) {
60 if r.URL.Path == "/json/v1/server" {
61 _, _ = w.Write(dataServerStatsJSON)
62 }
63 }))
64 defer ts.Close()
65
66 collr := New()
67 collr.URL = ts.URL + "/json/v1"
68
69 require.NoError(t, collr.Init(context.Background()))
70 require.NoError(t, collr.Check(context.Background()))
71 }
72
73 func TestCollector_CheckNG(t *testing.T) {
74 collr := New()
75
76 collr.URL = "http://127.0.0.1:38001/xml/v3"
77 require.NoError(t, collr.Init(context.Background()))
78 assert.Error(t, collr.Check(context.Background()))
79 }
80
81 func TestCollector_Charts(t *testing.T) {
82 assert.NotNil(t, New().Charts())
83 }
84
85 func TestCollector_CollectJSON(t *testing.T) {
86 ts := httptest.NewServer(
87 http.HandlerFunc(
88 func(w http.ResponseWriter, r *http.Request) {
89 if r.URL.Path == "/json/v1/server" {
90 _, _ = w.Write(dataServerStatsJSON)
91 }
92 }))
93 defer ts.Close()
94
95 collr := New()
96 collr.URL = ts.URL + "/json/v1"
97 collr.PermitView = "*"
98
99 require.NoError(t, collr.Init(context.Background()))
100 require.NoError(t, collr.Check(context.Background()))
101
102 expected := map[string]int64{
103 "_default_Queryv4": 4503685324,
104 "_default_NSEC": 53193,
105 "_default_NSEC3PARAM": 993,
106 "_default_ANY": 5149356,
107 "QryFORMERR": 8,
108 "CookieMatch": 125065,
109 "A6": 538255,
110 "MAILA": 44,
111 "ExpireOpt": 195,
112 "CNAME": 534171,
113 "TYPE115": 285,
114 "_default_RESERVED0": 19,
115 "_default_ClientCookieOut": 3790767469,
116 "_default_CookieClientOk": 297765763,
117 "QryFailure": 225786697,
118 "TYPE127": 1,
119 "_default_GlueFetchv4": 110619519,
120 "_default_Queryv6": 291939086,
121 "UPDATE": 18836,
122 "RESERVED0": 13705,
123 "_default_CacheHits": 405229520524,
124 "Requestv6": 155,
125 "QryTCP": 4226324,
126 "RESERVED15": 0,
127 "QUERY": 36766967932,
128 "EUI64": 627,
129 "_default_NXDOMAIN": 1245990908,
130 "_default_REFUSED": 106664780,
131 "_default_EUI64": 2087,
132 "QrySERVFAIL": 219515158,
133 "QryRecursion": 3666523564,
134 "MX": 1483690,
135 "DNSKEY": 143483,
136 "_default_TYPE115": 112,
137 "_default_Others": 813,
138 "_default_CacheMisses": 127371,
139 "RateDropped": 219,
140 "NAPTR": 109959,
141 "NSEC": 81,
142 "AAAA": 3304112238,
143 "_default_QryRTT500": 2071767970,
144 "_default_TYPE127": 2,
145 "_default_A6": 556692,
146 "QryAuthAns": 440508475,
147 "RecursClients": 74,
148 "XfrRej": 97,
149 "LOC": 52,
150 "CookieIn": 1217208,
151 "RRSIG": 25192,
152 "_default_LOC": 21,
153 "ReqBadEDNSVer": 450,
154 "MG": 4,
155 "_default_GlueFetchv6": 121100044,
156 "_default_HINFO": 1,
157 "IQUERY": 199,
158 "_default_BadCookieRcode": 14779,
159 "AuthQryRej": 148023,
160 "QrySuccess": 28766465065,
161 "SRV": 27637747,
162 "TYPE223": 2,
163 "CookieNew": 1058677,
164 "_default_QryRTT10": 628295,
165 "_default_ServerCookieOut": 364811250,
166 "RESERVED11": 3,
167 "_default_CookieIn": 298084581,
168 "_default_DS": 973892,
169 "_bind_CacheHits": 0,
170 "STATUS": 35546,
171 "TLSA": 297,
172 "_default_SERVFAIL": 6523360,
173 "_default_GlueFetchv4Fail": 3949012,
174 "_default_NULL": 3548,
175 "UpdateRej": 15661,
176 "RESERVED10": 5,
177 "_default_EDNS0Fail": 3982564,
178 "_default_DLV": 20418,
179 "ANY": 298451299,
180 "_default_GlueFetchv6Fail": 91728801,
181 "_default_RP": 134,
182 "_default_AAAA": 817525939,
183 "X25": 2,
184 "NS": 5537956,
185 "_default_NumFetch": 100,
186 "_default_DNSKEY": 182224,
187 "QryUDP": 36455909449,
188 "QryReferral": 1152155,
189 "QryNXDOMAIN": 5902446156,
190 "TruncatedResp": 25882799,
191 "DNAME": 1,
192 "DLV": 37676,
193 "_default_FORMERR": 3827518,
194 "_default_RRSIG": 191628,
195 "RecQryRej": 225638588,
196 "QryDropped": 52141050,
197 "Response": 36426730232,
198 "RESERVED14": 0,
199 "_default_SPF": 16521,
200 "_default_DNAME": 6,
201 "Requestv4": 36767496594,
202 "CookieNoMatch": 33466,
203 "RESERVED9": 0,
204 "_default_QryRTT800": 2709649,
205 "_default_QryRTT1600": 455315,
206 "_default_OtherError": 1426431,
207 "_default_MX": 1575795,
208 "QryNoauthAns": 35538538399,
209 "NSIDOpt": 81,
210 "ReqTCP": 4234792,
211 "SOA": 3860272,
212 "RESERVED8": 0,
213 "RESERVED13": 8,
214 "MAILB": 42,
215 "AXFR": 105,
216 "QryNxrrset": 1308983498,
217 "SPF": 2872,
218 "PTR": 693769261,
219 "_default_Responsev4": 4169576370,
220 "_default_QryRTT100": 2086168894,
221 "_default_Retry": 783763680,
222 "_default_SRV": 3848459,
223 "QryDuplicate": 288617636,
224 "ECSOpt": 8742938,
225 "A": 32327037206,
226 "DS": 1687895,
227 "RESERVED12": 1,
228 "_default_QryRTT1600+": 27639,
229 "_default_TXT": 43595113,
230 "_default_CDS": 251,
231 "RESERVED6": 7401,
232 "RESERVED3": 2,
233 "_default_Truncated": 14015078,
234 "_default_NextItem": 1788902,
235 "_default_Responsev6": 151,
236 "_default_QueryTimeout": 335575100,
237 "_default_A": 3673673090,
238 "ReqEdns0": 532104182,
239 "OtherOpt": 3425542,
240 "NULL": 3604,
241 "HINFO": 9,
242 "_default_SOA": 1326766,
243 "_default_NAPTR": 30685,
244 "_default_PTR": 208067284,
245 "_default_CNAME": 38153754,
246 "RespEDNS0": 527991455,
247 "RESERVED7": 0,
248 "TXT": 100045556,
249 "_default_Lame": 1975334,
250 "_bind_CacheMisses": 509,
251 "IXFR": 33,
252 "_default_NS": 675609,
253 "_default_AFSDB": 5,
254 "NOTIFY": 390443,
255 "Others": 74006,
256 }
257
258 assert.Equal(t, expected, collr.Collect(context.Background()))
259 assert.Len(t, *collr.charts, 17)
260 }
261
262 func TestCollector_CollectXML3(t *testing.T) {
263 ts := httptest.NewServer(
264 http.HandlerFunc(
265 func(w http.ResponseWriter, r *http.Request) {
266 if r.URL.Path == "/xml/v3/server" {
267 _, _ = w.Write(dataServerStatsXML)
268 }
269 }))
270 defer ts.Close()
271
272 collr := New()
273 collr.PermitView = "*"
274 collr.URL = ts.URL + "/xml/v3"
275
276 require.NoError(t, collr.Init(context.Background()))
277 require.NoError(t, collr.Check(context.Background()))
278
279 expected := map[string]int64{
280 "_bind_CookieClientOk": 0,
281 "_bind_ValNegOk": 0,
282 "_bind_GlueFetchv4Fail": 0,
283 "_bind_ValFail": 0,
284 "RateSlipped": 0,
285 "_default_ValFail": 0,
286 "_default_TYPE127": 2,
287 "TLSA": 299,
288 "_default_FORMERR": 3831796,
289 "_default_ValNegOk": 0,
290 "_default_RRSIG": 191877,
291 "_default_CacheHits": 405816752908,
292 "CookieBadTime": 0,
293 "RESERVED14": 0,
294 "_default_SPF": 16563,
295 "RESERVED3": 2,
296 "NS": 5545011,
297 "QrySERVFAIL": 219790234,
298 "UPDATE": 18839,
299 "_default_NAPTR": 30706,
300 "RESERVED13": 8,
301 "_default_CookieIn": 298556974,
302 "_bind_Retry": 0,
303 "_default_SOA": 1327966,
304 "_bind_Truncated": 0,
305 "RESERVED6": 7401,
306 "_default_CookieClientOk": 298237641,
307 "_default_QueryTimeout": 336165169,
308 "SPF": 2887,
309 "_default_DNAME": 6,
310 "_bind_Lame": 0,
311 "QryUDP": 36511992002,
312 "NOTIFY": 390521,
313 "DNAME": 1,
314 "DS": 1688561,
315 "_default_OtherError": 1464741,
316 "_default_Retry": 784916992,
317 "_default_TXT": 43650696,
318 "QryBADCOOKIE": 0,
319 "RespEDNS0": 528451140,
320 "TXT": 100195931,
321 "OtherOpt": 3431439,
322 "_default_HINFO": 1,
323 "RESERVED0": 13705,
324 "_bind_CacheHits": 0,
325 "ReqTCP": 4241537,
326 "RespTSIG": 0,
327 "RESERVED11": 3,
328 "_default_QryRTT100": 2087797539,
329 "_default_REFUSED": 106782830,
330 "_bind_SERVFAIL": 0,
331 "X25": 2,
332 "_default_RP": 134,
333 "QryDuplicate": 289518897,
334 "CookieNoMatch": 34013,
335 "_default_BadCookieRcode": 15399,
336 "_default_CacheMisses": 127371,
337 "_bind_Mismatch": 0,
338 "_default_ServerCookieOut": 365308714,
339 "_bind_QryRTT500": 0,
340 "RPZRewrites": 0,
341 "A": 32377004350,
342 "_default_NextItem": 1790135,
343 "_default_MX": 1576150,
344 "_bind_REFUSED": 0,
345 "_bind_ZoneQuota": 0,
346 "_default_ServerQuota": 0,
347 "_default_ANY": 5149916,
348 "_default_EUI64": 2087,
349 "_default_QueryCurUDP": 0,
350 "RESERVED7": 0,
351 "IXFR": 33,
352 "_default_Queryv4": 4509791268,
353 "_default_GlueFetchv4": 110749701,
354 "_default_TYPE115": 112,
355 "_bind_QueryAbort": 0,
356 "UpdateReqFwd": 0,
357 "_default_NSEC3PARAM": 995,
358 "_bind_NextItem": 0,
359 "RecursClients": 64,
360 "QryReferral": 1152178,
361 "QryFORMERR": 8,
362 "CookieIn": 1220424,
363 "NSIDOpt": 81,
364 "MAILA": 44,
365 "TYPE223": 2,
366 "RRSIG": 25193,
367 "UpdateBadPrereq": 0,
368 "UpdateRej": 15661,
369 "QryAuthAns": 440885288,
370 "_default_PTR": 208337408,
371 "_default_Others": 813,
372 "_default_NS": 676773,
373 "_bind_GlueFetchv4": 0,
374 "QryNoauthAns": 35593104164,
375 "QryRecursion": 3671792792,
376 "_default_ClientCookieOut": 3795901994,
377 "_bind_BadEDNSVersion": 0,
378 "ReqEdns0": 532586114,
379 "RateDropped": 230,
380 "_default_ValOk": 0,
381 "CNAME": 535141,
382 "AuthQryRej": 148159,
383 "RESERVED10": 5,
384 "_default_QueryCurTCP": 0,
385 "_bind_Queryv4": 0,
386 "_bind_CacheMisses": 509,
387 "ExpireOpt": 195,
388 "XfrRej": 97,
389 "_default_DNSKEY": 182399,
390 "RecQryRej": 225832466,
391 "NSEC": 81,
392 "_default_Responsev4": 4175093103,
393 "_bind_ValOk": 0,
394 "_bind_QueryCurTCP": 0,
395 "Requestv4": 36823884979,
396 "DNSKEY": 143600,
397 "_default_LOC": 21,
398 "UpdateRespFwd": 0,
399 "AXFR": 105,
400 "_bind_CookieIn": 0,
401 "_default_QryRTT1600": 455849,
402 "_bind_BadCookieRcode": 0,
403 "QryNXDOMAIN": 5911582433,
404 "ReqSIG0": 0,
405 "QUERY": 36823356081,
406 "NULL": 3606,
407 "_default_Lame": 1979599,
408 "_default_DS": 974240,
409 "SRV": 27709732,
410 "_bind_QuerySockFail": 0,
411 "MG": 4,
412 "_default_QryRTT800": 2712733,
413 "_bind_QryRTT1600+": 0,
414 "DNS64": 0,
415 "_default_Truncated": 14028716,
416 "_default_QryRTT10": 629577,
417 "_default_SERVFAIL": 6533579,
418 "_default_AFSDB": 5,
419 "STATUS": 35585,
420 "Response": 36482142477,
421 "KeyTagOpt": 0,
422 "_default_Mismatch": 0,
423 "Requestv6": 156,
424 "LOC": 52,
425 "_bind_NXDOMAIN": 0,
426 "PTR": 694347710,
427 "_default_NSEC": 53712,
428 "_bind_QryRTT100": 0,
429 "RESERVED8": 0,
430 "DLV": 37712,
431 "HINFO": 9,
432 "_default_AAAA": 818803359,
433 "QryNXRedirRLookup": 0,
434 "TYPE127": 1,
435 "_default_EDNS0Fail": 3987571,
436 "_default_CDS": 251,
437 "_bind_ServerCookieOut": 0,
438 "_bind_QueryCurUDP": 0,
439 "_bind_GlueFetchv6Fail": 0,
440 "UpdateFail": 0,
441 "_default_ZoneQuota": 0,
442 "_default_QuerySockFail": 0,
443 "_default_GlueFetchv6Fail": 91852240,
444 "RespSIG0": 0,
445 "_default_GlueFetchv4Fail": 3964627,
446 "_bind_Responsev6": 0,
447 "_default_GlueFetchv6": 121268854,
448 "_default_Queryv6": 292282376,
449 "TruncatedResp": 25899017,
450 "ReqTSIG": 0,
451 "_default_BadEDNSVersion": 0,
452 "_bind_NumFetch": 0,
453 "RESERVED12": 1,
454 "_default_Responsev6": 152,
455 "_default_SRV": 3855156,
456 "ANY": 298567781,
457 "_default_CNAME": 38213966,
458 "_bind_ClientCookieOut": 0,
459 "NAPTR": 109998,
460 "_default_QryRTT500": 2075608518,
461 "_default_A6": 558874,
462 "_bind_OtherError": 0,
463 "CookieMatch": 125340,
464 "_default_QryRTT1600+": 27681,
465 "_default_DLV": 20468,
466 "_default_NULL": 3554,
467 "_bind_Queryv6": 0,
468 "_bind_QueryTimeout": 0,
469 "_bind_ValAttempt": 0,
470 "RESERVED9": 0,
471 "A6": 539773,
472 "MX": 1484497,
473 "QrySuccess": 28810069822,
474 "XfrReqDone": 0,
475 "RESERVED15": 0,
476 "MAILB": 42,
477 "Others": 74007,
478 "_bind_ServerQuota": 0,
479 "_bind_EDNS0Fail": 0,
480 "QryNxrrset": 1311185019,
481 "QryFailure": 225980711,
482 "ReqBadSIG": 0,
483 "UpdateFwdFail": 0,
484 "ECSOpt": 8743959,
485 "QryDropped": 52215943,
486 "EUI64": 627,
487 "_default_ValAttempt": 0,
488 "_default_A": 3678445415,
489 "_bind_QryRTT800": 0,
490 "_default_NXDOMAIN": 1247746765,
491 "_default_RESERVED0": 19,
492 "_default_NumFetch": 62,
493 "_bind_Responsev4": 0,
494 "_bind_QryRTT1600": 0,
495 "CookieNew": 1061071,
496 "ReqBadEDNSVer": 450,
497 "TYPE115": 285,
498 "_bind_FORMERR": 0,
499 "SOA": 3863889,
500 "_bind_QryRTT10": 0,
501 "CookieBadSize": 0,
502 "_bind_GlueFetchv6": 0,
503 "QryNXRedir": 0,
504 "AAAA": 3309600766,
505 "_default_QueryAbort": 0,
506 "QryTCP": 4233061,
507 "UpdateDone": 0,
508 "IQUERY": 199,
509 }
510
511 assert.Equal(t, expected, collr.Collect(context.Background()))
512 assert.Len(t, *collr.charts, 20)
513 }
514
515 func TestCollector_InvalidData(t *testing.T) {
516 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
517 _, _ = w.Write([]byte("hello and goodbye"))
518 }))
519 defer ts.Close()
520
521 collr := New()
522 collr.URL = ts.URL + "/json/v1"
523 require.NoError(t, collr.Init(context.Background()))
524 assert.Error(t, collr.Check(context.Background()))
525 }
526
527 func TestCollector_404(t *testing.T) {
528 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
529 w.WriteHeader(404)
530 }))
531 defer ts.Close()
532
533 collr := New()
534 collr.URL = ts.URL + "/json/v1"
535 require.NoError(t, collr.Init(context.Background()))
536 assert.Error(t, collr.Check(context.Background()))
537 }