forked from percona/mysqld_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqld_exporter_test.go
372 lines (336 loc) · 8.47 KB
/
mysqld_exporter_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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"reflect"
"regexp"
"runtime"
"strings"
"syscall"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestParseMycnf(t *testing.T) {
const (
tcpConfig = `
[client]
user = root
password = abc123
`
tcpConfig2 = `
[client]
user = root
password = abc123
port = 3308
`
socketConfig = `
[client]
user = user
password = pass
socket = /var/lib/mysql/mysql.sock
`
socketConfig2 = `
[client]
user = dude
password = nopassword
# host and port will not be used because of socket presence
host = 1.2.3.4
port = 3307
socket = /var/lib/mysql/mysql.sock
`
remoteConfig = `
[client]
user = dude
password = nopassword
host = 1.2.3.4
port = 3307
`
badConfig = `
[client]
user = root
`
badConfig2 = `
[client]
password = abc123
socket = /var/lib/mysql/mysql.sock
`
badConfig3 = `
[hello]
world = ismine
`
badConfig4 = `
[hello]
world
`
)
convey.Convey("Various .my.cnf configurations", t, func() {
convey.Convey("Local tcp connection", func() {
dsn, _ := parseMycnf([]byte(tcpConfig))
convey.So(dsn, convey.ShouldEqual, "root:abc123@tcp(localhost:3306)/")
})
convey.Convey("Local tcp connection on non-default port", func() {
dsn, _ := parseMycnf([]byte(tcpConfig2))
convey.So(dsn, convey.ShouldEqual, "root:abc123@tcp(localhost:3308)/")
})
convey.Convey("Socket connection", func() {
dsn, _ := parseMycnf([]byte(socketConfig))
convey.So(dsn, convey.ShouldEqual, "user:pass@unix(/var/lib/mysql/mysql.sock)/")
})
convey.Convey("Socket connection ignoring defined host", func() {
dsn, _ := parseMycnf([]byte(socketConfig2))
convey.So(dsn, convey.ShouldEqual, "dude:nopassword@unix(/var/lib/mysql/mysql.sock)/")
})
convey.Convey("Remote connection", func() {
dsn, _ := parseMycnf([]byte(remoteConfig))
convey.So(dsn, convey.ShouldEqual, "dude:nopassword@tcp(1.2.3.4:3307)/")
})
convey.Convey("Missed user", func() {
_, err := parseMycnf([]byte(badConfig))
convey.So(err, convey.ShouldNotBeNil)
})
convey.Convey("Missed password", func() {
_, err := parseMycnf([]byte(badConfig2))
convey.So(err, convey.ShouldNotBeNil)
})
convey.Convey("No [client] section", func() {
_, err := parseMycnf([]byte(badConfig3))
convey.So(err, convey.ShouldNotBeNil)
})
convey.Convey("Invalid config", func() {
_, err := parseMycnf([]byte(badConfig4))
convey.So(err, convey.ShouldNotBeNil)
})
})
}
// bin stores information about path of executable and attached port
type bin struct {
path string
port int
}
// TestBin builds, runs and tests binary.
func TestBin(t *testing.T) {
var err error
binName := "mysqld_exporter"
binDir, err := ioutil.TempDir("/tmp", binName+"-test-bindir-")
if err != nil {
t.Fatal(err)
}
defer func() {
err := os.RemoveAll(binDir)
if err != nil {
t.Fatal(err)
}
}()
importpath := "github.com/percona/mysqld_exporter/vendor/github.com/prometheus/common"
path := binDir + "/" + binName
xVariables := map[string]string{
importpath + "/version.Version": "gotest-version",
importpath + "/version.Branch": "gotest-branch",
importpath + "/version.Revision": "gotest-revision",
}
var ldflags []string
for x, value := range xVariables {
ldflags = append(ldflags, fmt.Sprintf("-X %s=%s", x, value))
}
cmd := exec.Command(
"go",
"build",
"-o",
path,
"-ldflags",
strings.Join(ldflags, " "),
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Fatalf("Failed to build: %s", err)
}
tests := []func(*testing.T, bin){
testLandingPage,
testVersion,
testDefaultGatherer,
}
portStart := 56000
t.Run(binName, func(t *testing.T) {
for _, f := range tests {
f := f // capture range variable
fName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
portStart++
data := bin{
path: path,
port: portStart,
}
t.Run(fName, func(t *testing.T) {
t.Parallel()
f(t, data)
})
}
})
}
func testVersion(t *testing.T, data bin) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
cmd := exec.CommandContext(
ctx,
data.path,
"--version",
"--web.listen-address", fmt.Sprintf(":%d", data.port),
)
b := &bytes.Buffer{}
cmd.Stdout = b
cmd.Stderr = b
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
expectedRegexp := `mysqld_exporter, version gotest-version \(branch: gotest-branch, revision: gotest-revision\)
build user:
build date:
go version:
`
expectedScanner := bufio.NewScanner(bytes.NewBufferString(expectedRegexp))
defer func() {
if err := expectedScanner.Err(); err != nil {
t.Fatal(err)
}
}()
gotScanner := bufio.NewScanner(b)
defer func() {
if err := gotScanner.Err(); err != nil {
t.Fatal(err)
}
}()
for gotScanner.Scan() {
if !expectedScanner.Scan() {
t.Fatalf("didn't expected more data but got '%s'", gotScanner.Text())
}
ok, err := regexp.MatchString(expectedScanner.Text(), gotScanner.Text())
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatalf("'%s' does not match regexp '%s'", gotScanner.Text(), expectedScanner.Text())
}
}
if expectedScanner.Scan() {
t.Errorf("expected '%s' but didn't got more data", expectedScanner.Text())
}
}
func testLandingPage(t *testing.T, data bin) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Run exporter.
cmd := exec.CommandContext(
ctx,
data.path,
"--web.listen-address", fmt.Sprintf(":%d", data.port),
)
cmd.Env = append(os.Environ(), "DATA_SOURCE_NAME=127.0.0.1:3306")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
// Get the main page.
urlToGet := fmt.Sprintf("http://127.0.0.1:%d", data.port)
body, err := waitForBody(urlToGet)
if err != nil {
t.Fatal(err)
}
got := string(body)
expected := `<html>
<head><title>MySQLd 3-in-1 exporter</title></head>
<body>
<h1>MySQL 3-in-1 exporter</h1>
<li><a href="/metrics-hr">high-res metrics</a></li>
<li><a href="/metrics-mr">medium-res metrics</a></li>
<li><a href="/metrics-lr">low-res metrics</a></li>
</body>
</html>
`
if got != expected {
t.Fatalf("got '%s' but expected '%s'", got, expected)
}
}
func testDefaultGatherer(t *testing.T, data bin) {
metricPath := "/metrics"
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
cmd := exec.CommandContext(
ctx,
data.path,
"--web.telemetry-path", metricPath,
"--web.listen-address", fmt.Sprintf(":%d", data.port),
)
cmd.Env = append(os.Environ(), "DATA_SOURCE_NAME=127.0.0.1:3306")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
const resolution = "hr"
body, err := waitForBody(fmt.Sprintf("http://127.0.0.1:%d%s-%s", data.port, metricPath, resolution))
if err != nil {
t.Fatalf("unable to get metrics for '%s' resolution: %s", resolution, err)
}
got := string(body)
metricsPrefixes := []string{
"go_gc_duration_seconds",
"go_goroutines",
"go_memstats",
}
for _, prefix := range metricsPrefixes {
if !strings.Contains(got, prefix) {
t.Fatalf("no metric starting with %s in resolution %s", prefix, resolution)
}
}
}
// waitForBody is a helper function which makes http calls until http server is up
// and then returns body of the successful call.
func waitForBody(urlToGet string) (body []byte, err error) {
tries := 60
// Get data, but we need to wait a bit for http server.
for i := 0; i <= tries; i++ {
// Try to get web page.
body, err = getBody(urlToGet)
if err == nil {
return body, err
}
// If there is a syscall.ECONNREFUSED error (web server not available) then retry.
if urlError, ok := err.(*url.Error); ok {
if opError, ok := urlError.Err.(*net.OpError); ok {
if osSyscallError, ok := opError.Err.(*os.SyscallError); ok {
if osSyscallError.Err == syscall.ECONNREFUSED {
time.Sleep(1 * time.Second)
continue
}
}
}
}
// There was an error, and it wasn't syscall.ECONNREFUSED.
return nil, err
}
return nil, fmt.Errorf("failed to GET %s after %d tries: %s", urlToGet, tries, err)
}
// getBody is a helper function which retrieves http body from given address.
func getBody(urlToGet string) ([]byte, error) {
resp, err := http.Get(urlToGet)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}