forked from prometheus/mysqld_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysqld_exporter_test.go
100 lines (97 loc) · 2.41 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
package main
import (
"testing"
"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)
})
})
}