summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/traefik/traefik_test.go
blob: b6b77cfb812caab2783128f31312d031ff671904 (plain)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// SPDX-License-Identifier: GPL-3.0-or-later

package traefik

import (
	"net/http"
	"net/http/httptest"
	"os"
	"testing"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/tlscfg"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/web"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var (
	dataConfigJSON, _ = os.ReadFile("testdata/config.json")
	dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")

	dataVer221Metrics, _ = os.ReadFile("testdata/v2.2.1/metrics.txt")
)

func Test_testDataIsValid(t *testing.T) {
	for name, data := range map[string][]byte{
		"dataConfigJSON":    dataConfigJSON,
		"dataConfigYAML":    dataConfigYAML,
		"dataVer221Metrics": dataVer221Metrics,
	} {
		require.NotNil(t, data, name)
	}
}

func TestTraefik_ConfigurationSerialize(t *testing.T) {
	module.TestConfigurationSerialize(t, &Traefik{}, dataConfigJSON, dataConfigYAML)
}

func TestTraefik_Init(t *testing.T) {
	tests := map[string]struct {
		config   Config
		wantFail bool
	}{
		"success on default config": {
			config: New().Config,
		},
		"fails on unset 'url'": {
			wantFail: true,
			config: Config{HTTP: web.HTTP{
				Request: web.Request{},
			}},
		},
		"fails on invalid TLSCA": {
			wantFail: true,
			config: Config{
				HTTP: web.HTTP{
					Client: web.Client{
						TLSConfig: tlscfg.TLSConfig{TLSCA: "testdata/tls"},
					},
				}},
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			rdb := New()
			rdb.Config = test.config

			if test.wantFail {
				assert.Error(t, rdb.Init())
			} else {
				assert.NoError(t, rdb.Init())
			}
		})
	}
}

func TestTraefik_Charts(t *testing.T) {
	assert.NotNil(t, New().Charts())
}

func TestTraefik_Cleanup(t *testing.T) {
	assert.NotPanics(t, New().Cleanup)
}

func TestTraefik_Check(t *testing.T) {
	tests := map[string]struct {
		wantFail bool
		prepare  func(t *testing.T) (tk *Traefik, cleanup func())
	}{
		"success on valid response v2.3.1": {
			wantFail: false,
			prepare:  prepareCaseTraefikV221Metrics,
		},
		"fails on response with unexpected metrics (not HAProxy)": {
			wantFail: true,
			prepare:  prepareCaseNotTraefikMetrics,
		},
		"fails on 404 response": {
			wantFail: true,
			prepare:  prepareCase404Response,
		},
		"fails on connection refused": {
			wantFail: true,
			prepare:  prepareCaseConnectionRefused,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			tk, cleanup := test.prepare(t)
			defer cleanup()

			if test.wantFail {
				assert.Error(t, tk.Check())
			} else {
				assert.NoError(t, tk.Check())
			}
		})
	}
}

