-
Notifications
You must be signed in to change notification settings - Fork 104
/
varnish_test.go
357 lines (322 loc) · 11.4 KB
/
varnish_test.go
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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/prometheus/client_golang/prometheus"
)
var testFileVersions = []string{"3.0.5", "4.0.5", "4.1.1", "5.2.0", "6.0.0", "6.5.1"}
func Test_VarnishVersion(t *testing.T) {
tests := map[string]*varnishVersion{
"varnishstat (varnish-6.5.1 revision 1dae23376bb5ea7a6b8e9e4b9ed95cdc9469fb64)": &varnishVersion{
Major: 6, Minor: 5, Patch: 1, Revision: "1dae23376bb5ea7a6b8e9e4b9ed95cdc9469fb64",
},
"varnishstat (varnish-6.0.0 revision a068361dff0d25a0d85cf82a6e5fdaf315e06a7d)": &varnishVersion{
Major: 6, Minor: 0, Patch: 0, Revision: "a068361dff0d25a0d85cf82a6e5fdaf315e06a7d",
},
"varnishstat (varnish-5.2.0 revision 4c4875cbf)": &varnishVersion{
Major: 5, Minor: 2, Patch: 0, Revision: "4c4875cbf",
},
"varnishstat (varnish-4.1.10 revision 1d090c5a08f41c36562644bafcce9d3cb85d824f)": &varnishVersion{
Major: 4, Minor: 1, Patch: 10, Revision: "1d090c5a08f41c36562644bafcce9d3cb85d824f",
},
"varnishstat (varnish-4.1.0 revision 3041728)": &varnishVersion{
Major: 4, Minor: 1, Patch: 0, Revision: "3041728",
},
"varnishstat (varnish-4 revision)": &varnishVersion{
Major: 4, Minor: -1, Patch: -1,
},
"varnishstat (varnish-3.0.5 revision 1a89b1f)": &varnishVersion{
Major: 3, Minor: 0, Patch: 5, Revision: "1a89b1f",
},
"varnish 2.0": &varnishVersion{
Major: 2, Minor: 0, Patch: -1,
},
"varnish 1": &varnishVersion{
Major: 1, Minor: -1, Patch: -1,
},
}
for versionStr, test := range tests {
v := NewVarnishVersion()
if err := v.parseVersion(versionStr); err != nil {
t.Error(err.Error())
continue
}
if test.Major != v.Major ||
test.Minor != v.Minor ||
test.Patch != v.Patch ||
test.Revision != v.Revision {
t.Errorf("version mismatch on %q", versionStr)
continue
}
t.Logf("%q > %s\n", versionStr, v.String())
if !test.EqualsOrGreater(test.Major, test.Minor) {
t.Fatalf("%s does not satisfy itself", test)
}
if !test.EqualsOrGreater(test.Major-1, 0) {
t.Fatalf("%s should satisfy version %d.0", test, test.Major-1)
}
if test.EqualsOrGreater(test.Major, test.Minor+1) {
t.Fatalf("%s should not satisfy version %d.%d", test, test.Major, test.Minor+1)
}
}
}
func dummyBackendValue(backend string) (string, map[string]interface{}) {
return fmt.Sprintf("VBE.%s.happy", backend), map[string]interface{}{
"description": "Happy health probes",
"type": "VBE",
"ident": backend,
"flag": "b",
"format": "b",
"value": 0,
}
}
func matchStringSlices(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for i, v1 := range s1 {
if s2[i] != v1 {
return false
}
}
return true
}
func Test_VarnishBackendNames(t *testing.T) {
for _, variant := range [][]string{
{"eu1_x.y-z:w(192.52.0.192,,8085)", "eu1_x.y-z:w", "192.52.0.192,,8085"}, // 4.0.3
{"root:eu2_x.y-z:w", "eu2_x.y-z:w", "unknown"}, // 4.1
{"def0e7f7-a676-4eed-9d8b-78ef7ce21e93.us1_x.y-z:w", "us1_x.y-z:w", "def0e7f7-a676-4eed-9d8b-78ef7ce21e93"},
{"root:29813cbb-7329-4eb8-8969-26be2ef58c88.us2_x.y-z:w", "us2_x.y-z:w", "29813cbb-7329-4eb8-8969-26be2ef58c88"}, // ??
{"boot.default", "default", "unknown"},
{"reload_2019-08-29T100458.default", "default", "unknown"}, // varnish_reload_vcl in 4
{"reload_20191016_072034_54500.default", "default", "unknown"}, // varnishreload in 6+
{"ce19737f-72b5-4f4b-9d39-3d8c2d28240b.default", "default", "ce19737f-72b5-4f4b-9d39-3d8c2d28240b"},
} {
backend := variant[0]
expected_backend := variant[1]
expected_server := variant[2]
vName, data := dummyBackendValue(backend)
var (
vGroup = prometheusGroup(vName)
vDescription string
vIdentifier string
vErr error
)
if value, ok := data["description"]; ok && vErr == nil {
if vDescription, ok = value.(string); !ok {
vErr = fmt.Errorf("%s description it not a string", vName)
}
}
if value, ok := data["ident"]; ok && vErr == nil {
if vIdentifier, ok = value.(string); !ok {
vErr = fmt.Errorf("%s ident it not a string", vName)
}
}
if vErr != nil {
t.Error(vErr)
return
}
// Varnish < 5.2
name_1, _, labelKeys_1, labelValues_1 := computePrometheusInfo(vName, vGroup, vIdentifier, vDescription)
computed_backend := findLabelValue("backend", labelKeys_1, labelValues_1)
computed_server := findLabelValue("server", labelKeys_1, labelValues_1)
t.Logf("%s > %s > %s\n", vName, backend, name_1)
t.Logf(" ident : %s\n", vIdentifier)
t.Logf(" backend : %s\n", computed_backend)
t.Logf(" server : %s\n", computed_server)
if expected_backend != computed_backend {
t.Fatalf("backend %q != %q", computed_backend, expected_backend)
}
if expected_server != expected_server {
t.Fatalf("server %q != %q", computed_server, expected_server)
}
// Varnish >= 5.2 no longer has 'ident', test that detected correctly from vName
name_2, _, labelKeys_2, labelValues_2 := computePrometheusInfo(vName, vGroup, "", vDescription)
if name_1 != name_2 {
t.Fatalf("name %q != %q", name_1, name_2)
}
if !matchStringSlices(labelKeys_1, labelKeys_2) {
t.Fatalf("labelKeys %#v != %#v", labelKeys_1, labelKeys_2)
}
if !matchStringSlices(labelValues_1, labelValues_2) {
t.Fatalf("labelKeys %#v != %#v", labelValues_1, labelValues_2)
}
}
}
func Test_VarnishMetrics(t *testing.T) {
dir, _ := os.Getwd()
if !fileExists(filepath.Join(dir, "test/scrape")) {
t.Skipf("Cannot find test/scrape files from workind dir %s", dir)
}
for _, version := range testFileVersions {
test := filepath.Join(dir, "test/scrape", version+".json")
VarnishVersion.parseVersion(version)
t.Logf("test scrape %s", VarnishVersion)
buf, err := ioutil.ReadFile(test)
if err != nil {
t.Fatal(err.Error())
}
done := make(chan bool)
metrics := make(chan prometheus.Metric)
descs := []*prometheus.Desc{}
go func() {
for m := range metrics {
descs = append(descs, m.Desc())
}
done <- true
}()
_, err = ScrapeVarnishFrom(buf, metrics)
close(metrics)
<-done
if err != nil {
t.Fatal(err.Error())
}
t.Logf(" %d metrics", len(descs))
}
}
func Test_FindMostRecentVbeReloadPrefix(t *testing.T) {
type testConfig struct {
varnishCounters map[string]interface{}
expectedMostRecentVbeReloadPrefix string
}
for _, testConfig := range []testConfig{
// Varnish <= 4.0 has no duplicated stats on reload
{map[string]interface{}{
"VBE.default(127.0.0.1,,8080).happy": "any",
}, ""},
// Varnish 4.1 or later, not yet reloaded
{map[string]interface{}{
"VBE.boot.default.happy": "any",
}, ""},
// Varnish 4.1, reloaded 2 times
{map[string]interface{}{
"VBE.boot.default.happy": "any",
"VBE.reload_2019-08-29T100458.default.happy": "any",
"VBE.reload_2019-08-29T100459.default.happy": "any",
}, "VBE.reload_2019-08-29T100459"},
// Varnish 6+, reloaded 2 times
{map[string]interface{}{
"VBE.boot.default.happy": "any",
"VBE.reload_20191016_072034_54500.default.happy": "any",
"VBE.reload_20191016_072034_54501.default.happy": "any",
}, "VBE.reload_20191016_072034_54501"},
} {
computedMostRecentVbeReloadPrefix := findMostRecentVbeReloadPrefix(testConfig.varnishCounters)
t.Logf("Varnish counters: %s\n", testConfig.varnishCounters)
t.Logf(" expected most recent prefix : '%s'\n", testConfig.expectedMostRecentVbeReloadPrefix)
t.Logf(" computed most recent prefix : '%s'\n", computedMostRecentVbeReloadPrefix)
if computedMostRecentVbeReloadPrefix != testConfig.expectedMostRecentVbeReloadPrefix {
t.Fatalf("mostRecentVbeReloadPrefix %q != %q", computedMostRecentVbeReloadPrefix, testConfig.expectedMostRecentVbeReloadPrefix)
}
}
}
func Test_IsOutdatedVbe(t *testing.T) {
type testConfig struct {
vName string
mostRecentVbeReloadPrefix string
expectedIsOutdatedVbe bool
}
for _, testConfig := range []testConfig{
{"MGT.uptime", "", false}, // not VBE
{"MGT.uptime", "VBE.reload_20191016_072034_54500", false}, // not VBE
{"VBE.boot.default.conn", "", false}, // VCL not yet reloaded
{"VBE.boot.default.conn", "VBE.reload_20191016_072034_54500", true}, // VCL reloaded, 'boot' is now outdated
{"VBE.reload_20191016_072034_54500.default.conn", "VBE.reload_20191016_072034_54500", false}, // current VCL version
{"VBE.reload_20191016_072034_54499.default.conn", "VBE.reload_20191016_072034_54500", true}, // previous VCL version
} {
computedIsOutdatedVbe := isOutdatedVbe(testConfig.vName, testConfig.mostRecentVbeReloadPrefix)
t.Logf("'%s', '%s'\n", testConfig.vName, testConfig.mostRecentVbeReloadPrefix)
t.Logf(" expected outdated : %t\n", testConfig.expectedIsOutdatedVbe)
t.Logf(" computed outdated : %t\n", computedIsOutdatedVbe)
if computedIsOutdatedVbe != testConfig.expectedIsOutdatedVbe {
t.Fatalf("outdatedVbe %t != %t", computedIsOutdatedVbe, testConfig.expectedIsOutdatedVbe)
}
}
}
type testCollector struct {
filepath string
t *testing.T
}
func (tc *testCollector) Describe(ch chan<- *prometheus.Desc) {
}
func (tc *testCollector) Collect(ch chan<- prometheus.Metric) {
buf, err := ioutil.ReadFile(tc.filepath)
if err != nil {
tc.t.Fatal(err.Error())
}
_, err = ScrapeVarnishFrom(buf, ch)
if err != nil {
tc.t.Fatal(err.Error())
}
}
func Test_PrometheusExport(t *testing.T) {
dir, _ := os.Getwd()
if !fileExists(filepath.Join(dir, "test/scrape")) {
t.Skipf("Cannot find test/scrape files from workind dir %s", dir)
}
for _, version := range testFileVersions {
test := filepath.Join(dir, "test/scrape", version+".json")
VarnishVersion.parseVersion(version)
t.Logf("test scrape %s", VarnishVersion)
registry := prometheus.NewRegistry()
collector := &testCollector{filepath: test, t: t}
registry.MustRegister(collector)
gathering, err := registry.Gather()
if err != nil {
errors, ok := err.(prometheus.MultiError)
if ok {
for _, e := range errors {
t.Errorf(" Error in prometheus Gather: %#v", e)
}
} else {
t.Errorf(" Error in prometheus Gather: %#v", err)
}
}
metricCount := 0
includesVarnishBackendHappy := false
for _, mf := range gathering {
if *mf.Name == "varnish_backend_happy" {
includesVarnishBackendHappy = true
}
metricCount += len(mf.Metric)
}
// Validate presence of a single metric, which is present across all versions
if !includesVarnishBackendHappy {
t.Error("Missing metric varnish_backend_happy")
}
t.Logf(" %d metrics", metricCount)
}
}
// Testing against a live varnish instance is only executed in build bot(s).
// This is because the usual end user setup requires tests to be ran with sudo in order to work.
func Test_VarnishMetrics_CI(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("Host needs to be linux to run live metrics test: %s", runtime.GOOS)
return
} else if os.Getenv("CONTINUOUS_INTEGRATION") != "true" {
t.Skip("Live metrics test only ran on CI")
return
}
StartParams.Verbose = true
StartParams.Raw = true
if err := VarnishVersion.Initialize(); err != nil {
t.Fatal(err)
}
done := make(chan bool)
metrics := make(chan prometheus.Metric)
go func() {
for m := range metrics {
t.Logf("%s", m.Desc())
}
done <- true
}()
if _, err := ScrapeVarnish(metrics); err != nil {
t.Fatal(err)
}
close(metrics)
<-done
}