master
go 248 lines 7.62 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package haproxy
4
5 import (
6 "context"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14
15 "github.com/netdata/netdata/go/plugins/pkg/tlscfg"
16 "github.com/netdata/netdata/go/plugins/pkg/web"
17 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
18 )
19
20 var (
21 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
22 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
23
24 dataVer2310Metrics, _ = os.ReadFile("testdata/v2.3.10/metrics.txt")
25 )
26
27 func Test_testDataIsValid(t *testing.T) {
28 for name, data := range map[string][]byte{
29 "dataConfigJSON": dataConfigJSON,
30 "dataConfigYAML": dataConfigYAML,
31 "dataVer2310Metrics": dataVer2310Metrics,
32 } {
33 require.NotNil(t, data, name)
34 }
35 }
36
37 func TestCollector_ConfigurationSerialize(t *testing.T) {
38 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
39 }
40
41 func TestCollector_Init(t *testing.T) {
42 tests := map[string]struct {
43 config Config
44 wantFail bool
45 }{
46 "success on default config": {
47 config: New().Config,
48 },
49 "fails on unset 'url'": {
50 wantFail: true,
51 config: Config{HTTPConfig: web.HTTPConfig{
52 RequestConfig: web.RequestConfig{},
53 }},
54 },
55 "fails on invalid TLSCA": {
56 wantFail: true,
57 config: Config{
58 HTTPConfig: web.HTTPConfig{
59 ClientConfig: web.ClientConfig{
60 TLSConfig: tlscfg.TLSConfig{TLSCA: "testdata/tls"},
61 },
62 }},
63 },
64 }
65
66 for name, test := range tests {
67 t.Run(name, func(t *testing.T) {
68 collr := New()
69 collr.Config = test.config
70
71 if test.wantFail {
72 assert.Error(t, collr.Init(context.Background()))
73 } else {
74 assert.NoError(t, collr.Init(context.Background()))
75 }
76 })
77 }
78 }
79
80 func TestCollector_Charts(t *testing.T) {
81 assert.NotNil(t, New().Charts())
82 }
83
84 func TestCollector_Cleanup(t *testing.T) {
85 assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
86 }
87
88 func TestCollector_Check(t *testing.T) {
89 tests := map[string]struct {
90 wantFail bool
91 prepare func(t *testing.T) (h *Collector, cleanup func())
92 }{
93 "success on valid response v2.3.1": {
94 wantFail: false,
95 prepare: prepareCaseHaproxyV231Metrics,
96 },
97 "fails on response with unexpected metrics (not HAProxy)": {
98 wantFail: true,
99 prepare: prepareCaseNotHaproxyMetrics,
100 },
101 "fails on 404 response": {
102 wantFail: true,
103 prepare: prepareCase404Response,
104 },
105 "fails on connection refused": {
106 wantFail: true,
107 prepare: prepareCaseConnectionRefused,
108 },
109 }
110
111 for name, test := range tests {
112 t.Run(name, func(t *testing.T) {
113 collr, cleanup := test.prepare(t)
114 defer cleanup()
115
116 if test.wantFail {
117 assert.Error(t, collr.Check(context.Background()))
118 } else {
119 assert.NoError(t, collr.Check(context.Background()))
120 }
121 })
122 }
123 }
124
125 func TestCollector_Collect(t *testing.T) {
126 tests := map[string]struct {
127 prepare func(t *testing.T) (c *Collector, cleanup func())
128 wantCollected map[string]int64
129 }{
130 "success on valid response v2.3.1": {
131 prepare: prepareCaseHaproxyV231Metrics,
132 wantCollected: map[string]int64{
133 "haproxy_backend_bytes_in_proxy_proxy1": 21057046294,
134 "haproxy_backend_bytes_in_proxy_proxy2": 2493759083896,
135 "haproxy_backend_bytes_out_proxy_proxy1": 41352782609,
136 "haproxy_backend_bytes_out_proxy_proxy2": 5131407558,
137 "haproxy_backend_current_queue_proxy_proxy1": 1,
138 "haproxy_backend_current_queue_proxy_proxy2": 1,
139 "haproxy_backend_current_sessions_proxy_proxy1": 1,
140 "haproxy_backend_current_sessions_proxy_proxy2": 1322,
141 "haproxy_backend_http_responses_1xx_proxy_proxy1": 1,
142 "haproxy_backend_http_responses_1xx_proxy_proxy2": 4130401,
143 "haproxy_backend_http_responses_2xx_proxy_proxy1": 21338013,
144 "haproxy_backend_http_responses_2xx_proxy_proxy2": 1,
145 "haproxy_backend_http_responses_3xx_proxy_proxy1": 10004,
146 "haproxy_backend_http_responses_3xx_proxy_proxy2": 1,
147 "haproxy_backend_http_responses_4xx_proxy_proxy1": 10170758,
148 "haproxy_backend_http_responses_4xx_proxy_proxy2": 1,
149 "haproxy_backend_http_responses_5xx_proxy_proxy1": 3075,
150 "haproxy_backend_http_responses_5xx_proxy_proxy2": 1,
151 "haproxy_backend_http_responses_other_proxy_proxy1": 5657,
152 "haproxy_backend_http_responses_other_proxy_proxy2": 1,
153 "haproxy_backend_queue_time_average_proxy_proxy1": 0,
154 "haproxy_backend_queue_time_average_proxy_proxy2": 0,
155 "haproxy_backend_response_time_average_proxy_proxy1": 52,
156 "haproxy_backend_response_time_average_proxy_proxy2": 1,
157 "haproxy_backend_sessions_proxy_proxy1": 31527507,
158 "haproxy_backend_sessions_proxy_proxy2": 4131723,
159 },
160 },
161 "fails on response with unexpected metrics (not HAProxy)": {
162 prepare: prepareCaseNotHaproxyMetrics,
163 },
164 "fails on 404 response": {
165 prepare: prepareCase404Response,
166 },
167 "fails on connection refused": {
168 prepare: prepareCaseConnectionRefused,
169 },
170 }
171
172 for name, test := range tests {
173 t.Run(name, func(t *testing.T) {
174 collr, cleanup := test.prepare(t)
175 defer cleanup()
176
177 mx := collr.Collect(context.Background())
178
179 assert.Equal(t, test.wantCollected, mx)
180 if len(test.wantCollected) > 0 {
181 collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
182 }
183 })
184 }
185 }
186
187 func prepareCaseHaproxyV231Metrics(t *testing.T) (*Collector, func()) {
188 t.Helper()
189 srv := httptest.NewServer(http.HandlerFunc(
190 func(w http.ResponseWriter, r *http.Request) {
191 _, _ = w.Write(dataVer2310Metrics)
192 }))
193 collr := New()
194 collr.URL = srv.URL
195 require.NoError(t, collr.Init(context.Background()))
196
197 return collr, srv.Close
198 }
199
200 func prepareCaseNotHaproxyMetrics(t *testing.T) (*Collector, func()) {
201 t.Helper()
202 srv := httptest.NewServer(http.HandlerFunc(
203 func(w http.ResponseWriter, r *http.Request) {
204 _, _ = w.Write([]byte(`
205 # HELP haproxy_backend_http_responses_total Total number of HTTP responses.
206 # TYPE haproxy_backend_http_responses_total counter
207 application_backend_http_responses_total{proxy="infra-traefik-web",code="1xx"} 0
208 application_backend_http_responses_total{proxy="infra-vernemq-ws",code="1xx"} 4130401
209 application_backend_http_responses_total{proxy="infra-traefik-web",code="2xx"} 21338013
210 application_backend_http_responses_total{proxy="infra-vernemq-ws",code="2xx"} 0
211 application_backend_http_responses_total{proxy="infra-traefik-web",code="3xx"} 10004
212 application_backend_http_responses_total{proxy="infra-vernemq-ws",code="3xx"} 0
213 application_backend_http_responses_total{proxy="infra-traefik-web",code="4xx"} 10170758
214 application_backend_http_responses_total{proxy="infra-vernemq-ws",code="4xx"} 0
215 application_backend_http_responses_total{proxy="infra-traefik-web",code="5xx"} 3075
216 application_backend_http_responses_total{proxy="infra-vernemq-ws",code="5xx"} 0
217 application_backend_http_responses_total{proxy="infra-traefik-web",code="other"} 5657
218 application_backend_http_responses_total{proxy="infra-vernemq-ws",code="other"} 0
219 `))
220 }))
221 collr := New()
222 collr.URL = srv.URL
223 require.NoError(t, collr.Init(context.Background()))
224
225 return collr, srv.Close
226 }
227
228 func prepareCase404Response(t *testing.T) (*Collector, func()) {
229 t.Helper()
230 srv := httptest.NewServer(http.HandlerFunc(
231 func(w http.ResponseWriter, r *http.Request) {
232 w.WriteHeader(http.StatusNotFound)
233 }))
234 collr := New()
235 collr.URL = srv.URL
236 require.NoError(t, collr.Init(context.Background()))
237
238 return collr, srv.Close
239 }
240
241 func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) {
242 t.Helper()
243 collr := New()
244 collr.URL = "http://127.0.0.1:38001"
245 require.NoError(t, collr.Init(context.Background()))
246
247 return collr, func() {}
248 }