func TestTraefik_Collect(t *testing.T) {
	tests := map[string]struct {
		prepare       func(t *testing.T) (tk *Traefik, cleanup func())
		wantCollected []map[string]int64
	}{
		"success on valid response v2.2.1": {
			prepare: prepareCaseTraefikV221Metrics,
			wantCollected: []map[string]int64{
				{
					"entrypoint_open_connections_traefik_http_GET":          1,
					"entrypoint_open_connections_web_http_DELETE":           0,
					"entrypoint_open_connections_web_http_GET":              0,
					"entrypoint_open_connections_web_http_HEAD":             0,
					"entrypoint_open_connections_web_http_OPTIONS":          0,
					"entrypoint_open_connections_web_http_PATCH":            0,
					"entrypoint_open_connections_web_http_POST":             4,
					"entrypoint_open_connections_web_http_PUT":              0,
					"entrypoint_open_connections_web_websocket_GET":         0,
					"entrypoint_request_duration_average_traefik_http_1xx":  0,
					"entrypoint_request_duration_average_traefik_http_2xx":  0,
					"entrypoint_request_duration_average_traefik_http_3xx":  0,
					"entrypoint_request_duration_average_traefik_http_4xx":  0,
					"entrypoint_request_duration_average_traefik_http_5xx":  0,
					"entrypoint_request_duration_average_web_http_1xx":      0,
					"entrypoint_request_duration_average_web_http_2xx":      0,
					"entrypoint_request_duration_average_web_http_3xx":      0,
					"entrypoint_request_duration_average_web_http_4xx":      0,
					"entrypoint_request_duration_average_web_http_5xx":      0,
					"entrypoint_request_duration_average_web_websocket_1xx": 0,
					"entrypoint_request_duration_average_web_websocket_2xx": 0,
					"entrypoint_request_duration_average_web_websocket_3xx": 0,
					"entrypoint_request_duration_average_web_websocket_4xx": 0,
					"entrypoint_request_duration_average_web_websocket_5xx": 0,
					"entrypoint_requests_traefik_http_1xx":                  0,
					"entrypoint_requests_traefik_http_2xx":                  2840814,
					"entrypoint_requests_traefik_http_3xx":                  0,
					"entrypoint_requests_traefik_http_4xx":                  8,
					"entrypoint_requests_traefik_http_5xx":                  0,
					"entrypoint_requests_web_http_1xx":                      0,
					"entrypoint_requests_web_http_2xx":                      1036208982,
					"entrypoint_requests_web_http_3xx":                      416262,
					"entrypoint_requests_web_http_4xx":                      267591379,
					"entrypoint_requests_web_http_5xx":                      223136,
					"entrypoint_requests_web_websocket_1xx":                 0,
					"entrypoint_requests_web_websocket_2xx":                 0,
					"entrypoint_requests_web_websocket_3xx":                 0,
					"entrypoint_requests_web_websocket_4xx":                 79137,
					"entrypoint_requests_web_websocket_5xx":                 0,
				},
			},
		},
		"properly calculating entrypoint request duration delta": {
			prepare: prepareCaseTraefikEntrypointRequestDuration,
			wantCollected: []map[string]int64{
				{
					"entrypoint_request_duration_average_traefik_http_1xx":  0,
					"entrypoint_request_duration_average_traefik_http_2xx":  0,
					"entrypoint_request_duration_average_traefik_http_3xx":  0,
					"entrypoint_request_duration_average_traefik_http_4xx":  0,
					"entrypoint_request_duration_average_traefik_http_5xx":  0,
					"entrypoint_request_duration_average_web_websocket_1xx": 0,
					"entrypoint_request_duration_average_web_websocket_2xx": 0,
					"entrypoint_request_duration_average_web_websocket_3xx": 0,
					"entrypoint_request_duration_average_web_websocket_4xx": 0,
					"entrypoint_request_duration_average_web_websocket_5xx": 0,
				},
				{
					"entrypoint_request_duration_average_traefik_http_1xx":  0,
					"entrypoint_request_duration_average_traefik_http_2xx":  500,
					"entrypoint_request_duration_average_traefik_http_3xx":  0,
					"entrypoint_request_duration_average_traefik_http_4xx":  0,
					"entrypoint_request_duration_average_traefik_http_5xx":  0,
					"entrypoint_request_duration_average_web_websocket_1xx": 0,
					"entrypoint_request_duration_average_web_websocket_2xx": 0,
					"entrypoint_request_duration_average_web_websocket_3xx": 250,
					"entrypoint_request_duration_average_web_websocket_4xx": 0,
					"entrypoint_request_duration_average_web_websocket_5xx": 0,
				},
				{
					"entrypoint_request_duration_average_traefik_http_1xx":  0,
					"entrypoint_request_duration_average_traefik_http_2xx":  1000,
					"entrypoint_request_duration_average_traefik_http_3xx":  0,
					"entrypoint_request_duration_average_traefik_http_4xx":  0,
					"entrypoint_request_duration_average_traefik_http_5xx":  0,
					"entrypoint_request_duration_average_web_websocket_1xx": 0,
					"entrypoint_request_duration_average_web_websocket_2xx": 0,
					"entrypoint_request_duration_average_web_websocket_3xx": 500,
					"entrypoint_request_duration_average_web_websocket_4xx": 0,
					"entrypoint_request_duration_average_web_websocket_5xx": 0,
				},
				{
					"entrypoint_request_duration_average_traefik_http_1xx":  0,
					"entrypoint_request_duration_average_traefik_http_2xx":  0,
					"entrypoint_request_duration_average_traefik_http_3xx":  0,
					"entrypoint_request_duration_average_traefik_http_4xx":  0,
					"entrypoint_request_duration_average_traefik_http_5xx":  0,
					"entrypoint_request_duration_average_web_websocket_1xx": 0,
					"entrypoint_request_duration_average_web_websocket_2xx": 0,
					"entrypoint_request_duration_average_web_websocket_3xx": 0,
					"entrypoint_request_duration_average_web_websocket_4xx": 0,
					"entrypoint_request_duration_average_web_websocket_5xx": 0,
				},
			},
		},
		"fails on response with unexpected metrics (not Traefik)": {
			prepare: prepareCaseNotTraefikMetrics,
		},
		"fails on 404 response": {
			prepare: prepareCase404Response,
		},
		"fails on connection refused": {
			prepare: prepareCaseConnectionRefused,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			tk, cleanup := test.prepare(t)
			defer cleanup()

			var ms map[string]int64
			for _, want := range test.wantCollected {
				ms = tk.Collect()
				assert.Equal(t, want, ms)
			}
			if len(test.wantCollected) > 0 {
				ensureCollectedHasAllChartsDimsVarsIDs(t, tk, ms)
			}
		})
	}
}

