master
go 722 lines 29.5 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package consul
4
5 import (
6 "context"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "testing"
11
12 "github.com/netdata/netdata/go/plugins/pkg/web"
13 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
14
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17 )
18
19 var (
20 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
21 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
22
23 dataVer1132Checks, _ = os.ReadFile("testdata/v1.13.2/v1-agent-checks.json")
24 dataVer1132ClientSelf, _ = os.ReadFile("testdata/v1.13.2/client_v1-agent-self.json")
25 dataVer1132ClientPromMetrics, _ = os.ReadFile("testdata/v1.13.2/client_v1-agent-metrics.txt")
26 dataVer1132ServerSelf, _ = os.ReadFile("testdata/v1.13.2/server_v1-agent-self.json")
27 dataVer1132ServerSelfDisabledPrometheus, _ = os.ReadFile("testdata/v1.13.2/server_v1-agent-self_disabled_prom.json")
28 dataVer1132ServerSelfWithHostname, _ = os.ReadFile("testdata/v1.13.2/server_v1-agent-self_with_hostname.json")
29 dataVer1132ServerPromMetrics, _ = os.ReadFile("testdata/v1.13.2/server_v1-agent-metrics.txt")
30 dataVer1132ServerPromMetricsWithHostname, _ = os.ReadFile("testdata/v1.13.2/server_v1-agent-metrics_with_hostname.txt")
31 dataVer1132ServerOperatorAutopilotHealth, _ = os.ReadFile("testdata/v1.13.2/server_v1-operator-autopilot-health.json")
32 dataVer1132ServerCoordinateNodes, _ = os.ReadFile("testdata/v1.13.2/server_v1-coordinate-nodes.json")
33
34 dataVer1143CloudServerPromMetrics, _ = os.ReadFile("testdata/v1.14.3-cloud/server_v1-agent-metrics.txt")
35 dataVer1143CloudServerSelf, _ = os.ReadFile("testdata/v1.14.3-cloud/server_v1-agent-self.json")
36 dataVer1143CloudServerCoordinateNodes, _ = os.ReadFile("testdata/v1.14.3-cloud/server_v1-coordinate-nodes.json")
37 dataVer1143CloudChecks, _ = os.ReadFile("testdata/v1.14.3-cloud/v1-agent-checks.json")
38 )
39
40 func Test_testDataIsValid(t *testing.T) {
41 for name, data := range map[string][]byte{
42 "dataConfigJSON": dataConfigJSON,
43 "dataConfigYAML": dataConfigYAML,
44 "dataVer1132Checks": dataVer1132Checks,
45 "dataVer1132ClientSelf": dataVer1132ClientSelf,
46 "dataVer1132ClientPromMetrics": dataVer1132ClientPromMetrics,
47 "dataVer1132ServerSelf": dataVer1132ServerSelf,
48 "dataVer1132ServerSelfWithHostname": dataVer1132ServerSelfWithHostname,
49 "dataVer1132ServerSelfDisabledPrometheus": dataVer1132ServerSelfDisabledPrometheus,
50 "dataVer1132ServerPromMetrics": dataVer1132ServerPromMetrics,
51 "dataVer1132ServerPromMetricsWithHostname": dataVer1132ServerPromMetricsWithHostname,
52 "dataVer1132ServerOperatorAutopilotHealth": dataVer1132ServerOperatorAutopilotHealth,
53 "dataVer1132ServerCoordinateNodes": dataVer1132ServerCoordinateNodes,
54 "dataVer1143CloudServerPromMetrics": dataVer1143CloudServerPromMetrics,
55 "dataVer1143CloudServerSelf": dataVer1143CloudServerSelf,
56 "dataVer1143CloudServerCoordinateNodes": dataVer1143CloudServerCoordinateNodes,
57 "dataVer1143CloudChecks": dataVer1143CloudChecks,
58 } {
59 require.NotNil(t, data, name)
60 }
61 }
62
63 func TestCollector_ConfigurationSerialize(t *testing.T) {
64 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
65 }
66
67 func TestCollector_Init(t *testing.T) {
68 tests := map[string]struct {
69 wantFail bool
70 config Config
71 }{
72 "success with default": {
73 wantFail: false,
74 config: New().Config,
75 },
76 "fail when URL not set": {
77 wantFail: true,
78 config: Config{
79 HTTPConfig: web.HTTPConfig{
80 RequestConfig: web.RequestConfig{URL: ""},
81 },
82 },
83 },
84 }
85
86 for name, test := range tests {
87 t.Run(name, func(t *testing.T) {
88 collr := New()
89 collr.Config = test.config
90
91 if test.wantFail {
92 assert.Error(t, collr.Init(context.Background()))
93 } else {
94 assert.NoError(t, collr.Init(context.Background()))
95 }
96 })
97 }
98 }
99
100 func TestCollector_Check(t *testing.T) {
101 tests := map[string]struct {
102 wantFail bool
103 prepare func(t *testing.T) (collr *Collector, cleanup func())
104 }{
105 "success on response from Consul v1.13.2 server": {
106 wantFail: false,
107 prepare: caseConsulV1132ServerResponse,
108 },
109 "success on response from Consul v1.14.3 server cloud managed": {
110 wantFail: false,
111 prepare: caseConsulV1143CloudServerResponse,
112 },
113 "success on response from Consul v1.13.2 server with enabled hostname": {
114 wantFail: false,
115 prepare: caseConsulV1132ServerWithHostnameResponse,
116 },
117 "success on response from Consul v1.13.2 server with disabled prometheus": {
118 wantFail: false,
119 prepare: caseConsulV1132ServerWithDisabledPrometheus,
120 },
121 "success on response from Consul v1.13.2 client": {
122 wantFail: false,
123 prepare: caseConsulV1132ClientResponse,
124 },
125 "fail on invalid data response": {
126 wantFail: true,
127 prepare: caseInvalidDataResponse,
128 },
129 "fail on connection refused": {
130 wantFail: true,
131 prepare: caseConnectionRefused,
132 },
133 "fail on 404 response": {
134 wantFail: true,
135 prepare: case404,
136 },
137 }
138
139 for name, test := range tests {
140 t.Run(name, func(t *testing.T) {
141 collr, cleanup := test.prepare(t)
142 defer cleanup()
143
144 if test.wantFail {
145 assert.Error(t, collr.Check(context.Background()))
146 } else {
147 assert.NoError(t, collr.Check(context.Background()))
148 }
149 })
150 }
151 }
152
153 func TestCollector_Collect(t *testing.T) {
154 tests := map[string]struct {
155 prepare func(t *testing.T) (collr *Collector, cleanup func())
156 wantNumOfCharts int
157 wantMetrics map[string]int64
158 }{
159 "success on response from Consul v1.13.2 server": {
160 prepare: caseConsulV1132ServerResponse,
161 // 3 node, 1 service check, no license
162 wantNumOfCharts: len(serverCommonCharts) + len(serverAutopilotHealthCharts) + len(serverLeaderCharts) + 3 + 1 - 1,
163 wantMetrics: map[string]int64{
164 "autopilot_failure_tolerance": 1,
165 "autopilot_healthy_no": 0,
166 "autopilot_healthy_yes": 1,
167 "autopilot_server_healthy_no": 0,
168 "autopilot_server_healthy_yes": 1,
169 "autopilot_server_lastContact_leader": 13,
170 "autopilot_server_sefStatus_alive": 1,
171 "autopilot_server_sefStatus_failed": 0,
172 "autopilot_server_sefStatus_left": 0,
173 "autopilot_server_sefStatus_none": 0,
174 "autopilot_server_stable_time": 265849,
175 "autopilot_server_voter_no": 0,
176 "autopilot_server_voter_yes": 1,
177 "client_rpc": 6838,
178 "client_rpc_exceeded": 0,
179 "client_rpc_failed": 0,
180 "health_check_chk1_critical_status": 0,
181 "health_check_chk1_maintenance_status": 0,
182 "health_check_chk1_passing_status": 1,
183 "health_check_chk1_warning_status": 0,
184 "health_check_chk2_critical_status": 1,
185 "health_check_chk2_maintenance_status": 0,
186 "health_check_chk2_passing_status": 0,
187 "health_check_chk2_warning_status": 0,
188 "health_check_chk3_critical_status": 1,
189 "health_check_chk3_maintenance_status": 0,
190 "health_check_chk3_passing_status": 0,
191 "health_check_chk3_warning_status": 0,
192 "health_check_mysql_critical_status": 1,
193 "health_check_mysql_maintenance_status": 0,
194 "health_check_mysql_passing_status": 0,
195 "health_check_mysql_warning_status": 0,
196 "kvs_apply_count": 0,
197 "kvs_apply_quantile=0.5": 0,
198 "kvs_apply_quantile=0.9": 0,
199 "kvs_apply_quantile=0.99": 0,
200 "kvs_apply_sum": 0,
201 "network_lan_rtt_avg": 737592,
202 "network_lan_rtt_count": 2,
203 "network_lan_rtt_max": 991168,
204 "network_lan_rtt_min": 484017,
205 "network_lan_rtt_sum": 1475185,
206 "raft_apply": 10681000,
207 "raft_boltdb_freelistBytes": 11264,
208 "raft_boltdb_logsPerBatch_count": 12360,
209 "raft_boltdb_logsPerBatch_quantile=0.5": 1000000,
210 "raft_boltdb_logsPerBatch_quantile=0.9": 1000000,
211 "raft_boltdb_logsPerBatch_quantile=0.99": 1000000,
212 "raft_boltdb_logsPerBatch_sum": 12362000,
213 "raft_boltdb_storeLogs_count": 12360,
214 "raft_boltdb_storeLogs_quantile=0.5": 13176624,
215 "raft_boltdb_storeLogs_quantile=0.9": 13176624,
216 "raft_boltdb_storeLogs_quantile=0.99": 13176624,
217 "raft_boltdb_storeLogs_sum": 651888027,
218 "raft_commitTime_count": 12345,
219 "raft_commitTime_quantile=0.5": 41146488,
220 "raft_commitTime_quantile=0.9": 41146488,
221 "raft_commitTime_quantile=0.99": 41146488,
222 "raft_commitTime_sum": 955781149,
223 "raft_fsm_lastRestoreDuration": 2,
224 "raft_leader_lastContact_count": 80917,
225 "raft_leader_lastContact_quantile=0.5": 33000000,
226 "raft_leader_lastContact_quantile=0.9": 68000000,
227 "raft_leader_lastContact_quantile=0.99": 68000000,
228 "raft_leader_lastContact_sum": 3066900000,
229 "raft_leader_oldestLogAge": 166046464,
230 "raft_rpc_installSnapshot_count": 0,
231 "raft_rpc_installSnapshot_quantile=0.5": 0,
232 "raft_rpc_installSnapshot_quantile=0.9": 0,
233 "raft_rpc_installSnapshot_quantile=0.99": 0,
234 "raft_rpc_installSnapshot_sum": 0,
235 "raft_state_candidate": 1,
236 "raft_state_leader": 1,
237 "raft_thread_fsm_saturation_count": 11923,
238 "raft_thread_fsm_saturation_quantile=0.5": 0,
239 "raft_thread_fsm_saturation_quantile=0.9": 0,
240 "raft_thread_fsm_saturation_quantile=0.99": 0,
241 "raft_thread_fsm_saturation_sum": 90,
242 "raft_thread_main_saturation_count": 43067,
243 "raft_thread_main_saturation_quantile=0.5": 0,
244 "raft_thread_main_saturation_quantile=0.9": 0,
245 "raft_thread_main_saturation_quantile=0.99": 0,
246 "raft_thread_main_saturation_sum": 205409,
247 "runtime_alloc_bytes": 53065368,
248 "runtime_sys_bytes": 84955160,
249 "runtime_total_gc_pause_ns": 1372001280,
250 "server_isLeader_no": 0,
251 "server_isLeader_yes": 1,
252 "txn_apply_count": 0,
253 "txn_apply_quantile=0.5": 0,
254 "txn_apply_quantile=0.9": 0,
255 "txn_apply_quantile=0.99": 0,
256 "txn_apply_sum": 0,
257 },
258 },
259 "success on response from Consul v1.14.3 server cloud managed": {
260 prepare: caseConsulV1143CloudServerResponse,
261 // 3 node, 1 service check, license
262 wantNumOfCharts: len(serverCommonCharts) + len(serverLeaderCharts) + 3 + 1,
263 wantMetrics: map[string]int64{
264 "autopilot_failure_tolerance": 0,
265 "autopilot_healthy_no": 0,
266 "autopilot_healthy_yes": 1,
267 "client_rpc": 438718,
268 "client_rpc_exceeded": 0,
269 "client_rpc_failed": 0,
270 "health_check_chk1_critical_status": 0,
271 "health_check_chk1_maintenance_status": 0,
272 "health_check_chk1_passing_status": 1,
273 "health_check_chk1_warning_status": 0,
274 "health_check_chk2_critical_status": 1,
275 "health_check_chk2_maintenance_status": 0,
276 "health_check_chk2_passing_status": 0,
277 "health_check_chk2_warning_status": 0,
278 "health_check_chk3_critical_status": 1,
279 "health_check_chk3_maintenance_status": 0,
280 "health_check_chk3_passing_status": 0,
281 "health_check_chk3_warning_status": 0,
282 "health_check_mysql_critical_status": 1,
283 "health_check_mysql_maintenance_status": 0,
284 "health_check_mysql_passing_status": 0,
285 "health_check_mysql_warning_status": 0,
286 "kvs_apply_count": 2,
287 "kvs_apply_quantile=0.5": 0,
288 "kvs_apply_quantile=0.9": 0,
289 "kvs_apply_quantile=0.99": 0,
290 "kvs_apply_sum": 18550,
291 "network_lan_rtt_avg": 1321107,
292 "network_lan_rtt_count": 1,
293 "network_lan_rtt_max": 1321107,
294 "network_lan_rtt_min": 1321107,
295 "network_lan_rtt_sum": 1321107,
296 "raft_apply": 115252000,
297 "raft_boltdb_freelistBytes": 26008,
298 "raft_boltdb_logsPerBatch_count": 122794,
299 "raft_boltdb_logsPerBatch_quantile=0.5": 1000000,
300 "raft_boltdb_logsPerBatch_quantile=0.9": 1000000,
301 "raft_boltdb_logsPerBatch_quantile=0.99": 1000000,
302 "raft_boltdb_logsPerBatch_sum": 122856000,
303 "raft_boltdb_storeLogs_count": 122794,
304 "raft_boltdb_storeLogs_quantile=0.5": 1673303,
305 "raft_boltdb_storeLogs_quantile=0.9": 2210979,
306 "raft_boltdb_storeLogs_quantile=0.99": 2210979,
307 "raft_boltdb_storeLogs_sum": 278437403,
308 "raft_commitTime_count": 122785,
309 "raft_commitTime_quantile=0.5": 1718204,
310 "raft_commitTime_quantile=0.9": 2262192,
311 "raft_commitTime_quantile=0.99": 2262192,
312 "raft_commitTime_sum": 284260428,
313 "raft_fsm_lastRestoreDuration": 0,
314 "raft_leader_lastContact_count": 19,
315 "raft_leader_lastContact_quantile=0.5": 0,
316 "raft_leader_lastContact_quantile=0.9": 0,
317 "raft_leader_lastContact_quantile=0.99": 0,
318 "raft_leader_lastContact_sum": 598000,
319 "raft_leader_oldestLogAge": 68835264,
320 "raft_rpc_installSnapshot_count": 1,
321 "raft_rpc_installSnapshot_quantile=0.5": 0,
322 "raft_rpc_installSnapshot_quantile=0.9": 0,
323 "raft_rpc_installSnapshot_quantile=0.99": 0,
324 "raft_rpc_installSnapshot_sum": 473038,
325 "raft_state_candidate": 1,
326 "raft_state_leader": 1,
327 "raft_thread_fsm_saturation_count": 44326,
328 "raft_thread_fsm_saturation_quantile=0.5": 0,
329 "raft_thread_fsm_saturation_quantile=0.9": 0,
330 "raft_thread_fsm_saturation_quantile=0.99": 0,
331 "raft_thread_fsm_saturation_sum": 729,
332 "raft_thread_main_saturation_count": 451221,
333 "raft_thread_main_saturation_quantile=0.5": 0,
334 "raft_thread_main_saturation_quantile=0.9": 0,
335 "raft_thread_main_saturation_quantile=0.99": 9999,
336 "raft_thread_main_saturation_sum": 213059,
337 "runtime_alloc_bytes": 51729856,
338 "runtime_sys_bytes": 160156960,
339 "runtime_total_gc_pause_ns": 832754048,
340 "server_isLeader_no": 0,
341 "server_isLeader_yes": 1,
342 "system_licenseExpiration": 2949945,
343 "txn_apply_count": 0,
344 "txn_apply_quantile=0.5": 0,
345 "txn_apply_quantile=0.9": 0,
346 "txn_apply_quantile=0.99": 0,
347 "txn_apply_sum": 0,
348 },
349 },
350 "success on response from Consul v1.13.2 server with enabled hostname": {
351 prepare: caseConsulV1132ServerResponse,
352 // 3 node, 1 service check, no license
353 wantNumOfCharts: len(serverCommonCharts) + len(serverAutopilotHealthCharts) + len(serverLeaderCharts) + 3 + 1 - 1,
354 wantMetrics: map[string]int64{
355 "autopilot_failure_tolerance": 1,
356 "autopilot_healthy_no": 0,
357 "autopilot_healthy_yes": 1,
358 "autopilot_server_healthy_no": 0,
359 "autopilot_server_healthy_yes": 1,
360 "autopilot_server_lastContact_leader": 13,
361 "autopilot_server_sefStatus_alive": 1,
362 "autopilot_server_sefStatus_failed": 0,
363 "autopilot_server_sefStatus_left": 0,
364 "autopilot_server_sefStatus_none": 0,
365 "autopilot_server_stable_time": 265825,
366 "autopilot_server_voter_no": 0,
367 "autopilot_server_voter_yes": 1,
368 "client_rpc": 6838,
369 "client_rpc_exceeded": 0,
370 "client_rpc_failed": 0,
371 "health_check_chk1_critical_status": 0,
372 "health_check_chk1_maintenance_status": 0,
373 "health_check_chk1_passing_status": 1,
374 "health_check_chk1_warning_status": 0,
375 "health_check_chk2_critical_status": 1,
376 "health_check_chk2_maintenance_status": 0,
377 "health_check_chk2_passing_status": 0,
378 "health_check_chk2_warning_status": 0,
379 "health_check_chk3_critical_status": 1,
380 "health_check_chk3_maintenance_status": 0,
381 "health_check_chk3_passing_status": 0,
382 "health_check_chk3_warning_status": 0,
383 "health_check_mysql_critical_status": 1,
384 "health_check_mysql_maintenance_status": 0,
385 "health_check_mysql_passing_status": 0,
386 "health_check_mysql_warning_status": 0,
387 "kvs_apply_count": 0,
388 "kvs_apply_quantile=0.5": 0,
389 "kvs_apply_quantile=0.9": 0,
390 "kvs_apply_quantile=0.99": 0,
391 "kvs_apply_sum": 0,
392 "network_lan_rtt_avg": 737592,
393 "network_lan_rtt_count": 2,
394 "network_lan_rtt_max": 991168,
395 "network_lan_rtt_min": 484017,
396 "network_lan_rtt_sum": 1475185,
397 "raft_apply": 10681000,
398 "raft_boltdb_freelistBytes": 11264,
399 "raft_boltdb_logsPerBatch_count": 12360,
400 "raft_boltdb_logsPerBatch_quantile=0.5": 1000000,
401 "raft_boltdb_logsPerBatch_quantile=0.9": 1000000,
402 "raft_boltdb_logsPerBatch_quantile=0.99": 1000000,
403 "raft_boltdb_logsPerBatch_sum": 12362000,
404 "raft_boltdb_storeLogs_count": 12360,
405 "raft_boltdb_storeLogs_quantile=0.5": 13176624,
406 "raft_boltdb_storeLogs_quantile=0.9": 13176624,
407 "raft_boltdb_storeLogs_quantile=0.99": 13176624,
408 "raft_boltdb_storeLogs_sum": 651888027,
409 "raft_commitTime_count": 12345,
410 "raft_commitTime_quantile=0.5": 41146488,
411 "raft_commitTime_quantile=0.9": 41146488,
412 "raft_commitTime_quantile=0.99": 41146488,
413 "raft_commitTime_sum": 955781149,
414 "raft_fsm_lastRestoreDuration": 2,
415 "raft_leader_lastContact_count": 80917,
416 "raft_leader_lastContact_quantile=0.5": 33000000,
417 "raft_leader_lastContact_quantile=0.9": 68000000,
418 "raft_leader_lastContact_quantile=0.99": 68000000,
419 "raft_leader_lastContact_sum": 3066900000,
420 "raft_leader_oldestLogAge": 166046464,
421 "raft_rpc_installSnapshot_count": 0,
422 "raft_rpc_installSnapshot_quantile=0.5": 0,
423 "raft_rpc_installSnapshot_quantile=0.9": 0,
424 "raft_rpc_installSnapshot_quantile=0.99": 0,
425 "raft_rpc_installSnapshot_sum": 0,
426 "raft_state_candidate": 1,
427 "raft_state_leader": 1,
428 "raft_thread_fsm_saturation_count": 11923,
429 "raft_thread_fsm_saturation_quantile=0.5": 0,
430 "raft_thread_fsm_saturation_quantile=0.9": 0,
431 "raft_thread_fsm_saturation_quantile=0.99": 0,
432 "raft_thread_fsm_saturation_sum": 90,
433 "raft_thread_main_saturation_count": 43067,
434 "raft_thread_main_saturation_quantile=0.5": 0,
435 "raft_thread_main_saturation_quantile=0.9": 0,
436 "raft_thread_main_saturation_quantile=0.99": 0,
437 "raft_thread_main_saturation_sum": 205409,
438 "runtime_alloc_bytes": 53065368,
439 "runtime_sys_bytes": 84955160,
440 "runtime_total_gc_pause_ns": 1372001280,
441 "server_isLeader_no": 0,
442 "server_isLeader_yes": 1,
443 "txn_apply_count": 0,
444 "txn_apply_quantile=0.5": 0,
445 "txn_apply_quantile=0.9": 0,
446 "txn_apply_quantile=0.99": 0,
447 "txn_apply_sum": 0,
448 },
449 },
450 "success on response from Consul v1.13.2 server with disabled prometheus": {
451 prepare: caseConsulV1132ServerWithDisabledPrometheus,
452 // 3 node, 1 service check, no license
453 wantNumOfCharts: len(serverAutopilotHealthCharts) + 3 + 1,
454 wantMetrics: map[string]int64{
455 "autopilot_server_healthy_no": 0,
456 "autopilot_server_healthy_yes": 1,
457 "autopilot_server_lastContact_leader": 13,
458 "autopilot_server_sefStatus_alive": 1,
459 "autopilot_server_sefStatus_failed": 0,
460 "autopilot_server_sefStatus_left": 0,
461 "autopilot_server_sefStatus_none": 0,
462 "autopilot_server_stable_time": 265805,
463 "autopilot_server_voter_no": 0,
464 "autopilot_server_voter_yes": 1,
465 "health_check_chk1_critical_status": 0,
466 "health_check_chk1_maintenance_status": 0,
467 "health_check_chk1_passing_status": 1,
468 "health_check_chk1_warning_status": 0,
469 "health_check_chk2_critical_status": 1,
470 "health_check_chk2_maintenance_status": 0,
471 "health_check_chk2_passing_status": 0,
472 "health_check_chk2_warning_status": 0,
473 "health_check_chk3_critical_status": 1,
474 "health_check_chk3_maintenance_status": 0,
475 "health_check_chk3_passing_status": 0,
476 "health_check_chk3_warning_status": 0,
477 "health_check_mysql_critical_status": 1,
478 "health_check_mysql_maintenance_status": 0,
479 "health_check_mysql_passing_status": 0,
480 "health_check_mysql_warning_status": 0,
481 "network_lan_rtt_avg": 737592,
482 "network_lan_rtt_count": 2,
483 "network_lan_rtt_max": 991168,
484 "network_lan_rtt_min": 484017,
485 "network_lan_rtt_sum": 1475185,
486 },
487 },
488 "success on response from Consul v1.13.2 client": {
489 prepare: caseConsulV1132ClientResponse,
490 // 3 node, 1 service check, no license
491 wantNumOfCharts: len(clientCharts) + 3 + 1 - 1,
492 wantMetrics: map[string]int64{
493 "client_rpc": 34,
494 "client_rpc_exceeded": 0,
495 "client_rpc_failed": 0,
496 "health_check_chk1_critical_status": 0,
497 "health_check_chk1_maintenance_status": 0,
498 "health_check_chk1_passing_status": 1,
499 "health_check_chk1_warning_status": 0,
500 "health_check_chk2_critical_status": 1,
501 "health_check_chk2_maintenance_status": 0,
502 "health_check_chk2_passing_status": 0,
503 "health_check_chk2_warning_status": 0,
504 "health_check_chk3_critical_status": 1,
505 "health_check_chk3_maintenance_status": 0,
506 "health_check_chk3_passing_status": 0,
507 "health_check_chk3_warning_status": 0,
508 "health_check_mysql_critical_status": 1,
509 "health_check_mysql_maintenance_status": 0,
510 "health_check_mysql_passing_status": 0,
511 "health_check_mysql_warning_status": 0,
512 "runtime_alloc_bytes": 26333408,
513 "runtime_sys_bytes": 51201032,
514 "runtime_total_gc_pause_ns": 4182423,
515 },
516 },
517 "fail on invalid data response": {
518 prepare: caseInvalidDataResponse,
519 wantNumOfCharts: 0,
520 wantMetrics: nil,
521 },
522 "fail on connection refused": {
523 prepare: caseConnectionRefused,
524 wantNumOfCharts: 0,
525 wantMetrics: nil,
526 },
527 "fail on 404 response": {
528 prepare: case404,
529 wantNumOfCharts: 0,
530 wantMetrics: nil,
531 },
532 }
533
534 for name, test := range tests {
535 t.Run(name, func(t *testing.T) {
536 collr, cleanup := test.prepare(t)
537 defer cleanup()
538
539 mx := collr.Collect(context.Background())
540
541 delete(mx, "autopilot_server_stable_time")
542 delete(test.wantMetrics, "autopilot_server_stable_time")
543
544 require.Equal(t, test.wantMetrics, mx)
545 if len(test.wantMetrics) > 0 {
546 assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts()))
547 }
548 })
549 }
550 }
551
552 func caseConsulV1143CloudServerResponse(t *testing.T) (*Collector, func()) {
553 t.Helper()
554 srv := httptest.NewServer(http.HandlerFunc(
555 func(w http.ResponseWriter, r *http.Request) {
556 switch {
557 case r.URL.Path == urlPathAgentSelf:
558 _, _ = w.Write(dataVer1143CloudServerSelf)
559 case r.URL.Path == urlPathAgentChecks:
560 _, _ = w.Write(dataVer1143CloudChecks)
561 case r.URL.Path == urlPathAgentMetrics && r.URL.RawQuery == "format=prometheus":
562 _, _ = w.Write(dataVer1143CloudServerPromMetrics)
563 case r.URL.Path == urlPathOperationAutopilotHealth:
564 w.WriteHeader(http.StatusForbidden)
565 case r.URL.Path == urlPathCoordinateNodes:
566 _, _ = w.Write(dataVer1143CloudServerCoordinateNodes)
567 default:
568 w.WriteHeader(http.StatusNotFound)
569 }
570 }))
571
572 collr := New()
573 collr.URL = srv.URL
574
575 require.NoError(t, collr.Init(context.Background()))
576
577 return collr, srv.Close
578 }
579
580 func caseConsulV1132ServerResponse(t *testing.T) (*Collector, func()) {
581 t.Helper()
582 srv := httptest.NewServer(http.HandlerFunc(
583 func(w http.ResponseWriter, r *http.Request) {
584 switch {
585 case r.URL.Path == urlPathAgentSelf:
586 _, _ = w.Write(dataVer1132ServerSelf)
587 case r.URL.Path == urlPathAgentChecks:
588 _, _ = w.Write(dataVer1132Checks)
589 case r.URL.Path == urlPathAgentMetrics && r.URL.RawQuery == "format=prometheus":
590 _, _ = w.Write(dataVer1132ServerPromMetrics)
591 case r.URL.Path == urlPathOperationAutopilotHealth:
592 _, _ = w.Write(dataVer1132ServerOperatorAutopilotHealth)
593 case r.URL.Path == urlPathCoordinateNodes:
594 _, _ = w.Write(dataVer1132ServerCoordinateNodes)
595 default:
596 w.WriteHeader(http.StatusNotFound)
597 }
598 }))
599
600 collr := New()
601 collr.URL = srv.URL
602
603 require.NoError(t, collr.Init(context.Background()))
604
605 return collr, srv.Close
606 }
607
608 func caseConsulV1132ServerWithHostnameResponse(t *testing.T) (*Collector, func()) {
609 t.Helper()
610 srv := httptest.NewServer(http.HandlerFunc(
611 func(w http.ResponseWriter, r *http.Request) {
612 switch {
613 case r.URL.Path == urlPathAgentSelf:
614 _, _ = w.Write(dataVer1132ServerSelfWithHostname)
615 case r.URL.Path == urlPathAgentChecks:
616 _, _ = w.Write(dataVer1132Checks)
617 case r.URL.Path == urlPathAgentMetrics && r.URL.RawQuery == "format=prometheus":
618 _, _ = w.Write(dataVer1132ServerPromMetricsWithHostname)
619 case r.URL.Path == urlPathOperationAutopilotHealth:
620 _, _ = w.Write(dataVer1132ServerOperatorAutopilotHealth)
621 case r.URL.Path == urlPathCoordinateNodes:
622 _, _ = w.Write(dataVer1132ServerCoordinateNodes)
623 default:
624 w.WriteHeader(http.StatusNotFound)
625 }
626 }))
627
628 collr := New()
629 collr.URL = srv.URL
630
631 require.NoError(t, collr.Init(context.Background()))
632
633 return collr, srv.Close
634 }
635
636 func caseConsulV1132ServerWithDisabledPrometheus(t *testing.T) (*Collector, func()) {
637 t.Helper()
638 srv := httptest.NewServer(http.HandlerFunc(
639 func(w http.ResponseWriter, r *http.Request) {
640 switch r.URL.Path {
641 case urlPathAgentSelf:
642 _, _ = w.Write(dataVer1132ServerSelfDisabledPrometheus)
643 case urlPathAgentChecks:
644 _, _ = w.Write(dataVer1132Checks)
645 case urlPathOperationAutopilotHealth:
646 _, _ = w.Write(dataVer1132ServerOperatorAutopilotHealth)
647 case urlPathCoordinateNodes:
648 _, _ = w.Write(dataVer1132ServerCoordinateNodes)
649 default:
650 w.WriteHeader(http.StatusNotFound)
651 }
652 }))
653
654 collr := New()
655 collr.URL = srv.URL
656
657 require.NoError(t, collr.Init(context.Background()))
658
659 return collr, srv.Close
660 }
661
662 func caseConsulV1132ClientResponse(t *testing.T) (*Collector, func()) {
663 t.Helper()
664 srv := httptest.NewServer(http.HandlerFunc(
665 func(w http.ResponseWriter, r *http.Request) {
666 switch {
667 case r.URL.Path == urlPathAgentSelf:
668 _, _ = w.Write(dataVer1132ClientSelf)
669 case r.URL.Path == urlPathAgentChecks:
670 _, _ = w.Write(dataVer1132Checks)
671 case r.URL.Path == urlPathAgentMetrics && r.URL.RawQuery == "format=prometheus":
672 _, _ = w.Write(dataVer1132ClientPromMetrics)
673 default:
674 w.WriteHeader(http.StatusNotFound)
675 }
676 }))
677
678 collr := New()
679 collr.URL = srv.URL
680
681 require.NoError(t, collr.Init(context.Background()))
682
683 return collr, srv.Close
684 }
685
686 func caseInvalidDataResponse(t *testing.T) (*Collector, func()) {
687 t.Helper()
688 srv := httptest.NewServer(http.HandlerFunc(
689 func(w http.ResponseWriter, r *http.Request) {
690 _, _ = w.Write([]byte("hello and\n goodbye"))
691 }))
692
693 collr := New()
694 collr.URL = srv.URL
695
696 require.NoError(t, collr.Init(context.Background()))
697
698 return collr, srv.Close
699 }
700
701 func caseConnectionRefused(t *testing.T) (*Collector, func()) {
702 t.Helper()
703 collr := New()
704 collr.URL = "http://127.0.0.1:65535/"
705 require.NoError(t, collr.Init(context.Background()))
706
707 return collr, func() {}
708 }
709
710 func case404(t *testing.T) (*Collector, func()) {
711 t.Helper()
712 srv := httptest.NewServer(http.HandlerFunc(
713 func(w http.ResponseWriter, r *http.Request) {
714 w.WriteHeader(http.StatusNotFound)
715 }))
716
717 collr := New()
718 collr.URL = srv.URL
719 require.NoError(t, collr.Init(context.Background()))
720
721 return collr, srv.Close
722 }