func prepareCaseTraefikV221Metrics(t *testing.T) (*Traefik, func()) {
	t.Helper()
	srv := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			_, _ = w.Write(dataVer221Metrics)
		}))
	h := New()
	h.URL = srv.URL
	require.NoError(t, h.Init())

	return h, srv.Close
}

func prepareCaseTraefikEntrypointRequestDuration(t *testing.T) (*Traefik, func()) {
	t.Helper()
	var num int
	srv := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			num++
			switch num {
			case 1:
				_, _ = w.Write([]byte(`
traefik_entrypoint_request_duration_seconds_sum{code="200",entrypoint="traefik",method="GET",protocol="http"} 10.1
traefik_entrypoint_request_duration_seconds_sum{code="300",entrypoint="web",method="GET",protocol="websocket"} 20.1
traefik_entrypoint_request_duration_seconds_count{code="200",entrypoint="traefik",method="PUT",protocol="http"} 30
traefik_entrypoint_request_duration_seconds_count{code="300",entrypoint="web",method="PUT",protocol="websocket"} 40
`))
			case 2:
				_, _ = w.Write([]byte(`
traefik_entrypoint_request_duration_seconds_sum{code="200",entrypoint="traefik",method="GET",protocol="http"} 15.1
traefik_entrypoint_request_duration_seconds_sum{code="300",entrypoint="web",method="GET",protocol="websocket"} 25.1
traefik_entrypoint_request_duration_seconds_count{code="200",entrypoint="traefik",method="PUT",protocol="http"} 40
traefik_entrypoint_request_duration_seconds_count{code="300",entrypoint="web",method="PUT",protocol="websocket"} 60
`))
			default:
				_, _ = w.Write([]byte(`
traefik_entrypoint_request_duration_seconds_sum{code="200",entrypoint="traefik",method="GET",protocol="http"} 25.1
traefik_entrypoint_request_duration_seconds_sum{code="300",entrypoint="web",method="GET",protocol="websocket"} 35.1
traefik_entrypoint_request_duration_seconds_count{code="200",entrypoint="traefik",method="PUT",protocol="http"} 50
traefik_entrypoint_request_duration_seconds_count{code="300",entrypoint="web",method="PUT",protocol="websocket"} 80
`))
			}
		}))
	h := New()
	h.URL = srv.URL
	require.NoError(t, h.Init())

	return h, srv.Close
}

func prepareCaseNotTraefikMetrics(t *testing.T) (*Traefik, func()) {
	t.Helper()
	srv := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			_, _ = w.Write([]byte(`
# HELP application_backend_http_responses_total Total number of HTTP responses.
# TYPE application_backend_http_responses_total counter
application_backend_http_responses_total{proxy="infra-traefik-web",code="1xx"} 0
application_backend_http_responses_total{proxy="infra-vernemq-ws",code="1xx"} 4130401
application_backend_http_responses_total{proxy="infra-traefik-web",code="2xx"} 21338013
application_backend_http_responses_total{proxy="infra-vernemq-ws",code="2xx"} 0
application_backend_http_responses_total{proxy="infra-traefik-web",code="3xx"} 10004
application_backend_http_responses_total{proxy="infra-vernemq-ws",code="3xx"} 0
application_backend_http_responses_total{proxy="infra-traefik-web",code="4xx"} 10170758
application_backend_http_responses_total{proxy="infra-vernemq-ws",code="4xx"} 0
application_backend_http_responses_total{proxy="infra-traefik-web",code="5xx"} 3075
application_backend_http_responses_total{proxy="infra-vernemq-ws",code="5xx"} 0
application_backend_http_responses_total{proxy="infra-traefik-web",code="other"} 5657
application_backend_http_responses_total{proxy="infra-vernemq-ws",code="other"} 0
`))
		}))
	h := New()
	h.URL = srv.URL
	require.NoError(t, h.Init())

	return h, srv.Close
}

func prepareCase404Response(t *testing.T) (*Traefik, func()) {
	t.Helper()
	srv := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusNotFound)
		}))
	h := New()
	h.URL = srv.URL
	require.NoError(t, h.Init())

	return h, srv.Close
}

func prepareCaseConnectionRefused(t *testing.T) (*Traefik, func()) {
	t.Helper()
	h := New()
	h.URL = "http://127.0.0.1:38001"
	require.NoError(t, h.Init())

	return h, func() {}
}

func ensureCollectedHasAllChartsDimsVarsIDs(t *testing.T, tk *Traefik, ms map[string]int64) {
	for _, chart := range *tk.Charts() {
		if chart.Obsolete {
			continue
		}
		for _, dim := range chart.Dims {
			_, ok := ms[dim.ID]
			assert.Truef(t, ok, "chart '%s' dim '%s': no dim in collected", dim.ID, chart.ID)
		}
		for _, v := range chart.Vars {
			_, ok := ms[v.ID]
			assert.Truef(t, ok, "chart '%s' dim '%s': no dim in collected", v.ID, chart.ID)
		}
	}
}